1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*
2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  This file is part of drd, a thread error detector.
3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
4663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng  Copyright (C) 2006-2012 Bart Van Assche <bvanassche@acm.org>.
5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  This program is free software; you can redistribute it and/or
7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  modify it under the terms of the GNU General Public License as
8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  published by the Free Software Foundation; either version 2 of the
9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  License, or (at your option) any later version.
10ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
11ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  This program is distributed in the hope that it will be useful, but
12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  WITHOUT ANY WARRANTY; without even the implied warranty of
13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  General Public License for more details.
15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  You should have received a copy of the GNU General Public License
17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  along with this program; if not, write to the Free Software
18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  02111-1307, USA.
20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown  The GNU General Public License is contained in the file COPYING.
22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown*/
23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "drd_clientobj.h"
26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "drd_error.h"
27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "drd_semaphore.h"
28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "drd_suppression.h"
29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_errormgr.h"    // VG_(maybe_record_error)()
30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_libcassert.h"  // tl_assert()
31ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_libcprint.h"   // VG_(printf)()
32ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_machine.h"     // VG_(get_IP)()
33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_mallocfree.h"  // VG_(malloc), VG_(free)
34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
35ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
36ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
37ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Local functions. */
38ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
39ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic void semaphore_cleanup(struct semaphore_info* p);
40ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
41ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
42ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Local variables. */
43ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
44ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic Bool s_trace_semaphore;
45ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic ULong s_semaphore_segment_creation_count;
46ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
47ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
48ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Function definitions. */
49ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
50ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Push a segment at the end of the queue 'p->last_sem_post_seg'. */
51ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic void drd_segment_push(struct semaphore_info* p, Segment* sg)
52ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
53ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Word n;
54ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
55ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(sg);
56ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   n = VG_(addToXA)(p->last_sem_post_seg, &sg);
57ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if 0
58b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   VG_(message)(Vg_DebugMsg, "0x%lx push: added at position %ld/%ld",
59ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                p->a1, n, VG_(sizeXA)(p->last_sem_post_seg));
60ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif
61ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg);
62ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
63ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
64ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */
65ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic Segment* drd_segment_pop(struct semaphore_info* p)
66ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
67ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Word sz;
68ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
69ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
70ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   sz = VG_(sizeXA)(p->last_sem_post_seg);
71ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if 0
72b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   VG_(message)(Vg_DebugMsg, "0x%lx pop:  removed from position %ld/%ld",
73ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                p->a1, sz - 1, sz);
74ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif
75ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   sg = 0;
76ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (sz > 0)
77ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
78ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1);
79ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      tl_assert(sg);
80ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(dropTailXA)(p->last_sem_post_seg, 1);
81ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
82ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return sg;
83ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
84ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
85ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Enable or disable tracing of semaphore actions. */
86ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_set_trace)(const Bool trace_semaphore)
87ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
88ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   s_trace_semaphore = trace_semaphore;
89ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
90ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
91ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/**
92ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Initialize the memory 'p' points at as a semaphore_info structure for the
93ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * client semaphore at client addres 'semaphore'.
94ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */
95ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic
96ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid drd_semaphore_initialize(struct semaphore_info* const p,
97ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              const Addr semaphore)
98ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
99ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(semaphore != 0);
100ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(p->a1 == semaphore);
101ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(p->type == ClientSemaphore);
102ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
103ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->cleanup           = (void(*)(DrdClientobj*))semaphore_cleanup;
104ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->delete_thread     = 0;
105ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->waits_to_skip     = 0;
106ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->value             = 0;
107ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->waiters           = 0;
108ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->last_sem_post_tid = DRD_INVALID_THREADID;
109ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack",
110ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                                     VG_(free), sizeof(Segment*));
111ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
112ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
113ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/**
114ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Free the memory that was allocated by semaphore_initialize(). Called by
115ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * DRD_(clientobj_remove)().
116ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */
117ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic void semaphore_cleanup(struct semaphore_info* p)
118ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
119ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
120ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
121ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p->waiters > 0)
122ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
123ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 };
124ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(VG_(get_running_tid)(),
125ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              SemaphoreErr,
126ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(VG_(get_running_tid)()),
127ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Destruction of semaphore that is being waited"
128ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              " upon",
129ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &sei);
130ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
131ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   while ((sg = drd_segment_pop(p)))
132ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      DRD_(sg_put)(sg);
133ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(deleteXA)(p->last_sem_post_seg);
134ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
135ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
136ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/**
137ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Return a pointer to the structure with information about the specified
138ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * client semaphore. Allocate a new structure if such a structure did not
139ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * yet exist.
140ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */
141ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic
142ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstruct semaphore_info*
143ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Browndrd_semaphore_get_or_allocate(const Addr semaphore)
144ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
145ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info *p;
146ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
147ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(offsetof(DrdClientobj, semaphore) == 0);
148ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
149ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p == 0)
150ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
151ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      tl_assert(offsetof(DrdClientobj, semaphore) == 0);
152ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore);
153ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      drd_semaphore_initialize(p, semaphore);
154ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
155ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return p;
156ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
157ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
158ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/**
159ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Return a pointer to the structure with information about the specified
160ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * client semaphore, or null if no such structure was found.
161ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */
162ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic struct semaphore_info* semaphore_get(const Addr semaphore)
163ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
164ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(offsetof(DrdClientobj, semaphore) == 0);
165ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
166ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
167ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
168ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called before sem_init(). */
169ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstruct semaphore_info* DRD_(semaphore_init)(const Addr semaphore,
170ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                                            const Word pshared,
171ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                                            const UInt value)
172ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
173ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
174ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
175ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
176ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
177b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(trace_msg)("[%d] sem_init      0x%lx value %u",
178b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(), semaphore, value);
179b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
180ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = semaphore_get(semaphore);
181ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p)
182ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
183ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      const ThreadId vg_tid = VG_(get_running_tid)();
184ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
185ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(vg_tid,
186ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              SemaphoreErr,
187ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(vg_tid),
188ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Semaphore reinitialization",
189ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &SEI);
190ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // Remove all segments from the segment stack.
191ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      while ((sg = drd_segment_pop(p)))
192ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
193ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         DRD_(sg_put)(sg);
194ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
195ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
196ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else
197ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
198ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if defined(VGO_darwin)
199ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      const ThreadId vg_tid = VG_(get_running_tid)();
200b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      GenericErrInfo GEI = { DRD_(thread_get_running_tid)(), 0 };
201ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(vg_tid,
202ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown			      GenericErr,
203ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown			      VG_(get_IP)(vg_tid),
204ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown			      "sem_init() is not yet supported on Darwin",
205ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown			      &GEI);
206ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return NULL;
207ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#else
208ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      p = drd_semaphore_get_or_allocate(semaphore);
209ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif
210ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
211ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(p);
212ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->waits_to_skip = value;
213ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->value         = value;
214ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return p;
215ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
216ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
217ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called after sem_destroy(). */
218ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_destroy)(const Addr semaphore)
219ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
220ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
221ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
222ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = semaphore_get(semaphore);
223ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
224ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
225b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(trace_msg)("[%d] sem_destroy   0x%lx value %u",
226b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(), semaphore,
227b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      p ? p->value : 0);
228ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
229ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p == 0)
230ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
231ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      GenericErrInfo GEI = {
232ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown	 .tid  = DRD_(thread_get_running_tid)(),
233ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown	 .addr = semaphore,
234ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      };
235ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(VG_(get_running_tid)(),
236ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              GenericErr,
237ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(VG_(get_running_tid)()),
238ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Not a semaphore",
239ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &GEI);
240ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return;
241ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
242ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
243ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   DRD_(clientobj_remove)(semaphore, ClientSemaphore);
244ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
245ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
246ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called after sem_open(). */
247ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstruct semaphore_info* DRD_(semaphore_open)(const Addr semaphore,
248ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                                            const Char* name, const Word oflag,
249ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                                            const Word mode, const UInt value)
250ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
251ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
252ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
253ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
254ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
255b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(trace_msg)("[%d] sem_open      0x%lx name %s"
256b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      " oflag %#lx mode %#lo value %u",
257b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(),
258b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      semaphore, name, oflag, mode, value);
259ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
260ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Return if the sem_open() call failed. */
261ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (! semaphore)
262ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return NULL;
263ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
264ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = semaphore_get(semaphore);
265ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p)
266ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
267ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      const ThreadId vg_tid = VG_(get_running_tid)();
268ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
269ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(vg_tid,
270ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              SemaphoreErr,
271ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(vg_tid),
272ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Semaphore reinitialization",
273ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &SEI);
274ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // Remove all segments from the segment stack.
275ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      while ((sg = drd_segment_pop(p)))
276ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
277ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         DRD_(sg_put)(sg);
278ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
279ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
280ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else
281ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
282ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      p = drd_semaphore_get_or_allocate(semaphore);
283ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
284ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(p);
285ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->waits_to_skip = value;
286ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->value         = value;
287ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return p;
288ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
289ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
290ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called before sem_close(). */
291ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_close)(const Addr semaphore)
292ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
293ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
294ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
295ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = semaphore_get(semaphore);
296ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
297ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
298b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(trace_msg)("[%d] sem_close     0x%lx value %u",
299b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(), semaphore,
300b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      p ? p->value : 0);
301ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
302ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p == 0)
303ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
304ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      GenericErrInfo GEI = {
305ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown	 .tid  = DRD_(thread_get_running_tid)(),
306ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown	 .addr = semaphore,
307ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      };
308ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(VG_(get_running_tid)(),
309ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              GenericErr,
310ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(VG_(get_running_tid)()),
311ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Not a semaphore",
312ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &GEI);
313ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return;
314ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
315ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
316ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   DRD_(clientobj_remove)(semaphore, ClientSemaphore);
317ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
318ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
319ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called before sem_wait(). */
320ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_pre_wait)(const Addr semaphore)
321ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
322ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
323ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
324ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(semaphore < semaphore + 1);
325ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = drd_semaphore_get_or_allocate(semaphore);
326ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(p);
327ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->waiters++;
328ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
329ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if ((Word)(p->waiters) <= 0)
330ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
331ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
332ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(VG_(get_running_tid)(),
333ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              SemaphoreErr,
334ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(VG_(get_running_tid)()),
335ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Invalid semaphore",
336ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &sei);
337ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
338ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
339ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
340ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/**
341ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown * Called after sem_wait() finished.
342663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng * @note Some C libraries do not set the 'waited' value correctly.
343ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown */
344ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore,
345ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                               const Bool waited)
346ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
347ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
348ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
349ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
350663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   tl_assert(waited == 0 || waited == 1);
351ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = semaphore_get(semaphore);
352ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
353663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      DRD_(trace_msg)("[%d] sem_wait      0x%lx value %u -> %u%s",
354b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(), semaphore,
355663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                      p ? p->value : 0, p ? p->value - waited : 0,
356663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng		      waited ? "" : " (did not wait)");
357ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
358663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (p) {
359ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      p->waiters--;
360663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      p->value -= waited;
361ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
362ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
363ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /*
364ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * Note: if another thread destroyed and reinitialized a semaphore while
365ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * the current thread was waiting in sem_wait, p->waiters may have been
366ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * set to zero by drd_semaphore_initialize() after
367ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * DRD_(semaphore_pre_wait)() has finished before
368ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * DRD_(semaphore_post_wait)() has been called.
369ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    */
370ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0)
371ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
372ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
373ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(maybe_record_error)(VG_(get_running_tid)(),
374ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              SemaphoreErr,
375ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              VG_(get_IP)(VG_(get_running_tid)()),
376ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              "Invalid semaphore",
377ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                              &sei);
378ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return;
379ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
380ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
381663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (!waited)
382663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      return;
383663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
384ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (p->waits_to_skip > 0)
385ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      p->waits_to_skip--;
386ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else
387ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
388ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      sg = drd_segment_pop(p);
389ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      tl_assert(sg);
390b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      if (p->last_sem_post_tid != tid
391b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov          && p->last_sem_post_tid != DRD_INVALID_THREADID)
392ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
393b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov         DRD_(thread_new_segment_and_combine_vc)(tid, sg);
394ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
395b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      else
396b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov         DRD_(thread_new_segment)(tid);
397b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      s_semaphore_segment_creation_count++;
398b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(sg_put)(sg);
399ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
400ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
401ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
402ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called before sem_post(). */
403ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore)
404ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
405ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct semaphore_info* p;
406ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Segment* sg;
407ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
408ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p = drd_semaphore_get_or_allocate(semaphore);
409ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->value++;
410ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
411ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (s_trace_semaphore)
412b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      DRD_(trace_msg)("[%d] sem_post      0x%lx value %u -> %u",
413b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      DRD_(thread_get_running_tid)(),
414b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                      semaphore, p->value - 1, p->value);
415ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
416ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   p->last_sem_post_tid = tid;
417ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   sg = 0;
418ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   DRD_(thread_get_latest_segment)(&sg, tid);
419ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tl_assert(sg);
420ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   drd_segment_push(p, sg);
421ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   DRD_(thread_new_segment)(tid);
422ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   s_semaphore_segment_creation_count++;
423ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
424ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
425ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/** Called after sem_post() finished. */
426ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore,
427ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                               const Bool succeeded)
428ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
429ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /*
430ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * Note: it is hard to implement the sem_post() wrapper correctly in
431ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * case sem_post() returns an error code. This is because handling this
432ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * case correctly requires restoring the vector clock associated with
433ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * the semaphore to its original value here. In order to do that without
434ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * introducing a race condition, extra locking has to be added around
435ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * each semaphore call. Such extra locking would have to be added in
436ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * drd_pthread_intercepts.c. However, it is hard to implement
437ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * synchronization in drd_pthread_intercepts.c in a portable way without
438ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * calling already redirected functions.
439ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    */
440ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
441ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
442ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff BrownULong DRD_(get_semaphore_segment_creation_count)(void)
443ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
444ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return s_semaphore_segment_creation_count;
445ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
446