blob: 13941c5ea2aa35cf561a47f0dea503972453726c [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,
48 ClientSemaphore = 3,
49 ClientBarrier = 4,
50 ClientRwlock = 5,
bart28230a32008-02-29 17:27:03 +000051} ObjType;
barte7d58722008-02-28 19:08:04 +000052
53struct any
54{
bartbedfd232009-03-26 19:07:15 +000055 Addr a1;
56 ObjType type;
57 void (*cleanup)(union drd_clientobj*);
58 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
59 ExeContext* first_observed_at;
barte7d58722008-02-28 19:08:04 +000060};
61
62struct mutex_info
63{
bartbedfd232009-03-26 19:07:15 +000064 Addr a1;
65 ObjType type;
66 void (*cleanup)(union drd_clientobj*);
67 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
68 ExeContext* first_observed_at;
69 MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t.
70 int recursion_count; // 0 if free, >= 1 if locked.
71 DrdThreadId owner; // owner if locked, last owner if free.
72 struct segment* last_locked_segment;
73 ULong acquiry_time_ms;
74 ExeContext* acquired_at;
barte7d58722008-02-28 19:08:04 +000075};
76
bart28230a32008-02-29 17:27:03 +000077struct cond_info
78{
bartbedfd232009-03-26 19:07:15 +000079 Addr a1;
80 ObjType type;
81 void (*cleanup)(union drd_clientobj*);
82 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
83 ExeContext* first_observed_at;
84 int waiter_count;
85 Addr mutex; // Client mutex specified in pthread_cond_wait() call, and
86 // null if no client threads are currently waiting on this cond.var.
bart28230a32008-02-29 17:27:03 +000087};
88
89struct semaphore_info
90{
bartbedfd232009-03-26 19:07:15 +000091 Addr a1;
92 ObjType type;
93 void (*cleanup)(union drd_clientobj*);
94 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
95 ExeContext* first_observed_at;
96 UInt waits_to_skip; // Number of sem_wait() calls to skip
97 // (due to the value assigned by sem_init()).
98 UInt value; // Semaphore value.
99 UWord waiters; // Number of threads inside sem_wait().
100 DrdThreadId last_sem_post_tid; // Thread ID associated with last sem_post().
101 XArray* last_sem_post_seg; // array of Segment*, used as a stack.
bart28230a32008-02-29 17:27:03 +0000102};
103
104struct barrier_info
105{
bartbedfd232009-03-26 19:07:15 +0000106 Addr a1;
107 ObjType type;
108 void (*cleanup)(union drd_clientobj*);
109 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
110 ExeContext* first_observed_at;
111 BarrierT barrier_type; // pthread_barrier or gomp_barrier.
112 Word count; // Participant count in a barrier wait.
113 Word pre_iteration; // pre barrier completion count modulo two.
114 Word post_iteration; // post barrier completion count modulo two.
115 Word pre_waiters_left; // number of waiters left for a complete barrier.
116 Word post_waiters_left; // number of waiters left for a complete barrier.
117 OSet* oset; // Per-thread barrier information.
bart777f7fe2008-03-02 17:43:18 +0000118};
119
120struct rwlock_info
121{
bartbedfd232009-03-26 19:07:15 +0000122 Addr a1;
123 ObjType type;
124 void (*cleanup)(union drd_clientobj*);
125 void (*delete_thread)(union drd_clientobj*, DrdThreadId);
126 ExeContext* first_observed_at;
bartc8441502009-07-27 16:03:51 +0000127 RwLockT rwlock_type;
bartbedfd232009-03-26 19:07:15 +0000128 OSet* thread_info;
129 ULong acquiry_time_ms;
130 ExeContext* acquired_at;
bart28230a32008-02-29 17:27:03 +0000131};
132
barte7d58722008-02-28 19:08:04 +0000133typedef union drd_clientobj
134{
bartbedfd232009-03-26 19:07:15 +0000135 struct any any;
136 struct mutex_info mutex;
137 struct cond_info cond;
138 struct semaphore_info semaphore;
139 struct barrier_info barrier;
140 struct rwlock_info rwlock;
barte7d58722008-02-28 19:08:04 +0000141} DrdClientobj;
142
143
bart195e41f2009-02-15 11:34:57 +0000144/* Function declarations. */
barte7d58722008-02-28 19:08:04 +0000145
bart195e41f2009-02-15 11:34:57 +0000146void DRD_(clientobj_set_trace)(const Bool trace);
147void DRD_(clientobj_init)(void);
148void DRD_(clientobj_cleanup)(void);
149DrdClientobj* DRD_(clientobj_get_any)(const Addr addr);
150DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t);
151Bool DRD_(clientobj_present)(const Addr a1, const Addr a2);
152DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t);
153Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t);
154void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2);
bartd2c5eae2009-02-21 15:27:04 +0000155void DRD_(clientobj_delete_thread)(const DrdThreadId tid);
bart195e41f2009-02-15 11:34:57 +0000156const char* DRD_(clientobj_type_name)(const ObjType t);
bart391d9dc2008-07-03 10:57:30 +0000157
barte7d58722008-02-28 19:08:04 +0000158
159#endif /* __DRD_CLIENTOBJ_H */