diff options
Diffstat (limited to 'unix/mpthreadport.c')
-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 |