added some pthread_* and pthread_attr_* naive functions
I mapped pthread_t to thread_id for simplicity git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17879 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -5,8 +5,9 @@
|
|||||||
#define _PTHREAD_H_
|
#define _PTHREAD_H_
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <OS.h>
|
||||||
|
|
||||||
typedef struct _pthread *pthread_t;
|
typedef thread_id pthread_t;
|
||||||
typedef struct _pthread_attr *pthread_attr_t;
|
typedef struct _pthread_attr *pthread_attr_t;
|
||||||
typedef struct _pthread_mutex *pthread_mutex_t;
|
typedef struct _pthread_mutex *pthread_mutex_t;
|
||||||
typedef struct _pthread_mutexattr *pthread_mutexattr_t;
|
typedef struct _pthread_mutexattr *pthread_mutexattr_t;
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ SubDir HAIKU_TOP src system libroot posix pthread ;
|
|||||||
UsePrivateHeaders libroot ;
|
UsePrivateHeaders libroot ;
|
||||||
|
|
||||||
MergeObject posix_pthread.o :
|
MergeObject posix_pthread.o :
|
||||||
|
pthread.c
|
||||||
pthread_atfork.c
|
pthread_atfork.c
|
||||||
|
pthread_attr.c
|
||||||
pthread_mutex.c
|
pthread_mutex.c
|
||||||
pthread_mutexattr.c
|
pthread_mutexattr.c
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2006, Jérôme Duval. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
** Copyright 2006, Jérôme Duval. All rights reserved.
|
||||||
|
** Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include "pthread_private.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -25,4 +25,9 @@ typedef struct _pthread_mutex {
|
|||||||
pthread_mutexattr attr;
|
pthread_mutexattr attr;
|
||||||
} pthread_mutex;
|
} pthread_mutex;
|
||||||
|
|
||||||
|
typedef struct _pthread_attr {
|
||||||
|
int32 detach_state;
|
||||||
|
int32 sched_priority;
|
||||||
|
} pthread_attr;
|
||||||
|
|
||||||
#endif /* _PTHREAD_PRIVATE_H_ */
|
#endif /* _PTHREAD_PRIVATE_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user