Commit | Line | Data |
---|---|---|
656c7dc1 MD |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
15 | * | |
16 | * Copyright (c) 2009 Mathieu Desnoyers | |
17 | */ | |
18 | ||
19 | /* 16 CPUs max (byte has 8 bits, divided in two) */ | |
20 | ||
21 | #ifndef CONFIG_BITS_PER_BYTE | |
22 | #define BITS_PER_BYTE 8 | |
23 | #else | |
24 | #define BITS_PER_BYTE CONFIG_BITS_PER_BYTE | |
25 | #endif | |
26 | ||
27 | #define HBPB (BITS_PER_BYTE / 2) /* 4 */ | |
28 | #define HMASK ((1 << HBPB) - 1) /* 0x0F */ | |
29 | ||
30 | /* for byte type */ | |
31 | #define LOW_HALF(val) ((val) & HMASK) | |
32 | #define LOW_HALF_INC 1 | |
33 | ||
34 | #define HIGH_HALF(val) ((val) & (HMASK << HBPB)) | |
35 | #define HIGH_HALF_INC (1 << HBPB) | |
36 | ||
37 | byte lock = 0; | |
38 | byte refcount = 0; | |
39 | ||
40 | inline spin_lock(lock, ticket) | |
41 | { | |
42 | atomic { | |
43 | ticket = HIGH_HALF(lock) >> HBPB; | |
44 | lock = lock + HIGH_HALF_INC; /* overflow expected */ | |
45 | } | |
46 | ||
d149fa02 MD |
47 | /* busy-wait */ |
48 | LOW_HALF(lock) == ticket -> 1; | |
656c7dc1 MD |
49 | } |
50 | ||
51 | inline spin_unlock(lock) | |
52 | { | |
53 | lock = HIGH_HALF(lock) | LOW_HALF(lock + LOW_HALF_INC); | |
54 | } | |
55 | ||
56 | proctype proc_X() | |
57 | { | |
58 | byte ticket; | |
59 | ||
60 | do | |
d149fa02 | 61 | :: spin_lock(lock, ticket); |
656c7dc1 MD |
62 | refcount = refcount + 1; |
63 | refcount = refcount - 1; | |
64 | spin_unlock(lock); | |
65 | od; | |
66 | } | |
67 | ||
68 | init | |
69 | { | |
70 | run proc_X(); | |
71 | run proc_X(); | |
72 | run proc_X(); | |
73 | run proc_X(); | |
74 | run proc_X(); | |
75 | } |