| 1 | #define refcount_gt_one (refcount > 1) |
| 2 | /* |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | * |
| 17 | * Copyright (c) 2009 Mathieu Desnoyers |
| 18 | */ |
| 19 | |
| 20 | /* 16 CPUs max (byte has 8 bits, divided in two) */ |
| 21 | |
| 22 | #ifndef CONFIG_BITS_PER_BYTE |
| 23 | #define BITS_PER_BYTE 8 |
| 24 | #else |
| 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 == 1) |
| 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 | do |
| 58 | :: 1 -> |
| 59 | if |
| 60 | :: 1 -> skip; |
| 61 | :: 1 -> break; |
| 62 | fi; |
| 63 | od; |
| 64 | :: else -> |
| 65 | skip; |
| 66 | fi; |
| 67 | } |
| 68 | |
| 69 | inline spin_lock(lock, ticket) |
| 70 | { |
| 71 | atomic { |
| 72 | ticket = HIGH_HALF(lock) >> HBPB; |
| 73 | lock = lock + HIGH_HALF_INC; /* overflow expected */ |
| 74 | } |
| 75 | |
| 76 | do |
| 77 | :: 1 -> |
| 78 | if |
| 79 | :: (LOW_HALF(lock) == ticket) -> |
| 80 | break; |
| 81 | :: else -> |
| 82 | skip; |
| 83 | fi; |
| 84 | od; |
| 85 | } |
| 86 | |
| 87 | inline spin_unlock(lock) |
| 88 | { |
| 89 | lock = HIGH_HALF(lock) | LOW_HALF(lock + LOW_HALF_INC); |
| 90 | } |
| 91 | |
| 92 | proctype proc_X() |
| 93 | { |
| 94 | byte ticket; |
| 95 | |
| 96 | do |
| 97 | :: 1-> |
| 98 | spin_lock(lock, ticket); |
| 99 | progress: |
| 100 | refcount = refcount + 1; |
| 101 | do_pause(); |
| 102 | refcount = refcount - 1; |
| 103 | spin_unlock(lock); |
| 104 | od; |
| 105 | } |
| 106 | |
| 107 | init |
| 108 | { |
| 109 | run proc_X(); |
| 110 | run proc_X(); |
| 111 | //run proc_X(); |
| 112 | //run proc_X(); |
| 113 | //run proc_X(); |
| 114 | } |