sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 1 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame^] | 2 | This file is part of drd, a thread error detector. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 3 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame^] | 4 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU General Public License as |
| 8 | published by the Free Software Foundation; either version 2 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307, USA. |
| 20 | |
| 21 | The GNU General Public License is contained in the file COPYING. |
| 22 | */ |
| 23 | |
| 24 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 25 | #include "drd_clientobj.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 26 | #include "drd_error.h" |
| 27 | #include "drd_semaphore.h" |
| 28 | #include "drd_suppression.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 29 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 30 | #include "pub_tool_libcassert.h" // tl_assert() |
| 31 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 32 | #include "pub_tool_machine.h" // VG_(get_IP)() |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 33 | #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 34 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 35 | |
| 36 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 37 | /* Local functions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 38 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 39 | static void DRD_(semaphore_cleanup)(struct semaphore_info* p); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 40 | |
| 41 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 42 | /* Local variables. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 43 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 44 | static Bool DRD_(s_trace_semaphore); |
| 45 | static ULong DRD_(s_semaphore_segment_creation_count); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 46 | |
| 47 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 48 | /* Function definitions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 49 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 50 | /** Push a segment at the end of the queue 'p->last_sem_post_seg'. */ |
| 51 | static void DRD_(segment_push)(struct semaphore_info* p, Segment* sg) |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 52 | { |
| 53 | Word n; |
| 54 | |
| 55 | tl_assert(sg); |
| 56 | n = VG_(addToXA)(p->last_sem_post_seg, &sg); |
| 57 | #if 0 |
| 58 | VG_(message)(Vg_UserMsg, "0x%lx push: added at position %ld/%ld", |
| 59 | p->a1, n, VG_(sizeXA)(p->last_sem_post_seg)); |
| 60 | #endif |
| 61 | tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg); |
| 62 | } |
| 63 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 64 | /** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */ |
| 65 | static Segment* DRD_(segment_pop)(struct semaphore_info* p) |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 66 | { |
| 67 | Word sz; |
| 68 | Segment* sg; |
| 69 | |
| 70 | sz = VG_(sizeXA)(p->last_sem_post_seg); |
| 71 | #if 0 |
| 72 | VG_(message)(Vg_UserMsg, "0x%lx pop: removed from position %ld/%ld", |
| 73 | p->a1, sz - 1, sz); |
| 74 | #endif |
| 75 | sg = 0; |
| 76 | if (sz > 0) |
| 77 | { |
| 78 | sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1); |
| 79 | tl_assert(sg); |
| 80 | VG_(dropTailXA)(p->last_sem_post_seg, 1); |
| 81 | } |
| 82 | return sg; |
| 83 | } |
| 84 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 85 | /** Enable or disable tracing of semaphore actions. */ |
| 86 | void DRD_(semaphore_set_trace)(const Bool trace_semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 87 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 88 | DRD_(s_trace_semaphore) = trace_semaphore; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Initialize the memory 'p' points at as a semaphore_info structure for the |
| 93 | * client semaphore at client addres 'semaphore'. |
| 94 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 95 | static |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 96 | void DRD_(semaphore_initialize)(struct semaphore_info* const p, |
| 97 | const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 98 | { |
| 99 | tl_assert(semaphore != 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 100 | tl_assert(p->a1 == semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 101 | tl_assert(p->type == ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 102 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 103 | p->cleanup = (void(*)(DrdClientobj*))(DRD_(semaphore_cleanup)); |
bart | c8914e9 | 2008-12-24 09:45:41 +0000 | [diff] [blame] | 104 | p->waits_to_skip = 0; |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 105 | p->value = 0; |
| 106 | p->waiters = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 107 | p->last_sem_post_tid = DRD_INVALID_THREADID; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 108 | p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack", |
| 109 | VG_(free), sizeof(Segment*)); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 110 | } |
| 111 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 112 | /** |
| 113 | * Free the memory that was allocated by semaphore_initialize(). Called by |
| 114 | * DRD_(clientobj_remove)(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 115 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 116 | static void DRD_(semaphore_cleanup)(struct semaphore_info* p) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 117 | { |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 118 | Segment* sg; |
| 119 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 120 | if (p->waiters > 0) |
| 121 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 122 | SemaphoreErrInfo sei = { p->a1 }; |
| 123 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 124 | SemaphoreErr, |
| 125 | VG_(get_IP)(VG_(get_running_tid)()), |
| 126 | "Destruction of semaphore that is being waited" |
| 127 | " upon", |
| 128 | &sei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 129 | } |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 130 | while ((sg = DRD_(segment_pop)(p))) |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 131 | DRD_(sg_put)(sg); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 132 | VG_(deleteXA)(p->last_sem_post_seg); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Return a pointer to the structure with information about the specified |
| 137 | * client semaphore. Allocate a new structure if such a structure did not |
| 138 | * yet exist. |
| 139 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 140 | static |
| 141 | struct semaphore_info* |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 142 | DRD_(semaphore_get_or_allocate)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 143 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 144 | struct semaphore_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 145 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 146 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 147 | p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 148 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 149 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 150 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 151 | p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore); |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 152 | DRD_(semaphore_initialize)(p, semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 153 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 154 | return p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Return a pointer to the structure with information about the specified |
| 159 | * client semaphore, or null if no such structure was found. |
| 160 | */ |
| 161 | static struct semaphore_info* DRD_(semaphore_get)(const Addr semaphore) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 162 | { |
| 163 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 164 | return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** Called before sem_init(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 168 | struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore, |
| 169 | const Word pshared, |
| 170 | const UInt value) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 171 | { |
| 172 | struct semaphore_info* p; |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 173 | Segment* sg; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 174 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 175 | if (DRD_(s_trace_semaphore)) |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 176 | { |
| 177 | VG_(message)(Vg_UserMsg, |
bart | afb42b7 | 2008-12-17 07:32:09 +0000 | [diff] [blame] | 178 | "[%d/%d] semaphore_init 0x%lx value %u", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 179 | VG_(get_running_tid)(), |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 180 | DRD_(thread_get_running_tid)(), |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 181 | semaphore, |
| 182 | value); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 183 | } |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 184 | p = DRD_(semaphore_get)(semaphore); |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 185 | if (p) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 186 | { |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 187 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 188 | SemaphoreErrInfo SEI = { semaphore }; |
| 189 | VG_(maybe_record_error)(vg_tid, |
| 190 | SemaphoreErr, |
| 191 | VG_(get_IP)(vg_tid), |
| 192 | "Semaphore reinitialization", |
| 193 | &SEI); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 194 | // Remove all segments from the segment stack. |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 195 | while ((sg = DRD_(segment_pop)(p))) |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 196 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 197 | DRD_(sg_put)(sg); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 198 | } |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 199 | } |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 200 | else |
| 201 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 202 | p = DRD_(semaphore_get_or_allocate)(semaphore); |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 203 | } |
| 204 | tl_assert(p); |
bart | c8914e9 | 2008-12-24 09:45:41 +0000 | [diff] [blame] | 205 | p->waits_to_skip = value; |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 206 | p->value = value; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 207 | return p; |
| 208 | } |
| 209 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 210 | /** Called after sem_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 211 | void DRD_(semaphore_destroy)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 212 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 213 | struct semaphore_info* p; |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 214 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 215 | p = DRD_(semaphore_get)(semaphore); |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 216 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 217 | if (DRD_(s_trace_semaphore)) |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 218 | { |
| 219 | VG_(message)(Vg_UserMsg, |
bart | afb42b7 | 2008-12-17 07:32:09 +0000 | [diff] [blame] | 220 | "[%d/%d] semaphore_destroy 0x%lx value %u", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 221 | VG_(get_running_tid)(), |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 222 | DRD_(thread_get_running_tid)(), |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 223 | semaphore, |
| 224 | p ? p->value : 0); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 227 | if (p == 0) |
| 228 | { |
| 229 | GenericErrInfo GEI; |
| 230 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 231 | GenericErr, |
| 232 | VG_(get_IP)(VG_(get_running_tid)()), |
| 233 | "Not a semaphore", |
| 234 | &GEI); |
| 235 | return; |
| 236 | } |
| 237 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 238 | DRD_(clientobj_remove)(semaphore, ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 241 | /** Called before sem_wait(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 242 | void DRD_(semaphore_pre_wait)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 243 | { |
| 244 | struct semaphore_info* p; |
| 245 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 246 | p = DRD_(semaphore_get_or_allocate)(semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 247 | tl_assert(p); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 248 | tl_assert((int)p->waiters >= 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 249 | p->waiters++; |
| 250 | tl_assert(p->waiters > 0); |
| 251 | } |
| 252 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 253 | /** |
| 254 | * Called after sem_wait() finished. |
| 255 | * @note Do not rely on the value of 'waited' -- some glibc versions do |
| 256 | * not set it correctly. |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 257 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 258 | void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore, |
| 259 | const Bool waited) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 260 | { |
| 261 | struct semaphore_info* p; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 262 | Segment* sg; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 263 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 264 | p = DRD_(semaphore_get)(semaphore); |
| 265 | if (DRD_(s_trace_semaphore)) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 266 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 267 | VG_(message)(Vg_UserMsg, |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 268 | "[%d/%d] semaphore_wait 0x%lx value %u -> %u", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 269 | VG_(get_running_tid)(), |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 270 | DRD_(thread_get_running_tid)(), |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 271 | semaphore, |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 272 | p ? p->value : 0, |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 273 | p ? p->value - 1 : 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 274 | } |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 275 | tl_assert(p); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 276 | tl_assert(p->waiters > 0); |
| 277 | p->waiters--; |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 278 | tl_assert((int)p->waiters >= 0); |
| 279 | tl_assert((int)p->value >= 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 280 | if (p->value == 0) |
| 281 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 282 | SemaphoreErrInfo sei = { semaphore }; |
| 283 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 284 | SemaphoreErr, |
| 285 | VG_(get_IP)(VG_(get_running_tid)()), |
| 286 | "Invalid semaphore", |
| 287 | &sei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 290 | p->value--; |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 291 | tl_assert((int)p->value >= 0); |
bart | c8914e9 | 2008-12-24 09:45:41 +0000 | [diff] [blame] | 292 | if (p->waits_to_skip > 0) |
| 293 | p->waits_to_skip--; |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 294 | else |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 295 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 296 | sg = DRD_(segment_pop)(p); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 297 | tl_assert(sg); |
| 298 | if (sg) |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 299 | { |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 300 | if (p->last_sem_post_tid != tid |
| 301 | && p->last_sem_post_tid != DRD_INVALID_THREADID) |
| 302 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 303 | DRD_(thread_combine_vc2)(tid, &sg->vc); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 304 | } |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 305 | DRD_(sg_put)(sg); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 306 | DRD_(thread_new_segment)(tid); |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 307 | DRD_(s_semaphore_segment_creation_count)++; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 308 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 309 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** Called before sem_post(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 313 | void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 314 | { |
| 315 | struct semaphore_info* p; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 316 | Segment* sg; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 317 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 318 | p = DRD_(semaphore_get_or_allocate)(semaphore); |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 319 | p->value++; |
| 320 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 321 | if (DRD_(s_trace_semaphore)) |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 322 | { |
| 323 | VG_(message)(Vg_UserMsg, |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 324 | "[%d/%d] semaphore_post 0x%lx value %u -> %u", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 325 | VG_(get_running_tid)(), |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 326 | DRD_(thread_get_running_tid)(), |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 327 | semaphore, |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 328 | p->value - 1, p->value); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 329 | } |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 330 | |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 331 | p->last_sem_post_tid = tid; |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 332 | DRD_(thread_new_segment)(tid); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 333 | sg = 0; |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 334 | DRD_(thread_get_latest_segment)(&sg, tid); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 335 | tl_assert(sg); |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 336 | DRD_(segment_push)(p, sg); |
| 337 | DRD_(s_semaphore_segment_creation_count)++; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** Called after sem_post() finished successfully. */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 341 | void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore, |
| 342 | const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 343 | { |
bart | 25896d9 | 2008-02-17 09:21:05 +0000 | [diff] [blame] | 344 | /* Note: it is hard to implement the sem_post() wrapper correctly in */ |
| 345 | /* case sem_post() returns an error code. This is because handling this */ |
| 346 | /* case correctly requires restoring the vector clock associated with */ |
| 347 | /* the semaphore to its original value here. In order to do that without */ |
| 348 | /* introducing a race condition, extra locking has to be added around */ |
| 349 | /* each semaphore call. Such extra locking would have to be added in */ |
| 350 | /* drd_intercepts.c. However, it is hard to implement synchronization */ |
| 351 | /* in drd_intercepts.c in a portable way without calling already */ |
| 352 | /* redirected functions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 355 | void DRD_(semaphore_thread_delete)(const DrdThreadId threadid) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 356 | { } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 357 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 358 | ULong DRD_(get_semaphore_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 359 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 360 | return DRD_(s_semaphore_segment_creation_count); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 361 | } |