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