15 /* Pthread states. These are mostly examples for other 2LSs */
17 #define PTH_RUNNABLE 2
20 #define PTH_BLK_YIELDING 5 /* brief state btw pth_yield and pth_runnable */
21 #define PTH_BLK_JOINING 6 /* joining on a child */
22 #define PTH_BLK_SYSC 7 /* blocked on a syscall */
23 #define PTH_BLK_MUTEX 8 /* blocked externally, possibly on a mutex */
24 #define PTH_BLK_PAUSED 9 /* handed back to us from uthread code */
26 /* Pthread struct. First has to be the uthread struct, which the vcore code
27 * will access directly (as if pthread_tcb is a struct uthread). */
30 struct uthread uthread;
31 TAILQ_ENTRY(pthread_tcb) next;
34 struct pthread_tcb *joiner; /* raced on by exit and join */
38 void *(*start_routine)(void*);
43 typedef struct pthread_tcb* pthread_t;
44 TAILQ_HEAD(pthread_queue, pthread_tcb);
46 /* Per-vcore data structures to manage syscalls. The ev_q is where we tell the
47 * kernel to signal us. We don't need a lock since this is per-vcore and
48 * accessed in vcore context. */
50 struct event_queue *ev_q;
53 #define PTHREAD_ONCE_INIT 0
54 #define PTHREAD_BARRIER_SERIAL_THREAD 12345
55 #define PTHREAD_MUTEX_INITIALIZER {0,0}
56 #define PTHREAD_MUTEX_NORMAL 0
57 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
58 #define PTHREAD_MUTEX_SPINS 100 // totally arbitrary
59 #define PTHREAD_BARRIER_SPINS 100 // totally arbitrary
60 #define PTHREAD_COND_INITIALIZER {0,{0},{0},0}
61 #define PTHREAD_PROCESS_PRIVATE 0
66 } pthread_mutexattr_t;
70 const pthread_mutexattr_t* attr;
74 /* TODO: MAX_PTHREADS is arbitrarily defined for now.
75 * It indicates the maximum number of threads that can wait on
76 the same cond var/ barrier concurrently. */
78 #define MAX_PTHREADS 32
84 pthread_mutex_t pmutex;
87 #define WAITER_CLEARED 0
88 #define WAITER_WAITING 1
95 PTHREAD_CREATE_JOINABLE,
96 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
97 PTHREAD_CREATE_DETACHED
98 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
101 // TODO: how big do we want these? ideally, we want to be able to guard and map
102 // more space if we go too far.
103 #define PTHREAD_STACK_PAGES 4
104 #define PTHREAD_STACK_SIZE (PTHREAD_STACK_PAGES*PGSIZE)
109 } pthread_condattr_t;
114 const pthread_condattr_t* attr;
115 uint32_t waiters[MAX_PTHREADS];
116 uint32_t in_use[MAX_PTHREADS];
117 uint32_t next_waiter; //start the search for an available waiter at this spot
124 typedef int pthread_barrierattr_t;
125 typedef int pthread_once_t;
126 typedef dtls_key_t pthread_key_t;
128 /* Akaros pthread extensions / hacks */
129 void pthread_can_vcore_request(bool can); /* default is TRUE */
130 int pthread_lib_init(void);
132 /* The pthreads API */
133 int pthread_attr_init(pthread_attr_t *);
134 int pthread_attr_destroy(pthread_attr_t *);
135 int pthread_create(pthread_t *, const pthread_attr_t *,
136 void *(*)(void *), void *);
137 int pthread_detach(pthread_t __th);
138 int pthread_join(pthread_t, void **);
139 int pthread_yield(void);
141 int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
142 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
143 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
145 int pthread_mutex_destroy(pthread_mutex_t *);
146 int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
147 int pthread_mutex_lock(pthread_mutex_t *);
148 int pthread_mutex_trylock(pthread_mutex_t *);
149 int pthread_mutex_unlock(pthread_mutex_t *);
150 int pthread_mutex_destroy(pthread_mutex_t *);
152 int pthread_mutexattr_init(pthread_mutexattr_t *);
153 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
154 int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *);
155 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
157 int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
158 int pthread_cond_destroy(pthread_cond_t *);
159 int pthread_cond_broadcast(pthread_cond_t *);
160 int pthread_cond_signal(pthread_cond_t *);
161 int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
163 int pthread_condattr_init(pthread_condattr_t *);
164 int pthread_condattr_destroy(pthread_condattr_t *);
165 int pthread_condattr_setpshared(pthread_condattr_t *, int);
166 int pthread_condattr_getpshared(pthread_condattr_t *, int *);
168 #define pthread_rwlock_t pthread_mutex_t
169 #define pthread_rwlockattr_t pthread_mutexattr_t
170 #define pthread_rwlock_destroy pthread_mutex_destroy
171 #define pthread_rwlock_init pthread_mutex_init
172 #define pthread_rwlock_unlock pthread_mutex_unlock
173 #define pthread_rwlock_rdlock pthread_mutex_lock
174 #define pthread_rwlock_wrlock pthread_mutex_lock
175 #define pthread_rwlock_tryrdlock pthread_mutex_trylock
176 #define pthread_rwlock_trywrlock pthread_mutex_trylock
178 pthread_t pthread_self();
179 int pthread_equal(pthread_t t1, pthread_t t2);
180 void pthread_exit(void* ret);
181 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
183 int pthread_barrier_init(pthread_barrier_t* b, const pthread_barrierattr_t* a, int count);
184 int pthread_barrier_wait(pthread_barrier_t* b);
185 int pthread_barrier_destroy(pthread_barrier_t* b);
187 // POSIX signal compliance
188 int pthread_kill (pthread_t __threadid, int __signo);
189 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
190 int pthread_sigqueue(pthread_t *thread, int sig, const union sigval value);
193 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
194 int pthread_key_delete(pthread_key_t key);
195 void *pthread_getspecific(pthread_key_t key);
196 int pthread_setspecific(pthread_key_t key, const void *value);