diff options
author | Damien George <damien.p.george@gmail.com> | 2016-04-25 11:21:48 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-06-28 11:28:49 +0100 |
commit | 801d1b3803e4c8070a8bea6ee4f563a799d010b7 (patch) | |
tree | 5049f0f416ed64256e9ee6892afa209f1091bb71 /unix | |
parent | 2dacd604c51d27f08076e76abd91de43bc2481f3 (diff) | |
download | micropython-801d1b3803e4c8070a8bea6ee4f563a799d010b7.tar.gz micropython-801d1b3803e4c8070a8bea6ee4f563a799d010b7.zip |
py/modthread: Implement lock object, for creating a mutex.
Diffstat (limited to 'unix')
-rw-r--r-- | unix/mpthreadport.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/unix/mpthreadport.c b/unix/mpthreadport.c index 73e08dcb86..3a7e3ad4f5 100644 --- a/unix/mpthreadport.c +++ b/unix/mpthreadport.c @@ -76,4 +76,31 @@ er: nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ret))); } +void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { + pthread_mutex_init(mutex, NULL); +} + +int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { + int ret; + if (wait) { + ret = pthread_mutex_lock(mutex); + if (ret == 0) { + return 1; + } + } else { + ret = pthread_mutex_trylock(mutex); + if (ret == 0) { + return 1; + } else if (ret == EBUSY) { + return 0; + } + } + return -ret; +} + +void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { + pthread_mutex_unlock(mutex); + // TODO check return value +} + #endif // MICROPY_PY_THREAD |