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'. */ |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 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'. */ |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 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 | 25f9f54 | 2009-07-23 16:31:39 +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 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 124 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | } |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 132 | while ((sg = drd_segment_pop(p))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | 25f9f54 | 2009-07-23 16:31:39 +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); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 154 | drd_semaphore_initialize(p, semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 163 | static struct semaphore_info* 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, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 180 | "[%d] semaphore_init 0x%lx value %u\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 181 | DRD_(thread_get_running_tid)(), |
| 182 | semaphore, |
| 183 | value); |
| 184 | } |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 185 | p = semaphore_get(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 186 | if (p) |
| 187 | { |
| 188 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 189 | SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | VG_(maybe_record_error)(vg_tid, |
| 191 | SemaphoreErr, |
| 192 | VG_(get_IP)(vg_tid), |
| 193 | "Semaphore reinitialization", |
| 194 | &SEI); |
| 195 | // Remove all segments from the segment stack. |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 196 | while ((sg = drd_segment_pop(p))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 197 | { |
| 198 | DRD_(sg_put)(sg); |
| 199 | } |
| 200 | } |
| 201 | else |
| 202 | { |
bart | b7037bb | 2009-07-23 17:52:51 +0000 | [diff] [blame^] | 203 | #if defined(VGO_darwin) |
| 204 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 205 | GenericErrInfo GEI = { DRD_(thread_get_running_tid)() }; |
| 206 | VG_(maybe_record_error)(vg_tid, |
| 207 | GenericErr, |
| 208 | VG_(get_IP)(vg_tid), |
| 209 | "sem_init() is not yet supported on Darwin", |
| 210 | &GEI); |
| 211 | return NULL; |
| 212 | #else |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 213 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | b7037bb | 2009-07-23 17:52:51 +0000 | [diff] [blame^] | 214 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 215 | } |
| 216 | tl_assert(p); |
| 217 | p->waits_to_skip = value; |
| 218 | p->value = value; |
| 219 | return p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 220 | } |
| 221 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 222 | /** Called after sem_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 223 | void DRD_(semaphore_destroy)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 224 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 225 | struct semaphore_info* p; |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 226 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 227 | p = semaphore_get(semaphore); |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 228 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 229 | if (s_trace_semaphore) |
| 230 | { |
| 231 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 232 | "[%d] semaphore_destroy 0x%lx value %u\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 233 | DRD_(thread_get_running_tid)(), |
| 234 | semaphore, |
| 235 | p ? p->value : 0); |
| 236 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 237 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 238 | if (p == 0) |
| 239 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 240 | GenericErrInfo GEI = { DRD_(thread_get_running_tid)() }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 241 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 242 | GenericErr, |
| 243 | VG_(get_IP)(VG_(get_running_tid)()), |
| 244 | "Not a semaphore", |
| 245 | &GEI); |
| 246 | return; |
| 247 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 248 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 249 | DRD_(clientobj_remove)(semaphore, ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 252 | /** Called after sem_open(). */ |
| 253 | struct semaphore_info* DRD_(semaphore_open)(const Addr semaphore, |
| 254 | const Char* name, const Word oflag, |
| 255 | const Word mode, const UInt value) |
| 256 | { |
| 257 | struct semaphore_info* p; |
| 258 | Segment* sg; |
| 259 | |
| 260 | if (s_trace_semaphore) |
| 261 | { |
| 262 | VG_(message)(Vg_UserMsg, |
| 263 | "[%d] semaphore_open 0x%lx name %s" |
| 264 | " oflag %ld mode 0%lo value %u\n", |
| 265 | DRD_(thread_get_running_tid)(), |
| 266 | semaphore, name, oflag, mode, value); |
| 267 | } |
| 268 | |
| 269 | /* Return if the sem_open() call failed. */ |
| 270 | if (! semaphore) |
| 271 | return NULL; |
| 272 | |
| 273 | p = semaphore_get(semaphore); |
| 274 | if (p) |
| 275 | { |
| 276 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 277 | SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore }; |
| 278 | VG_(maybe_record_error)(vg_tid, |
| 279 | SemaphoreErr, |
| 280 | VG_(get_IP)(vg_tid), |
| 281 | "Semaphore reinitialization", |
| 282 | &SEI); |
| 283 | // Remove all segments from the segment stack. |
| 284 | while ((sg = drd_segment_pop(p))) |
| 285 | { |
| 286 | DRD_(sg_put)(sg); |
| 287 | } |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | p = drd_semaphore_get_or_allocate(semaphore); |
| 292 | } |
| 293 | tl_assert(p); |
| 294 | p->waits_to_skip = value; |
| 295 | p->value = value; |
| 296 | return p; |
| 297 | } |
| 298 | |
| 299 | /** Called before sem_close(). */ |
| 300 | void DRD_(semaphore_close)(const Addr semaphore) |
| 301 | { |
| 302 | struct semaphore_info* p; |
| 303 | |
| 304 | p = semaphore_get(semaphore); |
| 305 | |
| 306 | if (s_trace_semaphore) |
| 307 | { |
| 308 | VG_(message)(Vg_UserMsg, |
| 309 | "[%d] semaphore_close 0x%lx value %u\n", |
| 310 | DRD_(thread_get_running_tid)(), |
| 311 | semaphore, |
| 312 | p ? p->value : 0); |
| 313 | } |
| 314 | |
| 315 | if (p == 0) |
| 316 | { |
| 317 | GenericErrInfo GEI = { DRD_(thread_get_running_tid)() }; |
| 318 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 319 | GenericErr, |
| 320 | VG_(get_IP)(VG_(get_running_tid)()), |
| 321 | "Not a semaphore", |
| 322 | &GEI); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | DRD_(clientobj_remove)(semaphore, ClientSemaphore); |
| 327 | } |
| 328 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 329 | /** Called before sem_wait(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 330 | void DRD_(semaphore_pre_wait)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 331 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 332 | struct semaphore_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 333 | |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 334 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 335 | tl_assert(p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 336 | p->waiters++; |
bart | 3eecd9a | 2009-06-06 12:28:20 +0000 | [diff] [blame] | 337 | |
bart | 9986c99 | 2009-07-23 07:11:27 +0000 | [diff] [blame] | 338 | if ((Word)(p->waiters) <= 0) |
bart | 3eecd9a | 2009-06-06 12:28:20 +0000 | [diff] [blame] | 339 | { |
| 340 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; |
| 341 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 342 | SemaphoreErr, |
| 343 | VG_(get_IP)(VG_(get_running_tid)()), |
| 344 | "Invalid semaphore", |
| 345 | &sei); |
| 346 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 347 | } |
| 348 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 349 | /** |
| 350 | * Called after sem_wait() finished. |
| 351 | * @note Do not rely on the value of 'waited' -- some glibc versions do |
| 352 | * not set it correctly. |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 353 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 354 | void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore, |
| 355 | const Bool waited) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 356 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 357 | struct semaphore_info* p; |
| 358 | Segment* sg; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 359 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 360 | p = semaphore_get(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 361 | if (s_trace_semaphore) |
| 362 | { |
| 363 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 364 | "[%d] semaphore_wait 0x%lx value %u -> %u\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 365 | DRD_(thread_get_running_tid)(), |
| 366 | semaphore, |
| 367 | p ? p->value : 0, |
| 368 | p ? p->value - 1 : 0); |
| 369 | } |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 370 | |
| 371 | if (p) |
| 372 | { |
| 373 | p->waiters--; |
| 374 | p->value--; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Note: if another thread destroyed and reinitialized a semaphore while |
| 379 | * the current thread was waiting in sem_wait, p->waiters may have been |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 380 | * set to zero by drd_semaphore_initialize() after |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 381 | * DRD_(semaphore_pre_wait)() has finished before |
| 382 | * DRD_(semaphore_post_wait)() has been called. |
| 383 | */ |
bart | 9986c99 | 2009-07-23 07:11:27 +0000 | [diff] [blame] | 384 | if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 385 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 386 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 388 | SemaphoreErr, |
| 389 | VG_(get_IP)(VG_(get_running_tid)()), |
| 390 | "Invalid semaphore", |
| 391 | &sei); |
| 392 | return; |
| 393 | } |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 394 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 395 | if (p->waits_to_skip > 0) |
| 396 | p->waits_to_skip--; |
| 397 | else |
| 398 | { |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 399 | sg = drd_segment_pop(p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 400 | tl_assert(sg); |
| 401 | if (sg) |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 402 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 403 | if (p->last_sem_post_tid != tid |
| 404 | && p->last_sem_post_tid != DRD_INVALID_THREADID) |
| 405 | { |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 406 | DRD_(thread_new_segment_and_combine_vc)(tid, sg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 407 | } |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 408 | else |
| 409 | DRD_(thread_new_segment)(tid); |
| 410 | s_semaphore_segment_creation_count++; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 411 | DRD_(sg_put)(sg); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 412 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 413 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** Called before sem_post(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 417 | void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 418 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 419 | struct semaphore_info* p; |
| 420 | Segment* sg; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 421 | |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 422 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 423 | p->value++; |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 424 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 425 | if (s_trace_semaphore) |
| 426 | { |
| 427 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 428 | "[%d] semaphore_post 0x%lx value %u -> %u\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 429 | DRD_(thread_get_running_tid)(), |
| 430 | semaphore, |
| 431 | p->value - 1, p->value); |
| 432 | } |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 433 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 434 | p->last_sem_post_tid = tid; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 435 | sg = 0; |
| 436 | DRD_(thread_get_latest_segment)(&sg, tid); |
| 437 | tl_assert(sg); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 438 | drd_segment_push(p, sg); |
bart | c02dde4 | 2009-06-06 09:28:28 +0000 | [diff] [blame] | 439 | DRD_(thread_new_segment)(tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 440 | s_semaphore_segment_creation_count++; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 441 | } |
| 442 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 443 | /** Called after sem_post() finished. */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 444 | void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore, |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 445 | const Bool succeeded) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 446 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Note: it is hard to implement the sem_post() wrapper correctly in |
| 449 | * case sem_post() returns an error code. This is because handling this |
| 450 | * case correctly requires restoring the vector clock associated with |
| 451 | * the semaphore to its original value here. In order to do that without |
| 452 | * introducing a race condition, extra locking has to be added around |
| 453 | * each semaphore call. Such extra locking would have to be added in |
| 454 | * drd_pthread_intercepts.c. However, it is hard to implement |
| 455 | * synchronization in drd_pthread_intercepts.c in a portable way without |
| 456 | * calling already redirected functions. |
| 457 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 458 | } |
| 459 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 460 | ULong DRD_(get_semaphore_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 461 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 462 | return s_semaphore_segment_creation_count; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 463 | } |