Commit | Line | Data |
---|---|---|
5c020b8d 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 | // test progress failure with shorter byte size. Will fail with 5 proc. | |
25 | #define BITS_PER_BYTE CONFIG_BITS_PER_BYTE | |
26 | #endif | |
27 | ||
28 | #define HBPB (BITS_PER_BYTE / 2) /* 4 */ | |
29 | #define HMASK ((1 << HBPB) - 1) /* 0x0F */ | |
30 | ||
31 | /* for byte type */ | |
32 | #define LOW_HALF(val) ((val) & HMASK) | |
33 | #define LOW_HALF_INC 1 | |
34 | ||
35 | #define HIGH_HALF(val) ((val) & (HMASK << HBPB)) | |
36 | #define HIGH_HALF_INC (1 << HBPB) | |
37 | ||
38 | byte lock = 0; | |
39 | byte refcount = 0; | |
40 | ||
41 | #define need_pause() (_pid == 2) | |
42 | ||
f96ba336 MD |
43 | /* |
44 | * do_pause() disabled: | |
45 | * get similar effect by disabling weak fairness. | |
46 | */ | |
5c020b8d MD |
47 | /* |
48 | * Test weak fairness by either not pausing or cycling for any number of | |
49 | * steps, or forever. | |
50 | * Models a slow thread. Should be added between each atomic steps. | |
51 | * To test for wait-freedom (no starvation of a specific thread), add do_pause | |
52 | * in threads other than the one we are checking for progress (and which | |
53 | * contains the progress label). | |
54 | * To test for lock-freedom (system-wide progress), add to all threads except | |
55 | * one. All threads contain progress labels. | |
56 | */ | |
57 | inline do_pause() | |
58 | { | |
59 | if | |
60 | :: need_pause() -> | |
6e634233 | 61 | do |
5c020b8d | 62 | :: 1 -> |
6e634233 MD |
63 | skip; |
64 | od; | |
65 | :: 1 -> | |
5c020b8d MD |
66 | skip; |
67 | fi; | |
68 | } | |
69 | ||
70 | inline spin_lock(lock, ticket) | |
71 | { | |
72 | atomic { | |
73 | ticket = HIGH_HALF(lock) >> HBPB; | |
74 | lock = lock + HIGH_HALF_INC; /* overflow expected */ | |
75 | } | |
76 | ||
77 | do | |
78 | :: 1 -> | |
79 | if | |
80 | :: (LOW_HALF(lock) == ticket) -> | |
81 | break; | |
82 | :: else -> | |
83 | skip; | |
84 | fi; | |
85 | od; | |
86 | } | |
87 | ||
88 | inline spin_unlock(lock) | |
89 | { | |
90 | lock = HIGH_HALF(lock) | LOW_HALF(lock + LOW_HALF_INC); | |
91 | } | |
92 | ||
93 | proctype proc_A() | |
94 | { | |
95 | byte ticket; | |
96 | ||
97 | do | |
98 | :: 1 -> | |
99 | progress_A: | |
100 | spin_lock(lock, ticket); | |
101 | refcount = refcount + 1; | |
102 | refcount = refcount - 1; | |
103 | spin_unlock(lock); | |
104 | od; | |
105 | } | |
106 | ||
107 | proctype proc_B() | |
108 | { | |
109 | byte ticket; | |
110 | ||
111 | do | |
112 | :: 1 -> | |
f96ba336 MD |
113 | progress_B: |
114 | //do_pause(); | |
5c020b8d MD |
115 | spin_lock(lock, ticket); |
116 | refcount = refcount + 1; | |
f96ba336 | 117 | //do_pause(); |
5c020b8d MD |
118 | refcount = refcount - 1; |
119 | spin_unlock(lock); | |
120 | od; | |
121 | } | |
122 | ||
123 | init | |
124 | { | |
125 | run proc_A(); | |
126 | run proc_B(); | |
127 | run proc_B(); | |
128 | run proc_B(); | |
129 | run proc_B(); | |
130 | } |