992e627c073731d6ae077f1b755948f906a7bb5d
1 #include <pthread_extra.h>
7 //This is first prototype for two mutexes only, it is useful in order to understand how this all works
8 //Currently it was replaced by macro with the same name
9 int pthread_mutex_timedlock_two(pthread_mutex_t *a, pthread_mutex_t *b, const struct timespec *restrict abs_timeout) {
13 //Wait for the first mutex
14 ret = (abs_timeout==NULL ? pthread_mutex_lock(a) : pthread_mutex_timedlock(a, abs_timeout));
15 if(ret) return ret; //Something is fishy about this mutex. Abort!
17 //Try to lock the second mutex
18 ret = pthread_mutex_trylock(b);
19 if(!ret) return ret; //Locked BOTH Hooray!
21 //Unlock first if second failed to prevent deadlocks
22 pthread_mutex_unlock(a);
24 //Swap mutexes, so we will try to block on the second mutex next time:
25 pthread_mutex_swap(a, b);
31 int pthread_mutex_timedlock_multi_generic(pthread_mutex_t
**lck
, int cnt
, bool block
, const struct timespec
*restrict abs_timeout
) {
35 //Try to lock all mutexes
36 for(int i
= 0; i
<cnt
; i
++) {
37 //Block only on the first one
39 ret
= (abs_timeout
==NULL
? pthread_mutex_lock(lck
[i
]) : pthread_mutex_timedlock(lck
[i
], abs_timeout
));
40 if(ret
) return ret
; //Something went wrong
43 //Then try if we can lock the rest
44 ret
= pthread_mutex_trylock(lck
[i
]);
46 //Cannot lock this one
48 printf("Cannot lock #%d!\n", i
);
54 //Check if we managed to lock all locks
55 if(locked
== cnt
) return 0;
57 //Unlock all mutexes that we managed to lock so far
58 for(int i
= 0;i
<locked
;i
++) {
59 printf("Unlocking #%d\n", i
);
60 pthread_mutex_unlock(lck
[i
]);
62 if(!block
) return ret
; //We do not want to block, just trying
64 //Try to block on locked mutex in next round
65 pthread_mutex_swap(lck
[0],lck
[locked
]);
This page took 0.337818 seconds and 5 git commands to generate.