|
31 | 31 | #define JRD_THREADSTART_H |
32 | 32 |
|
33 | 33 | #include "../common/ThreadData.h" |
| 34 | +#include "../common/classes/semaphore.h" |
34 | 35 |
|
35 | 36 | #ifdef WIN_NT |
36 | 37 | #include <windows.h> |
@@ -89,4 +90,89 @@ inline ThreadId getThreadId() |
89 | 90 | return Thread::getId(); |
90 | 91 | } |
91 | 92 |
|
| 93 | + |
| 94 | +#ifndef USE_POSIX_THREADS |
| 95 | +#define USE_FINI_SEM |
| 96 | +#endif |
| 97 | + |
| 98 | +template <typename TA> |
| 99 | +class ThreadFinishSync |
| 100 | +{ |
| 101 | +public: |
| 102 | + typedef void ThreadRoutine(TA); |
| 103 | + |
| 104 | + ThreadFinishSync(Firebird::MemoryPool& pool, ThreadRoutine* routine, int priority_arg) |
| 105 | + : |
| 106 | +#ifdef USE_FINI_SEM |
| 107 | + fini(pool), |
| 108 | +#else |
| 109 | + threadHandle(0), |
| 110 | +#endif |
| 111 | + threadRoutine(routine), |
| 112 | + threadPriority(priority_arg) |
| 113 | + { } |
| 114 | + |
| 115 | + void run(TA arg) |
| 116 | + { |
| 117 | + threadArg = arg; |
| 118 | + |
| 119 | + Thread::start(internalRun, this, threadPriority |
| 120 | +#ifndef USE_FINI_SEM |
| 121 | + , &threadHandle |
| 122 | +#endif |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + void waitForCompletion() |
| 127 | + { |
| 128 | +#ifdef USE_FINI_SEM |
| 129 | + fini.enter(); |
| 130 | +#else |
| 131 | + Thread::waitForCompletion(threadHandle); |
| 132 | + threadHandle = 0; |
| 133 | +#endif |
| 134 | + } |
| 135 | + |
| 136 | +private: |
| 137 | +#ifdef USE_FINI_SEM |
| 138 | + Firebird::Semaphore fini; |
| 139 | +#else |
| 140 | + Thread::Handle threadHandle; |
| 141 | +#endif |
| 142 | + |
| 143 | + TA threadArg; |
| 144 | + ThreadRoutine* threadRoutine; |
| 145 | + int threadPriority; |
| 146 | + bool starting; |
| 147 | + |
| 148 | + static THREAD_ENTRY_DECLARE internalRun(THREAD_ENTRY_PARAM arg) |
| 149 | + { |
| 150 | + ((ThreadFinishSync*)arg)->internalRun(); |
| 151 | + return 0; |
| 152 | + } |
| 153 | + |
| 154 | + void internalRun() |
| 155 | + { |
| 156 | + try |
| 157 | + { |
| 158 | + threadRoutine(threadArg); |
| 159 | + } |
| 160 | + catch (const Firebird::Exception& ex) |
| 161 | + { |
| 162 | + threadArg->exceptionHandler(ex, threadRoutine); |
| 163 | + } |
| 164 | + |
| 165 | +#ifdef USE_FINI_SEM |
| 166 | + try |
| 167 | + { |
| 168 | + fini.release(); |
| 169 | + } |
| 170 | + catch (const Firebird::Exception& ex) |
| 171 | + { |
| 172 | + threadArg->exceptionHandler(ex, threadRoutine); |
| 173 | + } |
| 174 | +#endif |
| 175 | + } |
| 176 | +}; |
| 177 | + |
92 | 178 | #endif // JRD_THREADSTART_H |
0 commit comments