blob: 98f4fd93c3d25fd8e6fcf1bf61b0d1727a28d813 [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;
bartafb42b72008-12-17 07:32:09 +000092 UInt value; // Semaphore value.
bart28230a32008-02-29 17:27:03 +000093 UWord waiters; // Number of threads inside sem_wait().
94 DrdThreadId last_sem_post_tid; // Thread ID associated with last sem_post().
bart3e017fa2008-12-17 19:20:13 +000095 XArray* last_sem_post_seg; // array of Segment*, used as a stack.
bart28230a32008-02-29 17:27:03 +000096};
97
98struct barrier_info
99{
bart9d5b7962008-05-14 12:25:00 +0000100 Addr a1;
101 ObjType type;
102 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +0000103 ExeContext* first_observed_at;
bart306527d2008-03-12 17:23:07 +0000104 BarrierT barrier_type; // pthread_barrier or gomp_barrier.
bart777f7fe2008-03-02 17:43:18 +0000105 Word count; // Participant count in a barrier wait.
106 Word pre_iteration; // pthread_barrier_wait() call count modulo two.
107 Word post_iteration; // pthread_barrier_wait() call count modulo two.
108 Word pre_waiters_left; // number of waiters left for a complete barrier.
109 Word post_waiters_left; // number of waiters left for a complete barrier.
110 OSet* oset; // Thread-specific barrier information.
111};
112
113struct rwlock_info
114{
bart9d5b7962008-05-14 12:25:00 +0000115 Addr a1;
116 ObjType type;
117 void (*cleanup)(union drd_clientobj*);
bart391d9dc2008-07-03 10:57:30 +0000118 ExeContext* first_observed_at;
bart9d5b7962008-05-14 12:25:00 +0000119 OSet* thread_info;
120 ULong acquiry_time_ms;
121 ExeContext* acquired_at;
bart28230a32008-02-29 17:27:03 +0000122};
123
barte7d58722008-02-28 19:08:04 +0000124typedef union drd_clientobj
125{
bart28230a32008-02-29 17:27:03 +0000126 struct any any;
127 struct mutex_info mutex;
128 struct cond_info cond;
129 struct semaphore_info semaphore;
130 struct barrier_info barrier;
bart777f7fe2008-03-02 17:43:18 +0000131 struct rwlock_info rwlock;
barte7d58722008-02-28 19:08:04 +0000132} DrdClientobj;
133
134
135// Function declarations.
136
bart72b751c2008-03-01 13:44:24 +0000137void clientobj_set_trace(const Bool trace);
138void clientobj_init(void);
139void clientobj_cleanup(void);
bart391d9dc2008-07-03 10:57:30 +0000140DrdClientobj* clientobj_get_any(const Addr addr);
bart72b751c2008-03-01 13:44:24 +0000141DrdClientobj* clientobj_get(const Addr addr, const ObjType t);
142Bool clientobj_present(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000143DrdClientobj* clientobj_add(const Addr a1, const ObjType t);
bart72b751c2008-03-01 13:44:24 +0000144Bool clientobj_remove(const Addr addr, const ObjType t);
145void clientobj_stop_using_mem(const Addr a1, const Addr a2);
146void clientobj_resetiter(void);
147DrdClientobj* clientobj_next(const ObjType t);
bart391d9dc2008-07-03 10:57:30 +0000148const char* clientobj_type_name(const ObjType t);
149
barte7d58722008-02-28 19:08:04 +0000150
151#endif /* __DRD_CLIENTOBJ_H */