util: add the cat function as a utility
[akaros.git] / user / pthread / pthread.h
1 #pragma once
2
3 #include <sys/queue.h>
4 #include <signal.h>
5 #include <parlib/vcore.h>
6 #include <parlib/uthread.h>
7 #include <parlib/mcs.h>
8 #include <parlib/dtls.h>
9 #include <parlib/spinlock.h>
10 #include <parlib/signal.h>
11 #include <parlib/parlib.h>
12 /* GNU / POSIX scheduling crap */
13 #include <sched.h>
14
15 __BEGIN_DECLS
16
17 /* Pthread states.  These are mostly examples for other 2LSs */
18 #define PTH_CREATED                     1
19 #define PTH_RUNNABLE            2
20 #define PTH_RUNNING                     3
21 #define PTH_EXITING                     4
22 #define PTH_BLK_YIELDING        5       /* brief state btw pth_yield and pth_runnable */
23 #define PTH_BLK_SYSC            6       /* blocked on a syscall */
24 #define PTH_BLK_MUTEX           7       /* blocked externally, possibly on a mutex */
25 #define PTH_BLK_PAUSED          8       /* handed back to us from uthread code */
26 #define PTH_BLK_MISC            9       /* catch-all from uthread code */
27
28 /* Entry for a pthread_cleanup_routine on the stack of cleanup handlers. */
29 struct pthread_cleanup_routine {
30         SLIST_ENTRY(pthread_cleanup_routine) cr_next;
31         void (*routine)(void *);
32         void *arg;
33 };
34 SLIST_HEAD(pthread_cleanup_stack, pthread_cleanup_routine);
35
36 /* Pthread struct.  First has to be the uthread struct, which the vcore code
37  * will access directly (as if pthread_tcb is a struct uthread). */
38 struct pthread_tcb;
39 struct pthread_tcb {
40         struct uthread uthread;
41         TAILQ_ENTRY(pthread_tcb) tq_next;
42         int state;
43         uint32_t id;
44         uint32_t stacksize;
45         void *stacktop;
46         void *(*start_routine)(void*);
47         void *arg;
48         struct pthread_cleanup_stack cr_stack;
49 };
50 typedef struct pthread_tcb* pthread_t;
51 TAILQ_HEAD(pthread_queue, pthread_tcb);
52
53 /* Per-vcore data structures to manage syscalls.  The ev_q is where we tell the
54  * kernel to signal us.  We don't need a lock since this is per-vcore and
55  * accessed in vcore context. */
56 struct sysc_mgmt {
57         struct event_queue                      *ev_q;
58 };
59
60 #define PTHREAD_ONCE_INIT PARLIB_ONCE_INIT
61 #define PTHREAD_BARRIER_SERIAL_THREAD 12345
62 #define PTHREAD_BARRIER_SPINS 100 // totally arbitrary
63 #define PTHREAD_PROCESS_PRIVATE 0
64 #define PTHREAD_PROCESS_SHARED 1
65
66 #define PTHREAD_MUTEX_NORMAL 0
67 #define PTHREAD_MUTEX_RECURSIVE 1
68 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
69 typedef struct {
70         int type;
71 } pthread_mutexattr_t;
72
73 typedef struct {
74         int                                                     type;
75         uth_mutex_t                                     mtx;
76         uth_recurse_mutex_t                     r_mtx;
77 } pthread_mutex_t;
78 #define PTHREAD_MUTEX_INITIALIZER { PTHREAD_MUTEX_DEFAULT, UTH_MUTEX_INIT, \
79                                     UTH_RECURSE_MUTEX_INIT }
80
81 typedef int clockid_t;
82 typedef struct {
83         int                                                     pshared;
84         clockid_t                                       clock;
85 } pthread_condattr_t;
86 #define PTHREAD_COND_INITIALIZER UTH_COND_VAR_INIT
87
88 /* Regarding the spinlock vs MCS, I don't expect this lock to be heavily
89  * contended.  Most of the time, the caller already holds the mutex associated
90  * with the cond var. */
91 typedef uth_cond_var_t pthread_cond_t;
92
93 typedef uth_rwlock_t pthread_rwlock_t;
94 #define PTHREAD_RWLOCK_INITIALIZER UTH_RWLOCK_INIT
95 /* Will try to avoid the rwlockattr_t business for now */
96 typedef void * pthread_rwlockattr_t;
97
98 typedef struct
99 {
100         int                                                     total_threads;
101         volatile int                            sense;  /* state of barrier, flips btw runs */
102         atomic_t                                        count;
103         struct spin_pdr_lock            lock;
104         uth_sync_t                                      waiters;
105         int                                                     nr_waiters;
106 } pthread_barrier_t;
107
108 /* Detach state.  */
109 enum
110 {
111   PTHREAD_CREATE_JOINABLE,
112 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
113   PTHREAD_CREATE_DETACHED
114 #define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
115 };
116
117 #define PTHREAD_STACK_PAGES 1024
118 #define PTHREAD_STACK_SIZE (PTHREAD_STACK_PAGES*PGSIZE)
119 #define PTHREAD_STACK_MIN PTHREAD_STACK_SIZE
120
121 typedef struct 
122 {
123         void *stackaddr;
124         size_t stacksize;
125         size_t guardsize;
126         int detachstate;
127         int sched_priority;
128         int sched_policy;
129         int sched_inherit;
130 } pthread_attr_t;
131 typedef int pthread_barrierattr_t;
132 typedef parlib_once_t pthread_once_t;
133 typedef dtls_key_t pthread_key_t;
134
135 /* Akaros pthread extensions / hacks */
136 void pthread_need_tls(bool need);                       /* default is TRUE */
137 void pthread_mcp_init(void);
138 void __pthread_generic_yield(struct pthread_tcb *pthread);
139
140 /* Profiling alarms for pthreads.  (profalarm.c) */
141 void enable_profalarm(uint64_t usecs);
142 void disable_profalarm(void);
143
144 /* The pthreads API */
145 int pthread_attr_init(pthread_attr_t *);
146 int pthread_attr_destroy(pthread_attr_t *);
147 int __pthread_create(pthread_t *, const pthread_attr_t *,
148                      void *(*)(void *), void *);
149 int pthread_create(pthread_t *, const pthread_attr_t *,
150                    void *(*)(void *), void *);
151 int pthread_detach(pthread_t __th);
152 int pthread_join(pthread_t, void **);
153 int pthread_yield(void);
154
155 int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
156 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
157 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
158 int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
159 int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);
160
161 int pthread_mutex_destroy(pthread_mutex_t *);
162 int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
163 int pthread_mutex_lock(pthread_mutex_t *);
164 int pthread_mutex_trylock(pthread_mutex_t *);
165 int pthread_mutex_unlock(pthread_mutex_t *);
166 int pthread_mutex_destroy(pthread_mutex_t *);
167
168 int pthread_mutexattr_init(pthread_mutexattr_t *);
169 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
170 int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *);
171 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
172
173 int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
174 int pthread_cond_destroy(pthread_cond_t *);
175 int pthread_cond_broadcast(pthread_cond_t *);
176 int pthread_cond_signal(pthread_cond_t *);
177 int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
178
179 int pthread_condattr_init(pthread_condattr_t *);
180 int pthread_condattr_destroy(pthread_condattr_t *);
181 int pthread_condattr_getpshared(pthread_condattr_t *, int *);
182 int pthread_condattr_setpshared(pthread_condattr_t *, int);
183 int pthread_condattr_getclock(const pthread_condattr_t *attr,
184                               clockid_t *clock_id);
185 int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id);
186
187 int pthread_rwlock_init(pthread_rwlock_t *rwl, const pthread_rwlockattr_t *a);
188 int pthread_rwlock_destroy(pthread_rwlock_t *rwl);
189 int pthread_rwlock_rdlock(pthread_rwlock_t *rwl);
190 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwl);
191 int pthread_rwlock_wrlock(pthread_rwlock_t *rwl);
192 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwl);
193 int pthread_rwlock_unlock(pthread_rwlock_t *rwl);
194
195 pthread_t pthread_self();
196 int pthread_equal(pthread_t t1, pthread_t t2);
197 void pthread_exit(void* ret);
198 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
199
200 int pthread_barrier_init(pthread_barrier_t* b, const pthread_barrierattr_t* a, int count);
201 int pthread_barrier_wait(pthread_barrier_t* b);
202 int pthread_barrier_destroy(pthread_barrier_t* b);
203
204 // POSIX signal compliance
205 int pthread_kill (pthread_t __threadid, int __signo);
206 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
207 int pthread_sigqueue(pthread_t *thread, int sig, const union sigval value);
208
209 // Dynamic TLS stuff
210 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
211 int pthread_key_delete(pthread_key_t key);
212 void *pthread_getspecific(pthread_key_t key);
213 int pthread_setspecific(pthread_key_t key, const void *value);
214
215 /* Common stuff. */
216 int pthread_equal(pthread_t __thread1, pthread_t __thread2);
217 int pthread_getattr_np(pthread_t __th, pthread_attr_t *__attr);
218 int pthread_attr_getstack(const pthread_attr_t *__attr,
219                            void **__stackaddr, size_t *__stacksize);
220 int pthread_cancel(pthread_t __th);
221 void pthread_cleanup_push(void (*routine)(void *), void *arg);
222 void pthread_cleanup_pop(int execute);
223
224 /* Scheduling Stuff, mostly ignored by the actual 2LS */
225 int pthread_attr_setschedparam(pthread_attr_t *attr,
226                                const struct sched_param *param);
227 int pthread_attr_getschedparam(pthread_attr_t *attr,
228                                struct sched_param *param);
229 /* Policies are from sched.h. */
230 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
231 int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
232
233 #define PTHREAD_SCOPE_SYSTEM    1
234 #define PTHREAD_SCOPE_PROCESS   2
235 int pthread_attr_setscope(pthread_attr_t *attr, int scope);
236 int pthread_attr_getscope(pthread_attr_t *attr, int *scope);
237
238 #define PTHREAD_INHERIT_SCHED   1
239 #define PTHREAD_EXPLICIT_SCHED  2
240 int pthread_attr_setinheritsched(pthread_attr_t *attr,
241                                  int inheritsched);
242 int pthread_attr_getinheritsched(const pthread_attr_t *attr,
243                                  int *inheritsched);
244
245 int pthread_setschedparam(pthread_t thread, int policy,
246                           const struct sched_param *param);
247 int pthread_getschedparam(pthread_t thread, int *policy,
248                           struct sched_param *param);
249
250 /* Unsupported Stuff */
251 extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
252                     const struct timespec *__restrict
253                     __abstime) __THROWNL __nonnull ((1, 2));
254 extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
255                    pthread_mutex_t *__restrict __mutex,
256                    const struct timespec *__restrict __abstime)
257      __nonnull ((1, 2, 3));
258
259 __END_DECLS