1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*++
Module Name:
include/pal/threadsusp.hpp
Abstract:
Declarations for thread suspension
--*/
#ifndef _PAL_THREADSUSP_HPP
#define _PAL_THREADSUSP_HPP
// Need this ifdef since this header is included by .c files so they can use the diagnostic function.
#ifdef __cplusplus
// Note: do not include malloc.hpp from this header. The template InternalDelete
// needs to know the layout of class CPalThread, which includes a member of type
// CThreadSuspensionInfo, which is defined later in this header, and it is not
// yet known at this point.
// If any future change should bring this issue back, the circular dependency can
// be further broken by making the InternalDelete's CPalThread argument a
// templatized argument, so that type checking on it takes place only at
// instantiation time.
#include "pal/threadinfo.hpp"
#include "pal/thread.hpp"
#include "pal/printfcpp.hpp"
#include "pal/mutex.hpp"
#include "pal/init.h"
#if !HAVE_MACH_EXCEPTIONS
#include <signal.h>
#endif // !HAVE_MACH_EXCEPTIONS
#include <semaphore.h>
#include <sched.h>
// We have a variety of options for synchronizing thread suspensions and resumptions between the requestor and
// target threads. Analyze the various capabilities given to us by configure and define one of three macros
// here for simplicity:
// USE_POSIX_SEMAPHORES
// USE_SYSV_SEMAPHORES
// USE_PTHREAD_CONDVARS
#if HAS_POSIX_SEMAPHORES
// Favor posix semaphores.
#define USE_POSIX_SEMAPHORES 1
#if HAVE_SYS_SEMAPHORE_H
#include <sys/semaphore.h>
#elif HAVE_SEMAPHORE_H
#include <semaphore.h>
#endif // HAVE_SYS_SEMAPHORE_H
#elif HAS_PTHREAD_MUTEXES && HAVE_MACH_EXCEPTIONS
// Can only use the pthread solution if we're not using signals since pthread mutexes are not signal safe.
#define USE_PTHREAD_CONDVARS 1
#include <pthread.h>
#elif HAS_SYSV_SEMAPHORES
// SYSV semaphores are our last choice since they're shared across processes so it's possible to leak them
// on abnormal process termination.
#define USE_SYSV_SEMAPHORES 1
#include <sys/sem.h>
#include <sys/types.h>
#else
#error "Don't know how to synchronize thread suspends and resumes on this platform"
#endif // HAS_POSIX_SEMAPHORES
#include <stdarg.h>
namespace CorUnix
{
#ifdef _DEBUG
#define MAX_TRACKED_CRITSECS 8
#endif
PAL_ERROR
InternalResumeThread(
CPalThread *pthrResumer,
HANDLE hTarget,
DWORD *pdwSuspendCount
);
class CThreadSuspensionInfo : public CThreadInfoInitializer
{
private:
BOOL m_fPending; // TRUE if a suspension is pending on a thread (because the thread is in an unsafe region)
BOOL m_fSelfsusp; // TRUE if thread is self suspending and while thread is self suspended
BOOL m_fSuspendedForShutdown; // TRUE once the thread is suspended during PAL cleanup
int m_nBlockingPipe; // blocking pipe used for a process that was created suspended
#ifdef _DEBUG
Volatile<LONG> m_lNumThreadsSuspendedByThisThread; // number of threads that this thread has suspended; used for suspension diagnostics
#endif
#if DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
int m_nSpinlock; // thread's suspension spinlock, which is used to synchronize suspension and resumption attempts
#else // DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
pthread_mutex_t m_ptmSuspmutex; // thread's suspension mutex, which is used to synchronize suspension and resumption attempts
BOOL m_fSuspmutexInitialized;
#endif // DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
#if USE_POSIX_SEMAPHORES
sem_t m_semSusp; // suspension semaphore
sem_t m_semResume; // resumption semaphore
BOOL m_fSemaphoresInitialized;
#elif USE_SYSV_SEMAPHORES
// necessary id's and sembuf structures for SysV semaphores
int m_nSemsuspid; // id for the suspend semaphore
int m_nSemrespid; // id for the resume semaphore
struct sembuf m_sbSemwait; // struct representing a wait operation
struct sembuf m_sbSempost; // struct representing a post operation
#elif USE_PTHREAD_CONDVARS
pthread_cond_t m_condSusp; // suspension condition variable
pthread_mutex_t m_mutexSusp; // mutex associated with the condition above
BOOL m_fSuspended; // set to true once the suspend has been acknowledged
pthread_cond_t m_condResume; // resumption condition variable
pthread_mutex_t m_mutexResume; // mutex associated with the condition above
BOOL m_fResumed; // set to true once the resume has been acknowledged
BOOL m_fSemaphoresInitialized;
#endif // USE_POSIX_SEMAPHORES
/* Most of the variables above are either accessed by a thread
holding the appropriate suspension mutex(es) or are only
accessed by their own threads (and thus don't require
synchronization).
m_fPending, m_fSuspendedForShutdown,
m_fSuspendSignalSent, and m_fResumeSignalSent
may be set by a different thread than the owner and thus
require synchronization.
m_fSelfsusp is set to TRUE only by its own thread but may be later
accessed by other threads.
m_lNumThreadsSuspendedByThisThread is accessed by its owning
thread and therefore does not require synchronization. */
#ifdef _DEBUG
VOID
IncrNumThreadsSuspendedByThisThread(
)
{
InterlockedIncrement(&m_lNumThreadsSuspendedByThisThread);
};
VOID
DecrNumThreadsSuspendedByThisThread(
)
{
InterlockedDecrement(&m_lNumThreadsSuspendedByThisThread);
};
#endif
VOID
AcquireSuspensionLocks(
CPalThread *pthrSuspender,
CPalThread *pthrTarget
);
VOID
ReleaseSuspensionLocks(
CPalThread *pthrSuspender,
CPalThread *pthrTarget
);
#if USE_POSIX_SEMAPHORES
sem_t*
GetSuspendSemaphore(
void
)
{
return &m_semSusp;
};
sem_t*
GetResumeSemaphore(
void
)
{
return &m_semResume;
};
#elif USE_SYSV_SEMAPHORES
int
GetSuspendSemaphoreId(
void
)
{
return m_nSemsuspid;
};
sembuf*
GetSemaphorePostBuffer(
void
)
{
return &m_sbSempost;
};
#endif // USE_POSIX_SEMAPHORES
#if DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
LONG*
GetSuspensionSpinlock(
void
)
{
return &m_nSpinlock;
}
#else // DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
pthread_mutex_t*
GetSuspensionMutex(
void
)
{
return &m_ptmSuspmutex;
}
#endif // DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
void
SetSuspPending(
BOOL fPending
)
{
m_fPending = fPending;
};
BOOL
GetSuspPending(
void
)
{
return m_fPending;
};
void
SetSelfSusp(
BOOL fSelfsusp
)
{
m_fSelfsusp = fSelfsusp;
};
BOOL
GetSelfSusp(
void
)
{
return m_fSelfsusp;
};
void
PostOnSuspendSemaphore();
void
WaitOnSuspendSemaphore();
void
PostOnResumeSemaphore();
void
WaitOnResumeSemaphore();
static
BOOL
TryAcquireSuspensionLock(
CPalThread* pthrTarget
);
int GetBlockingPipe(
void
)
{
return m_nBlockingPipe;
};
public:
virtual PAL_ERROR InitializePreCreate();
CThreadSuspensionInfo()
: m_fPending(FALSE)
, m_fSelfsusp(FALSE)
, m_fSuspendedForShutdown(FALSE)
, m_nBlockingPipe(-1)
#ifdef _DEBUG
, m_lNumThreadsSuspendedByThisThread(0)
#endif // _DEBUG
#if !DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
, m_fSuspmutexInitialized(FALSE)
#endif
#if USE_POSIX_SEMAPHORES || USE_PTHREAD_CONDVARS
, m_fSemaphoresInitialized(FALSE)
#endif
{
InitializeSuspensionLock();
};
virtual ~CThreadSuspensionInfo();
#ifdef _DEBUG
LONG
GetNumThreadsSuspendedByThisThread(
void
)
{
return m_lNumThreadsSuspendedByThisThread;
};
#endif // _DEBUG
#if USE_SYSV_SEMAPHORES
void
DestroySemaphoreIds(
void
);
#endif
void
SetSuspendedForShutdown(
BOOL fSuspendedForShutdown
)
{
m_fSuspendedForShutdown = fSuspendedForShutdown;
};
BOOL
GetSuspendedForShutdown(
void
)
{
return m_fSuspendedForShutdown;
};
void
AcquireSuspensionLock(
CPalThread *pthrCurrent
);
void
ReleaseSuspensionLock(
CPalThread *pthrCurrent
);
PAL_ERROR
InternalSuspendNewThreadFromData(
CPalThread *pThread
);
PAL_ERROR
InternalResumeThreadFromData(
CPalThread *pthrResumer,
CPalThread *pthrTarget,
DWORD *pdwSuspendCount
);
VOID InitializeSuspensionLock();
void SetBlockingPipe(
int nBlockingPipe
)
{
m_nBlockingPipe = nBlockingPipe;
};
};
} //end CorUnix
extern const BYTE WAKEUPCODE; // use for pipe reads during self suspend.
#endif // __cplusplus
#ifdef USE_GLOBAL_LOCK_FOR_SUSPENSION
extern LONG g_ssSuspensionLock;
#endif
#endif // _PAL_THREADSUSP_HPP
|