add ticketlock test for wait-free (failed, as expected)
[urcu.git] / ticketlock-testwait / mem-progress.spin
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() ->
57 if
58 :: 1 ->
59 do
60 :: 1 ->
61 skip;
62 od;
63 :: 1 -> skip;
64 fi;
65 :: else ->
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 ->
113 do_pause();
114 spin_lock(lock, ticket);
115 refcount = refcount + 1;
116 do_pause();
117 refcount = refcount - 1;
118 spin_unlock(lock);
119 od;
120 }
121
122 init
123 {
124 run proc_A();
125 run proc_B();
126 run proc_B();
127 run proc_B();
128 run proc_B();
129 }
This page took 0.031801 seconds and 4 git commands to generate.