blob: b1f3c66f1bbbd19097c6c4974e0b772e01f23643 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
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_vc.h"
27#include "pub_tool_basics.h" // Addr, SizeT
28#include "pub_tool_libcassert.h" // tl_assert()
bart41b226c2009-02-14 16:55:19 +000029#include "pub_tool_libcbase.h" // VG_(memcpy)
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_libcprint.h" // VG_(printf)
31#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
sewardjaf44c822007-11-25 14:01:38 +000032
33
bart41b226c2009-02-14 16:55:19 +000034/* Local function declarations. */
35
sewardjaf44c822007-11-25 14:01:38 +000036static
bart41b226c2009-02-14 16:55:19 +000037void DRD_(vc_reserve)(VectorClock* const vc, const unsigned new_capacity);
sewardjaf44c822007-11-25 14:01:38 +000038
39
bart41b226c2009-02-14 16:55:19 +000040/* Function definitions. */
41
42/**
43 * Initialize the memory 'vc' points at as a vector clock with size 'size'.
44 * If the pointer 'vcelem' is not null, it is assumed to be an array with
45 * 'size' elements and it becomes the initial value of the vector clock.
46 */
47void DRD_(vc_init)(VectorClock* const vc,
48 const VCElem* const vcelem,
49 const unsigned size)
sewardjaf44c822007-11-25 14:01:38 +000050{
bartbedfd232009-03-26 19:07:15 +000051 tl_assert(vc);
52 vc->size = 0;
53 vc->capacity = 0;
54 vc->vc = 0;
55 DRD_(vc_reserve)(vc, size);
56 tl_assert(size == 0 || vc->vc != 0);
57 if (vcelem)
58 {
59 VG_(memcpy)(vc->vc, vcelem, size * sizeof(vcelem[0]));
60 vc->size = size;
61 }
sewardjaf44c822007-11-25 14:01:38 +000062}
63
bart41b226c2009-02-14 16:55:19 +000064/** Reset vc to the empty vector clock. */
65void DRD_(vc_cleanup)(VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +000066{
bartbedfd232009-03-26 19:07:15 +000067 DRD_(vc_reserve)(vc, 0);
sewardjaf44c822007-11-25 14:01:38 +000068}
69
bartc46c2322008-02-24 18:26:46 +000070/** Copy constructor -- initializes *new. */
bart41b226c2009-02-14 16:55:19 +000071void DRD_(vc_copy)(VectorClock* const new, const VectorClock* const rhs)
sewardjaf44c822007-11-25 14:01:38 +000072{
bartbedfd232009-03-26 19:07:15 +000073 DRD_(vc_init)(new, rhs->vc, rhs->size);
sewardjaf44c822007-11-25 14:01:38 +000074}
75
bartc46c2322008-02-24 18:26:46 +000076/** Assignment operator -- *lhs is already a valid vector clock. */
bart41b226c2009-02-14 16:55:19 +000077void DRD_(vc_assign)(VectorClock* const lhs, const VectorClock* const rhs)
bartc46c2322008-02-24 18:26:46 +000078{
bartbedfd232009-03-26 19:07:15 +000079 DRD_(vc_cleanup)(lhs);
80 DRD_(vc_copy)(lhs, rhs);
bartc46c2322008-02-24 18:26:46 +000081}
82
bart41b226c2009-02-14 16:55:19 +000083/** Increment the clock of thread 'tid' in vector clock 'vc'. */
84void DRD_(vc_increment)(VectorClock* const vc, DrdThreadId const tid)
sewardjaf44c822007-11-25 14:01:38 +000085{
bartbedfd232009-03-26 19:07:15 +000086 unsigned i;
87 for (i = 0; i < vc->size; i++)
88 {
89 if (vc->vc[i].threadid == tid)
90 {
91 typeof(vc->vc[i].count) const oldcount = vc->vc[i].count;
92 vc->vc[i].count++;
93 // Check for integer overflow.
94 tl_assert(oldcount < vc->vc[i].count);
95 return;
96 }
97 }
sewardjaf44c822007-11-25 14:01:38 +000098
bartbedfd232009-03-26 19:07:15 +000099 /*
100 * The specified thread ID does not yet exist in the vector clock
101 * -- insert it.
102 */
103 {
104 const VCElem vcelem = { tid, 1 };
105 VectorClock vc2;
106 DRD_(vc_init)(&vc2, &vcelem, 1);
107 DRD_(vc_combine)(vc, &vc2);
108 DRD_(vc_cleanup)(&vc2);
109 }
sewardjaf44c822007-11-25 14:01:38 +0000110}
111
112/**
sewardjaf44c822007-11-25 14:01:38 +0000113 * @return True if vector clocks vc1 and vc2 are ordered, and false otherwise.
114 * Order is as imposed by thread synchronization actions ("happens before").
115 */
bart41b226c2009-02-14 16:55:19 +0000116Bool DRD_(vc_ordered)(const VectorClock* const vc1,
117 const VectorClock* const vc2)
sewardjaf44c822007-11-25 14:01:38 +0000118{
bartbedfd232009-03-26 19:07:15 +0000119 return DRD_(vc_lte)(vc1, vc2) || DRD_(vc_lte)(vc2, vc1);
sewardjaf44c822007-11-25 14:01:38 +0000120}
121
bart5bd9f2d2008-03-03 20:31:58 +0000122/** Compute elementwise minimum. */
bart41b226c2009-02-14 16:55:19 +0000123void DRD_(vc_min)(VectorClock* const result, const VectorClock* const rhs)
sewardjaf44c822007-11-25 14:01:38 +0000124{
bartbedfd232009-03-26 19:07:15 +0000125 unsigned i;
126 unsigned j;
sewardjaf44c822007-11-25 14:01:38 +0000127
bartbedfd232009-03-26 19:07:15 +0000128 tl_assert(result);
129 tl_assert(rhs);
sewardjaf44c822007-11-25 14:01:38 +0000130
bartbedfd232009-03-26 19:07:15 +0000131 DRD_(vc_check)(result);
sewardjaf44c822007-11-25 14:01:38 +0000132
bartbedfd232009-03-26 19:07:15 +0000133 /* Next, combine both vector clocks into one. */
134 i = 0;
135 for (j = 0; j < rhs->size; j++)
136 {
137 while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid)
sewardjaf44c822007-11-25 14:01:38 +0000138 {
bartbedfd232009-03-26 19:07:15 +0000139 /* Thread ID is missing in second vector clock. Clear the count. */
140 result->vc[i].count = 0;
141 i++;
sewardjaf44c822007-11-25 14:01:38 +0000142 }
bartbedfd232009-03-26 19:07:15 +0000143 if (i >= result->size)
144 {
145 break;
146 }
147 if (result->vc[i].threadid <= rhs->vc[j].threadid)
148 {
149 /* The thread ID is present in both vector clocks. Compute the */
150 /* minimum of vc[i].count and vc[j].count. */
151 tl_assert(result->vc[i].threadid == rhs->vc[j].threadid);
152 if (rhs->vc[j].count < result->vc[i].count)
153 {
154 result->vc[i].count = rhs->vc[j].count;
155 }
156 }
157 }
158 DRD_(vc_check)(result);
sewardjaf44c822007-11-25 14:01:38 +0000159}
160
161/**
162 * Compute elementwise maximum.
163 */
bart41b226c2009-02-14 16:55:19 +0000164void DRD_(vc_combine)(VectorClock* const result, const VectorClock* const rhs)
sewardjaf44c822007-11-25 14:01:38 +0000165{
bartbedfd232009-03-26 19:07:15 +0000166 unsigned i;
167 unsigned j;
168 unsigned shared;
169 unsigned new_size;
sewardjaf44c822007-11-25 14:01:38 +0000170
bartbedfd232009-03-26 19:07:15 +0000171 tl_assert(result);
172 tl_assert(rhs);
sewardjaf44c822007-11-25 14:01:38 +0000173
bartbedfd232009-03-26 19:07:15 +0000174 // First count the number of shared thread id's.
175 j = 0;
176 shared = 0;
177 for (i = 0; i < result->size; i++)
178 {
179 while (j < rhs->size && rhs->vc[j].threadid < result->vc[i].threadid)
180 j++;
181 if (j >= rhs->size)
182 break;
183 if (result->vc[i].threadid == rhs->vc[j].threadid)
184 shared++;
185 }
sewardjaf44c822007-11-25 14:01:38 +0000186
bartbedfd232009-03-26 19:07:15 +0000187 DRD_(vc_check)(result);
sewardjaf44c822007-11-25 14:01:38 +0000188
bartbedfd232009-03-26 19:07:15 +0000189 new_size = result->size + rhs->size - shared;
190 if (new_size > result->capacity)
191 DRD_(vc_reserve)(result, new_size);
sewardjaf44c822007-11-25 14:01:38 +0000192
bartbedfd232009-03-26 19:07:15 +0000193 DRD_(vc_check)(result);
sewardjaf44c822007-11-25 14:01:38 +0000194
bartbedfd232009-03-26 19:07:15 +0000195 // Next, combine both vector clocks into one.
196 i = 0;
197 for (j = 0; j < rhs->size; j++)
198 {
199 /* First of all, skip those clocks in result->vc[] for which there */
200 /* is no corresponding clock in rhs->vc[]. */
201 while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid)
bartfbbac092008-04-06 14:57:37 +0000202 {
bartbedfd232009-03-26 19:07:15 +0000203 i++;
bartfbbac092008-04-06 14:57:37 +0000204 }
bartbedfd232009-03-26 19:07:15 +0000205 /* If the end of *result is met, append rhs->vc[j] to *result. */
206 if (i >= result->size)
bartfbbac092008-04-06 14:57:37 +0000207 {
bartbedfd232009-03-26 19:07:15 +0000208 result->size++;
209 result->vc[i] = rhs->vc[j];
bartfbbac092008-04-06 14:57:37 +0000210 }
bartbedfd232009-03-26 19:07:15 +0000211 /* If clock rhs->vc[j] is not in *result, insert it. */
212 else if (result->vc[i].threadid > rhs->vc[j].threadid)
sewardjaf44c822007-11-25 14:01:38 +0000213 {
bartbedfd232009-03-26 19:07:15 +0000214 unsigned k;
215 for (k = result->size; k > i; k--)
216 {
217 result->vc[k] = result->vc[k - 1];
218 }
219 result->size++;
220 result->vc[i] = rhs->vc[j];
sewardjaf44c822007-11-25 14:01:38 +0000221 }
bartbedfd232009-03-26 19:07:15 +0000222 /* Otherwise, both *result and *rhs have a clock for thread */
223 /* result->vc[i].threadid == rhs->vc[j].threadid. Compute the maximum. */
224 else
bartfbbac092008-04-06 14:57:37 +0000225 {
bartbedfd232009-03-26 19:07:15 +0000226 tl_assert(result->vc[i].threadid == rhs->vc[j].threadid);
bartbedfd232009-03-26 19:07:15 +0000227 if (rhs->vc[j].count > result->vc[i].count)
228 {
229 result->vc[i].count = rhs->vc[j].count;
230 }
bartfbbac092008-04-06 14:57:37 +0000231 }
bartbedfd232009-03-26 19:07:15 +0000232 }
233 DRD_(vc_check)(result);
234 tl_assert(result->size == new_size);
sewardjaf44c822007-11-25 14:01:38 +0000235}
236
bart41b226c2009-02-14 16:55:19 +0000237/** Print the contents of vector clock 'vc'. */
238void DRD_(vc_print)(const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000239{
bart8f822af2009-06-08 18:20:42 +0000240 char* str;
sewardjaf44c822007-11-25 14:01:38 +0000241
bart8f822af2009-06-08 18:20:42 +0000242 if ((str = DRD_(vc_aprint)(vc)) != NULL)
bartbedfd232009-03-26 19:07:15 +0000243 {
bart8f822af2009-06-08 18:20:42 +0000244 VG_(printf)("%s", str);
245 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +0000246 }
sewardjaf44c822007-11-25 14:01:38 +0000247}
248
bart41b226c2009-02-14 16:55:19 +0000249/**
bart8f822af2009-06-08 18:20:42 +0000250 * Print the contents of vector clock 'vc' to a newly allocated string.
251 * The caller must call VG_(free)() on the return value of this function.
bart41b226c2009-02-14 16:55:19 +0000252 */
bart8f822af2009-06-08 18:20:42 +0000253char* DRD_(vc_aprint)(const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000254{
bartbedfd232009-03-26 19:07:15 +0000255 unsigned i;
bart8f822af2009-06-08 18:20:42 +0000256 unsigned reserved;
257 unsigned size;
258 char* str = 0;
sewardjaf44c822007-11-25 14:01:38 +0000259
bartbedfd232009-03-26 19:07:15 +0000260 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +0000261 reserved = 64;
262 size = 0;
263 str = VG_(realloc)("drd.vc.aprint.1", str, reserved);
264 if (! str)
265 return str;
266 size += VG_(snprintf)(str, reserved, "[");
bartbedfd232009-03-26 19:07:15 +0000267 for (i = 0; i < vc->size; i++)
268 {
269 tl_assert(vc->vc);
bart8f822af2009-06-08 18:20:42 +0000270 if (VG_(strlen)(str) + 32 > reserved)
bartbedfd232009-03-26 19:07:15 +0000271 {
bart8f822af2009-06-08 18:20:42 +0000272 reserved *= 2;
273 str = VG_(realloc)("drd.vc.aprint.2", str, reserved);
274 if (! str)
275 return str;
bartbedfd232009-03-26 19:07:15 +0000276 }
bart8f822af2009-06-08 18:20:42 +0000277 size += VG_(snprintf)(str + size, reserved - size,
278 "%s %d: %d", i > 0 ? "," : "",
279 vc->vc[i].threadid, vc->vc[i].count);
bartbedfd232009-03-26 19:07:15 +0000280 }
bart8f822af2009-06-08 18:20:42 +0000281 size += VG_(snprintf)(str + size, reserved - size, " ]");
282
283 return str;
sewardjaf44c822007-11-25 14:01:38 +0000284}
285
286/**
287 * Invariant test.
bart41b226c2009-02-14 16:55:19 +0000288 *
289 * The function below tests whether the following two conditions are
290 * satisfied:
291 * - size <= capacity.
292 * - Vector clock elements are stored in thread ID order.
293 *
294 * If one of these conditions is not met, an assertion failure is triggered.
sewardjaf44c822007-11-25 14:01:38 +0000295 */
bart41b226c2009-02-14 16:55:19 +0000296void DRD_(vc_check)(const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000297{
bartbedfd232009-03-26 19:07:15 +0000298 unsigned i;
299 tl_assert(vc->size <= vc->capacity);
300 for (i = 1; i < vc->size; i++)
301 {
302 tl_assert(vc->vc[i-1].threadid < vc->vc[i].threadid);
303 }
sewardjaf44c822007-11-25 14:01:38 +0000304}
305
306/**
307 * Change the size of the memory block pointed at by vc->vc.
308 * Changes capacity, but does not change size. If the size of the memory
309 * block is increased, the newly allocated memory is not initialized.
310 */
311static
bart41b226c2009-02-14 16:55:19 +0000312void DRD_(vc_reserve)(VectorClock* const vc, const unsigned new_capacity)
sewardjaf44c822007-11-25 14:01:38 +0000313{
bartbedfd232009-03-26 19:07:15 +0000314 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +0000315 tl_assert(vc->capacity > VC_PREALLOCATED
316 || vc->vc == 0
317 || vc->vc == vc->preallocated);
318
bartbedfd232009-03-26 19:07:15 +0000319 if (new_capacity > vc->capacity)
320 {
bart8f822af2009-06-08 18:20:42 +0000321 if (vc->vc && vc->capacity > VC_PREALLOCATED)
bartbedfd232009-03-26 19:07:15 +0000322 {
bart8f822af2009-06-08 18:20:42 +0000323 tl_assert(vc->vc
324 && vc->vc != vc->preallocated
325 && vc->capacity > VC_PREALLOCATED);
bartbedfd232009-03-26 19:07:15 +0000326 vc->vc = VG_(realloc)("drd.vc.vr.1",
327 vc->vc, new_capacity * sizeof(vc->vc[0]));
328 }
bart8f822af2009-06-08 18:20:42 +0000329 else if (vc->vc && new_capacity > VC_PREALLOCATED)
bartbedfd232009-03-26 19:07:15 +0000330 {
bart8f822af2009-06-08 18:20:42 +0000331 tl_assert((vc->vc == 0 || vc->vc == vc->preallocated)
332 && new_capacity > VC_PREALLOCATED
333 && vc->capacity <= VC_PREALLOCATED);
bartbedfd232009-03-26 19:07:15 +0000334 vc->vc = VG_(malloc)("drd.vc.vr.2",
335 new_capacity * sizeof(vc->vc[0]));
bart8f822af2009-06-08 18:20:42 +0000336 VG_(memcpy)(vc->vc, vc->preallocated,
337 vc->capacity * sizeof(vc->vc[0]));
338 }
339 else if (vc->vc)
340 {
341 tl_assert(vc->vc == vc->preallocated
342 && new_capacity <= VC_PREALLOCATED
343 && vc->capacity <= VC_PREALLOCATED);
344 }
345 else if (new_capacity > VC_PREALLOCATED)
346 {
347 tl_assert(vc->vc == 0
348 && new_capacity > VC_PREALLOCATED
349 && vc->capacity == 0);
350 vc->vc = VG_(malloc)("drd.vc.vr.3",
351 new_capacity * sizeof(vc->vc[0]));
bartbedfd232009-03-26 19:07:15 +0000352 }
353 else
354 {
bart8f822af2009-06-08 18:20:42 +0000355 tl_assert(vc->vc == 0
356 && new_capacity <= VC_PREALLOCATED
357 && vc->capacity == 0);
358 vc->vc = vc->preallocated;
bartbedfd232009-03-26 19:07:15 +0000359 }
360 vc->capacity = new_capacity;
361 }
bart48fcfc62009-06-03 12:44:50 +0000362 else if (new_capacity == 0 && vc->vc)
363 {
bart8f822af2009-06-08 18:20:42 +0000364 if (vc->capacity > VC_PREALLOCATED)
365 VG_(free)(vc->vc);
bart48fcfc62009-06-03 12:44:50 +0000366 vc->vc = 0;
bart8f822af2009-06-08 18:20:42 +0000367 vc->capacity = 0;
bart48fcfc62009-06-03 12:44:50 +0000368 }
bart8f822af2009-06-08 18:20:42 +0000369
bartbedfd232009-03-26 19:07:15 +0000370 tl_assert(new_capacity == 0 || vc->vc != 0);
bart8f822af2009-06-08 18:20:42 +0000371 tl_assert(vc->capacity > VC_PREALLOCATED
372 || vc->vc == 0
373 || vc->vc == vc->preallocated);
sewardjaf44c822007-11-25 14:01:38 +0000374}