root / omnithread / nt.cc @ 4d84dd43
History | View | Annotate | Download (21.7 kB)
| 1 | // Package : omnithread
|
|---|---|
| 2 | // omnithread/nt.cc Created : 6/95 tjr
|
| 3 | //
|
| 4 | // Copyright (C) 2006 Free Software Foundation, Inc.
|
| 5 | // Copyright (C) 1995-1999 AT&T Laboratories Cambridge
|
| 6 | //
|
| 7 | // This file is part of the omnithread library
|
| 8 | //
|
| 9 | // The omnithread library is free software; you can redistribute it and/or
|
| 10 | // modify it under the terms of the GNU Library General Public
|
| 11 | // License as published by the Free Software Foundation; either
|
| 12 | // version 2 of the License, or (at your option) any later version.
|
| 13 | //
|
| 14 | // This library is distributed in the hope that it will be useful,
|
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 17 | // Library General Public License for more details.
|
| 18 | //
|
| 19 | // You should have received a copy of the GNU Library General Public
|
| 20 | // License along with this library; if not, write to the Free
|
| 21 | // Software Foundation, Inc., 51 Franklin Street, Boston, MA
|
| 22 | // 02110-1301, USA
|
| 23 | //
|
| 24 | |
| 25 | //
|
| 26 | // Implementation of OMNI thread abstraction for NT threads
|
| 27 | //
|
| 28 | |
| 29 | #ifdef HAVE_CONFIG_H
|
| 30 | #include <config.h> |
| 31 | #endif
|
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <errno.h> |
| 35 | #include <WinError.h> |
| 36 | #include <omnithread.h> |
| 37 | #include <process.h> |
| 38 | |
| 39 | #define DB(x) // x |
| 40 | //#include <iostream.h> or #include <iostream> if DB is on.
|
| 41 | |
| 42 | static void get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec); |
| 43 | |
| 44 | ///////////////////////////////////////////////////////////////////////////
|
| 45 | //
|
| 46 | // Mutex
|
| 47 | //
|
| 48 | ///////////////////////////////////////////////////////////////////////////
|
| 49 | |
| 50 | |
| 51 | omni_mutex::omni_mutex(void)
|
| 52 | {
|
| 53 | InitializeCriticalSection(&crit); |
| 54 | } |
| 55 | |
| 56 | omni_mutex::~omni_mutex(void)
|
| 57 | {
|
| 58 | DeleteCriticalSection(&crit); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | ///////////////////////////////////////////////////////////////////////////
|
| 64 | //
|
| 65 | // Condition variable
|
| 66 | //
|
| 67 | ///////////////////////////////////////////////////////////////////////////
|
| 68 | |
| 69 | |
| 70 | //
|
| 71 | // Condition variables are tricky to implement using NT synchronisation
|
| 72 | // primitives, since none of them have the atomic "release mutex and wait to be
|
| 73 | // signalled" which is central to the idea of a condition variable. To get
|
| 74 | // around this the solution is to record which threads are waiting and
|
| 75 | // explicitly wake up those threads.
|
| 76 | //
|
| 77 | // Here we implement a condition variable using a list of waiting threads
|
| 78 | // (protected by a critical section), and a per-thread semaphore (which
|
| 79 | // actually only needs to be a binary semaphore).
|
| 80 | //
|
| 81 | // To wait on the cv, a thread puts itself on the list of waiting threads for
|
| 82 | // that cv, then releases the mutex and waits on its own personal semaphore. A
|
| 83 | // signalling thread simply takes a thread from the head of the list and kicks
|
| 84 | // that thread's semaphore. Broadcast is simply implemented by kicking the
|
| 85 | // semaphore of each waiting thread.
|
| 86 | //
|
| 87 | // The only other tricky part comes when a thread gets a timeout from a timed
|
| 88 | // wait on its semaphore. Between returning with a timeout from the wait and
|
| 89 | // entering the critical section, a signalling thread could get in, kick the
|
| 90 | // waiting thread's semaphore and remove it from the list. If this happens,
|
| 91 | // the waiting thread's semaphore is now out of step so it needs resetting, and
|
| 92 | // the thread should indicate that it was signalled rather than that it timed
|
| 93 | // out.
|
| 94 | //
|
| 95 | // It is possible that the thread calling wait or timedwait is not a
|
| 96 | // omni_thread. In this case we have to provide a temporary data structure,
|
| 97 | // i.e. for the duration of the call, for the thread to link itself on the
|
| 98 | // list of waiting threads. _internal_omni_thread_dummy provides such
|
| 99 | // a data structure and _internal_omni_thread_helper is a helper class to
|
| 100 | // deal with this special case for wait() and timedwait(). Once created,
|
| 101 | // the _internal_omni_thread_dummy is cached for use by the next wait() or
|
| 102 | // timedwait() call from a non-omni_thread. This is probably worth doing
|
| 103 | // because creating a Semaphore is quite heavy weight.
|
| 104 | |
| 105 | class _internal_omni_thread_helper; |
| 106 | |
| 107 | class _internal_omni_thread_dummy : public omni_thread { |
| 108 | public:
|
| 109 | inline _internal_omni_thread_dummy() : next(0) { } |
| 110 | inline ~_internal_omni_thread_dummy() { }
|
| 111 | friend class _internal_omni_thread_helper; |
| 112 | private:
|
| 113 | _internal_omni_thread_dummy* next; |
| 114 | }; |
| 115 | |
| 116 | class _internal_omni_thread_helper { |
| 117 | public:
|
| 118 | inline _internal_omni_thread_helper() {
|
| 119 | d = 0;
|
| 120 | t = omni_thread::self(); |
| 121 | if (!t) {
|
| 122 | omni_mutex_lock sync(cachelock); |
| 123 | if (cache) {
|
| 124 | d = cache; |
| 125 | cache = cache->next; |
| 126 | } |
| 127 | else {
|
| 128 | d = new _internal_omni_thread_dummy;
|
| 129 | } |
| 130 | t = d; |
| 131 | } |
| 132 | } |
| 133 | inline ~_internal_omni_thread_helper() {
|
| 134 | if (d) {
|
| 135 | omni_mutex_lock sync(cachelock); |
| 136 | d->next = cache; |
| 137 | cache = d; |
| 138 | } |
| 139 | } |
| 140 | inline operator omni_thread* () { return t; } |
| 141 | inline omni_thread* operator->() { return t; } |
| 142 | |
| 143 | static _internal_omni_thread_dummy* cache;
|
| 144 | static omni_mutex cachelock;
|
| 145 | |
| 146 | private:
|
| 147 | _internal_omni_thread_dummy* d; |
| 148 | omni_thread* t; |
| 149 | }; |
| 150 | |
| 151 | _internal_omni_thread_dummy* _internal_omni_thread_helper::cache = 0;
|
| 152 | omni_mutex _internal_omni_thread_helper::cachelock; |
| 153 | |
| 154 | |
| 155 | omni_condition::omni_condition(omni_mutex* m) : mutex(m) |
| 156 | {
|
| 157 | InitializeCriticalSection(&crit); |
| 158 | waiting_head = waiting_tail = NULL;
|
| 159 | } |
| 160 | |
| 161 | |
| 162 | omni_condition::~omni_condition(void)
|
| 163 | {
|
| 164 | DeleteCriticalSection(&crit); |
| 165 | DB( if (waiting_head != NULL) { |
| 166 | cerr << "omni_condition::~omni_condition: list of waiting threads "
|
| 167 | << "is not empty\n";
|
| 168 | } ) |
| 169 | } |
| 170 | |
| 171 | |
| 172 | void
|
| 173 | omni_condition::wait(void)
|
| 174 | {
|
| 175 | _internal_omni_thread_helper me; |
| 176 | |
| 177 | EnterCriticalSection(&crit); |
| 178 | |
| 179 | me->cond_next = NULL;
|
| 180 | me->cond_prev = waiting_tail; |
| 181 | if (waiting_head == NULL) |
| 182 | waiting_head = me; |
| 183 | else
|
| 184 | waiting_tail->cond_next = me; |
| 185 | waiting_tail = me; |
| 186 | me->cond_waiting = TRUE; |
| 187 | |
| 188 | LeaveCriticalSection(&crit); |
| 189 | |
| 190 | mutex->unlock(); |
| 191 | |
| 192 | DWORD result = WaitForSingleObject(me->cond_semaphore, INFINITE); |
| 193 | |
| 194 | mutex->lock(); |
| 195 | |
| 196 | if (result != WAIT_OBJECT_0)
|
| 197 | throw omni_thread_fatal(GetLastError());
|
| 198 | } |
| 199 | |
| 200 | |
| 201 | int
|
| 202 | omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec) |
| 203 | {
|
| 204 | _internal_omni_thread_helper me; |
| 205 | |
| 206 | EnterCriticalSection(&crit); |
| 207 | |
| 208 | me->cond_next = NULL;
|
| 209 | me->cond_prev = waiting_tail; |
| 210 | if (waiting_head == NULL) |
| 211 | waiting_head = me; |
| 212 | else
|
| 213 | waiting_tail->cond_next = me; |
| 214 | waiting_tail = me; |
| 215 | me->cond_waiting = TRUE; |
| 216 | |
| 217 | LeaveCriticalSection(&crit); |
| 218 | |
| 219 | mutex->unlock(); |
| 220 | |
| 221 | unsigned long now_sec, now_nsec; |
| 222 | |
| 223 | get_time_now(&now_sec, &now_nsec); |
| 224 | |
| 225 | DWORD timeout; |
| 226 | if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < now_nsec)))
|
| 227 | timeout = 0;
|
| 228 | else {
|
| 229 | timeout = (abs_sec-now_sec) * 1000;
|
| 230 | |
| 231 | if( abs_nsec < now_nsec ) timeout -= (now_nsec-abs_nsec) / 1000000; |
| 232 | else timeout += (abs_nsec-now_nsec) / 1000000; |
| 233 | } |
| 234 | |
| 235 | DWORD result = WaitForSingleObject(me->cond_semaphore, timeout); |
| 236 | |
| 237 | if (result == WAIT_TIMEOUT) {
|
| 238 | EnterCriticalSection(&crit); |
| 239 | |
| 240 | if (me->cond_waiting) {
|
| 241 | if (me->cond_prev != NULL) |
| 242 | me->cond_prev->cond_next = me->cond_next; |
| 243 | else
|
| 244 | waiting_head = me->cond_next; |
| 245 | if (me->cond_next != NULL) |
| 246 | me->cond_next->cond_prev = me->cond_prev; |
| 247 | else
|
| 248 | waiting_tail = me->cond_prev; |
| 249 | me->cond_waiting = FALSE; |
| 250 | |
| 251 | LeaveCriticalSection(&crit); |
| 252 | |
| 253 | mutex->lock(); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | //
|
| 258 | // We timed out but another thread still signalled us. Wait for
|
| 259 | // the semaphore (it _must_ have been signalled) to decrement it
|
| 260 | // again. Return that we were signalled, not that we timed out.
|
| 261 | //
|
| 262 | |
| 263 | LeaveCriticalSection(&crit); |
| 264 | |
| 265 | result = WaitForSingleObject(me->cond_semaphore, INFINITE); |
| 266 | } |
| 267 | |
| 268 | if (result != WAIT_OBJECT_0)
|
| 269 | throw omni_thread_fatal(GetLastError());
|
| 270 | |
| 271 | mutex->lock(); |
| 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void
|
| 277 | omni_condition::signal(void)
|
| 278 | {
|
| 279 | EnterCriticalSection(&crit); |
| 280 | |
| 281 | if (waiting_head != NULL) { |
| 282 | omni_thread* t = waiting_head; |
| 283 | waiting_head = t->cond_next; |
| 284 | if (waiting_head == NULL) |
| 285 | waiting_tail = NULL;
|
| 286 | else
|
| 287 | waiting_head->cond_prev = NULL;
|
| 288 | t->cond_waiting = FALSE; |
| 289 | |
| 290 | if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) { |
| 291 | int rc = GetLastError();
|
| 292 | LeaveCriticalSection(&crit); |
| 293 | throw omni_thread_fatal(rc);
|
| 294 | } |
| 295 | } |
| 296 | |
| 297 | LeaveCriticalSection(&crit); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | void
|
| 302 | omni_condition::broadcast(void)
|
| 303 | {
|
| 304 | EnterCriticalSection(&crit); |
| 305 | |
| 306 | while (waiting_head != NULL) { |
| 307 | omni_thread* t = waiting_head; |
| 308 | waiting_head = t->cond_next; |
| 309 | if (waiting_head == NULL) |
| 310 | waiting_tail = NULL;
|
| 311 | else
|
| 312 | waiting_head->cond_prev = NULL;
|
| 313 | t->cond_waiting = FALSE; |
| 314 | |
| 315 | if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) { |
| 316 | int rc = GetLastError();
|
| 317 | LeaveCriticalSection(&crit); |
| 318 | throw omni_thread_fatal(rc);
|
| 319 | } |
| 320 | } |
| 321 | |
| 322 | LeaveCriticalSection(&crit); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | ///////////////////////////////////////////////////////////////////////////
|
| 328 | //
|
| 329 | // Counting semaphore
|
| 330 | //
|
| 331 | ///////////////////////////////////////////////////////////////////////////
|
| 332 | |
| 333 | |
| 334 | #define SEMAPHORE_MAX 0x7fffffff |
| 335 | |
| 336 | |
| 337 | omni_semaphore::omni_semaphore(unsigned int initial, unsigned int max_count) |
| 338 | {
|
| 339 | if (max_count > SEMAPHORE_MAX)
|
| 340 | max_count= SEMAPHORE_MAX; |
| 341 | |
| 342 | nt_sem = CreateSemaphore(NULL, initial, max_count, NULL); |
| 343 | |
| 344 | if (nt_sem == NULL) { |
| 345 | DB( cerr << "omni_semaphore::omni_semaphore: CreateSemaphore error "
|
| 346 | << GetLastError() << endl ); |
| 347 | throw omni_thread_fatal(GetLastError());
|
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | omni_semaphore::~omni_semaphore(void)
|
| 353 | {
|
| 354 | if (!CloseHandle(nt_sem)) {
|
| 355 | DB( cerr << "omni_semaphore::~omni_semaphore: CloseHandle error "
|
| 356 | << GetLastError() << endl ); |
| 357 | throw omni_thread_fatal(GetLastError());
|
| 358 | } |
| 359 | } |
| 360 | |
| 361 | |
| 362 | void
|
| 363 | omni_semaphore::wait(void)
|
| 364 | {
|
| 365 | if (WaitForSingleObject(nt_sem, INFINITE) != WAIT_OBJECT_0)
|
| 366 | throw omni_thread_fatal(GetLastError());
|
| 367 | } |
| 368 | |
| 369 | |
| 370 | int
|
| 371 | omni_semaphore::trywait(void)
|
| 372 | {
|
| 373 | switch (WaitForSingleObject(nt_sem, 0)) { |
| 374 | |
| 375 | case WAIT_OBJECT_0:
|
| 376 | return 1; |
| 377 | case WAIT_TIMEOUT:
|
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | throw omni_thread_fatal(GetLastError());
|
| 382 | return 0; /* keep msvc++ happy */ |
| 383 | } |
| 384 | |
| 385 | |
| 386 | void
|
| 387 | omni_semaphore::post(void)
|
| 388 | {
|
| 389 | if (!ReleaseSemaphore(nt_sem, 1, NULL) |
| 390 | && GetLastError() != ERROR_TOO_MANY_POSTS ) // MinGW fix--see ticket:95 in trac
|
| 391 | throw omni_thread_fatal(GetLastError());
|
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | ///////////////////////////////////////////////////////////////////////////
|
| 397 | //
|
| 398 | // Thread
|
| 399 | //
|
| 400 | ///////////////////////////////////////////////////////////////////////////
|
| 401 | |
| 402 | |
| 403 | //
|
| 404 | // Static variables
|
| 405 | //
|
| 406 | |
| 407 | omni_mutex* omni_thread::next_id_mutex; |
| 408 | int omni_thread::next_id = 0; |
| 409 | static DWORD self_tls_index;
|
| 410 | |
| 411 | static unsigned int stack_size = 0; |
| 412 | |
| 413 | //
|
| 414 | // Initialisation function (gets called before any user code).
|
| 415 | //
|
| 416 | |
| 417 | static int& count() { |
| 418 | static int the_count = 0; |
| 419 | return the_count;
|
| 420 | } |
| 421 | |
| 422 | omni_thread::init_t::init_t(void)
|
| 423 | {
|
| 424 | if (count()++ != 0) // only do it once however many objects get created. |
| 425 | return;
|
| 426 | |
| 427 | DB(cerr << "omni_thread::init: NT implementation initialising\n");
|
| 428 | |
| 429 | self_tls_index = TlsAlloc(); |
| 430 | |
| 431 | if (self_tls_index == 0xffffffff) |
| 432 | throw omni_thread_fatal(GetLastError());
|
| 433 | |
| 434 | next_id_mutex = new omni_mutex;
|
| 435 | |
| 436 | //
|
| 437 | // Create object for this (i.e. initial) thread.
|
| 438 | //
|
| 439 | |
| 440 | omni_thread* t = new omni_thread;
|
| 441 | |
| 442 | t->_state = STATE_RUNNING; |
| 443 | |
| 444 | if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
| 445 | GetCurrentProcess(), &t->handle, |
| 446 | 0, FALSE, DUPLICATE_SAME_ACCESS))
|
| 447 | throw omni_thread_fatal(GetLastError());
|
| 448 | |
| 449 | t->nt_id = GetCurrentThreadId(); |
| 450 | |
| 451 | DB(cerr << "initial thread " << t->id() << " NT thread id " << t->nt_id |
| 452 | << endl); |
| 453 | |
| 454 | if (!TlsSetValue(self_tls_index, (LPVOID)t))
|
| 455 | throw omni_thread_fatal(GetLastError());
|
| 456 | |
| 457 | if (!SetThreadPriority(t->handle, nt_priority(PRIORITY_NORMAL)))
|
| 458 | throw omni_thread_fatal(GetLastError());
|
| 459 | } |
| 460 | |
| 461 | omni_thread::init_t::~init_t(void)
|
| 462 | {
|
| 463 | if (--count() != 0) return; |
| 464 | |
| 465 | omni_thread* self = omni_thread::self(); |
| 466 | if (!self) return; |
| 467 | |
| 468 | TlsSetValue(self_tls_index, (LPVOID)0);
|
| 469 | delete self;
|
| 470 | |
| 471 | delete next_id_mutex;
|
| 472 | |
| 473 | TlsFree(self_tls_index); |
| 474 | } |
| 475 | |
| 476 | //
|
| 477 | // Wrapper for thread creation.
|
| 478 | //
|
| 479 | |
| 480 | extern "C" |
| 481 | #ifndef __BCPLUSPLUS__
|
| 482 | unsigned __stdcall
|
| 483 | #else
|
| 484 | void _USERENTRY
|
| 485 | #endif
|
| 486 | omni_thread_wrapper(void* ptr)
|
| 487 | {
|
| 488 | omni_thread* me = (omni_thread*)ptr; |
| 489 | |
| 490 | DB(cerr << "omni_thread_wrapper: thread " << me->id()
|
| 491 | << " started\n");
|
| 492 | |
| 493 | if (!TlsSetValue(self_tls_index, (LPVOID)me))
|
| 494 | throw omni_thread_fatal(GetLastError());
|
| 495 | |
| 496 | //
|
| 497 | // Now invoke the thread function with the given argument.
|
| 498 | //
|
| 499 | |
| 500 | if (me->fn_void != NULL) { |
| 501 | (*me->fn_void)(me->thread_arg); |
| 502 | omni_thread::exit(); |
| 503 | } |
| 504 | |
| 505 | if (me->fn_ret != NULL) { |
| 506 | void* return_value = (*me->fn_ret)(me->thread_arg);
|
| 507 | omni_thread::exit(return_value); |
| 508 | } |
| 509 | |
| 510 | if (me->detached) {
|
| 511 | me->run(me->thread_arg); |
| 512 | omni_thread::exit(); |
| 513 | } else {
|
| 514 | void* return_value = me->run_undetached(me->thread_arg);
|
| 515 | omni_thread::exit(return_value); |
| 516 | } |
| 517 | |
| 518 | // should never get here.
|
| 519 | #ifndef __BCPLUSPLUS__
|
| 520 | return 0; |
| 521 | #endif
|
| 522 | } |
| 523 | |
| 524 | |
| 525 | //
|
| 526 | // Constructors for omni_thread - set up the thread object but don't
|
| 527 | // start it running.
|
| 528 | //
|
| 529 | |
| 530 | // construct a detached thread running a given function.
|
| 531 | |
| 532 | omni_thread::omni_thread(void (*fn)(void*), void* arg, priority_t pri) |
| 533 | {
|
| 534 | common_constructor(arg, pri, 1);
|
| 535 | fn_void = fn; |
| 536 | fn_ret = NULL;
|
| 537 | } |
| 538 | |
| 539 | // construct an undetached thread running a given function.
|
| 540 | |
| 541 | omni_thread::omni_thread(void* (*fn)(void*), void* arg, priority_t pri) |
| 542 | {
|
| 543 | common_constructor(arg, pri, 0);
|
| 544 | fn_void = NULL;
|
| 545 | fn_ret = fn; |
| 546 | } |
| 547 | |
| 548 | // construct a thread which will run either run() or run_undetached().
|
| 549 | |
| 550 | omni_thread::omni_thread(void* arg, priority_t pri)
|
| 551 | {
|
| 552 | common_constructor(arg, pri, 1);
|
| 553 | fn_void = NULL;
|
| 554 | fn_ret = NULL;
|
| 555 | } |
| 556 | |
| 557 | // common part of all constructors.
|
| 558 | |
| 559 | void
|
| 560 | omni_thread::common_constructor(void* arg, priority_t pri, int det) |
| 561 | {
|
| 562 | _state = STATE_NEW; |
| 563 | _priority = pri; |
| 564 | |
| 565 | next_id_mutex->lock(); |
| 566 | _id = next_id++; |
| 567 | next_id_mutex->unlock(); |
| 568 | |
| 569 | thread_arg = arg; |
| 570 | detached = det; // may be altered in start_undetached()
|
| 571 | |
| 572 | cond_semaphore = CreateSemaphore(NULL, 0, SEMAPHORE_MAX, NULL); |
| 573 | |
| 574 | if (cond_semaphore == NULL) |
| 575 | throw omni_thread_fatal(GetLastError());
|
| 576 | |
| 577 | cond_next = cond_prev = NULL;
|
| 578 | cond_waiting = FALSE; |
| 579 | |
| 580 | handle = NULL;
|
| 581 | |
| 582 | _dummy = 0;
|
| 583 | _values = 0;
|
| 584 | _value_alloc = 0;
|
| 585 | } |
| 586 | |
| 587 | |
| 588 | //
|
| 589 | // Destructor for omni_thread.
|
| 590 | //
|
| 591 | |
| 592 | omni_thread::~omni_thread(void)
|
| 593 | {
|
| 594 | DB(cerr << "destructor called for thread " << id() << endl);
|
| 595 | if (_values) {
|
| 596 | for (key_t i=0; i < _value_alloc; i++) { |
| 597 | if (_values[i]) {
|
| 598 | delete _values[i];
|
| 599 | } |
| 600 | } |
| 601 | delete [] _values;
|
| 602 | } |
| 603 | if (handle && !CloseHandle(handle))
|
| 604 | throw omni_thread_fatal(GetLastError());
|
| 605 | if (cond_semaphore && !CloseHandle(cond_semaphore))
|
| 606 | throw omni_thread_fatal(GetLastError());
|
| 607 | } |
| 608 | |
| 609 | |
| 610 | //
|
| 611 | // Start the thread
|
| 612 | //
|
| 613 | |
| 614 | void
|
| 615 | omni_thread::start(void)
|
| 616 | {
|
| 617 | omni_mutex_lock l(mutex); |
| 618 | |
| 619 | if (_state != STATE_NEW)
|
| 620 | throw omni_thread_invalid();
|
| 621 | |
| 622 | #ifndef __BCPLUSPLUS__
|
| 623 | // MSVC++ or compatiable
|
| 624 | unsigned int t; |
| 625 | handle = (HANDLE)_beginthreadex( |
| 626 | NULL,
|
| 627 | stack_size, |
| 628 | omni_thread_wrapper, |
| 629 | (LPVOID)this,
|
| 630 | CREATE_SUSPENDED, |
| 631 | &t); |
| 632 | nt_id = t; |
| 633 | if (handle == NULL) |
| 634 | throw omni_thread_fatal(GetLastError());
|
| 635 | #else
|
| 636 | // Borland C++
|
| 637 | handle = (HANDLE)_beginthreadNT(omni_thread_wrapper, |
| 638 | stack_size, |
| 639 | (void*)this, |
| 640 | NULL,
|
| 641 | CREATE_SUSPENDED, |
| 642 | &nt_id); |
| 643 | if (handle == INVALID_HANDLE_VALUE)
|
| 644 | throw omni_thread_fatal(errno);
|
| 645 | #endif
|
| 646 | |
| 647 | if (!SetThreadPriority(handle, nt_priority(_priority)))
|
| 648 | throw omni_thread_fatal(GetLastError());
|
| 649 | |
| 650 | if (ResumeThread(handle) == 0xffffffff) |
| 651 | throw omni_thread_fatal(GetLastError());
|
| 652 | |
| 653 | _state = STATE_RUNNING; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | //
|
| 658 | // Start a thread which will run the member function run_undetached().
|
| 659 | //
|
| 660 | |
| 661 | void
|
| 662 | omni_thread::start_undetached(void)
|
| 663 | {
|
| 664 | if ((fn_void != NULL) || (fn_ret != NULL)) |
| 665 | throw omni_thread_invalid();
|
| 666 | |
| 667 | detached = 0;
|
| 668 | start(); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | //
|
| 673 | // join - simply check error conditions & call WaitForSingleObject.
|
| 674 | //
|
| 675 | |
| 676 | void
|
| 677 | omni_thread::join(void** status)
|
| 678 | {
|
| 679 | mutex.lock(); |
| 680 | |
| 681 | if ((_state != STATE_RUNNING) && (_state != STATE_TERMINATED)) {
|
| 682 | mutex.unlock(); |
| 683 | throw omni_thread_invalid();
|
| 684 | } |
| 685 | |
| 686 | mutex.unlock(); |
| 687 | |
| 688 | if (this == self()) |
| 689 | throw omni_thread_invalid();
|
| 690 | |
| 691 | if (detached)
|
| 692 | throw omni_thread_invalid();
|
| 693 | |
| 694 | DB(cerr << "omni_thread::join: doing WaitForSingleObject\n");
|
| 695 | |
| 696 | if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0)
|
| 697 | throw omni_thread_fatal(GetLastError());
|
| 698 | |
| 699 | DB(cerr << "omni_thread::join: WaitForSingleObject succeeded\n");
|
| 700 | |
| 701 | if (status)
|
| 702 | *status = return_val; |
| 703 | |
| 704 | delete this; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | //
|
| 709 | // Change this thread's priority.
|
| 710 | //
|
| 711 | |
| 712 | void
|
| 713 | omni_thread::set_priority(priority_t pri) |
| 714 | {
|
| 715 | omni_mutex_lock l(mutex); |
| 716 | |
| 717 | if (_state != STATE_RUNNING)
|
| 718 | throw omni_thread_invalid();
|
| 719 | |
| 720 | _priority = pri; |
| 721 | |
| 722 | if (!SetThreadPriority(handle, nt_priority(pri)))
|
| 723 | throw omni_thread_fatal(GetLastError());
|
| 724 | } |
| 725 | |
| 726 | |
| 727 | //
|
| 728 | // create - construct a new thread object and start it running. Returns thread
|
| 729 | // object if successful, null pointer if not.
|
| 730 | //
|
| 731 | |
| 732 | // detached version
|
| 733 | |
| 734 | omni_thread* |
| 735 | omni_thread::create(void (*fn)(void*), void* arg, priority_t pri) |
| 736 | {
|
| 737 | omni_thread* t = new omni_thread(fn, arg, pri);
|
| 738 | t->start(); |
| 739 | return t;
|
| 740 | } |
| 741 | |
| 742 | // undetached version
|
| 743 | |
| 744 | omni_thread* |
| 745 | omni_thread::create(void* (*fn)(void*), void* arg, priority_t pri) |
| 746 | {
|
| 747 | omni_thread* t = new omni_thread(fn, arg, pri);
|
| 748 | t->start(); |
| 749 | return t;
|
| 750 | } |
| 751 | |
| 752 | |
| 753 | //
|
| 754 | // exit() _must_ lock the mutex even in the case of a detached thread. This is
|
| 755 | // because a thread may run to completion before the thread that created it has
|
| 756 | // had a chance to get out of start(). By locking the mutex we ensure that the
|
| 757 | // creating thread must have reached the end of start() before we delete the
|
| 758 | // thread object. Of course, once the call to start() returns, the user can
|
| 759 | // still incorrectly refer to the thread object, but that's their problem.
|
| 760 | //
|
| 761 | |
| 762 | void
|
| 763 | omni_thread::exit(void* return_value)
|
| 764 | {
|
| 765 | omni_thread* me = self(); |
| 766 | |
| 767 | if (me)
|
| 768 | {
|
| 769 | me->mutex.lock(); |
| 770 | |
| 771 | me->_state = STATE_TERMINATED; |
| 772 | |
| 773 | me->mutex.unlock(); |
| 774 | |
| 775 | DB(cerr << "omni_thread::exit: thread " << me->id() << " detached " |
| 776 | << me->detached << " return value " << return_value << endl);
|
| 777 | |
| 778 | if (me->detached) {
|
| 779 | delete me;
|
| 780 | } else {
|
| 781 | me->return_val = return_value; |
| 782 | } |
| 783 | } |
| 784 | else
|
| 785 | {
|
| 786 | DB(cerr << "omni_thread::exit: called with a non-omnithread. Exit quietly." << endl);
|
| 787 | } |
| 788 | #ifndef __BCPLUSPLUS__
|
| 789 | // MSVC++ or compatiable
|
| 790 | // _endthreadex() does not automatically closes the thread handle.
|
| 791 | // The omni_thread dtor closes the thread handle.
|
| 792 | _endthreadex(0);
|
| 793 | #else
|
| 794 | // Borland C++
|
| 795 | // _endthread() does not automatically closes the thread handle.
|
| 796 | // _endthreadex() is only available if __MFC_COMPAT__ is defined and
|
| 797 | // all it does is to call _endthread().
|
| 798 | _endthread(); |
| 799 | #endif
|
| 800 | } |
| 801 | |
| 802 | |
| 803 | omni_thread* |
| 804 | omni_thread::self(void)
|
| 805 | {
|
| 806 | LPVOID me; |
| 807 | |
| 808 | me = TlsGetValue(self_tls_index); |
| 809 | |
| 810 | if (me == NULL) { |
| 811 | DB(cerr << "omni_thread::self: called with a non-ominthread. NULL is returned." << endl);
|
| 812 | } |
| 813 | return (omni_thread*)me;
|
| 814 | } |
| 815 | |
| 816 | |
| 817 | void
|
| 818 | omni_thread::yield(void)
|
| 819 | {
|
| 820 | Sleep(0);
|
| 821 | } |
| 822 | |
| 823 | |
| 824 | #define MAX_SLEEP_SECONDS (DWORD)4294966 // (2**32-2)/1000 |
| 825 | |
| 826 | void
|
| 827 | omni_thread::sleep(unsigned long secs, unsigned long nanosecs) |
| 828 | {
|
| 829 | if (secs <= MAX_SLEEP_SECONDS) {
|
| 830 | Sleep(secs * 1000 + nanosecs / 1000000); |
| 831 | return;
|
| 832 | } |
| 833 | |
| 834 | DWORD no_of_max_sleeps = secs / MAX_SLEEP_SECONDS; |
| 835 | |
| 836 | for (DWORD i = 0; i < no_of_max_sleeps; i++) |
| 837 | Sleep(MAX_SLEEP_SECONDS * 1000);
|
| 838 | |
| 839 | Sleep((secs % MAX_SLEEP_SECONDS) * 1000 + nanosecs / 1000000); |
| 840 | } |
| 841 | |
| 842 | |
| 843 | void
|
| 844 | omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec, |
| 845 | unsigned long rel_sec, unsigned long rel_nsec) |
| 846 | {
|
| 847 | get_time_now(abs_sec, abs_nsec); |
| 848 | *abs_nsec += rel_nsec; |
| 849 | *abs_sec += rel_sec + *abs_nsec / 1000000000;
|
| 850 | *abs_nsec = *abs_nsec % 1000000000;
|
| 851 | } |
| 852 | |
| 853 | |
| 854 | int
|
| 855 | omni_thread::nt_priority(priority_t pri) |
| 856 | {
|
| 857 | switch (pri) {
|
| 858 | |
| 859 | case PRIORITY_LOW:
|
| 860 | return THREAD_PRIORITY_LOWEST;
|
| 861 | |
| 862 | case PRIORITY_NORMAL:
|
| 863 | return THREAD_PRIORITY_NORMAL;
|
| 864 | |
| 865 | case PRIORITY_HIGH:
|
| 866 | return THREAD_PRIORITY_HIGHEST;
|
| 867 | } |
| 868 | |
| 869 | throw omni_thread_invalid();
|
| 870 | return 0; /* keep msvc++ happy */ |
| 871 | } |
| 872 | |
| 873 | |
| 874 | static void |
| 875 | get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec) |
| 876 | {
|
| 877 | static int days_in_preceding_months[12] |
| 878 | = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
|
| 879 | static int days_in_preceding_months_leap[12] |
| 880 | = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
|
| 881 | |
| 882 | SYSTEMTIME st; |
| 883 | |
| 884 | GetSystemTime(&st); |
| 885 | *abs_nsec = st.wMilliseconds * 1000000;
|
| 886 | |
| 887 | // this formula should work until 1st March 2100
|
| 888 | |
| 889 | DWORD days = ((st.wYear - 1970) * 365 + (st.wYear - 1969) / 4 |
| 890 | + ((st.wYear % 4)
|
| 891 | ? days_in_preceding_months[st.wMonth - 1]
|
| 892 | : days_in_preceding_months_leap[st.wMonth - 1])
|
| 893 | + st.wDay - 1);
|
| 894 | |
| 895 | *abs_sec = st.wSecond + 60 * (st.wMinute + 60 * (st.wHour + 24 * days)); |
| 896 | } |
| 897 | |
| 898 | void
|
| 899 | omni_thread::stacksize(unsigned long sz) |
| 900 | {
|
| 901 | stack_size = sz; |
| 902 | } |
| 903 | |
| 904 | unsigned long |
| 905 | omni_thread::stacksize() |
| 906 | {
|
| 907 | return stack_size;
|
| 908 | } |
| 909 | |
| 910 | //
|
| 911 | // Dummy thread
|
| 912 | //
|
| 913 | |
| 914 | class omni_thread_dummy : public omni_thread { |
| 915 | public:
|
| 916 | inline omni_thread_dummy() : omni_thread()
|
| 917 | {
|
| 918 | _dummy = 1;
|
| 919 | _state = STATE_RUNNING; |
| 920 | |
| 921 | if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
| 922 | GetCurrentProcess(), &handle, |
| 923 | 0, FALSE, DUPLICATE_SAME_ACCESS))
|
| 924 | throw omni_thread_fatal(GetLastError());
|
| 925 | |
| 926 | nt_id = GetCurrentThreadId(); |
| 927 | |
| 928 | if (!TlsSetValue(self_tls_index, (LPVOID)this)) |
| 929 | throw omni_thread_fatal(GetLastError());
|
| 930 | } |
| 931 | inline ~omni_thread_dummy()
|
| 932 | {
|
| 933 | if (!TlsSetValue(self_tls_index, (LPVOID)0)) |
| 934 | throw omni_thread_fatal(GetLastError());
|
| 935 | } |
| 936 | }; |
| 937 | |
| 938 | omni_thread* |
| 939 | omni_thread::create_dummy() |
| 940 | {
|
| 941 | if (omni_thread::self())
|
| 942 | throw omni_thread_invalid();
|
| 943 | |
| 944 | return new omni_thread_dummy; |
| 945 | } |
| 946 | |
| 947 | void
|
| 948 | omni_thread::release_dummy() |
| 949 | {
|
| 950 | omni_thread* self = omni_thread::self(); |
| 951 | if (!self || !self->_dummy)
|
| 952 | throw omni_thread_invalid();
|
| 953 | |
| 954 | omni_thread_dummy* dummy = (omni_thread_dummy*)self; |
| 955 | delete dummy;
|
| 956 | } |
| 957 | |
| 958 | |
| 959 | #if defined(__DMC__) && defined(_WINDLL)
|
| 960 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 961 | {
|
| 962 | return TRUE;
|
| 963 | } |
| 964 | #endif
|
| 965 | |
| 966 | |
| 967 | #define INSIDE_THREAD_IMPL_CC
|
| 968 | #include "threaddata.cc" |
| 969 | #undef INSIDE_THREAD_IMPL_CC
|