sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 26 | #include "drd_clientobj.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #include "drd_cond.h" |
| 28 | #include "drd_error.h" |
| 29 | #include "drd_mutex.h" |
| 30 | #include "drd_suppression.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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)() |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 35 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 39 | // Local functions. |
| 40 | |
| 41 | static void cond_cleanup(struct cond_info* p); |
| 42 | |
| 43 | |
| 44 | // Local variables. |
| 45 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | static Bool s_trace_cond; |
| 47 | |
| 48 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 49 | // Function definitions. |
| 50 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 51 | void cond_set_trace(const Bool trace_cond) |
| 52 | { |
| 53 | s_trace_cond = trace_cond; |
| 54 | } |
| 55 | |
| 56 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 57 | void cond_initialize(struct cond_info* const p, const Addr cond, |
| 58 | const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | { |
| 60 | tl_assert(cond != 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 61 | tl_assert(p->a1 == cond); |
| 62 | tl_assert(p->a2 - p->a1 == size); |
| 63 | tl_assert(p->type == ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 64 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 65 | p->cleanup = (void(*)(DrdClientobj*))cond_cleanup; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 66 | p->waiter_count = 0; |
| 67 | p->mutex = 0; |
| 68 | } |
| 69 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 70 | /** Free the memory that was allocated by cond_initialize(). Called by |
| 71 | * drd_clientobj_remove(). |
| 72 | */ |
| 73 | static void cond_cleanup(struct cond_info* p) |
| 74 | { |
| 75 | tl_assert(p); |
| 76 | if (p->mutex) |
| 77 | { |
| 78 | struct mutex_info* q; |
| 79 | q = &drd_clientobj_get(p->mutex, ClientMutex)->mutex; |
| 80 | VG_(message)(Vg_UserMsg, |
| 81 | "Error: destroying condition variable 0x%lx while thread %d" |
| 82 | " is waiting on it.\n", |
| 83 | p->a1, q ? q->owner : -1); |
| 84 | } |
| 85 | } |
| 86 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 87 | static struct cond_info* |
| 88 | cond_get_or_allocate(const Addr cond, const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 90 | struct cond_info *p; |
| 91 | |
| 92 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
| 93 | p = &drd_clientobj_get(cond, ClientCondvar)->cond; |
| 94 | if (p == 0) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 95 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 96 | p = &drd_clientobj_add(cond, cond + size, ClientCondvar)->cond; |
| 97 | cond_initialize(p, cond, size); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 98 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 99 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 102 | struct cond_info* cond_get(const Addr cond) |
| 103 | { |
| 104 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
| 105 | return &drd_clientobj_get(cond, ClientCondvar)->cond; |
| 106 | } |
| 107 | |
| 108 | /** Called before pthread_cond_init(). */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 109 | void cond_init(const Addr cond, const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | { |
| 111 | if (s_trace_cond) |
| 112 | { |
| 113 | VG_(message)(Vg_UserMsg, "Initializing condition variable 0x%lx", cond); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 114 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), VG_(clo_backtrace_size)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 115 | } |
| 116 | tl_assert(cond_get(cond) == 0); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 117 | tl_assert(size > 0); |
| 118 | cond_get_or_allocate(cond, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | } |
| 120 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 121 | /** Called after pthread_cond_destroy(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 122 | void cond_destroy(struct cond_info* const p) |
| 123 | { |
| 124 | if (s_trace_cond) |
| 125 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 126 | VG_(message)(Vg_UserMsg, "Destroying condition variable 0x%lx", p->a1); |
| 127 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), VG_(clo_backtrace_size)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // TO DO: print a proper error message if waiter_count != 0. |
| 131 | tl_assert(p->waiter_count == 0); |
| 132 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 133 | drd_clientobj_remove(p->a1, ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 136 | /** Called before pthread_cond_wait(). */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 137 | int cond_pre_wait(const Addr cond, const SizeT cond_size, const Addr mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | { |
| 139 | struct cond_info* p; |
| 140 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 141 | p = cond_get_or_allocate(cond, cond_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | if (p->waiter_count == 0) |
| 143 | { |
| 144 | p->mutex = mutex; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | // TO DO: print a proper error message if two different threads call |
| 149 | // pthread_cond_*wait() on the same condition variable but with a different |
| 150 | // mutex argument. |
| 151 | tl_assert(p->mutex == mutex); |
| 152 | } |
| 153 | return ++p->waiter_count; |
| 154 | } |
| 155 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 156 | /** Called after pthread_cond_wait(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 157 | int cond_post_wait(const Addr cond) |
| 158 | { |
| 159 | struct cond_info* p; |
| 160 | |
| 161 | p = cond_get(cond); |
| 162 | tl_assert(p); |
| 163 | tl_assert(p->waiter_count > 0); |
| 164 | tl_assert(p->mutex); |
| 165 | if (--p->waiter_count == 0) |
| 166 | { |
| 167 | p->mutex = 0; |
| 168 | } |
| 169 | return p->waiter_count; |
| 170 | } |
| 171 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 172 | /** Called before pthread_cond_signal(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 173 | void cond_pre_signal(Addr const cond) |
| 174 | { |
| 175 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 176 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 177 | struct cond_info* const cond_p = cond_get(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 178 | if (cond_p && cond_p->waiter_count > 0) |
| 179 | { |
| 180 | if (! mutex_is_locked_by(cond_p->mutex, drd_tid)) |
| 181 | { |
| 182 | CondRaceErrInfo cei; |
| 183 | cei.cond = cond; |
| 184 | cei.mutex = cond_p->mutex; |
| 185 | VG_(maybe_record_error)(vg_tid, |
| 186 | CondRaceErr, |
| 187 | VG_(get_IP)(vg_tid), |
| 188 | "CondErr", |
| 189 | &cei); |
| 190 | } |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | /* No other thread is waiting for the signal, hence the signal will be */ |
| 195 | /* lost. This is normal in a POSIX threads application. */ |
| 196 | } |
| 197 | } |
| 198 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 199 | /** Called before pthread_cond_broadcast(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 200 | void cond_pre_broadcast(Addr const cond) |
| 201 | { |
| 202 | cond_pre_signal(cond); |
| 203 | } |
| 204 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 205 | /** Called after pthread_cond_destroy(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 206 | void cond_thread_delete(const DrdThreadId tid) |
| 207 | { } |