1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | #ifdef __PTHREAD__
|
|---|
| 2 | #else
|
|---|
| 3 | #define __PTHREAD__
|
|---|
| 4 |
|
|---|
| 5 | #include<pthread-common.h>
|
|---|
| 6 | #include <civlc.h>
|
|---|
| 7 |
|
|---|
| 8 | void pthread_cond_init(pthread_cond_t *cond, int kind){
|
|---|
| 9 | cond->proccount = 0;
|
|---|
| 10 | cond->signal = $false;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | //Initializes mutex as unlocked and kind as defined by int m
|
|---|
| 14 | void pthread_mutex_init(pthread_mutex_t *mutex, int m){
|
|---|
| 15 | mutex->kind = m;
|
|---|
| 16 | mutex->lock = 0;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | int pthread_mutex_lock(pthread_mutex_t *mutex) {
|
|---|
| 20 | $when(mutex->lock == 0) mutex->lock = 1;
|
|---|
| 21 | mutex->owner = $self;
|
|---|
| 22 | return 0;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | int pthread_mutex_unlock(pthread_mutex_t *mutex) {
|
|---|
| 27 | mutex->lock = 0;
|
|---|
| 28 | return 0;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex){
|
|---|
| 32 | if(mutex->owner != $self)
|
|---|
| 33 | {
|
|---|
| 34 | printf("Mutex not owned by thread");
|
|---|
| 35 | $assert($false);
|
|---|
| 36 | return 0;
|
|---|
| 37 | }
|
|---|
| 38 | cond->proccount++;
|
|---|
| 39 | pthread_mutex_unlock(mutex);
|
|---|
| 40 | $when(cond->signal == $true);
|
|---|
| 41 | cond->signal = $false;
|
|---|
| 42 | --cond->proccount;
|
|---|
| 43 | $when(mutex->lock == 0){pthread_mutex_lock(mutex);}
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | int pthread_cond_signal(pthread_cond_t *cond){
|
|---|
| 47 | cond->signal = $true;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | int pthread_cond_broadcast(pthread_cond_t *cond){
|
|---|
| 51 | while(cond->proccount > 0){
|
|---|
| 52 | cond->signal = $true;
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|---|
| 57 | void *(*start_routine)(void*), void *arg){
|
|---|
| 58 | *thread = $spawn start_routine(arg);
|
|---|
| 59 | return 0;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | int pthread_join(pthread_t thread, void **value_ptr) {
|
|---|
| 63 | $wait(thread);
|
|---|
| 64 | return 0;
|
|---|
| 65 | }
|
|---|
| 66 | #endif
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.