-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex_inline.h
executable file
·55 lines (40 loc) · 939 Bytes
/
mutex_inline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "mutex_assam.h"
static __inline__ int __mutex_down(int *counter)
{
int oval, val = *counter;
if (val < 0){
return val;
}
oval = compare_and_swap(counter, val-1, val);
if (oval == val){
return val-1;
}
return -1;
}
static __inline__ int __mutex_up(int *counter)
{
int oval, val = *counter;
oval = compare_and_swap(counter, val+1, val);
return (oval == val && oval == 0);
}
static __inline__ int my_mutex_unlock(int *counter)
{
int val = *counter;
int oval = compare_and_swap(counter, val+1, val);
return oval;
}
static __inline__ int my_mutex_trylock(int *counter)
{
int oval, val = *counter;
oval = compare_and_swap(counter, val-1, val);
return (oval == val) && (val+1 < 0);
}
/* se bloquea y envia los escrito (datos) antes de activar
* el semaforo
*/
static __inline__ void my_mutex_lock(void)
{
#ifdef __i386__
__asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory");
#endif
}