aboutsummaryrefslogtreecommitdiff
path: root/py/modthread.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-06 10:47:20 +1100
committerDamien George <damien.p.george@gmail.com>2017-02-15 11:28:02 +1100
commit234f07f16cc845128658cc0b39ac681cae5a8fba (patch)
tree40beeeefc8470d8538b127156f520af648423d02 /py/modthread.c
parent06a12ada48c11a101eb138220cd0e0ca33a612be (diff)
py/modthread: Use system-provided mutexs for _thread locks.
It's more efficient using the system mutexs instead of synthetic ones with a busy-wait loop. The system can do proper scheduling and blocking of the threads waiting on the mutex.
Diffstat (limited to 'py/modthread.c')
-rw-r--r--py/modthread.c25
1 files changed, 4 insertions, 21 deletions
diff --git a/py/modthread.c b/py/modthread.c
index 6f55281ad..975e7d1f6 100644
--- a/py/modthread.c
+++ b/py/modthread.c
@@ -44,24 +44,19 @@
/****************************************************************/
// Lock object
-// Note: with the GIL enabled we can easily synthesise a lock object
STATIC const mp_obj_type_t mp_type_thread_lock;
typedef struct _mp_obj_thread_lock_t {
mp_obj_base_t base;
- #if !MICROPY_PY_THREAD_GIL
mp_thread_mutex_t mutex;
- #endif
volatile bool locked;
} mp_obj_thread_lock_t;
STATIC mp_obj_thread_lock_t *mp_obj_new_thread_lock(void) {
mp_obj_thread_lock_t *self = m_new_obj(mp_obj_thread_lock_t);
self->base.type = &mp_type_thread_lock;
- #if !MICROPY_PY_THREAD_GIL
mp_thread_mutex_init(&self->mutex);
- #endif
self->locked = false;
return self;
}
@@ -73,20 +68,9 @@ STATIC mp_obj_t thread_lock_acquire(size_t n_args, const mp_obj_t *args) {
wait = mp_obj_get_int(args[1]);
// TODO support timeout arg
}
- #if MICROPY_PY_THREAD_GIL
- if (self->locked) {
- if (!wait) {
- return mp_const_false;
- }
- do {
- MP_THREAD_GIL_EXIT();
- MP_THREAD_GIL_ENTER();
- } while (self->locked);
- }
- self->locked = true;
- return mp_const_true;
- #else
+ MP_THREAD_GIL_EXIT();
int ret = mp_thread_mutex_lock(&self->mutex, wait);
+ MP_THREAD_GIL_ENTER();
if (ret == 0) {
return mp_const_false;
} else if (ret == 1) {
@@ -95,7 +79,6 @@ STATIC mp_obj_t thread_lock_acquire(size_t n_args, const mp_obj_t *args) {
} else {
mp_raise_OSError(-ret);
}
- #endif
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock_acquire_obj, 1, 3, thread_lock_acquire);
@@ -103,9 +86,9 @@ STATIC mp_obj_t thread_lock_release(mp_obj_t self_in) {
mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in);
// TODO check if already unlocked
self->locked = false;
- #if !MICROPY_PY_THREAD_GIL
+ MP_THREAD_GIL_EXIT();
mp_thread_mutex_unlock(&self->mutex);
- #endif
+ MP_THREAD_GIL_ENTER();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(thread_lock_release_obj, thread_lock_release);