| 1 | #include "cor_platform.h" |
| 2 | #include <assert.h> |
| 3 | #include "timespec_utils.h" |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | void _Cor_Mutex_ctor(_Cor_Mutex *mut){ |
| 7 | int r; |
| 8 | r = pthread_mutex_init(&mut->mut, NULL); |
| 9 | assert(r == 0); |
| 10 | } |
| 11 | |
| 12 | |
| 13 | void _Cor_Mutex_dtor(_Cor_Mutex *mut){ |
| 14 | int r; |
| 15 | r = pthread_mutex_destroy(&mut->mut); |
| 16 | assert(r == 0); |
| 17 | } |
| 18 | |
| 19 | |
| 20 | void _Cor_Mutex_Lock(_Cor_Mutex *mut){ |
| 21 | int r; |
| 22 | r = pthread_mutex_lock(&mut->mut); |
| 23 | assert(r == 0); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | void _Cor_Mutex_Unlock(_Cor_Mutex *mut){ |
| 28 | int r; |
| 29 | r = pthread_mutex_unlock(&mut->mut); |
| 30 | assert(r == 0); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | int64_t _Cor_Realtime_Now(){ |
| 35 | int r; |
| 36 | struct timespec now; |
| 37 | r = clock_gettime(CLOCK_REALTIME, &now); |
| 38 | assert(r == 0); |
| 39 | return int64_ns_from_timespec(now); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void _Cor_Semaphore_ctor(_Cor_Semaphore *sem){ |
| 44 | int r; |
| 45 | r = pthread_mutex_init(&sem->mut, NULL); |
| 46 | assert(r == 0); |
| 47 | r = pthread_cond_init(&sem->cond, NULL); |
| 48 | assert(r == 0); |
| 49 | sem->count = 0; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void _Cor_Sempahore_dtor(_Cor_Semaphore *sem){ |
| 54 | int r; |
| 55 | r = pthread_cond_destroy(&sem->cond); |
| 56 | assert(r == 0); |
| 57 | r = pthread_mutex_destroy(&sem->mut); |
| 58 | assert(r == 0); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // timeout_when < 0 means 'wait forever' |
| 63 | // Returns true for success, false for timeout |
| 64 | bool _Cor_Semaphore_Wait(_Cor_Semaphore *sem, int64_t timeout_when){ |
| 65 | int r; |
| 66 | r = pthread_mutex_lock(&sem->mut); |
| 67 | assert(r == 0); |
| 68 | if (sem->count == 0) { |
| 69 | if (timeout_when >= 0) { |
| 70 | struct timespec ts = timespec_from_int64_ns(timeout_when); |
| 71 | r = pthread_cond_timedwait(&sem->cond, &sem->mut, &ts); |
| 72 | } |
| 73 | else { |
| 74 | r = pthread_cond_wait(&sem->cond, &sem->mut); |
| 75 | } |
| 76 | } |
| 77 | assert(r == 0 || r == ETIMEDOUT); |
| 78 | bool res; |
| 79 | if (sem->count > 0) { |
| 80 | sem->count--; |
| 81 | res = true; |
| 82 | } else { |
| 83 | res = false; |
| 84 | } |
| 85 | r = pthread_mutex_unlock(&sem->mut); |
| 86 | assert(r == 0); |
| 87 | |
| 88 | return res; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void _Cor_Semaphore_Signal(_Cor_Semaphore *sem){ |
| 93 | int r; |
| 94 | r = pthread_mutex_lock(&sem->mut); |
| 95 | assert(r == 0); |
| 96 | sem->count++; |
| 97 | r = pthread_cond_signal(&sem->cond); |
| 98 | assert(r == 0); |
| 99 | r = pthread_mutex_unlock(&sem->mut); |
| 100 | assert(r == 0); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void _Cor_Thread_ctor(_Cor_Thread *th, void *(*fn)(void *), void *param){ |
| 105 | int r; |
| 106 | th->joined = false; |
| 107 | r = pthread_create(&th->th, NULL, fn, param); |
| 108 | assert(r == 0); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void _Cor_Thread_dtor(_Cor_Thread *th){ |
| 113 | int r; |
| 114 | if (!th->joined){ |
| 115 | r = pthread_detach(th->th); |
| 116 | assert(r == 0); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void *_Cor_Thread_Join(_Cor_Thread *th){ |
| 122 | int r; |
| 123 | void *res; |
| 124 | r = pthread_join(th->th, &res); |
| 125 | assert(r == 0); |
| 126 | th->joined = true; |
| 127 | return res; |
| 128 | } |
| 129 | |