Author: bonefish Date: 2011-06-06 16:34:38 +0200 (Mon, 06 Jun 2011) New Revision: 41967 Changeset: https://dev.haiku-os.org/changeset/41967 Modified: haiku/branches/developer/bonefish/signals/headers/private/libroot/pthread_private.h haiku/branches/developer/bonefish/signals/headers/private/system/thread_defs.h haiku/branches/developer/bonefish/signals/src/system/libroot/os/thread.c haiku/branches/developer/bonefish/signals/src/system/libroot/posix/pthread/pthread.cpp Log: * thread_creation_attributes::entry: Changed first parameter to void*. * __allocate_pthread(): Also pass the entry function. * Added __init_pthread(), pulled out of __allocate_pthread(). * Added __pthread_init_creation_attributes(), initializing a thread_creation_attributes structure from an optional pthread_attr_t structure and other parameters. Used by both pthread_create() and spawn_thread(). Modified: haiku/branches/developer/bonefish/signals/headers/private/libroot/pthread_private.h =================================================================== --- haiku/branches/developer/bonefish/signals/headers/private/libroot/pthread_private.h 2011-06-06 14:15:22 UTC (rev 41966) +++ haiku/branches/developer/bonefish/signals/headers/private/libroot/pthread_private.h 2011-06-06 14:34:38 UTC (rev 41967) @@ -11,6 +11,13 @@ #include <OS.h> + +#define THREAD_DETACHED 0x01 +#define THREAD_DEAD 0x02 + + +struct thread_creation_attributes; + // The public *_t types are only pointers to these structures // This way, we are completely free to change them, which might be // necessary in the future (not only due to the incomplete implementation @@ -69,7 +76,13 @@ void __pthread_key_call_destructors(pthread_thread *thread); void __pthread_destroy_thread(void); -pthread_thread *__allocate_pthread(void *data); +pthread_thread *__allocate_pthread(void* (*entry)(void*), void *data); +void __init_pthread(pthread_thread* thread, void* (*entry)(void*), void* data); +status_t __pthread_init_creation_attributes( + const pthread_attr_t* pthreadAttributes, pthread_t thread, + status_t (*entryFunction)(void*, void*), void* argument1, + void* argument2, const char* name, + struct thread_creation_attributes* attributes); #ifdef __cplusplus } Modified: haiku/branches/developer/bonefish/signals/headers/private/system/thread_defs.h =================================================================== --- haiku/branches/developer/bonefish/signals/headers/private/system/thread_defs.h 2011-06-06 14:15:22 UTC (rev 41966) +++ haiku/branches/developer/bonefish/signals/headers/private/system/thread_defs.h 2011-06-06 14:34:38 UTC (rev 41967) @@ -43,7 +43,7 @@ struct thread_creation_attributes { - int32 (*entry)(thread_func, void*); + int32 (*entry)(void*, void*); const char* name; int32 priority; void* args1; Modified: haiku/branches/developer/bonefish/signals/src/system/libroot/os/thread.c =================================================================== --- haiku/branches/developer/bonefish/signals/src/system/libroot/os/thread.c 2011-06-06 14:15:22 UTC (rev 41966) +++ haiku/branches/developer/bonefish/signals/src/system/libroot/os/thread.c 2011-06-06 14:34:38 UTC (rev 41967) @@ -32,8 +32,9 @@ static status_t -thread_entry(thread_func entry, void* _thread) +thread_entry(void* _entry, void* _thread) { + thread_func entry = (thread_func)_entry; pthread_thread* thread = (pthread_thread*)_thread; status_t returnCode; @@ -82,22 +83,17 @@ struct thread_creation_attributes attributes; pthread_thread* thread; - thread = __allocate_pthread(data); + thread = __allocate_pthread(NULL, data); if (thread == NULL) return B_NO_MEMORY; _single_threaded = false; // used for I/O locking - BeOS compatibility issue - attributes.entry = &thread_entry; - attributes.name = name; + __pthread_init_creation_attributes(NULL, thread, &thread_entry, entry, + thread, name, &attributes); + attributes.priority = priority; - attributes.args1 = entry; - attributes.args2 = thread; - attributes.stack_address = NULL; - attributes.stack_size = 0; - attributes.pthread = thread; - attributes.flags = 0; return _kern_spawn_thread(&attributes); } Modified: haiku/branches/developer/bonefish/signals/src/system/libroot/posix/pthread/pthread.cpp =================================================================== --- haiku/branches/developer/bonefish/signals/src/system/libroot/posix/pthread/pthread.cpp 2011-06-06 14:15:22 UTC (rev 41966) +++ haiku/branches/developer/bonefish/signals/src/system/libroot/posix/pthread/pthread.cpp 2011-06-06 14:34:38 UTC (rev 41967) @@ -20,10 +20,6 @@ #include <user_thread.h> -#define THREAD_DETACHED 0x01 -#define THREAD_DEAD 0x02 - - static const pthread_attr pthread_attr_default = { PTHREAD_CREATE_JOINABLE, B_NORMAL_PRIORITY, @@ -36,7 +32,7 @@ static status_t -pthread_thread_entry(thread_func _unused, void* _thread) +pthread_thread_entry(void*, void* _thread) { pthread_thread* thread = (pthread_thread*)_thread; @@ -71,13 +67,22 @@ pthread_thread* -__allocate_pthread(void *data) +__allocate_pthread(void* (*entry)(void*), void *data) { pthread_thread* thread = (pthread_thread*)malloc(sizeof(pthread_thread)); if (thread == NULL) return NULL; - thread->entry = NULL; + __init_pthread(thread, entry, data); + + return thread; +} + + +void +__init_pthread(pthread_thread* thread, void* (*entry)(void*), void* data) +{ + thread->entry = entry; thread->entry_argument = data; thread->exit_value = NULL; thread->cancel_state = PTHREAD_CANCEL_ENABLE; @@ -87,8 +92,38 @@ thread->flags = 0; memset(thread->specific, 0, sizeof(thread->specific)); +} - return thread; + +status_t +__pthread_init_creation_attributes(const pthread_attr_t* pthreadAttributes, + pthread_t thread, status_t (*entryFunction)(void*, void*), + void* argument1, void* argument2, const char* name, + thread_creation_attributes* attributes) +{ + const pthread_attr* attr = NULL; + if (pthreadAttributes == NULL) { + attr = &pthread_attr_default; + } else { + attr = *pthreadAttributes; + if (attr == NULL) + return EINVAL; + } + + attributes->entry = entryFunction; + attributes->name = name; + attributes->priority = attr->sched_priority; + attributes->args1 = argument1; + attributes->args2 = argument2; + attributes->stack_address = NULL; + attributes->stack_size = attr->stack_size; + attributes->pthread = thread; + attributes->flags = 0; + + if (thread != NULL && attr->detach_state == PTHREAD_CREATE_DETACHED) + thread->flags |= THREAD_DETACHED; + + return B_OK; } @@ -96,43 +131,24 @@ int -pthread_create(pthread_t* _thread, const pthread_attr_t* _attr, +pthread_create(pthread_t* _thread, const pthread_attr_t* attr, void* (*startRoutine)(void*), void* arg) { - const pthread_attr* attr = NULL; - pthread_thread* thread; - if (_thread == NULL) return EINVAL; - if (_attr == NULL) - attr = &pthread_attr_default; - else { - attr = *_attr; - if (attr == NULL) - return EINVAL; - } - - thread = __allocate_pthread(arg); + pthread_thread* thread = __allocate_pthread(startRoutine, arg); if (thread == NULL) return EAGAIN; - thread->entry = startRoutine; + thread_creation_attributes attributes; + status_t error = __pthread_init_creation_attributes(attr, thread, + &pthread_thread_entry, NULL, thread, "pthread func", &attributes); + if (error != B_OK) { + free(thread); + return error; + } - if (attr->detach_state == PTHREAD_CREATE_DETACHED) - thread->flags |= THREAD_DETACHED; - - struct thread_creation_attributes attributes; - attributes.entry = pthread_thread_entry; - attributes.name = "pthread func"; - attributes.priority = attr->sched_priority; - attributes.args1 = NULL; - attributes.args2 = thread; - attributes.stack_address = NULL; - attributes.stack_size = attr->stack_size; - attributes.pthread = thread; - attributes.flags = 0; - thread->id = _kern_spawn_thread(&attributes); if (thread->id < 0) { // stupid error code but demanded by POSIX