Pthread create, join, and exit
[akaros.git] / user / include / pthread.h
1 #ifndef _PTHREAD_H
2 #define _PTHREAD_H
3
4 #include <sys/queue.h>
5 #include <vcore.h>
6 #include <mcs.h>
7
8 #ifdef __cplusplus
9   extern "C" {
10 #endif
11
12 /* Our internal types for pthread management */
13 struct pthread_tcb {
14         TAILQ_ENTRY(pthread_tcb) next;
15         void* (*start_routine)(void*);
16         void* arg;
17
18         struct user_trapframe utf;
19         struct ancillary_state as;
20         void *tls_desc;
21         void *stacktop;
22         uint32_t id;
23
24         int finished;
25         void *retval;
26         int detached;
27         // whether or not the scheduler can migrate you from your vcore
28         bool dont_migrate;
29 };
30 typedef struct pthread_tcb* pthread_t;
31 TAILQ_HEAD(pthread_queue, pthread_tcb);
32
33 /* For compatibility with the existing pthread library */
34 enum
35 {
36   PTHREAD_CREATE_JOINABLE,
37 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
38   PTHREAD_CREATE_DETACHED
39 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
40 };
41
42 #define PTHREAD_ONCE_INIT 0
43 #define PTHREAD_BARRIER_SERIAL_THREAD 12345
44 #define PTHREAD_MUTEX_INITIALIZER {0}
45 #define PTHREAD_MUTEX_NORMAL 0
46 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
47 #define PTHREAD_COND_INITIALIZER {0}
48 #define PTHREAD_PROCESS_PRIVATE 0
49
50 typedef struct
51 {
52   int type;
53 } pthread_mutexattr_t;
54
55 typedef struct
56 {
57   const pthread_mutexattr_t* attr;
58   int lock;
59 } pthread_mutex_t;
60
61 typedef struct
62 {
63   int local_sense[32*MAX_VCORES];
64   volatile int sense;
65   int count;
66   int nprocs;
67   mcs_lock_t lock;
68 } pthread_barrier_t;
69
70 typedef struct
71 {
72   int pshared;
73 } pthread_condattr_t;
74
75 typedef struct
76 {
77   const pthread_condattr_t* attr;
78   int waiters[MAX_VCORES];
79 } pthread_cond_t;
80
81 typedef int pthread_attr_t;
82 typedef int pthread_barrierattr_t;
83 typedef int pthread_once_t;
84 typedef void** pthread_key_t;
85
86 /* The pthreads API */
87 int pthread_attr_init(pthread_attr_t *);
88 int pthread_attr_destroy(pthread_attr_t *);
89 int pthread_create(pthread_t *, const pthread_attr_t *,
90                    void *(*)(void *), void *);
91 int pthread_join(pthread_t, void **);
92
93 int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
94
95 int pthread_mutex_destroy(pthread_mutex_t *);
96 int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
97 int pthread_mutex_lock(pthread_mutex_t *);
98 int pthread_mutex_trylock(pthread_mutex_t *);
99 int pthread_mutex_unlock(pthread_mutex_t *);
100 int pthread_mutex_destroy(pthread_mutex_t *);
101
102 int pthread_mutexattr_init(pthread_mutexattr_t *);
103 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
104 int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *);
105 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
106
107 int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
108 int pthread_cond_destroy(pthread_cond_t *);
109 int pthread_cond_broadcast(pthread_cond_t *);
110 int pthread_cond_signal(pthread_cond_t *);
111 int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
112
113 int pthread_condattr_init(pthread_condattr_t *);
114 int pthread_condattr_destroy(pthread_condattr_t *);
115 int pthread_condattr_setpshared(pthread_condattr_t *, int);
116 int pthread_condattr_getpshared(pthread_condattr_t *, int *);
117
118 #define pthread_rwlock_t pthread_mutex_t
119 #define pthread_rwlockattr_t pthread_mutexattr_t
120 #define pthread_rwlock_destroy pthread_mutex_destroy
121 #define pthread_rwlock_init pthread_mutex_init
122 #define pthread_rwlock_unlock pthread_mutex_unlock
123 #define pthread_rwlock_rdlock pthread_mutex_lock
124 #define pthread_rwlock_wrlock pthread_mutex_lock
125 #define pthread_rwlock_tryrdlock pthread_mutex_trylock
126 #define pthread_rwlock_trywrlock pthread_mutex_trylock
127
128 pthread_t pthread_self();
129 int pthread_equal(pthread_t t1, pthread_t t2);
130 void pthread_exit(void* ret);
131 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
132
133 int pthread_barrier_init(pthread_barrier_t* b, const pthread_barrierattr_t* a, int count);
134 int pthread_barrier_wait(pthread_barrier_t* b);
135 int pthread_barrier_destroy(pthread_barrier_t* b);
136
137 #ifdef __cplusplus
138   }
139 #endif
140
141 #endif