blob: f9c94469a3f9e268be18fb761c2a09c6e0da03c7 [file] [log] [blame]
barte7d58722008-02-28 19:08:04 +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
26#ifndef __DRD_CLIENTOBJ_H
27#define __DRD_CLIENTOBJ_H
28
29
bart9d5b7962008-05-14 12:25:00 +000030#include "drd_clientreq.h" /* MutexT */
31#include "drd_thread.h" /* DrdThreadId */
barte7d58722008-02-28 19:08:04 +000032#include "pub_tool_basics.h"
bart9d5b7962008-05-14 12:25:00 +000033#include "pub_tool_execontext.h" /* ExeContext */
bart28230a32008-02-29 17:27:03 +000034#include "pub_tool_oset.h"
bart3e017fa2008-12-17 19:20:13 +000035#include "pub_tool_xarray.h"
barte7d58722008-02-28 19:08:04 +000036
37
38// Forward declarations.
39
40union drd_clientobj;
41
42
43// Type definitions.
44
bart28230a32008-02-29 17:27:03 +000045typedef enum {
46 ClientMutex = 1,
47 ClientCondvar = 2,
48 ClientSemaphore = 3,
49 ClientBarrier = 4,
bart777f7fe2008-03-02 17:43:18 +000050 ClientRwlock = 5,
bart28230a32008-02-29 17:27:03 +000051} ObjType;
barte7d58722008-02-28 19:08:04 +000052
53struct any
54{
bart391d9dc2008-07-03 10:57:30 +000055 Addr a1;
56 ObjType type;
57 void (*cleanup)(union drd_clientobj*);
58 ExeContext* first_observed_at;
barte7d58722008-02-28 19:08:04 +000059};
60
61struct mutex_info
62{
63 Addr a1;
barte7d58722008-02-28 19:08:04 +000064 ObjType type;
65 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +000066 ExeContext* first_observed_at;
barte7d58722008-02-28 19:08:04 +000067 MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t.
68 int recursion_count; // 0 if free, >= 1 if locked.
69 DrdThreadId owner; // owner if locked, last owner if free.
barta2b6e1b2008-03-17 18:32:39 +000070 Segment* last_locked_segment;
bart9d5b7962008-05-14 12:25:00 +000071 ULong acquiry_time_ms;
72 ExeContext* acquired_at;
barte7d58722008-02-28 19:08:04 +000073};
74
bart28230a32008-02-29 17:27:03 +000075struct cond_info
76{
bart391d9dc2008-07-03 10:57:30 +000077 Addr a1;
78 ObjType type;
79 void (*cleanup)(union drd_clientobj*);
80 ExeContext* first_observed_at;
81 int waiter_count;
82 Addr mutex; // Client mutex specified in pthread_cond_wait() call, and
83 // null if no client threads are currently waiting on this cond.var.
bart28230a32008-02-29 17:27:03 +000084};
85
86struct semaphore_info
87{
88 Addr a1;
bart28230a32008-02-29 17:27:03 +000089 ObjType type;
90 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +000091 ExeContext* first_observed_at;
bart94866cc2008-12-21 17:20:22 +000092 UInt initial_value; // Value assigned through sem_init().
bartafb42b72008-12-17 07:32:09 +000093 UInt value; // Semaphore value.
bart28230a32008-02-29 17:27:03 +000094 UWord waiters; // Number of threads inside sem_wait().
95 DrdThreadId last_sem_post_tid; // Thread ID associated with last sem_post().
bart3e017fa2008-12-17 19:20:13 +000096 XArray* last_sem_post_seg; // array of Segment*, used as a stack.
bart28230a32008-02-29 17:27:03 +000097};
98
99struct barrier_info
100{
bart9d5b7962008-05-14 12:25:00 +0000101 Addr a1;
102 ObjType type;
103 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +0000104 ExeContext* first_observed_at;
bart306527d2008-03-12 17:23:07 +0000105 BarrierT barrier_type; // pthread_barrier or gomp_barrier.
bart777f7fe2008-03-02 17:43:18 +0000106 Word count; // Participant count in a barrier wait.
107 Word pre_iteration; // pthread_barrier_wait() call count modulo two.
108 Word post_iteration; // pthread_barrier_wait() call count modulo two.
109 Word pre_waiters_left; // number of waiters left for a complete barrier.
110 Word post_waiters_left; // number of waiters left for a complete barrier.
111 OSet* oset; // Thread-specific barrier information.
112};
113
114struct rwlock_info
115{
bart9d5b7962008-05-14 12:25:00 +0000116 Addr a1;
117 ObjType type;
118 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +0000119 ExeContext* first_observed_at;
bart9d5b7962008-05-14 12:25:00 +0000120 OSet* thread_info;
121 ULong acquiry_time_ms;
122 ExeContext* acquired_at;
bart28230a32008-02-29 17:27:03 +0000123};
124
barte7d58722008-02-28 19:08:04 +0000125typedef union drd_clientobj
126{
bart28230a32008-02-29 17:27:03 +0000127 struct any any;
128 struct mutex_info mutex;
129 struct cond_info cond;
130 struct semaphore_info semaphore;
131 struct barrier_info barrier;
bart777f7fe2008-03-02 17:43:18 +0000132 struct rwlock_info rwlock;
barte7d58722008-02-28 19:08:04 +0000133} DrdClientobj;
134
135
136// Function declarations.
137
bart72b751c2008-03-01 13:44:24 +0000138void clientobj_set_trace(const Bool trace);
139void clientobj_init(void);
140void clientobj_cleanup(void);
bart391d9dc2008-07-03 10:57:30 +0000141DrdClientobj* clientobj_get_any(const Addr addr);
bart72b751c2008-03-01 13:44:24 +0000142DrdClientobj* clientobj_get(const Addr addr, const ObjType t);
143Bool clientobj_present(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000144DrdClientobj* clientobj_add(const Addr a1, const ObjType t);
bart72b751c2008-03-01 13:44:24 +0000145Bool clientobj_remove(const Addr addr, const ObjType t);
146void clientobj_stop_using_mem(const Addr a1, const Addr a2);
147void clientobj_resetiter(void);
148DrdClientobj* clientobj_next(const ObjType t);
bart391d9dc2008-07-03 10:57:30 +0000149const char* clientobj_type_name(const ObjType t);
150
barte7d58722008-02-28 19:08:04 +0000151
152#endif /* __DRD_CLIENTOBJ_H */