- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathThreadStart.h
181 lines (151 loc) · 3.76 KB
/
ThreadStart.h
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
/*
* PROGRAM: JRD access method
* MODULE: thd.h
* DESCRIPTION: Thread support definitions
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2002.10.28 Sean Leyne - Completed removal of obsolete "DGUX" port
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* Alex Peshkov
*/
#ifndef JRD_THREADSTART_H
#defineJRD_THREADSTART_H
#include"../common/ThreadData.h"
#include"../common/classes/semaphore.h"
#ifdef WIN_NT
#include<windows.h>
#endif
// Thread priorities (may be ignored)
constint THREAD_high = 1;
constint THREAD_medium_high = 2;
constint THREAD_medium = 3;
constint THREAD_medium_low = 4;
constint THREAD_low = 5;
constint THREAD_critical = 6;
// Thread startup
// BRS 01/07/2004
// Hack due to a bug in mingw.
// The definition inside the thdd class should be replaced with the following one.
typedef THREAD_ENTRY_DECLARE ThreadEntryPoint(THREAD_ENTRY_PARAM);
#if defined(WIN_NT)
typedef DWORD ThreadId;
#elif defined(LINUX) && !defined(ANDROID) && !defined(LSB_BUILD)
#defineUSE_LWP_AS_THREAD_ID
typedefint ThreadId;
#elif defined(USE_POSIX_THREADS)
typedefpthread_t ThreadId;
#else
error - unknown ThreadId type
#endif
classThread
{
public:
#ifdef WIN_NT
typedef DWORD InternalId;
typedef HANDLE Handle;
#endif
#ifdef USE_POSIX_THREADS
typedefpthread_tHandle;
typedefpthread_t InternalId;
#endif
static Thread start(ThreadEntryPoint* routine, void* arg, int priority_arg, Handle* p_handle = NULL);
staticvoidwaitForCompletion(Handle& handle);
staticvoidkill(Handle& handle);
static ThreadId getId();
static ThreadId getIdFromHandle(Handle threadHandle);
staticvoidsleep(unsigned milliseconds);
staticvoidyield();
staticboolisCurrent(InternalId iid);
boolisCurrent();
Thread()
{
memset(&internalId, 0, sizeof(internalId));
}
private:
Thread(InternalId iid)
: internalId(iid)
{ }
InternalId internalId;
};
inline ThreadId getThreadId()
{
returnThread::getId();
}
template <typename TA, void (*cleanup) (TA) = nullptr>
classThreadFinishSync
{
public:
typedefvoidThreadRoutine(TA);
ThreadFinishSync(Firebird::MemoryPool& pool, ThreadRoutine* routine, int priority_arg = THREAD_medium)
: threadHandle(0),
threadRoutine(routine),
threadPriority(priority_arg),
closing(false)
{ }
voidrun(TA arg)
{
threadArg = arg;
Thread::start(internalRun, this, threadPriority, &threadHandle);
}
booltryWait()
{
if (closing)
{
waitForCompletion();
returntrue;
}
returnfalse;
}
voidwaitForCompletion()
{
if (threadHandle)
{
Thread::waitForCompletion(threadHandle);
threadHandle = 0;
}
}
private:
Thread::Handle threadHandle;
TA threadArg;
ThreadRoutine* threadRoutine;
int threadPriority;
bool closing;
static THREAD_ENTRY_DECLARE internalRun(THREAD_ENTRY_PARAM arg)
{
((ThreadFinishSync*) arg)->internalRun();
return0;
}
voidinternalRun()
{
try
{
threadRoutine(threadArg);
}
catch (const Firebird::Exception& ex)
{
threadArg->exceptionHandler(ex, threadRoutine);
}
if (cleanup)
cleanup(threadArg);
closing = true;
}
};
#endif // JRD_THREADSTART_H