Addition of c3po library, including revamp of Make system for user libraries.
[akaros.git] / user / pthread / 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 stacksize;
23         uint32_t id;
24
25         int finished;
26         void *retval;
27         bool detached;
28         // whether or not the scheduler can migrate you from your vcore
29         // will be slightly difficult to see this when given just a vcoreid and
30         // notif_tf ptr
31         bool dont_migrate;
32 };
33 typedef struct pthread_tcb* pthread_t;
34 TAILQ_HEAD(pthread_queue, pthread_tcb);
35
36 #define PTHREAD_ONCE_INIT 0
37 #define PTHREAD_BARRIER_SERIAL_THREAD 12345
38 #define PTHREAD_MUTEX_INITIALIZER {0}
39 #define PTHREAD_MUTEX_NORMAL 0
40 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
41 #define PTHREAD_MUTEX_SPINS 100 // totally arbitrary
42 #define PTHREAD_BARRIER_SPINS 100 // totally arbitrary
43 #define PTHREAD_COND_INITIALIZER {0}
44 #define PTHREAD_PROCESS_PRIVATE 0
45
46 typedef struct
47 {
48   int type;
49 } pthread_mutexattr_t;
50
51 typedef struct
52 {
53   const pthread_mutexattr_t* attr;
54   int lock;
55 } pthread_mutex_t;
56
57 /* TODO: MAX_PTHREADS is arbitrarily defined for now.
58  * It indicates the maximum number of threads that can wait on  
59    the same cond var/ barrier concurrently. */
60
61 #define MAX_PTHREADS 32
62 typedef struct
63 {
64   volatile int sense;
65   int count;
66   int nprocs;
67   pthread_mutex_t pmutex;
68 } pthread_barrier_t;
69
70 #define WAITER_CLEARED 0
71 #define WAITER_WAITING 1
72 #define SLOT_FREE 0
73 #define SLOT_IN_USE 1
74
75 /* Detach state.  */
76 enum
77 {
78   PTHREAD_CREATE_JOINABLE,
79 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
80   PTHREAD_CREATE_DETACHED
81 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
82 };
83
84 // TODO: how big do we want these?  ideally, we want to be able to guard and map
85 // more space if we go too far.
86 #define PTHREAD_STACK_PAGES 4
87 #define PTHREAD_STACK_SIZE (PTHREAD_STACK_PAGES*PGSIZE)
88
89 typedef struct
90 {
91   int pshared;
92 } pthread_condattr_t;
93
94
95 typedef struct
96 {
97   const pthread_condattr_t* attr;
98   int waiters[MAX_PTHREADS];
99   int in_use[MAX_PTHREADS];
100   int next_waiter; //start the search for an available waiter at this spot
101 } pthread_cond_t;
102 typedef struct 
103 {
104         size_t stacksize;
105         int detachstate;
106 } pthread_attr_t;
107 typedef int pthread_barrierattr_t;
108 typedef int pthread_once_t;
109 typedef void** pthread_key_t;
110
111 /* The pthreads API */
112 int pthread_attr_init(pthread_attr_t *);
113 int pthread_attr_destroy(pthread_attr_t *);
114 int pthread_create(pthread_t *, const pthread_attr_t *,
115                    void *(*)(void *), void *);
116 int pthread_join(pthread_t, void **);
117 int pthread_yield(void);
118
119 int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
120
121 int pthread_mutex_destroy(pthread_mutex_t *);
122 int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
123 int pthread_mutex_lock(pthread_mutex_t *);
124 int pthread_mutex_trylock(pthread_mutex_t *);
125 int pthread_mutex_unlock(pthread_mutex_t *);
126 int pthread_mutex_destroy(pthread_mutex_t *);
127
128 int pthread_mutexattr_init(pthread_mutexattr_t *);
129 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
130 int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *);
131 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
132
133 int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
134 int pthread_cond_destroy(pthread_cond_t *);
135 int pthread_cond_broadcast(pthread_cond_t *);
136 int pthread_cond_signal(pthread_cond_t *);
137 int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
138
139 int pthread_condattr_init(pthread_condattr_t *);
140 int pthread_condattr_destroy(pthread_condattr_t *);
141 int pthread_condattr_setpshared(pthread_condattr_t *, int);
142 int pthread_condattr_getpshared(pthread_condattr_t *, int *);
143
144 #define pthread_rwlock_t pthread_mutex_t
145 #define pthread_rwlockattr_t pthread_mutexattr_t
146 #define pthread_rwlock_destroy pthread_mutex_destroy
147 #define pthread_rwlock_init pthread_mutex_init
148 #define pthread_rwlock_unlock pthread_mutex_unlock
149 #define pthread_rwlock_rdlock pthread_mutex_lock
150 #define pthread_rwlock_wrlock pthread_mutex_lock
151 #define pthread_rwlock_tryrdlock pthread_mutex_trylock
152 #define pthread_rwlock_trywrlock pthread_mutex_trylock
153
154 pthread_t pthread_self();
155 int pthread_equal(pthread_t t1, pthread_t t2);
156 void pthread_exit(void* ret);
157 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
158
159 int pthread_barrier_init(pthread_barrier_t* b, const pthread_barrierattr_t* a, int count);
160 int pthread_barrier_wait(pthread_barrier_t* b);
161 int pthread_barrier_destroy(pthread_barrier_t* b);
162
163 //added for redis compile
164 int pthread_detach(pthread_t __th);
165 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
166 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
167
168 #ifdef __cplusplus
169   }
170 #endif
171
172 #endif