blob: 71253813412fdc6d9ff5a3e6d56ae58cae52ef0b [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardj85642922008-01-14 11:54:56 +00003
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 Bart Van Assche <bvanassche@acm.org>.
sewardj85642922008-01-14 11:54:56 +00005
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
bart28230a32008-02-29 17:27:03 +000025#include "drd_clientobj.h"
sewardj85642922008-01-14 11:54:56 +000026#include "drd_error.h"
27#include "drd_semaphore.h"
28#include "drd_suppression.h"
sewardj85642922008-01-14 11:54:56 +000029#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)()
bart3e017fa2008-12-17 19:20:13 +000033#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
sewardj85642922008-01-14 11:54:56 +000034#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
35
36
bartdc1ef032009-02-15 14:18:02 +000037/* Local functions. */
sewardj85642922008-01-14 11:54:56 +000038
bartd2c5eae2009-02-21 15:27:04 +000039static void semaphore_cleanup(struct semaphore_info* p);
sewardj85642922008-01-14 11:54:56 +000040
41
bartdc1ef032009-02-15 14:18:02 +000042/* Local variables. */
sewardj85642922008-01-14 11:54:56 +000043
bartd2c5eae2009-02-21 15:27:04 +000044static Bool s_trace_semaphore;
45static ULong s_semaphore_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000046
47
bartdc1ef032009-02-15 14:18:02 +000048/* Function definitions. */
sewardj85642922008-01-14 11:54:56 +000049
bartdc1ef032009-02-15 14:18:02 +000050/** Push a segment at the end of the queue 'p->last_sem_post_seg'. */
bart25f9f542009-07-23 16:31:39 +000051static void drd_segment_push(struct semaphore_info* p, Segment* sg)
bart3e017fa2008-12-17 19:20:13 +000052{
bartbedfd232009-03-26 19:07:15 +000053 Word n;
bart3e017fa2008-12-17 19:20:13 +000054
bartbedfd232009-03-26 19:07:15 +000055 tl_assert(sg);
56 n = VG_(addToXA)(p->last_sem_post_seg, &sg);
bart3e017fa2008-12-17 19:20:13 +000057#if 0
florian9af04c42014-11-29 17:50:10 +000058 VG_(message)(Vg_DebugMsg, "0x%lx push: added at position %ld/%ld\n",
bartbedfd232009-03-26 19:07:15 +000059 p->a1, n, VG_(sizeXA)(p->last_sem_post_seg));
bart3e017fa2008-12-17 19:20:13 +000060#endif
bartbedfd232009-03-26 19:07:15 +000061 tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg);
bart3e017fa2008-12-17 19:20:13 +000062}
63
bartdc1ef032009-02-15 14:18:02 +000064/** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */
bart25f9f542009-07-23 16:31:39 +000065static Segment* drd_segment_pop(struct semaphore_info* p)
bart3e017fa2008-12-17 19:20:13 +000066{
bartbedfd232009-03-26 19:07:15 +000067 Word sz;
68 Segment* sg;
bart3e017fa2008-12-17 19:20:13 +000069
bartbedfd232009-03-26 19:07:15 +000070 sz = VG_(sizeXA)(p->last_sem_post_seg);
bart3e017fa2008-12-17 19:20:13 +000071#if 0
florian9af04c42014-11-29 17:50:10 +000072 VG_(message)(Vg_DebugMsg, "0x%lx pop: removed from position %ld/%ld\n",
bartbedfd232009-03-26 19:07:15 +000073 p->a1, sz - 1, sz);
bart3e017fa2008-12-17 19:20:13 +000074#endif
bartbedfd232009-03-26 19:07:15 +000075 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;
bart3e017fa2008-12-17 19:20:13 +000083}
84
bartdc1ef032009-02-15 14:18:02 +000085/** Enable or disable tracing of semaphore actions. */
86void DRD_(semaphore_set_trace)(const Bool trace_semaphore)
sewardj85642922008-01-14 11:54:56 +000087{
bartbedfd232009-03-26 19:07:15 +000088 s_trace_semaphore = trace_semaphore;
sewardj85642922008-01-14 11:54:56 +000089}
90
bartdc1ef032009-02-15 14:18:02 +000091/**
92 * Initialize the memory 'p' points at as a semaphore_info structure for the
florianad4e9792015-07-05 21:53:33 +000093 * client semaphore at client address 'semaphore'.
bartdc1ef032009-02-15 14:18:02 +000094 */
sewardj85642922008-01-14 11:54:56 +000095static
bart25f9f542009-07-23 16:31:39 +000096void drd_semaphore_initialize(struct semaphore_info* const p,
97 const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +000098{
bartbedfd232009-03-26 19:07:15 +000099 tl_assert(semaphore != 0);
100 tl_assert(p->a1 == semaphore);
101 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000102
bartbedfd232009-03-26 19:07:15 +0000103 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*));
sewardj85642922008-01-14 11:54:56 +0000111}
112
bart195e41f2009-02-15 11:34:57 +0000113/**
114 * Free the memory that was allocated by semaphore_initialize(). Called by
115 * DRD_(clientobj_remove)().
bart28230a32008-02-29 17:27:03 +0000116 */
bartd2c5eae2009-02-21 15:27:04 +0000117static void semaphore_cleanup(struct semaphore_info* p)
bart28230a32008-02-29 17:27:03 +0000118{
bartbedfd232009-03-26 19:07:15 +0000119 Segment* sg;
bart3e017fa2008-12-17 19:20:13 +0000120
bartbedfd232009-03-26 19:07:15 +0000121 if (p->waiters > 0)
122 {
bartd45d9952009-05-31 18:53:54 +0000123 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000124 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 }
bart25f9f542009-07-23 16:31:39 +0000131 while ((sg = drd_segment_pop(p)))
bartbedfd232009-03-26 19:07:15 +0000132 DRD_(sg_put)(sg);
133 VG_(deleteXA)(p->last_sem_post_seg);
bart28230a32008-02-29 17:27:03 +0000134}
135
bartdc1ef032009-02-15 14:18:02 +0000136/**
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 */
sewardj85642922008-01-14 11:54:56 +0000141static
142struct semaphore_info*
bart25f9f542009-07-23 16:31:39 +0000143drd_semaphore_get_or_allocate(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000144{
bartbedfd232009-03-26 19:07:15 +0000145 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +0000146
bartbedfd232009-03-26 19:07:15 +0000147 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);
bart25f9f542009-07-23 16:31:39 +0000153 drd_semaphore_initialize(p, semaphore);
bartbedfd232009-03-26 19:07:15 +0000154 }
155 return p;
sewardj85642922008-01-14 11:54:56 +0000156}
157
bartdc1ef032009-02-15 14:18:02 +0000158/**
159 * Return a pointer to the structure with information about the specified
160 * client semaphore, or null if no such structure was found.
161 */
bartd45d9952009-05-31 18:53:54 +0000162static struct semaphore_info* semaphore_get(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000163{
bartbedfd232009-03-26 19:07:15 +0000164 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
165 return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
bart28230a32008-02-29 17:27:03 +0000166}
167
168/** Called before sem_init(). */
bartdc1ef032009-02-15 14:18:02 +0000169struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore,
170 const Word pshared,
171 const UInt value)
sewardj85642922008-01-14 11:54:56 +0000172{
bartbedfd232009-03-26 19:07:15 +0000173 struct semaphore_info* p;
174 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000177 DRD_(trace_msg)("[%u] sem_init 0x%lx value %u",
bartb92ff0f2011-10-08 08:29:29 +0000178 DRD_(thread_get_running_tid)(), semaphore, value);
179
bartd45d9952009-05-31 18:53:54 +0000180 p = semaphore_get(semaphore);
bartbedfd232009-03-26 19:07:15 +0000181 if (p)
182 {
183 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000184 SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
bartbedfd232009-03-26 19:07:15 +0000185 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.
bart25f9f542009-07-23 16:31:39 +0000191 while ((sg = drd_segment_pop(p)))
bartbedfd232009-03-26 19:07:15 +0000192 {
193 DRD_(sg_put)(sg);
194 }
195 }
196 else
197 {
bartb7037bb2009-07-23 17:52:51 +0000198#if defined(VGO_darwin)
199 const ThreadId vg_tid = VG_(get_running_tid)();
barta9d292e2011-03-05 09:05:47 +0000200 GenericErrInfo GEI = { DRD_(thread_get_running_tid)(), 0 };
bartb7037bb2009-07-23 17:52:51 +0000201 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
bart25f9f542009-07-23 16:31:39 +0000208 p = drd_semaphore_get_or_allocate(semaphore);
bartb7037bb2009-07-23 17:52:51 +0000209#endif
bartbedfd232009-03-26 19:07:15 +0000210 }
211 tl_assert(p);
212 p->waits_to_skip = value;
213 p->value = value;
214 return p;
sewardj85642922008-01-14 11:54:56 +0000215}
216
bart28230a32008-02-29 17:27:03 +0000217/** Called after sem_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000218void DRD_(semaphore_destroy)(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000219{
bartbedfd232009-03-26 19:07:15 +0000220 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000221
bartd45d9952009-05-31 18:53:54 +0000222 p = semaphore_get(semaphore);
bartda9436b2008-12-14 08:56:49 +0000223
bartbedfd232009-03-26 19:07:15 +0000224 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000225 DRD_(trace_msg)("[%u] sem_destroy 0x%lx value %u",
bartb92ff0f2011-10-08 08:29:29 +0000226 DRD_(thread_get_running_tid)(), semaphore,
227 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000228
bartbedfd232009-03-26 19:07:15 +0000229 if (p == 0)
230 {
bart62cc2322010-03-07 10:54:21 +0000231 GenericErrInfo GEI = {
232 .tid = DRD_(thread_get_running_tid)(),
233 .addr = semaphore,
234 };
bartbedfd232009-03-26 19:07:15 +0000235 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 }
bart72b751c2008-03-01 13:44:24 +0000242
bartbedfd232009-03-26 19:07:15 +0000243 DRD_(clientobj_remove)(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000244}
245
bart25f9f542009-07-23 16:31:39 +0000246/** Called after sem_open(). */
247struct semaphore_info* DRD_(semaphore_open)(const Addr semaphore,
florian19f91bb2012-11-10 22:29:54 +0000248 const HChar* name, const Word oflag,
bart25f9f542009-07-23 16:31:39 +0000249 const Word mode, const UInt value)
250{
251 struct semaphore_info* p;
252 Segment* sg;
253
254 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000255 DRD_(trace_msg)("[%u] sem_open 0x%lx name %s"
bartad994e82011-10-13 18:04:30 +0000256 " oflag %#lx mode %#lo value %u",
bartb92ff0f2011-10-08 08:29:29 +0000257 DRD_(thread_get_running_tid)(),
florianea71ffb2015-08-05 14:38:57 +0000258 semaphore, name, (UWord)oflag, (UWord)mode, value);
bart25f9f542009-07-23 16:31:39 +0000259
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(). */
291void DRD_(semaphore_close)(const Addr semaphore)
292{
293 struct semaphore_info* p;
294
295 p = semaphore_get(semaphore);
296
297 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000298 DRD_(trace_msg)("[%u] sem_close 0x%lx value %u",
bartb92ff0f2011-10-08 08:29:29 +0000299 DRD_(thread_get_running_tid)(), semaphore,
300 p ? p->value : 0);
bart25f9f542009-07-23 16:31:39 +0000301
302 if (p == 0)
303 {
bart62cc2322010-03-07 10:54:21 +0000304 GenericErrInfo GEI = {
305 .tid = DRD_(thread_get_running_tid)(),
306 .addr = semaphore,
307 };
bart25f9f542009-07-23 16:31:39 +0000308 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
bart28230a32008-02-29 17:27:03 +0000319/** Called before sem_wait(). */
bartdc1ef032009-02-15 14:18:02 +0000320void DRD_(semaphore_pre_wait)(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000321{
bartbedfd232009-03-26 19:07:15 +0000322 struct semaphore_info* p;
sewardj85642922008-01-14 11:54:56 +0000323
bart7a06e122010-05-13 08:10:52 +0000324 tl_assert(semaphore < semaphore + 1);
bart25f9f542009-07-23 16:31:39 +0000325 p = drd_semaphore_get_or_allocate(semaphore);
bartbedfd232009-03-26 19:07:15 +0000326 tl_assert(p);
bartbedfd232009-03-26 19:07:15 +0000327 p->waiters++;
bart3eecd9a2009-06-06 12:28:20 +0000328
bart9986c992009-07-23 07:11:27 +0000329 if ((Word)(p->waiters) <= 0)
bart3eecd9a2009-06-06 12:28:20 +0000330 {
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 }
bart28230a32008-02-29 17:27:03 +0000338}
339
bartdc1ef032009-02-15 14:18:02 +0000340/**
341 * Called after sem_wait() finished.
bart333e42b2012-08-24 17:59:03 +0000342 * @note Some C libraries do not set the 'waited' value correctly.
bart28230a32008-02-29 17:27:03 +0000343 */
bartdc1ef032009-02-15 14:18:02 +0000344void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore,
345 const Bool waited)
bart28230a32008-02-29 17:27:03 +0000346{
bartbedfd232009-03-26 19:07:15 +0000347 struct semaphore_info* p;
348 Segment* sg;
bart28230a32008-02-29 17:27:03 +0000349
bart311abd62012-08-25 07:25:00 +0000350 tl_assert(waited == 0 || waited == 1);
bartd45d9952009-05-31 18:53:54 +0000351 p = semaphore_get(semaphore);
bartbedfd232009-03-26 19:07:15 +0000352 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000353 DRD_(trace_msg)("[%u] sem_wait 0x%lx value %u -> %u%s",
bartb92ff0f2011-10-08 08:29:29 +0000354 DRD_(thread_get_running_tid)(), semaphore,
bart311abd62012-08-25 07:25:00 +0000355 p ? p->value : 0, p ? p->value - waited : 0,
356 waited ? "" : " (did not wait)");
bart57ae7ad2009-06-06 10:56:40 +0000357
bart333e42b2012-08-24 17:59:03 +0000358 if (p) {
bart57ae7ad2009-06-06 10:56:40 +0000359 p->waiters--;
bart311abd62012-08-25 07:25:00 +0000360 p->value -= waited;
bart57ae7ad2009-06-06 10:56:40 +0000361 }
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
bart25f9f542009-07-23 16:31:39 +0000366 * set to zero by drd_semaphore_initialize() after
bart57ae7ad2009-06-06 10:56:40 +0000367 * DRD_(semaphore_pre_wait)() has finished before
368 * DRD_(semaphore_post_wait)() has been called.
369 */
bart9986c992009-07-23 07:11:27 +0000370 if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0)
bartbedfd232009-03-26 19:07:15 +0000371 {
bartd45d9952009-05-31 18:53:54 +0000372 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
bartbedfd232009-03-26 19:07:15 +0000373 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 }
bart57ae7ad2009-06-06 10:56:40 +0000380
bart333e42b2012-08-24 17:59:03 +0000381 if (!waited)
382 return;
383
bartbedfd232009-03-26 19:07:15 +0000384 if (p->waits_to_skip > 0)
385 p->waits_to_skip--;
386 else
387 {
bart25f9f542009-07-23 16:31:39 +0000388 sg = drd_segment_pop(p);
bartbedfd232009-03-26 19:07:15 +0000389 tl_assert(sg);
bart7a2cc3c2011-04-30 07:27:41 +0000390 if (p->last_sem_post_tid != tid
391 && p->last_sem_post_tid != DRD_INVALID_THREADID)
bart94866cc2008-12-21 17:20:22 +0000392 {
bart7a2cc3c2011-04-30 07:27:41 +0000393 DRD_(thread_new_segment_and_combine_vc)(tid, sg);
bart94866cc2008-12-21 17:20:22 +0000394 }
bart7a2cc3c2011-04-30 07:27:41 +0000395 else
396 DRD_(thread_new_segment)(tid);
397 s_semaphore_segment_creation_count++;
398 DRD_(sg_put)(sg);
bartbedfd232009-03-26 19:07:15 +0000399 }
sewardj85642922008-01-14 11:54:56 +0000400}
401
402/** Called before sem_post(). */
bartdc1ef032009-02-15 14:18:02 +0000403void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000404{
bartbedfd232009-03-26 19:07:15 +0000405 struct semaphore_info* p;
406 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000407
bart25f9f542009-07-23 16:31:39 +0000408 p = drd_semaphore_get_or_allocate(semaphore);
bartbedfd232009-03-26 19:07:15 +0000409 p->value++;
bartda9436b2008-12-14 08:56:49 +0000410
bartbedfd232009-03-26 19:07:15 +0000411 if (s_trace_semaphore)
florianea71ffb2015-08-05 14:38:57 +0000412 DRD_(trace_msg)("[%u] sem_post 0x%lx value %u -> %u",
bartb92ff0f2011-10-08 08:29:29 +0000413 DRD_(thread_get_running_tid)(),
414 semaphore, p->value - 1, p->value);
bartda9436b2008-12-14 08:56:49 +0000415
bartbedfd232009-03-26 19:07:15 +0000416 p->last_sem_post_tid = tid;
bartbedfd232009-03-26 19:07:15 +0000417 sg = 0;
418 DRD_(thread_get_latest_segment)(&sg, tid);
419 tl_assert(sg);
bart25f9f542009-07-23 16:31:39 +0000420 drd_segment_push(p, sg);
bartc02dde42009-06-06 09:28:28 +0000421 DRD_(thread_new_segment)(tid);
bartbedfd232009-03-26 19:07:15 +0000422 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000423}
424
bartd45d9952009-05-31 18:53:54 +0000425/** Called after sem_post() finished. */
bartdc1ef032009-02-15 14:18:02 +0000426void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore,
bartd45d9952009-05-31 18:53:54 +0000427 const Bool succeeded)
sewardj85642922008-01-14 11:54:56 +0000428{
bartd45d9952009-05-31 18:53:54 +0000429 /*
bart31b983d2010-02-21 14:52:59 +0000430 * 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
bartd45d9952009-05-31 18:53:54 +0000433 * the semaphore to its original value here. In order to do that without
bart31b983d2010-02-21 14:52:59 +0000434 * introducing a race condition, extra locking has to be added around
435 * each semaphore call. Such extra locking would have to be added in
bartd45d9952009-05-31 18:53:54 +0000436 * 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 */
sewardj85642922008-01-14 11:54:56 +0000440}
441
bartdc1ef032009-02-15 14:18:02 +0000442ULong DRD_(get_semaphore_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000443{
bartbedfd232009-03-26 19:07:15 +0000444 return s_semaphore_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000445}