Commit Graph

2515 Commits

Author SHA1 Message Date
Paul Sokolovsky
ed1c194ebf py/objstrunicode: str_index_to_ptr: Implement positive indexing properly.
Order out-of-bounds check, completion check, and increment in the right way.
2016-07-25 19:28:04 +03:00
Paul Sokolovsky
6af90b2972 py/objstrunicode: str_index_to_ptr: Should handle bytes too.
There's single str_index_to_ptr() function, called for both bytes and
unicode objects, so should handle each properly.
2016-07-25 14:45:08 +03:00
Paul Sokolovsky
16f324641f py/stream.h: Remove dated comment of POSIX-specificity of EAGAIN.
We have adopted POSIX-compatible error numbers as MicroPython's native.
2016-07-25 00:47:00 +03:00
Paul Sokolovsky
1a7e28d8b7 py/stream.h: Move mp_stream_write_adaptor() inside ifdef block. 2016-07-25 00:16:51 +03:00
Paul Sokolovsky
918851e836 unix: Enable MICROPY_PY_STR_BYTES_CMP_WARN.
Also, fix a warning text (remove "duplicate" BytesWarning).
2016-07-22 00:52:07 +03:00
Paul Sokolovsky
a1b442bc07 py/mpconfig.h: Fix description for MICROPY_PY_STR_BYTES_CMP_WARN. 2016-07-22 00:46:24 +03:00
Paul Sokolovsky
707cae7494 py/obj: Issue a warning when str and bytes objects are compared.
Something like:

if foo == "bar":

will be always false if foo is b"bar". In CPython, warning is issued if
interpreter is started as "python3 -b". In MicroPython,
MICROPY_PY_STR_BYTES_CMP_WARN setting controls it.
2016-07-22 00:34:34 +03:00
Dave Hylands
26b7d8a7be py: Fix nlrthumb.c when DEBUG=1 is defined 2016-07-21 00:54:21 +03:00
Paul Sokolovsky
93e353e384 py/gc: Implement GC running by allocation threshold.
Currently, MicroPython runs GC when it could not allocate a block of memory,
which happens when heap is exhausted. However, that policy can't work well
with "inifinity" heaps, e.g. backed by a virtual memory - there will be a
lot of swap thrashing long before VM will be exhausted. Instead, in such
cases "allocation threshold" policy is used: a GC is run after some number of
allocations have been made. Details vary, for example, number or total amount
of allocations can be used, threshold may be self-adjusting based on GC
outcome, etc.

This change implements a simple variant of such policy for MicroPython. Amount
of allocated memory so far is used for threshold, to make it useful to typical
finite-size, and small, heaps as used with MicroPython ports. And such GC policy
is indeed useful for such types of heaps too, as it allows to better control
fragmentation. For example, if a threshold is set to half size of heap, then
for an application which usually makes big number of small allocations, that
will (try to) keep half of heap memory in a nice defragmented state for an
occasional large allocation.

For an application which doesn't exhibit such behavior, there won't be any
visible effects, except for GC running more frequently, which however may
affect performance. To address this, the GC threshold is configurable, and
by default is off so far. It's configured with gc.threshold(amount_in_bytes)
call (can be queries without an argument).
2016-07-21 00:37:30 +03:00
Paul Sokolovsky
6aa7c805cc esp8266: Cache Xtensa-built libaxtls.a in local build dir.
Allows to build the library variant for other archs in parallel.
2016-07-16 04:56:23 +03:00
Paul Sokolovsky
ad9b9c7621 py/stream: Implement 2- and 3-arg write() method as an extension to CPython.
3-arg form:

stream.write(data, offset, length)

2-arg form:

stream.write(data, length)

These allow efficient buffer writing without incurring extra memory
allocation for slicing or creating memoryview() object, what is
important for low-memory ports.

All arguments must be positional. It might be not so bad idea to standardize
on 3-arg form, but 2-arg case would need check and raising an exception
anyway then, so instead it was just made to work.
2016-07-14 01:44:50 +03:00
Paul Sokolovsky
20283aec10 extmod/modussl_axtls: Further changes to allow alternative SSL modules.
Make variable MICROPY_SSL_AXTLS=1 should be defined to activate modussl_axtls
and link with -laxtls.
2016-07-13 01:49:38 +03:00
Paul Sokolovsky
e32d1e17bb extmod/modussl: Rename to modussl_axtls.c, to allow impl using other SSL libs. 2016-07-13 01:35:59 +03:00
Damien George
3593d8e10c py/nlrx64.S: Prefix mp_thread_get_state with an underscore on Mac. 2016-07-11 14:27:47 +01:00
Daniel Tralamazza
9626662819 qemu-arm: Enable gcc LTO option for nlrthumb.c
LTO can't "see" inside naked functions, but we can mark `nlr_push_tail` as used.
2016-07-11 12:04:56 +01:00
Paul Sokolovsky
ec7fe92531 py/objarray: Split out header to allow direct access to object.
This follows source code/header file organization similar to few other
objects, and intended to be used only is special cases, where efficiency/
simplicity matters.
2016-07-06 18:19:27 +03:00
Paul Sokolovsky
426112ce88 extmod/moduos_dupterm: Reserve buffer bytearray object for dupterm.
Allocating it for each read/write operation is a memory fragmentation
hazard.
2016-07-04 13:32:30 +03:00
Paul Sokolovsky
737bd9c314 py/mpconfig.h: Mention MICROPY_PY_BTREE config option.
However, as it requires linking with external libraries, it actually
should be ste on Makefile level.
2016-07-02 14:57:42 +03:00
Paul Sokolovsky
ae184cb9de py/builtinimport: Disable "imported as namespace package" warning.
Namespace packages are natural part of Python3, CPython3 doesn't have such
warning, it made sense only from point of view of Python2 legacy.
2016-07-02 14:45:49 +03:00
Paul Sokolovsky
749cbaca7f py/gc: Calculate (and report) maximum contiguous free block size.
Just as maximum allocated block size, it's reported in allocation units
(not bytes).
2016-07-01 00:09:55 +03:00
Paul Sokolovsky
6a6e0b7e05 py/gc: Be sure to count last allocated block at heap end in stats.
Previously, if there was chain of allocated blocks ending with the last
block of heap, it wasn't included in number of 1/2-block or max block
size stats.
2016-06-30 12:56:21 +03:00
Damien George
77e37ff98b py/mpthread: Include mpstate.h when defining GIL macros. 2016-06-28 11:28:52 +01:00
Damien George
94238d4ae5 py/nlrsetjmp: Update to take into account new location of nlr_top.
It's now accessed via the MP_STATE_THREAD macro.
2016-06-28 11:28:52 +01:00
Damien George
1df4168c33 py/nlrthumb: Convert NLR thumb funcs from asm to C with inline-asm.
Now only the bits that really need to be written in assembler are written
in it, otherwise C is used.  This means that the assembler code no longer
needs to know about the global state structure which makes it much easier
to maintain.
2016-06-28 11:28:52 +01:00
Damien George
df95f52583 py/modthread: Allow to properly set the stack limit of a thread.
We rely on the port setting and adjusting the stack size so there is
enough room to recover from hitting the stack limit.
2016-06-28 11:28:51 +01:00
Damien George
e90b6ce0b5 py/mpthread.h: Move GIL macros outside MICROPY_PY_THREAD block.
The GIL macros are needed even if threading is not enabled.
2016-06-28 11:28:50 +01:00
Damien George
c567afc5fa py/modthread: Make Lock objects work when GIL is enabled. 2016-06-28 11:28:50 +01:00
Damien George
a1c93a62b1 py: Don't use gc or qstr mutex when the GIL is enabled.
There is no need since the GIL already makes gc and qstr operations
atomic.
2016-06-28 11:28:50 +01:00
Damien George
4cec63a9db py: Implement a simple global interpreter lock.
This makes the VM/runtime thread safe, at the cost of not being able to
run code in parallel.
2016-06-28 11:28:50 +01:00
Damien George
1f54ad2aed py: Make interning of qstrs thread safe. 2016-06-28 11:28:50 +01:00
Damien George
3653f5144a py/gc: Fix GC+thread bug where ptr gets lost because it's not computed.
GC_EXIT() can cause a pending thread (waiting on the mutex) to be
scheduled right away.  This other thread may trigger a garbage
collection.  If the pointer to the newly-allocated block (allocated by
the original thread) is not computed before the switch (so it's just left
as a block number) then the block will be wrongly reclaimed.

This patch makes sure the pointer is computed before allowing any thread
switch to occur.
2016-06-28 11:28:49 +01:00
Damien George
9172c0cb25 py/modthread: Call mp_thread_start/mp_thread_finish around threads.
So the underlying thread implementation can do any necessary bookkeeping.
2016-06-28 11:28:49 +01:00
Damien George
722cff5fd0 py/modthread: Be more careful with root pointers when creating a thread. 2016-06-28 11:28:49 +01:00
Damien George
e33806aaff py/gc: Fix 2 cases of concurrent access to ATB and FTB. 2016-06-28 11:28:49 +01:00
Damien George
7f4658a7ee py/modthread: Satisfy unused-args warning. 2016-06-28 11:28:49 +01:00
Damien George
c93d9caa8b py/gc: Make memory manager and garbage collector thread safe.
By using a single, global mutex, all memory-related functions (alloc,
free, realloc, collect, etc) are made thread safe.  This means that only
one thread can be in such a function at any one time.
2016-06-28 11:28:49 +01:00
Damien George
34fc006f5e py/modthread: Add with-context capabilities to lock object. 2016-06-28 11:28:49 +01:00
Damien George
801d1b3803 py/modthread: Implement lock object, for creating a mutex. 2016-06-28 11:28:49 +01:00
Damien George
2dacd604c5 py/modthread: Add exit() function.
Simply raises the SystemExit exception.
2016-06-28 11:28:48 +01:00
Damien George
707f98f207 py/modthread: Add stack_size() function. 2016-06-28 11:28:48 +01:00
Damien George
3eb7a26809 py/modthread: Properly cast concrete exception pointer to an object. 2016-06-28 11:28:48 +01:00
Damien George
27cc07721b py: Add basic _thread module, with ability to start a new thread. 2016-06-28 11:28:48 +01:00
Damien George
330165a2cc py: Add MP_STATE_THREAD to hold state specific to a given thread. 2016-06-28 11:09:31 +01:00
Paul Sokolovsky
c7fba524cb py/objtype: Inherit protocol vtable from base class only if it exists. 2016-06-19 00:56:06 +03:00
Paul Sokolovsky
41167554e4 py/mphal.h: If virtpin API is used, automagically include its header. 2016-06-19 00:12:37 +03:00
Paul Sokolovsky
413c3e10b4 py/objtype: instance: Inherit protocol vtable from a base class.
This allows to define an  abstract base class which would translate
C-level protocol to Python method calls, and any subclass inheriting
from it will support this feature. This in particular actually enables
recently introduced machine.PinBase class.
2016-06-19 00:01:59 +03:00
Paul Sokolovsky
3fecbb2462 extmod/machine_pinbase: Implementation of PinBase class.
Allows to translate C-level pin API to Python-level pin API. In other
words, allows to implement a pin class and Python which will be usable
for efficient C-coded algorithms, like bitbanging SPI/I2C, time_pulse,
etc.
2016-06-18 23:40:28 +03:00
Paul Sokolovsky
07209f8592 all: Rename mp_obj_type_t::stream_p to protocol.
It's now used for more than just stream protocol (e.g. pin protocol), so
don't use false names.
2016-06-18 18:44:57 +03:00
Paul Sokolovsky
f469c76442 py: Rename __QSTR_EXTRACT flag to NO_QSTR.
It has more usages than just qstr extraction, for example, embedding (where
people don't care about efficient predefined qstrs).
2016-06-16 01:42:48 +03:00
Paul Sokolovsky
230d5cda05 py/mkrules.mk: Define "lib" outside conditional block.
"lib" happened to be defined inside block conditional on $(PROG).
2016-06-16 01:11:24 +03:00