diff --git a/headers/posix/pthread.h b/headers/posix/pthread.h index 1d2f70d671..d57165926d 100644 --- a/headers/posix/pthread.h +++ b/headers/posix/pthread.h @@ -5,8 +5,9 @@ #define _PTHREAD_H_ #include +#include -typedef struct _pthread *pthread_t; +typedef thread_id pthread_t; typedef struct _pthread_attr *pthread_attr_t; typedef struct _pthread_mutex *pthread_mutex_t; typedef struct _pthread_mutexattr *pthread_mutexattr_t; diff --git a/src/system/libroot/posix/pthread/Jamfile b/src/system/libroot/posix/pthread/Jamfile index 7a93614302..ab4ef86f91 100644 --- a/src/system/libroot/posix/pthread/Jamfile +++ b/src/system/libroot/posix/pthread/Jamfile @@ -3,7 +3,9 @@ SubDir HAIKU_TOP src system libroot posix pthread ; UsePrivateHeaders libroot ; MergeObject posix_pthread.o : + pthread.c pthread_atfork.c + pthread_attr.c pthread_mutex.c pthread_mutexattr.c ; diff --git a/src/system/libroot/posix/pthread/pthread.c b/src/system/libroot/posix/pthread/pthread.c new file mode 100644 index 0000000000..7897534d1f --- /dev/null +++ b/src/system/libroot/posix/pthread/pthread.c @@ -0,0 +1,75 @@ +/* + * Copyright 2006, Jérôme Duval. All rights reserved. + * Distributed under the terms of the MIT License. + */ + +#include +#include +#include "pthread_private.h" + +static const pthread_attr pthread_attr_default = { + PTHREAD_CREATE_JOINABLE, + B_NORMAL_PRIORITY +}; + + +int +pthread_create(pthread_t *_thread, const pthread_attr_t *_attr, + void *(*start_routine)(void*), void *arg) +{ + thread_id thread; + const pthread_attr *attr = NULL; + + if (_thread == NULL) + return B_BAD_VALUE; + + if (_attr == NULL) + attr = &pthread_attr_default; + else + attr = *_attr; + + thread = spawn_thread((thread_entry)start_routine, "pthread func", attr->sched_priority, arg); + if (thread < B_OK) + return thread; + + resume_thread(thread); + + *_thread = thread; + + return B_OK; +} + + +pthread_t +pthread_self() +{ + return find_thread(NULL); +} + + +int +pthread_equal(pthread_t t1, pthread_t t2) +{ + return (t1 > 0 && t2 > 0 && t1 == t2); +} + +int +pthread_join(pthread_t thread, void **value_ptr) +{ + return wait_for_thread(thread, (status_t *)value_ptr); +} + + +void +pthread_exit(void *value_ptr) +{ + exit_thread((status_t)value_ptr); +} + + +int +pthread_kill(pthread_t thread, int sig) +{ + return kill(thread, sig); +} + diff --git a/src/system/libroot/posix/pthread/pthread_attr.c b/src/system/libroot/posix/pthread/pthread_attr.c new file mode 100644 index 0000000000..dcd4d22fa5 --- /dev/null +++ b/src/system/libroot/posix/pthread/pthread_attr.c @@ -0,0 +1,77 @@ +/* +** Copyright 2006, Jérôme Duval. All rights reserved. +** Distributed under the terms of the MIT License. +*/ + + +#include +#include "pthread_private.h" + +#include + + +int +pthread_attr_init(pthread_attr_t *_attr) +{ + pthread_attr *attr; + + if (_attr == NULL) + return B_BAD_VALUE; + + attr = (pthread_attr *)malloc(sizeof(pthread_attr)); + if (attr == NULL) + return B_NO_MEMORY; + + attr->detach_state = PTHREAD_CREATE_JOINABLE; + attr->sched_priority = B_NORMAL_PRIORITY; + + *_attr = attr; + return B_OK; +} + + +int +pthread_attr_destroy(pthread_attr_t *_attr) +{ + pthread_attr *attr; + + if (_attr == NULL || (attr = *_attr) == NULL) + return B_BAD_VALUE; + + *_attr = NULL; + free(attr); + + return B_OK; +} + + +int +pthread_attr_getdetachstate(const pthread_attr_t *_attr, int *state) +{ + pthread_attr *attr; + + if (_attr == NULL || (attr = *_attr) == NULL) + return B_BAD_VALUE; + + *state = attr->detach_state; + + return B_OK; +} + + +int +pthread_attr_setdetachstate(pthread_attr_t *_attr, int state) +{ + pthread_attr *attr; + + if (_attr == NULL || (attr = *_attr) == NULL) + return B_BAD_VALUE; + + if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED) + return B_BAD_VALUE; + + attr->detach_state = state; + + return B_OK; +} + diff --git a/src/system/libroot/posix/pthread/pthread_private.h b/src/system/libroot/posix/pthread/pthread_private.h index 3a28d42f3f..3eafaf472b 100644 --- a/src/system/libroot/posix/pthread/pthread_private.h +++ b/src/system/libroot/posix/pthread/pthread_private.h @@ -25,4 +25,9 @@ typedef struct _pthread_mutex { pthread_mutexattr attr; } pthread_mutex; +typedef struct _pthread_attr { + int32 detach_state; + int32 sched_priority; +} pthread_attr; + #endif /* _PTHREAD_PRIVATE_H_ */