bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 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 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 30 | #include "drd_basics.h" /* DrdThreadId */ |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 31 | #include "drd_clientreq.h" /* MutexT */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics.h" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 33 | #include "pub_tool_execontext.h" /* ExeContext */ |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 34 | #include "pub_tool_oset.h" |
bart | 3e017fa | 2008-12-17 19:20:13 +0000 | [diff] [blame] | 35 | #include "pub_tool_xarray.h" |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 36 | |
| 37 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 38 | /* Forward declarations. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 39 | |
| 40 | union drd_clientobj; |
| 41 | |
| 42 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 43 | /* Type definitions. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 44 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 45 | typedef enum { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 46 | ClientMutex = 1, |
| 47 | ClientCondvar = 2, |
| 48 | ClientSemaphore = 3, |
| 49 | ClientBarrier = 4, |
| 50 | ClientRwlock = 5, |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 51 | } ObjType; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 52 | |
| 53 | struct any |
| 54 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 55 | Addr a1; |
| 56 | ObjType type; |
| 57 | void (*cleanup)(union drd_clientobj*); |
| 58 | void (*delete_thread)(union drd_clientobj*, DrdThreadId); |
| 59 | ExeContext* first_observed_at; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct mutex_info |
| 63 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 64 | 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; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 77 | struct cond_info |
| 78 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 79 | 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. |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | struct semaphore_info |
| 90 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 91 | 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. |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | struct barrier_info |
| 105 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 106 | 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. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct rwlock_info |
| 121 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 122 | Addr a1; |
| 123 | ObjType type; |
| 124 | void (*cleanup)(union drd_clientobj*); |
| 125 | void (*delete_thread)(union drd_clientobj*, DrdThreadId); |
| 126 | ExeContext* first_observed_at; |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 127 | RwLockT rwlock_type; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 128 | OSet* thread_info; |
| 129 | ULong acquiry_time_ms; |
| 130 | ExeContext* acquired_at; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 133 | typedef union drd_clientobj |
| 134 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 135 | 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; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 141 | } DrdClientobj; |
| 142 | |
| 143 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 144 | /* Function declarations. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 145 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 146 | void DRD_(clientobj_set_trace)(const Bool trace); |
| 147 | void DRD_(clientobj_init)(void); |
| 148 | void DRD_(clientobj_cleanup)(void); |
| 149 | DrdClientobj* DRD_(clientobj_get_any)(const Addr addr); |
| 150 | DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t); |
| 151 | Bool DRD_(clientobj_present)(const Addr a1, const Addr a2); |
| 152 | DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t); |
| 153 | Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t); |
| 154 | void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 155 | void DRD_(clientobj_delete_thread)(const DrdThreadId tid); |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 156 | const char* DRD_(clientobj_type_name)(const ObjType t); |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 157 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 158 | |
| 159 | #endif /* __DRD_CLIENTOBJ_H */ |