blob: 66882d65bf0ab3e843bf971ded4e22e6c726f466 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
barte7d58722008-02-28 19:08:04 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
barte7d58722008-02-28 19:08:04 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
barte7d58722008-02-28 19:08:04 +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#ifndef __DRD_CLIENTOBJ_H
27#define __DRD_CLIENTOBJ_H
28
29
bart195e41f2009-02-15 11:34:57 +000030#include "drd_basics.h" /* DrdThreadId */
bart9d5b7962008-05-14 12:25:00 +000031#include "drd_clientreq.h" /* MutexT */
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
bart195e41f2009-02-15 11:34:57 +000038/* Forward declarations. */
barte7d58722008-02-28 19:08:04 +000039
40union drd_clientobj;
41
42
bart195e41f2009-02-15 11:34:57 +000043/* Type definitions. */
barte7d58722008-02-28 19:08:04 +000044
bart28230a32008-02-29 17:27:03 +000045typedef enum {
bartbedfd232009-03-26 19:07:15 +000046 ClientMutex = 1,
47 ClientCondvar = 2,
bart62cc2322010-03-07 10:54:21 +000048 ClientHbvar = 3,
49 ClientSemaphore = 4,
50 ClientBarrier = 5,
51 ClientRwlock = 6,
bart28230a32008-02-29 17:27:03 +000052} ObjType;
barte7d58722008-02-28 19:08:04 +000053
54struct any
55{
bartbedfd232009-03-26 19:07:15 +000056 Addr a1;
57 ObjType type;
58 void (*cleanup)(union drd_clientobj*);
59 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
60 ExeContext* first_observed_at;
barte7d58722008-02-28 19:08:04 +000061};
62
63struct mutex_info
64{
bartbedfd232009-03-26 19:07:15 +000065 Addr a1;
66 ObjType type;
67 void (*cleanup)(union drd_clientobj*);
68 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
69 ExeContext* first_observed_at;
70 MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t.
71 int recursion_count; // 0 if free, >= 1 if locked.
72 DrdThreadId owner; // owner if locked, last owner if free.
73 struct segment* last_locked_segment;
74 ULong acquiry_time_ms;
75 ExeContext* acquired_at;
barte7d58722008-02-28 19:08:04 +000076};
77
bart28230a32008-02-29 17:27:03 +000078struct cond_info
79{
bartbedfd232009-03-26 19:07:15 +000080 Addr a1;
81 ObjType type;
82 void (*cleanup)(union drd_clientobj*);
83 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
84 ExeContext* first_observed_at;
85 int waiter_count;
86 Addr mutex; // Client mutex specified in pthread_cond_wait() call, and
bart62cc2322010-03-07 10:54:21 +000087 // null if no client threads are currently waiting on this cond.var.
88};
89
90struct hb_info
91{
92 Addr a1;
93 ObjType type;
94 void (*cleanup)(union drd_clientobj*);
95 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
96 ExeContext* first_observed_at;
97 OSet* oset; // Per-thread order annotation information.
98 Bool done; // Whether happens-done has already been invoked.
bart28230a32008-02-29 17:27:03 +000099};
100
101struct semaphore_info
102{
bartbedfd232009-03-26 19:07:15 +0000103 Addr a1;
104 ObjType type;
105 void (*cleanup)(union drd_clientobj*);
106 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
107 ExeContext* first_observed_at;
108 UInt waits_to_skip; // Number of sem_wait() calls to skip
109 // (due to the value assigned by sem_init()).
110 UInt value; // Semaphore value.
111 UWord waiters; // Number of threads inside sem_wait().
112 DrdThreadId last_sem_post_tid; // Thread ID associated with last sem_post().
113 XArray* last_sem_post_seg; // array of Segment*, used as a stack.
bart28230a32008-02-29 17:27:03 +0000114};
115
116struct barrier_info
117{
bartbedfd232009-03-26 19:07:15 +0000118 Addr a1;
119 ObjType type;
120 void (*cleanup)(union drd_clientobj*);
121 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
122 ExeContext* first_observed_at;
123 BarrierT barrier_type; // pthread_barrier or gomp_barrier.
124 Word count; // Participant count in a barrier wait.
125 Word pre_iteration; // pre barrier completion count modulo two.
126 Word post_iteration; // post barrier completion count modulo two.
127 Word pre_waiters_left; // number of waiters left for a complete barrier.
128 Word post_waiters_left; // number of waiters left for a complete barrier.
129 OSet* oset; // Per-thread barrier information.
bart777f7fe2008-03-02 17:43:18 +0000130};
131
132struct rwlock_info
133{
bartbedfd232009-03-26 19:07:15 +0000134 Addr a1;
135 ObjType type;
136 void (*cleanup)(union drd_clientobj*);
137 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
138 ExeContext* first_observed_at;
bartc8441502009-07-27 16:03:51 +0000139 RwLockT rwlock_type;
bartbedfd232009-03-26 19:07:15 +0000140 OSet* thread_info;
141 ULong acquiry_time_ms;
142 ExeContext* acquired_at;
bart28230a32008-02-29 17:27:03 +0000143};
144
barte7d58722008-02-28 19:08:04 +0000145typedef union drd_clientobj
146{
bartbedfd232009-03-26 19:07:15 +0000147 struct any any;
148 struct mutex_info mutex;
149 struct cond_info cond;
bart62cc2322010-03-07 10:54:21 +0000150 struct hb_info hb;
bartbedfd232009-03-26 19:07:15 +0000151 struct semaphore_info semaphore;
152 struct barrier_info barrier;
153 struct rwlock_info rwlock;
barte7d58722008-02-28 19:08:04 +0000154} DrdClientobj;
155
156
bart195e41f2009-02-15 11:34:57 +0000157/* Function declarations. */
barte7d58722008-02-28 19:08:04 +0000158
bart195e41f2009-02-15 11:34:57 +0000159void DRD_(clientobj_set_trace)(const Bool trace);
160void DRD_(clientobj_init)(void);
161void DRD_(clientobj_cleanup)(void);
162DrdClientobj* DRD_(clientobj_get_any)(const Addr addr);
163DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t);
164Bool DRD_(clientobj_present)(const Addr a1, const Addr a2);
165DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t);
166Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t);
167void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2);
bartd2c5eae2009-02-21 15:27:04 +0000168void DRD_(clientobj_delete_thread)(const DrdThreadId tid);
bart195e41f2009-02-15 11:34:57 +0000169const char* DRD_(clientobj_type_name)(const ObjType t);
bart391d9dc2008-07-03 10:57:30 +0000170
barte7d58722008-02-28 19:08:04 +0000171
172#endif /* __DRD_CLIENTOBJ_H */