diff --git a/base/usr/include/pthread.h b/base/usr/include/pthread.h index 48a9fd93..eeae188a 100644 --- a/base/usr/include/pthread.h +++ b/base/usr/include/pthread.h @@ -19,4 +19,24 @@ extern int pthread_kill(pthread_t thread, int sig); extern int clone(uintptr_t,uintptr_t,void*); extern int gettid(); +extern void pthread_cleanup_push(void (*routine)(void *), void *arg); +extern void pthread_cleanup_pop(int execute); + +typedef int volatile pthread_mutex_t; +typedef int pthread_mutexattr_t; + +extern int pthread_join(pthread_t thread, void **retval); + +#define PTHREAD_MUTEX_INITIALIZER 0 + +extern int pthread_mutex_lock(pthread_mutex_t *mutex); +extern int pthread_mutex_trylock(pthread_mutex_t *mutex); +extern int pthread_mutex_unlock(pthread_mutex_t *mutex); +extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); +extern int pthread_mutex_destroy(pthread_mutex_t *mutex); + +extern int pthread_attr_init(pthread_attr_t *attr); +extern int pthread_attr_destroy(pthread_attr_t *attr); + + _End_C_Header diff --git a/libc/pthread/pthread.c b/libc/pthread/pthread.c index 7f6c95c3..9e03857e 100644 --- a/libc/pthread/pthread.c +++ b/libc/pthread/pthread.c @@ -10,6 +10,8 @@ #include #include +#include + DEFN_SYSCALL3(clone, SYS_CLONE, uintptr_t, uintptr_t, void *); DEFN_SYSCALL0(gettid, SYS_GETTID); @@ -45,3 +47,57 @@ void pthread_exit(void * value) { void (*magic_exit_func)(void) = (void *)magic_exit_target; magic_exit_func(); } + +void pthread_cleanup_push(void (*routine)(void *), void *arg) { + /* do nothing */ +} + +void pthread_cleanup_pop(int execute) { + /* do nothing */ +} + +int pthread_mutex_lock(pthread_mutex_t *mutex) { + while (__sync_lock_test_and_set(mutex, 0x01)) { + syscall_yield(); + } + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t *mutex) { + if (__sync_lock_test_and_set(mutex, 0x01)) { + return EBUSY; + } + return 0; +} + +int pthread_mutex_unlock(pthread_mutex_t *mutex) { + __sync_lock_release(mutex); + return 0; +} + +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { + *mutex = 0; + return 0; +} + +int pthread_mutex_destroy(pthread_mutex_t *mutex) { + return 0; +} + +int pthread_attr_init(pthread_attr_t *attr) { + *attr = 0; + return 0; +} + +int pthread_attr_destroy(pthread_attr_t *attr) { + return 0; +} + +int pthread_join(pthread_t thread, void **retval) { + int status; + int result = waitpid(thread.id, &status, 0); + if (retval) { + *retval = (void*)status; + } + return result; +}