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 | ||
43 | /* | |
44 | * Test weak fairness by either not pausing or cycling for any number of | |
45 | * steps, or forever. | |
46 | * Models a slow thread. Should be added between each atomic steps. | |
47 | * To test for wait-freedom (no starvation of a specific thread), add do_pause | |
48 | * in threads other than the one we are checking for progress (and which | |
49 | * contains the progress label). | |
50 | * To test for lock-freedom (system-wide progress), add to all threads except | |
51 | * one. All threads contain progress labels. | |
52 | */ | |
53 | inline do_pause() | |
54 | { | |
55 | if | |
56 | :: need_pause() -> | |
6e634233 | 57 | do |
5c020b8d | 58 | :: 1 -> |
6e634233 MD |
59 | skip; |
60 | od; | |
61 | :: 1 -> | |
5c020b8d MD |
62 | skip; |
63 | fi; | |
64 | } | |
65 | ||
66 | inline spin_lock(lock, ticket) | |
67 | { | |
68 | atomic { | |
69 | ticket = HIGH_HALF(lock) >> HBPB; | |
70 | lock = lock + HIGH_HALF_INC; /* overflow expected */ | |
71 | } | |
72 | ||
73 | do | |
74 | :: 1 -> | |
75 | if | |
76 | :: (LOW_HALF(lock) == ticket) -> | |
77 | break; | |
78 | :: else -> | |
79 | skip; | |
80 | fi; | |
81 | od; | |
82 | } | |
83 | ||
84 | inline spin_unlock(lock) | |
85 | { | |
86 | lock = HIGH_HALF(lock) | LOW_HALF(lock + LOW_HALF_INC); | |
87 | } | |
88 | ||
89 | proctype proc_A() | |
90 | { | |
91 | byte ticket; | |
92 | ||
93 | do | |
94 | :: 1 -> | |
95 | progress_A: | |
96 | spin_lock(lock, ticket); | |
97 | refcount = refcount + 1; | |
98 | refcount = refcount - 1; | |
99 | spin_unlock(lock); | |
100 | od; | |
101 | } | |
102 | ||
103 | proctype proc_B() | |
104 | { | |
105 | byte ticket; | |
106 | ||
107 | do | |
108 | :: 1 -> | |
109 | do_pause(); | |
110 | spin_lock(lock, ticket); | |
111 | refcount = refcount + 1; | |
112 | do_pause(); | |
113 | refcount = refcount - 1; | |
114 | spin_unlock(lock); | |
115 | od; | |
116 | } | |
117 | ||
118 | init | |
119 | { | |
120 | run proc_A(); | |
121 | run proc_B(); | |
122 | run proc_B(); | |
123 | run proc_B(); | |
124 | run proc_B(); | |
125 | } |