The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pthread.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file pthread.h
5 * @brief POSIX threads: start, wait for and end threads; mutexes,
6 * condition variables, read-write locks, barriers, spin locks,
7 * one-time initialization and thread-specific data.
8 *
9 * each thread is a kernel thread (clone) with its own stack and
10 * thread-local storage. the locks sleep on futexes, which are private to
11 * the process: PTHREAD_PROCESS_SHARED is not supported. there is no
12 * cancellation.
13 *
14 * the functions return 0 or an error number; they do not set errno.
15 * every object of these types is valid when it is all zero bytes, so the
16 * static initializers are {0}.
17 */
18
19#include <sched.h>
20#include <signal.h>
21#include <stddef.h>
22#include <time.h>
23
24typedef unsigned long pthread_t;
25
26typedef struct
27{
29 size_t stack_size;
30 size_t guard_size;
32
33#define PTHREAD_CREATE_JOINABLE 0
34#define PTHREAD_CREATE_DETACHED 1
35
36#define PTHREAD_PROCESS_PRIVATE 0
37#define PTHREAD_PROCESS_SHARED 1
38
39typedef struct
40{
41 volatile int lock; // 0 free, 1 locked, 2 locked with threads asleep
42 int type;
43 volatile int owner; // the thread id, for the recursive and error-check types
44 int count; // the times a recursive mutex is locked
46
47typedef struct
48{
49 int type;
51
52#define PTHREAD_MUTEX_NORMAL 0
53#define PTHREAD_MUTEX_RECURSIVE 1
54#define PTHREAD_MUTEX_ERRORCHECK 2
55#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
56
57#define PTHREAD_MUTEX_INITIALIZER {0}
58
59typedef struct
60{
61 volatile int sequence; // changes with each signal and broadcast
62 int clock; // the clock of a timed wait
64
65typedef struct
66{
67 int clock;
69
70#define PTHREAD_COND_INITIALIZER {0}
71
72typedef struct
73{
77 int readers; // the threads that hold it to read
78 int writer; // 1 while a thread holds it to write
79 int writers_waiting; // readers wait for these first
81
82typedef struct
83{
84 int unused;
86
87#define PTHREAD_RWLOCK_INITIALIZER {0}
88
89typedef struct
90{
93 unsigned count;
94 unsigned waiting;
95 unsigned round;
97
98typedef struct
99{
102
103#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
104
105typedef volatile int pthread_spinlock_t;
106
107typedef volatile int pthread_once_t;
108#define PTHREAD_ONCE_INIT 0
109
110typedef unsigned pthread_key_t;
111
112int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *),
113 void *arg);
114int pthread_join(pthread_t thread, void **result);
116void pthread_exit(void *result) __attribute__((noreturn));
119int pthread_kill(pthread_t thread, int sig);
120int pthread_sigmask(int how, const sigset_t *set, sigset_t *old);
121// every thread is SCHED_OTHER, priority 0; another policy is EPERM
122int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
123int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
124
128int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *state);
130int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *size);
132int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *size);
133
138int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
145
150 const struct timespec *abstime);
158
163int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *abstime);
166int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *abstime);
170
172 unsigned count);
177
183
184int pthread_once(pthread_once_t *once, void (*init)(void));
185
186int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
189int pthread_setspecific(pthread_key_t key, const void *value);
signals: kill, sigaction, masks.
unsigned int sigset_t
Definition signal.h:13
NULL, size_t, ptrdiff_t, offsetof.
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock)
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t size)
int pthread_condattr_destroy(pthread_condattr_t *attr)
volatile int pthread_once_t
Definition pthread.h:107
int pthread_attr_init(pthread_attr_t *attr)
int pthread_rwlock_destroy(pthread_rwlock_t *lock)
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
int pthread_cond_signal(pthread_cond_t *cond)
int pthread_setspecific(pthread_key_t key, const void *value)
int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock)
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
int pthread_mutex_trylock(pthread_mutex_t *mutex)
int pthread_rwlock_unlock(pthread_rwlock_t *lock)
int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
int pthread_join(pthread_t thread, void **result)
int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock)
int pthread_condattr_init(pthread_condattr_t *attr)
int pthread_spin_unlock(pthread_spinlock_t *lock)
int pthread_mutex_destroy(pthread_mutex_t *mutex)
int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int shared)
int pthread_attr_destroy(pthread_attr_t *attr)
pthread_t pthread_self(void)
int pthread_spin_lock(pthread_spinlock_t *lock)
int pthread_kill(pthread_t thread, int sig)
unsigned long pthread_t
Definition pthread.h:24
int pthread_rwlock_init(pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr)
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg)
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *abstime)
volatile int pthread_spinlock_t
Definition pthread.h:105
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
int pthread_detach(pthread_t thread)
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
void pthread_exit(void *result) __attribute__((noreturn))
int pthread_barrier_wait(pthread_barrier_t *barrier)
int pthread_condattr_setpshared(pthread_condattr_t *attr, int shared)
int pthread_spin_destroy(pthread_spinlock_t *lock)
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *size)
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
int pthread_cond_destroy(pthread_cond_t *cond)
int pthread_equal(pthread_t a, pthread_t b)
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
int pthread_once(pthread_once_t *once, void(*init)(void))
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *abstime)
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
int pthread_spin_trylock(pthread_spinlock_t *lock)
void * pthread_getspecific(pthread_key_t key)
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
int pthread_sigmask(int how, const sigset_t *set, sigset_t *old)
int pthread_spin_init(pthread_spinlock_t *lock, int shared)
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *state)
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count)
unsigned pthread_key_t
Definition pthread.h:110
int pthread_barrier_destroy(pthread_barrier_t *barrier)
int pthread_key_delete(pthread_key_t key)
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t size)
int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *size)
int pthread_key_create(pthread_key_t *key, void(*destructor)(void *))
int pthread_rwlock_trywrlock(pthread_rwlock_t *lock)
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
int pthread_cond_broadcast(pthread_cond_t *cond)
the scheduler: give up the CPU.
ELF32 file header.
Definition elf.h:51
Definition pthread.h:27
size_t guard_size
Definition pthread.h:30
int detach_state
Definition pthread.h:28
size_t stack_size
Definition pthread.h:29
Definition pthread.h:90
unsigned count
Definition pthread.h:93
unsigned waiting
Definition pthread.h:94
pthread_mutex_t mutex
Definition pthread.h:91
pthread_cond_t all_in
Definition pthread.h:92
unsigned round
Definition pthread.h:95
Definition pthread.h:99
int unused
Definition pthread.h:100
Definition pthread.h:60
volatile int sequence
Definition pthread.h:61
int clock
Definition pthread.h:62
Definition pthread.h:66
int clock
Definition pthread.h:67
Definition pthread.h:40
int type
Definition pthread.h:42
volatile int owner
Definition pthread.h:43
volatile int lock
Definition pthread.h:41
int count
Definition pthread.h:44
Definition pthread.h:48
int type
Definition pthread.h:49
Definition pthread.h:73
int readers
Definition pthread.h:77
pthread_cond_t readers_go
Definition pthread.h:75
int writers_waiting
Definition pthread.h:79
int writer
Definition pthread.h:78
pthread_cond_t writers_go
Definition pthread.h:76
pthread_mutex_t mutex
Definition pthread.h:74
Definition pthread.h:83
int unused
Definition pthread.h:84
Definition sched.h:14
Definition time.h:26
clocks, sleeping and calendar time (UTC only: there are no time zones, so local time is UTC).
int clockid_t
Definition time.h:17
clock_t clock(void)