blob: 51a15ae2142564f58476097c7cb2e182ec875d36 [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2008 Bart Van Assche
5 bart.vanassche@gmail.com
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
bart28230a32008-02-29 17:27:03 +000026#include "drd_clientobj.h"
sewardj85642922008-01-14 11:54:56 +000027#include "drd_error.h"
28#include "drd_semaphore.h"
29#include "drd_suppression.h"
30#include "priv_drd_clientreq.h"
31#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
32#include "pub_tool_libcassert.h" // tl_assert()
33#include "pub_tool_libcprint.h" // VG_(printf)()
34#include "pub_tool_machine.h" // VG_(get_IP)()
35#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
36
37
bart28230a32008-02-29 17:27:03 +000038// Local functions.
sewardj85642922008-01-14 11:54:56 +000039
bart28230a32008-02-29 17:27:03 +000040static void semaphore_cleanup(struct semaphore_info* p);
sewardj85642922008-01-14 11:54:56 +000041
42
43// Local variables.
44
45static Bool s_trace_semaphore;
sewardj85642922008-01-14 11:54:56 +000046
47
48// Function definitions.
49
50void semaphore_set_trace(const Bool trace_semaphore)
51{
52 s_trace_semaphore = trace_semaphore;
53}
54
55static
56void semaphore_initialize(struct semaphore_info* const p,
57 const Addr semaphore,
58 const SizeT size,
59 const UWord value)
60{
61 tl_assert(semaphore != 0);
62 tl_assert(size > 0);
bart28230a32008-02-29 17:27:03 +000063 tl_assert(p->a1 == semaphore);
64 tl_assert(p->a2 - p->a1 == size);
65 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000066
bart28230a32008-02-29 17:27:03 +000067 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
sewardj85642922008-01-14 11:54:56 +000068 p->value = value;
bart28230a32008-02-29 17:27:03 +000069 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +000070 p->last_sem_post_tid = DRD_INVALID_THREADID;
71 vc_init(&p->vc, 0, 0);
72}
73
bart28230a32008-02-29 17:27:03 +000074/** Free the memory that was allocated by semaphore_initialize(). Called by
75 * drd_clientobj_remove().
76 */
77static void semaphore_cleanup(struct semaphore_info* p)
78{
79 if (p->waiters > 0)
80 {
81 VG_(message)(Vg_UserMsg, "Error: destroying semaphore while %d threads are"
82 "still waiting on the semaphore.\n", p->waiters);
83 }
84 vc_cleanup(&p->vc);
85}
86
sewardj85642922008-01-14 11:54:56 +000087static
88struct semaphore_info*
89semaphore_get_or_allocate(const Addr semaphore, const SizeT size)
90{
bart28230a32008-02-29 17:27:03 +000091 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +000092
bart28230a32008-02-29 17:27:03 +000093 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
94 p = &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore;
95 if (p == 0)
sewardj85642922008-01-14 11:54:56 +000096 {
bart28230a32008-02-29 17:27:03 +000097 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
98 p = &drd_clientobj_add(semaphore, semaphore + size,
99 ClientSemaphore)->semaphore;
100 semaphore_initialize(p, semaphore, size, 0);
sewardj85642922008-01-14 11:54:56 +0000101 }
bart28230a32008-02-29 17:27:03 +0000102 return p;
sewardj85642922008-01-14 11:54:56 +0000103}
104
bart28230a32008-02-29 17:27:03 +0000105struct semaphore_info* semaphore_get(const Addr semaphore)
106{
107 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
108 return &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore;
109}
110
111/** Called before sem_init(). */
sewardj85642922008-01-14 11:54:56 +0000112struct semaphore_info* semaphore_init(const Addr semaphore, const SizeT size,
113 const Word pshared, const UWord value)
114{
115 struct semaphore_info* p;
116
117 tl_assert(semaphore_get(semaphore) == 0);
118 p = semaphore_get_or_allocate(semaphore, size);
119 p->value = value;
120 return p;
121}
122
bart28230a32008-02-29 17:27:03 +0000123/** Called after sem_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000124void semaphore_destroy(struct semaphore_info* const p)
125{
bart28230a32008-02-29 17:27:03 +0000126 drd_clientobj_remove(p->a1, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000127}
128
bart28230a32008-02-29 17:27:03 +0000129/** Called before sem_wait(). */
130void semaphore_pre_wait(const Addr semaphore, const SizeT size)
sewardj85642922008-01-14 11:54:56 +0000131{
132 struct semaphore_info* p;
133
134 p = semaphore_get_or_allocate(semaphore, size);
bart28230a32008-02-29 17:27:03 +0000135 if (s_trace_semaphore)
136 {
137 VG_(message)(Vg_UserMsg, "semaphore_pre_wait(0x%lx, %d)", semaphore, size);
138 }
139 tl_assert(p);
140 tl_assert(p->waiters >= 0);
141 p->waiters++;
142 tl_assert(p->waiters > 0);
143}
144
145/** Called after sem_wait() finished.
146 * @note Do not rely on the value of 'waited' -- some glibc versions do
147 * not set it correctly.
148 */
149void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
150 const Bool waited)
151{
152 struct semaphore_info* p;
153
154 p = semaphore_get(semaphore);
155 if (s_trace_semaphore)
156 {
157 VG_(message)(Vg_UserMsg, "semaphore_post_wait(0x%lx, %d)", semaphore);
158 }
159 tl_assert(p->waiters > 0);
160 p->waiters--;
161 tl_assert(p->waiters >= 0);
sewardj85642922008-01-14 11:54:56 +0000162 tl_assert(p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000163 if (p->value == 0)
164 {
165 VG_(message)(Vg_UserMsg, "Invalid semaphore 0x%lx", semaphore);
166 return;
167 }
sewardj85642922008-01-14 11:54:56 +0000168 p->value--;
169 tl_assert(p->value >= 0);
170 if (p->last_sem_post_tid != tid)
171 thread_combine_vc2(tid, &p->vc);
172 thread_new_segment(tid);
173}
174
175/** Called before sem_post(). */
176void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore,
177 const SizeT size)
178{
179 struct semaphore_info* p;
180
181 p = semaphore_get_or_allocate(semaphore, size);
182 p->value++;
183 if (p->value == 1)
184 {
185 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000186 thread_new_segment(tid);
bart9cdaf1e2008-02-24 18:29:43 +0000187 vc_assign(&p->vc, thread_get_vc(tid));
sewardj85642922008-01-14 11:54:56 +0000188 }
189}
190
191/** Called after sem_post() finished successfully. */
192void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
sewardje3b57aa2008-01-18 07:42:01 +0000193 const SizeT size, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000194{
bart25896d92008-02-17 09:21:05 +0000195 /* Note: it is hard to implement the sem_post() wrapper correctly in */
196 /* case sem_post() returns an error code. This is because handling this */
197 /* case correctly requires restoring the vector clock associated with */
198 /* the semaphore to its original value here. In order to do that without */
199 /* introducing a race condition, extra locking has to be added around */
200 /* each semaphore call. Such extra locking would have to be added in */
201 /* drd_intercepts.c. However, it is hard to implement synchronization */
202 /* in drd_intercepts.c in a portable way without calling already */
203 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000204}
205
206void semaphore_thread_delete(const DrdThreadId threadid)
207{ }