forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_atomics.c
79 lines (70 loc) · 1.22 KB
/
sim_atomics.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "atomics.h"
void *surely_malloc (size_t n) {
void *p = malloc(n);
if (!p) exit(1);
return p;
}
typedef struct atomic_loc { int val; lock_t *lock; } atomic_loc;
atomic_loc *make_atomic(int i){
atomic_loc *a = surely_malloc(sizeof(atomic_loc));
lock_t *l = surely_malloc(sizeof(lock_t));
a->val = i;
a->lock = l;
makelock(l);
release(l);
return a;
}
int free_atomic(atomic_loc *tgt){
lock_t *l = tgt->lock;
acquire(l);
freelock(l);
free(l);
int i = tgt->val;
free(tgt);
return i;
}
int load_SC(atomic_loc *tgt){
int x;
lock_t *l = tgt->lock;
acquire(l);
x = tgt->val;
release(l);
return x;
}
void store_SC(atomic_loc *tgt, int v){
int x;
lock_t *l = tgt->lock;
acquire(l);
tgt->val = v;
release(l);
}
int CAS_SC(atomic_loc *tgt, int c, int v){
int x;
lock_t *l = tgt->lock;
acquire(l);
x = tgt->val;
if(x == c){
tgt->val = v;
x = 1;
}
else x = 0;
release(l);
return x;
}
int atomic_exchange_SC(atomic_loc *tgt, int v){
int x;
lock_t *l = tgt->lock;
acquire(l);
x = tgt->val;
tgt->val = v;
release(l);
return x;
}
int load_relaxed(atomic_loc *tgt){
int x;
lock_t *l = tgt->lock;
acquire(l);
x = tgt->val;
release(l);
return x;
}