Fix dependency on vcoreid in barrier and cond var.
[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         // will be slightly difficult to see this when given just a vcoreid and
29         // notif_tf ptr
30         bool dont_migrate;
31 };
32 typedef struct pthread_tcb* pthread_t;
33 TAILQ_HEAD(pthread_queue, pthread_tcb);
34
35 /* For compatibility with the existing pthread library */
36 enum
37 {
38   PTHREAD_CREATE_JOINABLE,
39 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
40   PTHREAD_CREATE_DETACHED
41 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
42 };
43
44 #define PTHREAD_ONCE_INIT 0
45 #define PTHREAD_BARRIER_SERIAL_THREAD 12345
46 #define PTHREAD_MUTEX_INITIALIZER {0}
47 #define PTHREAD_MUTEX_NORMAL 0
48 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
49 #define PTHREAD_MUTEX_SPINS 100 // totally arbitrary
50 #define PTHREAD_BARRIER_SPINS 100 // totally arbitrary
51 #define PTHREAD_COND_INITIALIZER {0}
52 #define PTHREAD_PROCESS_PRIVATE 0
53
54 typedef struct
55 {
56   int type;
57 } pthread_mutexattr_t;
58
59 typedef struct
60 {
61   const pthread_mutexattr_t* attr;
62   int lock;
63 } pthread_mutex_t;
64
65 /* TODO: MAX_PTHREADS is arbitrarily defined for now.
66  * It indicates the maximum number of threads that can wait on  
67    the same cond var/ barrier concurrently. */
68
69 #define MAX_PTHREADS 32
70 typedef struct
71 {
72   int local_sense[32*MAX_PTHREADS];
73   int in_use[MAX_PTHREADS];
74   volatile int sense;
75   int count;
76   int nprocs;
77   int next_slot;
78   pthread_mutex_t pmutex;
79 } pthread_barrier_t;
80
81 #define WAITER_CLEARED 0
82 #define WAITER_WAITING 1
83 #define SLOT_FREE 0
84 #define SLOT_IN_USE 1
85 typedef struct
86 {
87   int pshared;
88 } pthread_condattr_t;
89
90
91 typedef struct
92 {
93   const pthread_condattr_t* attr;
94   int waiters[MAX_PTHREADS];
95   int in_use[MAX_PTHREADS];
96   int next_waiter; //start the search for an available waiter at this spot
97 } pthread_cond_t;
98
99 typedef int pthread_attr_t;
100 typedef int pthread_barrierattr_t;
101 typedef int pthread_once_t;
102 typedef void** pthread_key_t;
103
104 /* The pthreads API */
105 int pthread_attr_init(pthread_attr_t *);
106 int pthread_attr_destroy(pthread_attr_t *);
107 int pthread_create(pthread_t *, const pthread_attr_t *,
108                    void *(*)(void *), void *);
109 int pthread_join(pthread_t, void **);
110 int pthread_yield(void);
111
112 int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
113
114 int pthread_mutex_destroy(pthread_mutex_t *);
115 int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
116 int pthread_mutex_lock(pthread_mutex_t *);
117 int pthread_mutex_trylock(pthread_mutex_t *);
118 int pthread_mutex_unlock(pthread_mutex_t *);
119 int pthread_mutex_destroy(pthread_mutex_t *);
120
121 int pthread_mutexattr_init(pthread_mutexattr_t *);
122 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
123 int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *);
124 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
125
126 int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
127 int pthread_cond_destroy(pthread_cond_t *);
128 int pthread_cond_broadcast(pthread_cond_t *);
129 int pthread_cond_signal(pthread_cond_t *);
130 int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
131
132 int pthread_condattr_init(pthread_condattr_t *);
133 int pthread_condattr_destroy(pthread_condattr_t *);
134 int pthread_condattr_setpshared(pthread_condattr_t *, int);
135 int pthread_condattr_getpshared(pthread_condattr_t *, int *);
136
137 #define pthread_rwlock_t pthread_mutex_t
138 #define pthread_rwlockattr_t pthread_mutexattr_t
139 #define pthread_rwlock_destroy pthread_mutex_destroy
140 #define pthread_rwlock_init pthread_mutex_init
141 #define pthread_rwlock_unlock pthread_mutex_unlock
142 #define pthread_rwlock_rdlock pthread_mutex_lock
143 #define pthread_rwlock_wrlock pthread_mutex_lock
144 #define pthread_rwlock_tryrdlock pthread_mutex_trylock
145 #define pthread_rwlock_trywrlock pthread_mutex_trylock
146
147 pthread_t pthread_self();
148 int pthread_equal(pthread_t t1, pthread_t t2);
149 void pthread_exit(void* ret);
150 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
151
152 int pthread_barrier_init(pthread_barrier_t* b, const pthread_barrierattr_t* a, int count);
153 int pthread_barrier_wait(pthread_barrier_t* b);
154 int pthread_barrier_destroy(pthread_barrier_t* b);
155
156 #ifdef __cplusplus
157   }
158 #endif
159
160 #endif