#define _PTHREAD_H
#include <sys/queue.h>
+#include <signal.h>
#include <vcore.h>
#include <uthread.h>
#include <mcs.h>
+#include <dtls.h>
+#include <spinlock.h>
#ifdef __cplusplus
extern "C" {
#endif
+/* Pthread states. These are mostly examples for other 2LSs */
+#define PTH_CREATED 1
+#define PTH_RUNNABLE 2
+#define PTH_RUNNING 3
+#define PTH_EXITING 4
+#define PTH_BLK_YIELDING 5 /* brief state btw pth_yield and pth_runnable */
+#define PTH_BLK_JOINING 6 /* joining on a child */
+#define PTH_BLK_SYSC 7 /* blocked on a syscall */
+#define PTH_BLK_MUTEX 8 /* blocked externally, possibly on a mutex */
+#define PTH_BLK_PAUSED 9 /* handed back to us from uthread code */
+
/* Pthread struct. First has to be the uthread struct, which the vcore code
* will access directly (as if pthread_tcb is a struct uthread). */
+struct pthread_tcb;
struct pthread_tcb {
struct uthread uthread;
TAILQ_ENTRY(pthread_tcb) next;
- int finished;
+ int state;
bool detached;
+ struct pthread_tcb *joiner; /* raced on by exit and join */
uint32_t id;
uint32_t stacksize;
+ void *stacktop;
void *(*start_routine)(void*);
void *arg;
- void *stacktop;
void *retval;
+ sigset_t sigmask;
+ sigset_t sigpending;
+ struct sigdata *sigdata;
};
typedef struct pthread_tcb* pthread_t;
TAILQ_HEAD(pthread_queue, pthread_tcb);
* kernel to signal us. We don't need a lock since this is per-vcore and
* accessed in vcore context. */
struct sysc_mgmt {
- struct event_queue ev_q;
+ struct event_queue *ev_q;
};
#define PTHREAD_ONCE_INIT 0
#define PTHREAD_BARRIER_SERIAL_THREAD 12345
-#define PTHREAD_MUTEX_INITIALIZER {0}
+#define PTHREAD_MUTEX_INITIALIZER {0,0}
#define PTHREAD_MUTEX_NORMAL 0
#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
#define PTHREAD_MUTEX_SPINS 100 // totally arbitrary
#define PTHREAD_BARRIER_SPINS 100 // totally arbitrary
-#define PTHREAD_COND_INITIALIZER {0}
+#define PTHREAD_COND_INITIALIZER {0,{0},{0},0}
#define PTHREAD_PROCESS_PRIVATE 0
+#define PTHREAD_PROCESS_SHARED 1
typedef struct
{
atomic_t lock;
} pthread_mutex_t;
-/* TODO: MAX_PTHREADS is arbitrarily defined for now.
- * It indicates the maximum number of threads that can wait on
- the same cond var/ barrier concurrently. */
-
-#define MAX_PTHREADS 32
typedef struct
{
- volatile int sense;
- int count;
- int nprocs;
- pthread_mutex_t pmutex;
+ int total_threads;
+ volatile int sense; /* state of barrier, flips btw runs */
+ atomic_t count;
+ struct spin_pdr_lock lock;
+ struct pthread_queue waiters;
+ int nr_waiters;
} pthread_barrier_t;
#define WAITER_CLEARED 0
#define PTHREAD_STACK_PAGES 4
#define PTHREAD_STACK_SIZE (PTHREAD_STACK_PAGES*PGSIZE)
+typedef int clockid_t;
typedef struct
{
int pshared;
+ clockid_t clock;
} pthread_condattr_t;
-
+/* Regarding the spinlock vs MCS, I don't expect this lock to be heavily
+ * contended. Most of the time, the caller already holds the mutex associated
+ * with the cond var. */
typedef struct
{
- const pthread_condattr_t* attr;
- uint32_t waiters[MAX_PTHREADS];
- uint32_t in_use[MAX_PTHREADS];
- uint32_t next_waiter; //start the search for an available waiter at this spot
+ struct pthread_queue waiters;
+ struct spin_pdr_lock spdr_lock;
+ int attr_pshared;
+ int attr_clock;
} pthread_cond_t;
+
typedef struct
{
+ void *stackaddr;
size_t stacksize;
int detachstate;
} pthread_attr_t;
typedef int pthread_barrierattr_t;
typedef int pthread_once_t;
-typedef void** pthread_key_t;
+typedef dtls_key_t pthread_key_t;
+
+/* Akaros pthread extensions / hacks */
+void pthread_can_vcore_request(bool can); /* default is TRUE */
+void pthread_need_tls(bool need); /* default is TRUE */
+void pthread_lib_init(void);
+void __pthread_generic_yield(struct pthread_tcb *pthread);
/* The pthreads API */
int pthread_attr_init(pthread_attr_t *);
int pthread_attr_destroy(pthread_attr_t *);
+int __pthread_create(pthread_t *, const pthread_attr_t *,
+ void *(*)(void *), void *);
int pthread_create(pthread_t *, const pthread_attr_t *,
void *(*)(void *), void *);
+int pthread_detach(pthread_t __th);
int pthread_join(pthread_t, void **);
int pthread_yield(void);
int pthread_attr_setdetachstate(pthread_attr_t *__attr,int __detachstate);
+int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
+int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
int pthread_mutex_destroy(pthread_mutex_t *);
int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
int pthread_condattr_init(pthread_condattr_t *);
int pthread_condattr_destroy(pthread_condattr_t *);
-int pthread_condattr_setpshared(pthread_condattr_t *, int);
int pthread_condattr_getpshared(pthread_condattr_t *, int *);
+int pthread_condattr_setpshared(pthread_condattr_t *, int);
+int pthread_condattr_getclock(const pthread_condattr_t *attr,
+ clockid_t *clock_id);
+int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id);
#define pthread_rwlock_t pthread_mutex_t
#define pthread_rwlockattr_t pthread_mutexattr_t
int pthread_barrier_wait(pthread_barrier_t* b);
int pthread_barrier_destroy(pthread_barrier_t* b);
-//added for redis compile
-int pthread_detach(pthread_t __th);
-int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
-int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
+// POSIX signal compliance
+int pthread_kill (pthread_t __threadid, int __signo);
+int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
+int pthread_sigqueue(pthread_t *thread, int sig, const union sigval value);
+
+// Dynamic TLS stuff
+int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
+int pthread_key_delete(pthread_key_t key);
+void *pthread_getspecific(pthread_key_t key);
+int pthread_setspecific(pthread_key_t key, const void *value);
+
+/* Common stuff. */
+int pthread_equal(pthread_t __thread1, pthread_t __thread2);
+int pthread_getattr_np(pthread_t __th, pthread_attr_t *__attr);
+int pthread_attr_getstack(const pthread_attr_t *__attr,
+ void **__stackaddr, size_t *__stacksize);
+
+/* Unsupported Stuff */
+typedef void *upthread_once_t;
+extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
+ const struct timespec *__restrict
+ __abstime) __THROWNL __nonnull ((1, 2));
+extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
+ pthread_mutex_t *__restrict __mutex,
+ const struct timespec *__restrict __abstime)
+ __nonnull ((1, 2, 3));
+extern int pthread_once (pthread_once_t *__once_control,
+ void (*__init_routine) (void)) __nonnull ((1, 2));
+extern int pthread_cancel (pthread_t __th);
#ifdef __cplusplus
}