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