sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
| 4 | Copyright (C) 2006-2007 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 | |
| 26 | #include "drd_cond.h" |
| 27 | #include "drd_error.h" |
| 28 | #include "drd_mutex.h" |
| 29 | #include "drd_suppression.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 30 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 31 | #include "pub_tool_libcassert.h" // tl_assert() |
| 32 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 33 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 34 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 35 | #include "pub_core_options.h" // VG_(clo_backtrace_size) |
| 36 | |
| 37 | |
| 38 | static struct cond_info s_cond[256]; |
| 39 | static Bool s_trace_cond; |
| 40 | |
| 41 | |
| 42 | void cond_set_trace(const Bool trace_cond) |
| 43 | { |
| 44 | s_trace_cond = trace_cond; |
| 45 | } |
| 46 | |
| 47 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 48 | void cond_initialize(struct cond_info* const p, const Addr cond, |
| 49 | const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 50 | { |
| 51 | tl_assert(cond != 0); |
| 52 | |
| 53 | p->cond = cond; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 54 | p->size = size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | p->waiter_count = 0; |
| 56 | p->mutex = 0; |
| 57 | } |
| 58 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 59 | static struct cond_info* |
| 60 | cond_get_or_allocate(const Addr cond, const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 61 | { |
| 62 | int i; |
| 63 | for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 64 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | if (s_cond[i].cond == cond) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 66 | { |
| 67 | tl_assert(s_cond[i].size == size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 68 | return &s_cond[i]; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 69 | } |
| 70 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++) |
| 72 | { |
| 73 | if (s_cond[i].cond == 0) |
| 74 | { |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 75 | cond_initialize(&s_cond[i], cond, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 76 | /* TO DO: replace the constant below by a symbolic constant referring */ |
| 77 | /* to sizeof(pthread_cond_t). */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 78 | drd_start_suppression(cond, cond + size, "cond"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | return &s_cond[i]; |
| 80 | } |
| 81 | } |
| 82 | tl_assert(0); |
| 83 | return 0; |
| 84 | } |
| 85 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 86 | void cond_init(const Addr cond, const SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 87 | { |
| 88 | if (s_trace_cond) |
| 89 | { |
| 90 | VG_(message)(Vg_UserMsg, "Initializing condition variable 0x%lx", cond); |
| 91 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 92 | VG_(clo_backtrace_size)); |
| 93 | } |
| 94 | tl_assert(cond_get(cond) == 0); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 95 | tl_assert(size > 0); |
| 96 | cond_get_or_allocate(cond, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void cond_destroy(struct cond_info* const p) |
| 100 | { |
| 101 | if (s_trace_cond) |
| 102 | { |
| 103 | VG_(message)(Vg_UserMsg, "Destroying condition variable 0x%lx", p->cond); |
| 104 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 105 | VG_(clo_backtrace_size)); |
| 106 | } |
| 107 | |
| 108 | // TO DO: print a proper error message if waiter_count != 0. |
| 109 | tl_assert(p->waiter_count == 0); |
| 110 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 111 | drd_finish_suppression(p->cond, p->cond + p->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | |
| 113 | p->cond = 0; |
| 114 | p->waiter_count = 0; |
| 115 | p->mutex = 0; |
| 116 | } |
| 117 | |
| 118 | struct cond_info* cond_get(const Addr cond) |
| 119 | { |
| 120 | int i; |
| 121 | for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++) |
| 122 | if (s_cond[i].cond == cond) |
| 123 | return &s_cond[i]; |
| 124 | return 0; |
| 125 | } |
| 126 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 127 | 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] | 128 | { |
| 129 | struct cond_info* p; |
| 130 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 131 | p = cond_get_or_allocate(cond, cond_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 132 | if (p->waiter_count == 0) |
| 133 | { |
| 134 | p->mutex = mutex; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | // TO DO: print a proper error message if two different threads call |
| 139 | // pthread_cond_*wait() on the same condition variable but with a different |
| 140 | // mutex argument. |
| 141 | tl_assert(p->mutex == mutex); |
| 142 | } |
| 143 | return ++p->waiter_count; |
| 144 | } |
| 145 | |
| 146 | int cond_post_wait(const Addr cond) |
| 147 | { |
| 148 | struct cond_info* p; |
| 149 | |
| 150 | p = cond_get(cond); |
| 151 | tl_assert(p); |
| 152 | tl_assert(p->waiter_count > 0); |
| 153 | tl_assert(p->mutex); |
| 154 | if (--p->waiter_count == 0) |
| 155 | { |
| 156 | p->mutex = 0; |
| 157 | } |
| 158 | return p->waiter_count; |
| 159 | } |
| 160 | |
| 161 | void cond_pre_signal(Addr const cond) |
| 162 | { |
| 163 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 164 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 165 | struct cond_info* const cond_p = cond_get(cond); |
| 166 | #if 0 |
| 167 | VG_(message)(Vg_DebugMsg, "cond_pre_signal cond %d, w.c. %d, mutex %d", |
| 168 | cond, |
| 169 | cond_p ? cond_p->waiter_count : 0, |
| 170 | cond_p ? cond_p->mutex : 0); |
| 171 | #endif |
| 172 | if (cond_p && cond_p->waiter_count > 0) |
| 173 | { |
| 174 | if (! mutex_is_locked_by(cond_p->mutex, drd_tid)) |
| 175 | { |
| 176 | CondRaceErrInfo cei; |
| 177 | cei.cond = cond; |
| 178 | cei.mutex = cond_p->mutex; |
| 179 | VG_(maybe_record_error)(vg_tid, |
| 180 | CondRaceErr, |
| 181 | VG_(get_IP)(vg_tid), |
| 182 | "CondErr", |
| 183 | &cei); |
| 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | /* No other thread is waiting for the signal, hence the signal will be */ |
| 189 | /* lost. This is normal in a POSIX threads application. */ |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void cond_pre_broadcast(Addr const cond) |
| 194 | { |
| 195 | cond_pre_signal(cond); |
| 196 | } |
| 197 | |
| 198 | void cond_stop_using_mem(const Addr a1, const Addr a2) |
| 199 | { |
| 200 | unsigned i; |
| 201 | for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++) |
| 202 | { |
| 203 | if (a1 <= s_cond[i].cond && s_cond[i].cond < a2) |
| 204 | { |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame^] | 205 | tl_assert(s_cond[i].cond + s_cond[i].size <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 206 | cond_destroy(&s_cond[i]); |
| 207 | } |
| 208 | } |
| 209 | } |