-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlock.h
78 lines (65 loc) · 1.96 KB
/
lock.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (C) 2007 Scott Schneider, Christos Antonopoulos
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __LOCK_H_
#define __LOCK_H_
#include "atomic.h"
typedef volatile unsigned int lock_t;
static void spin_init(lock_t *lock);
static void spin_lock(lock_t *lock);
static void spin_unlock(lock_t *lock);
typedef struct {
unsigned long num_threads;
volatile unsigned long arrived;
volatile unsigned long global_sense;
} barrier_t;
static void bar_init(barrier_t *barr, unsigned long num_threads);
static void bar(barrier_t *barr);
static inline void spin_init(lock_t* lock)
{
*(lock) = 0;
}
static inline void spin_lock(lock_t* lock)
{
while (fetch_and_store((volatile unsigned int *)(lock), 1)) {
while(*(lock));
}
}
static inline void spin_unlock(lock_t* lock)
{
*(lock) = 0;
}
static inline void bar_init(barrier_t* barr, unsigned long num_threads)
{
barr->global_sense = 0;
barr->arrived = 0;
barr->num_threads = num_threads;
}
static inline void bar(barrier_t* barr)
{
unsigned long local_sense, my_num;
local_sense = barr->global_sense;
my_num = fetch_and_add(&(barr->arrived), 1);
if (my_num == barr->num_threads - 1) {
barr->arrived = 0;
barr->global_sense = !barr->global_sense;
}
else {
while(local_sense == barr->global_sense);
}
}
#endif