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