forked from pxy/locklocklock
/
cs_queue.c
163 lines (138 loc) · 3.66 KB
/
cs_queue.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Experimenting with synchronization and memory consistency. Queue
* locks version of critical sections.
*
* !BEGIN_SOLUTION!
* Reference solution.
* !END_SOLUTION!
*
* Course: Advanced Computer Architecture, Uppsala University
* Course Part: Lab assignment 2
*
* Author: Andreas Sandberg <andreas.sandberg@it.uu.se>
*
* $Id: cs_queue.c 672 2010-09-01 14:51:46Z ansan501 $
*/
/* Enable padding of data structures. Setting the correct padding
* significantly improves performance of the lock. Disable this define
* to disable padding.
*/
#define PAD_SIZE 4096
/* Define the following to disable checking of asserts. Improves
* performance slightly.
*/
/* #define NDEBUG */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "lab2.h"
#include "lab2_asm.h"
typedef struct {
int *i;
int *p;
#ifdef PAD_SIZE
char pad1[PAD_SIZE - 2 * sizeof(int *)];
#endif
} lh_thread_t;
typedef struct {
int v;
#ifdef PAD_SIZE
char pad[PAD_SIZE - 1 * sizeof(int)];
#endif
} lh_cell_t;
#ifdef PAD_SIZE
#define COND_ALIGN __attribute__((aligned (PAD_SIZE)))
#else
#define COND_ALIGN
#endif
static lh_thread_t *lh_threads COND_ALIGN;
static lh_cell_t *lock_cells COND_ALIGN;
static int *lock_l COND_ALIGN;
static int lock_cell0 COND_ALIGN;
static int num_threads;
/**
* Acquire a lock using the CLH locking algorithm. See the lecture
* notes for details.
*/
static void
lh_acquire(int ** volatile l, int ** volatile i, volatile int ** volatile p)
{
assert (**i == 0);
assert (*i == *p);
/* BONUS TASK: Implement the acquire part of the CLH locking
* algorithm as described in the lecture notes. */
/* !BEGIN_SOLUTION! */
**i = 1;
__builtin_ia32_sfence();
*p = (int *)asm_atomic_xchg_voidp((void **)l, *(void **)p);
while (**p)
__asm__("pause;");
/* !END_SOLUTION! */
}
/**
* Release a lock using the CLH locking algorithm. See the lecture
* notes for details.
*/
static void
lh_release(int **i, int **p)
{
assert (**i != 0);
assert (*i != *p);
/* BONUS TASK: Implement the release part of the CLH locking
* algorithm as described in the lecture notes. */
/* !BEGIN_SOLUTION! */
__builtin_ia32_sfence();
**i = 0;
*i = *p;
/* !END_SOLUTION! */
}
static void
impl_init(int _num_threads)
{
num_threads = _num_threads;
lh_threads = malloc(sizeof(*lh_threads) * num_threads);
lock_cells = malloc(sizeof(*lock_cells) * num_threads);
assert(lh_threads && lock_cells);
lock_l = &lock_cell0;
for (int i = 0; i < num_threads; i++) {
lh_threads[i].i = &lock_cells[i].v;
lh_threads[i].p = lh_threads[i].i;
}
}
static void
impl_fini()
{
free(lh_threads);
free(lock_cells);
}
static void
impl_enter_critical(int thread)
{
assert(thread >= 0 && thread < num_threads);
lh_acquire((int ** volatile)&lock_l,
(int ** volatile)&lh_threads[thread].i,
(volatile int **)&lh_threads[thread].p);
}
static void
impl_exit_critical(int thread)
{
assert(thread >= 0 && thread < num_threads);
lh_release(&lh_threads[thread].i, &lh_threads[thread].p);
}
critical_section_impl_t cs_impl_queue = {
.name = "queue",
.desc = "CLH Queue Locks",
.max_threads = (unsigned int)-1,
.init = &impl_init,
.fini = &impl_fini,
.enter = &impl_enter_critical,
.exit = &impl_exit_critical
};
/*
* Local Variables:
* mode: c
* c-basic-offset: 8
* indent-tabs-mode: nil
* c-file-style: "linux"
* End:
*/