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