| 1 | //Mutex types
|
|---|
| 2 | enum{
|
|---|
| 3 | PTHREAD_MUTEX_NORMAL,
|
|---|
| 4 | PTHREAD_MUTEX_RECURSIVE,
|
|---|
| 5 | PTHREAD_MUTEX_ERRORCHECK
|
|---|
| 6 | };
|
|---|
| 7 |
|
|---|
| 8 | enum{
|
|---|
| 9 | PTHREAD_MUTEX_STALLED,
|
|---|
| 10 | PTHREAD_MUTEX_ROBUST
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | enum{
|
|---|
| 14 | PTHREAD_CREATE_DETACHED,
|
|---|
| 15 | PTHREAD_CREATE_JOINABLE
|
|---|
| 16 | };
|
|---|
| 17 |
|
|---|
| 18 | enum{
|
|---|
| 19 | PTHREAD_SCOPE_SYSTEM,
|
|---|
| 20 | PTHREAD_SCOPE_PROCESS
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | enum{
|
|---|
| 24 | PTHREAD_INHERIT_SCHED,
|
|---|
| 25 | PTHREAD_EXPLICIT_SCHED
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | //Mutex initializer
|
|---|
| 29 | #define __LOCK_INITIALIZER 0
|
|---|
| 30 | #define PTHREAD_MUTEX_INITIALIZER {0,0,0,PTHREAD_MUTEX_NORMAL,__LOCK_INITIALIZER}
|
|---|
| 31 | #define PTHREAD_COND_INITIALIZER {__LOCK_INITIALIZER,0}
|
|---|
| 32 |
|
|---|
| 33 | //Error definitions
|
|---|
| 34 | enum{
|
|---|
| 35 | EINVAL, //Designates an invalid value
|
|---|
| 36 | ENOTSUP,
|
|---|
| 37 | EOWNERDEAD, //Designates the termination of the owning thread
|
|---|
| 38 | EBUSY, //Mutex is already locked
|
|---|
| 39 | EDEADLK, //If mutex type is errorcheck and already owns the mutex
|
|---|
| 40 | EPERM //If mutex is robust or errorcheck and does not own the mutex
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | /* Struct definitions */
|
|---|
| 44 |
|
|---|
| 45 | /* pthread_attr_t struct definition
|
|---|
| 46 | Description: This struct corresponds to the pthread_attr_t which is the attribute of a pthread_t. It's fields
|
|---|
| 47 | define the way the pthread_t is able to interact (join/detach), (memory capacity), (scope) etc.
|
|---|
| 48 |
|
|---|
| 49 | Fields: void * stackaddr: The address of the attr's stack in memory
|
|---|
| 50 | size_t stacksize: The memory capacity for the attr's stack
|
|---|
| 51 | size_t guardsize: The guardsize attribute controls the size of the guard area for the created thread' stack. The
|
|---|
| 52 | guardsize attribute provides protection against overflow of the stack pointer.
|
|---|
| 53 | int detachstate: Defines a threads ability to join with two values: PTHREAD_CREATE_DETACHED and PTHREAD_CREATE_JOINABLE
|
|---|
| 54 | int inheritsched: The inheritance scheduling policy of the thread
|
|---|
| 55 | int contentionscope: Defines the contention scope of the thread
|
|---|
| 56 | int schedpolicy: Determines the scheduling policy of the thread
|
|---|
| 57 |
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | typedef struct {
|
|---|
| 61 | void *stackaddr;
|
|---|
| 62 | size_t stacksize;
|
|---|
| 63 | size_t guardsize;
|
|---|
| 64 | int detachstate;
|
|---|
| 65 | int inheritsched;
|
|---|
| 66 | int contentionscope;
|
|---|
| 67 | int schedpolicy;
|
|---|
| 68 | } pthread_attr_t;
|
|---|
| 69 |
|
|---|
| 70 | /* pthread_mutexattr_t struct definition
|
|---|
| 71 | Description: The pthread_mutexattr_t defines multiple attributes of a mutex and controls its interactions with threads
|
|---|
| 72 | Fields: robust: defines the robustness of the mutex; if robust and the owning thread terminates, it will notify the
|
|---|
| 73 | next thread of this to prevent deadlocks and other errors
|
|---|
| 74 | pshared: defines the process shared element of the thread and which processes can interact with the mutex
|
|---|
| 75 | protocol: defines the priority protocol of the mutex and which threads may interact first
|
|---|
| 76 | type: defines the type of the mutex as either PTHREAD_MUTEX_DEFAULT/NORMAL, PTHREAD_MUTEX_ERRORCHECK, or
|
|---|
| 77 | PTHREAD_MUTEX_RECURSIVE, each explained in pthread_mutex_lock below
|
|---|
| 78 | prioceiling: defines the lowest priority the mutex's critical section can be at
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | typedef struct {
|
|---|
| 82 | int robust;
|
|---|
| 83 | int pshared;
|
|---|
| 84 | int protocol;
|
|---|
| 85 | int type;
|
|---|
| 86 | int prioceiling;
|
|---|
| 87 | }pthread_mutexattr_t;
|
|---|
| 88 |
|
|---|
| 89 | /* pthread_mutex_t struct definition
|
|---|
| 90 | Description: The pthread_mutex_t is a locking mechanism for threads to interact with in order to control the scheduling
|
|---|
| 91 | of the threads. It can be locked, which allows for blocking of other threads waiting on the mutex and unlocked, allowing
|
|---|
| 92 | access. It has a pthread_mutexattr_t which defines its behavior.
|
|---|
| 93 | Fields: count - used for recursive mutex, incremented when locked, decremented when unlocked, mutex released when count is 0
|
|---|
| 94 | owner - current process owner of the mutex
|
|---|
| 95 | lock - int of 0 or 1, respectively 0 if unlocked, 1 if locked
|
|---|
| 96 | prioceiling - allows locking without adherence to the priority ceiling
|
|---|
| 97 | attr - see above
|
|---|
| 98 | */
|
|---|
| 99 |
|
|---|
| 100 | typedef struct {
|
|---|
| 101 | int count;
|
|---|
| 102 | $proc owner;
|
|---|
| 103 | int lock;
|
|---|
| 104 | int prioceiling;
|
|---|
| 105 | pthread_mutexattr_t *attr;
|
|---|
| 106 | } pthread_mutex_t;
|
|---|
| 107 |
|
|---|
| 108 | //Unimplemented
|
|---|
| 109 | /*
|
|---|
| 110 | typedef struct {
|
|---|
| 111 | int pshared;
|
|---|
| 112 | clockid_t clock_id;
|
|---|
| 113 | }pthread_condattr_t;
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | /* pthread_cond_t struct definition
|
|---|
| 117 | Description: The pthread_cond_t is another locking mechanism which interacts with the mutex variable. When
|
|---|
| 118 | the mutex is locked, the condition can be accessed, leading the accessing thread to unlock it, and sleep
|
|---|
| 119 | until the signal is given
|
|---|
| 120 | Fields: proccount - specifies the number of processes/threads still waiting on this condition variable
|
|---|
| 121 | signal - Boolean value stating whether the condition is satisfied (indicated by 1) or not (0)
|
|---|
| 122 | */
|
|---|
| 123 |
|
|---|
| 124 | typedef struct {
|
|---|
| 125 | int proccount;
|
|---|
| 126 | _Bool signal;
|
|---|
| 127 | } pthread_cond_t;
|
|---|
| 128 |
|
|---|
| 129 | /* pthread_t struct definition
|
|---|
| 130 | Description: The pthread_t is a struct containing a $proc variable as well as a thread attribute which defines
|
|---|
| 131 | its interactions with other threads. It encapsulates the $proc and allows attributes to apply to it.
|
|---|
| 132 | Fields: thr: the $proc variable that is the heart of the thread
|
|---|
| 133 | attr: see above
|
|---|
| 134 | */
|
|---|
| 135 |
|
|---|
| 136 | typedef struct {
|
|---|
| 137 | $proc thr;
|
|---|
| 138 | const pthread_attr_t *attr;
|
|---|
| 139 | } pthread_t;
|
|---|
| 140 |
|
|---|
| 141 | typedef struct __pthread_pool_t {
|
|---|
| 142 | pthread_t** threads;
|
|---|
| 143 | int len;
|
|---|
| 144 | } __pthread_pool_t;
|
|---|