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 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2012 Bart Van Assche <bvanassche@acm.org>. |
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 | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 39 | static void 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 | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 44 | static Bool s_trace_semaphore; |
| 45 | static ULong 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'. */ |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 51 | static void drd_segment_push(struct semaphore_info* p, Segment* sg) |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 52 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 53 | Word n; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 54 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 55 | tl_assert(sg); |
| 56 | n = VG_(addToXA)(p->last_sem_post_seg, &sg); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 57 | #if 0 |
bart | 352db6d | 2011-10-08 08:56:27 +0000 | [diff] [blame] | 58 | VG_(message)(Vg_DebugMsg, "0x%lx push: added at position %ld/%ld", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 59 | p->a1, n, VG_(sizeXA)(p->last_sem_post_seg)); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 60 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 61 | tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 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'. */ |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 65 | static Segment* drd_segment_pop(struct semaphore_info* p) |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 66 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 67 | Word sz; |
| 68 | Segment* sg; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 69 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | sz = VG_(sizeXA)(p->last_sem_post_seg); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 71 | #if 0 |
bart | 352db6d | 2011-10-08 08:56:27 +0000 | [diff] [blame] | 72 | VG_(message)(Vg_DebugMsg, "0x%lx pop: removed from position %ld/%ld", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 73 | p->a1, sz - 1, sz); |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 74 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 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 | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 88 | 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 | 25f9f54 | 2009-07-23 16:31:39 +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 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 99 | tl_assert(semaphore != 0); |
| 100 | tl_assert(p->a1 == semaphore); |
| 101 | tl_assert(p->type == ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 102 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 103 | p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup; |
| 104 | p->delete_thread = 0; |
| 105 | p->waits_to_skip = 0; |
| 106 | p->value = 0; |
| 107 | p->waiters = 0; |
| 108 | p->last_sem_post_tid = DRD_INVALID_THREADID; |
| 109 | p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack", |
| 110 | VG_(free), sizeof(Segment*)); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 113 | /** |
| 114 | * Free the memory that was allocated by semaphore_initialize(). Called by |
| 115 | * DRD_(clientobj_remove)(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 116 | */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 117 | static void semaphore_cleanup(struct semaphore_info* p) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 118 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 119 | Segment* sg; |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 120 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 121 | if (p->waiters > 0) |
| 122 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 123 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 124 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 125 | SemaphoreErr, |
| 126 | VG_(get_IP)(VG_(get_running_tid)()), |
| 127 | "Destruction of semaphore that is being waited" |
| 128 | " upon", |
| 129 | &sei); |
| 130 | } |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 131 | while ((sg = drd_segment_pop(p))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 132 | DRD_(sg_put)(sg); |
| 133 | VG_(deleteXA)(p->last_sem_post_seg); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Return a pointer to the structure with information about the specified |
| 138 | * client semaphore. Allocate a new structure if such a structure did not |
| 139 | * yet exist. |
| 140 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 141 | static |
| 142 | struct semaphore_info* |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 143 | drd_semaphore_get_or_allocate(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 144 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 145 | struct semaphore_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 146 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 147 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 148 | p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); |
| 149 | if (p == 0) |
| 150 | { |
| 151 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 152 | p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 153 | drd_semaphore_initialize(p, semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 154 | } |
| 155 | return p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 156 | } |
| 157 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 158 | /** |
| 159 | * Return a pointer to the structure with information about the specified |
| 160 | * client semaphore, or null if no such structure was found. |
| 161 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 162 | static struct semaphore_info* semaphore_get(const Addr semaphore) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 163 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 164 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 165 | return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** Called before sem_init(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 169 | struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore, |
| 170 | const Word pshared, |
| 171 | const UInt value) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 172 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 173 | struct semaphore_info* p; |
| 174 | Segment* sg; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 175 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 176 | if (s_trace_semaphore) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 177 | DRD_(trace_msg)("[%d] sem_init 0x%lx value %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 178 | DRD_(thread_get_running_tid)(), semaphore, value); |
| 179 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 180 | p = semaphore_get(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 181 | if (p) |
| 182 | { |
| 183 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 184 | SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 185 | VG_(maybe_record_error)(vg_tid, |
| 186 | SemaphoreErr, |
| 187 | VG_(get_IP)(vg_tid), |
| 188 | "Semaphore reinitialization", |
| 189 | &SEI); |
| 190 | // Remove all segments from the segment stack. |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 191 | while ((sg = drd_segment_pop(p))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 192 | { |
| 193 | DRD_(sg_put)(sg); |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
bart | b7037bb | 2009-07-23 17:52:51 +0000 | [diff] [blame] | 198 | #if defined(VGO_darwin) |
| 199 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | a9d292e | 2011-03-05 09:05:47 +0000 | [diff] [blame] | 200 | GenericErrInfo GEI = { DRD_(thread_get_running_tid)(), 0 }; |
bart | b7037bb | 2009-07-23 17:52:51 +0000 | [diff] [blame] | 201 | VG_(maybe_record_error)(vg_tid, |
| 202 | GenericErr, |
| 203 | VG_(get_IP)(vg_tid), |
| 204 | "sem_init() is not yet supported on Darwin", |
| 205 | &GEI); |
| 206 | return NULL; |
| 207 | #else |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 208 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | b7037bb | 2009-07-23 17:52:51 +0000 | [diff] [blame] | 209 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 210 | } |
| 211 | tl_assert(p); |
| 212 | p->waits_to_skip = value; |
| 213 | p->value = value; |
| 214 | return p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 215 | } |
| 216 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 217 | /** Called after sem_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 218 | void DRD_(semaphore_destroy)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 219 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 220 | struct semaphore_info* p; |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 221 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 222 | p = semaphore_get(semaphore); |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 223 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 224 | if (s_trace_semaphore) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 225 | DRD_(trace_msg)("[%d] sem_destroy 0x%lx value %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 226 | DRD_(thread_get_running_tid)(), semaphore, |
| 227 | p ? p->value : 0); |
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 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 231 | GenericErrInfo GEI = { |
| 232 | .tid = DRD_(thread_get_running_tid)(), |
| 233 | .addr = semaphore, |
| 234 | }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 235 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 236 | GenericErr, |
| 237 | VG_(get_IP)(VG_(get_running_tid)()), |
| 238 | "Not a semaphore", |
| 239 | &GEI); |
| 240 | return; |
| 241 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 242 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 243 | DRD_(clientobj_remove)(semaphore, ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 244 | } |
| 245 | |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 246 | /** Called after sem_open(). */ |
| 247 | struct semaphore_info* DRD_(semaphore_open)(const Addr semaphore, |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 248 | const HChar* name, const Word oflag, |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 249 | const Word mode, const UInt value) |
| 250 | { |
| 251 | struct semaphore_info* p; |
| 252 | Segment* sg; |
| 253 | |
| 254 | if (s_trace_semaphore) |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 255 | DRD_(trace_msg)("[%d] sem_open 0x%lx name %s" |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 256 | " oflag %#lx mode %#lo value %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 257 | DRD_(thread_get_running_tid)(), |
| 258 | semaphore, name, oflag, mode, value); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 259 | |
| 260 | /* Return if the sem_open() call failed. */ |
| 261 | if (! semaphore) |
| 262 | return NULL; |
| 263 | |
| 264 | p = semaphore_get(semaphore); |
| 265 | if (p) |
| 266 | { |
| 267 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 268 | SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore }; |
| 269 | VG_(maybe_record_error)(vg_tid, |
| 270 | SemaphoreErr, |
| 271 | VG_(get_IP)(vg_tid), |
| 272 | "Semaphore reinitialization", |
| 273 | &SEI); |
| 274 | // Remove all segments from the segment stack. |
| 275 | while ((sg = drd_segment_pop(p))) |
| 276 | { |
| 277 | DRD_(sg_put)(sg); |
| 278 | } |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | p = drd_semaphore_get_or_allocate(semaphore); |
| 283 | } |
| 284 | tl_assert(p); |
| 285 | p->waits_to_skip = value; |
| 286 | p->value = value; |
| 287 | return p; |
| 288 | } |
| 289 | |
| 290 | /** Called before sem_close(). */ |
| 291 | void DRD_(semaphore_close)(const Addr semaphore) |
| 292 | { |
| 293 | struct semaphore_info* p; |
| 294 | |
| 295 | p = semaphore_get(semaphore); |
| 296 | |
| 297 | if (s_trace_semaphore) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 298 | DRD_(trace_msg)("[%d] sem_close 0x%lx value %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 299 | DRD_(thread_get_running_tid)(), semaphore, |
| 300 | p ? p->value : 0); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 301 | |
| 302 | if (p == 0) |
| 303 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 304 | GenericErrInfo GEI = { |
| 305 | .tid = DRD_(thread_get_running_tid)(), |
| 306 | .addr = semaphore, |
| 307 | }; |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 308 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 309 | GenericErr, |
| 310 | VG_(get_IP)(VG_(get_running_tid)()), |
| 311 | "Not a semaphore", |
| 312 | &GEI); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | DRD_(clientobj_remove)(semaphore, ClientSemaphore); |
| 317 | } |
| 318 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 319 | /** Called before sem_wait(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 320 | void DRD_(semaphore_pre_wait)(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 321 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 322 | struct semaphore_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 323 | |
bart | 7a06e12 | 2010-05-13 08:10:52 +0000 | [diff] [blame] | 324 | tl_assert(semaphore < semaphore + 1); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 325 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 326 | tl_assert(p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 327 | p->waiters++; |
bart | 3eecd9a | 2009-06-06 12:28:20 +0000 | [diff] [blame] | 328 | |
bart | 9986c99 | 2009-07-23 07:11:27 +0000 | [diff] [blame] | 329 | if ((Word)(p->waiters) <= 0) |
bart | 3eecd9a | 2009-06-06 12:28:20 +0000 | [diff] [blame] | 330 | { |
| 331 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; |
| 332 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 333 | SemaphoreErr, |
| 334 | VG_(get_IP)(VG_(get_running_tid)()), |
| 335 | "Invalid semaphore", |
| 336 | &sei); |
| 337 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 338 | } |
| 339 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Called after sem_wait() finished. |
bart | 333e42b | 2012-08-24 17:59:03 +0000 | [diff] [blame] | 342 | * @note Some C libraries do not set the 'waited' value correctly. |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 343 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 344 | void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore, |
| 345 | const Bool waited) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 346 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 347 | struct semaphore_info* p; |
| 348 | Segment* sg; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 349 | |
bart | 311abd6 | 2012-08-25 07:25:00 +0000 | [diff] [blame] | 350 | tl_assert(waited == 0 || waited == 1); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 351 | p = semaphore_get(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 352 | if (s_trace_semaphore) |
bart | 311abd6 | 2012-08-25 07:25:00 +0000 | [diff] [blame] | 353 | DRD_(trace_msg)("[%d] sem_wait 0x%lx value %u -> %u%s", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 354 | DRD_(thread_get_running_tid)(), semaphore, |
bart | 311abd6 | 2012-08-25 07:25:00 +0000 | [diff] [blame] | 355 | p ? p->value : 0, p ? p->value - waited : 0, |
| 356 | waited ? "" : " (did not wait)"); |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 357 | |
bart | 333e42b | 2012-08-24 17:59:03 +0000 | [diff] [blame] | 358 | if (p) { |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 359 | p->waiters--; |
bart | 311abd6 | 2012-08-25 07:25:00 +0000 | [diff] [blame] | 360 | p->value -= waited; |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Note: if another thread destroyed and reinitialized a semaphore while |
| 365 | * the current thread was waiting in sem_wait, p->waiters may have been |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 366 | * set to zero by drd_semaphore_initialize() after |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 367 | * DRD_(semaphore_pre_wait)() has finished before |
| 368 | * DRD_(semaphore_post_wait)() has been called. |
| 369 | */ |
bart | 9986c99 | 2009-07-23 07:11:27 +0000 | [diff] [blame] | 370 | if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 371 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 372 | SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 373 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 374 | SemaphoreErr, |
| 375 | VG_(get_IP)(VG_(get_running_tid)()), |
| 376 | "Invalid semaphore", |
| 377 | &sei); |
| 378 | return; |
| 379 | } |
bart | 57ae7ad | 2009-06-06 10:56:40 +0000 | [diff] [blame] | 380 | |
bart | 333e42b | 2012-08-24 17:59:03 +0000 | [diff] [blame] | 381 | if (!waited) |
| 382 | return; |
| 383 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 384 | if (p->waits_to_skip > 0) |
| 385 | p->waits_to_skip--; |
| 386 | else |
| 387 | { |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 388 | sg = drd_segment_pop(p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 389 | tl_assert(sg); |
bart | 7a2cc3c | 2011-04-30 07:27:41 +0000 | [diff] [blame] | 390 | if (p->last_sem_post_tid != tid |
| 391 | && p->last_sem_post_tid != DRD_INVALID_THREADID) |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 392 | { |
bart | 7a2cc3c | 2011-04-30 07:27:41 +0000 | [diff] [blame] | 393 | DRD_(thread_new_segment_and_combine_vc)(tid, sg); |
bart | 94866cc | 2008-12-21 17:20:22 +0000 | [diff] [blame] | 394 | } |
bart | 7a2cc3c | 2011-04-30 07:27:41 +0000 | [diff] [blame] | 395 | else |
| 396 | DRD_(thread_new_segment)(tid); |
| 397 | s_semaphore_segment_creation_count++; |
| 398 | DRD_(sg_put)(sg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 399 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** Called before sem_post(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 403 | void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 404 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 405 | struct semaphore_info* p; |
| 406 | Segment* sg; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 407 | |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 408 | p = drd_semaphore_get_or_allocate(semaphore); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 409 | p->value++; |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 410 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 411 | if (s_trace_semaphore) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 412 | DRD_(trace_msg)("[%d] sem_post 0x%lx value %u -> %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 413 | DRD_(thread_get_running_tid)(), |
| 414 | semaphore, p->value - 1, p->value); |
bart | da9436b | 2008-12-14 08:56:49 +0000 | [diff] [blame] | 415 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 416 | p->last_sem_post_tid = tid; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 417 | sg = 0; |
| 418 | DRD_(thread_get_latest_segment)(&sg, tid); |
| 419 | tl_assert(sg); |
bart | 25f9f54 | 2009-07-23 16:31:39 +0000 | [diff] [blame] | 420 | drd_segment_push(p, sg); |
bart | c02dde4 | 2009-06-06 09:28:28 +0000 | [diff] [blame] | 421 | DRD_(thread_new_segment)(tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 422 | s_semaphore_segment_creation_count++; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 423 | } |
| 424 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 425 | /** Called after sem_post() finished. */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 426 | void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore, |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 427 | const Bool succeeded) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 428 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 429 | /* |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 430 | * Note: it is hard to implement the sem_post() wrapper correctly in |
| 431 | * case sem_post() returns an error code. This is because handling this |
| 432 | * case correctly requires restoring the vector clock associated with |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 433 | * the semaphore to its original value here. In order to do that without |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 434 | * introducing a race condition, extra locking has to be added around |
| 435 | * each semaphore call. Such extra locking would have to be added in |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 436 | * drd_pthread_intercepts.c. However, it is hard to implement |
| 437 | * synchronization in drd_pthread_intercepts.c in a portable way without |
| 438 | * calling already redirected functions. |
| 439 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 440 | } |
| 441 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 442 | ULong DRD_(get_semaphore_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 443 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 444 | return s_semaphore_segment_creation_count; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 445 | } |