Mercurial > ecos
annotate packages/kernel/current/src/sync/mutex.cxx @ 28:18ee5a9c102e ecos-sw-1999-08-09
Merge from eCos master repository on 1999-08-09-17:04:48-BST
| author | jlarmour |
|---|---|
| date | Mon, 09 Aug 1999 16:37:19 +0000 |
| parents | 443894e2e912 |
| children | 7a6ac9edc838 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/mutex.cxx |
| 0 | 4 // |
| 2 | 5 // Mutex and condition variable implementation |
| 0 | 6 // |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Cygnus eCos Public License | |
| 12 // Version 1.0 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://sourceware.cygnus.com/ecos | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Cygnus Operating System, released | |
| 22 // September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 2 | 25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
| 0 | 26 // ------------------------------------------- |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //========================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 2 | 32 // Author(s): nickg |
| 33 // Contributors: nickg, jlarmour | |
| 34 // Date: 1999-02-17 | |
| 35 // Purpose: Mutex implementation | |
| 36 // Description: This file contains the implementations of the mutex | |
| 37 // and condition variable classes. | |
| 0 | 38 // |
| 39 //####DESCRIPTIONEND#### | |
| 40 // | |
| 41 //========================================================================== | |
| 42 | |
| 43 #include <pkgconf/kernel.h> | |
| 44 | |
| 45 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 46 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 47 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 48 #include <cyg/kernel/instrmnt.h> // instrumentation | |
| 49 | |
| 50 #include <cyg/kernel/mutex.hxx> // our header | |
| 51 | |
| 52 #include <cyg/kernel/thread.inl> // thread inlines | |
| 53 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 54 #include <cyg/kernel/clock.inl> // clock inlines | |
| 55 | |
| 56 | |
| 57 // ------------------------------------------------------------------------- | |
| 58 // Constructor | |
| 59 | |
| 60 Cyg_Mutex::Cyg_Mutex() | |
| 61 { | |
| 62 CYG_REPORT_FUNCTION(); | |
| 63 | |
| 64 locked = false; | |
| 65 owner = NULL; | |
| 2 | 66 |
| 67 CYG_REPORT_RETURN(); | |
| 0 | 68 } |
| 69 | |
| 70 // ------------------------------------------------------------------------- | |
| 71 // Destructor | |
| 72 | |
| 73 Cyg_Mutex::~Cyg_Mutex() | |
| 74 { | |
| 75 CYG_REPORT_FUNCTION(); | |
| 76 | |
| 77 CYG_ASSERT( owner == NULL, "Deleting mutex with owner"); | |
| 78 CYG_ASSERT( queue.empty(), "Deleting mutex with waiting threads"); | |
| 2 | 79 CYG_REPORT_RETURN(); |
| 0 | 80 } |
| 81 | |
| 82 // ------------------------------------------------------------------------- | |
| 83 | |
| 84 #ifdef CYGDBG_USE_ASSERTS | |
| 85 | |
| 2 | 86 bool |
| 87 Cyg_Mutex::check_this( cyg_assert_class_zeal zeal) const | |
| 0 | 88 { |
| 89 // CYG_REPORT_FUNCTION(); | |
| 90 | |
| 91 // check that we have a non-NULL pointer first | |
| 92 if( this == NULL ) return false; | |
| 93 | |
| 94 switch( zeal ) | |
| 95 { | |
| 96 case cyg_system_test: | |
| 97 case cyg_extreme: | |
| 98 case cyg_thorough: | |
| 99 case cyg_quick: | |
| 100 case cyg_trivial: | |
| 101 if( locked && owner == NULL ) return false; | |
| 102 if( !locked && owner != NULL ) return false; | |
| 103 case cyg_none: | |
| 104 default: | |
| 105 break; | |
| 106 }; | |
| 107 | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 #endif | |
| 112 | |
| 113 // ------------------------------------------------------------------------- | |
| 114 // Lock and/or wait | |
| 115 | |
| 2 | 116 cyg_bool |
| 117 Cyg_Mutex::lock(void) | |
| 0 | 118 { |
| 2 | 119 CYG_REPORT_FUNCTYPE("returning %d"); |
| 0 | 120 |
| 121 cyg_bool result = true; | |
| 122 Cyg_Thread *self = Cyg_Thread::self(); | |
| 123 | |
| 124 // Prevent preemption | |
| 125 Cyg_Scheduler::lock(); | |
| 126 | |
| 127 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 128 | |
| 129 CYG_INSTRUMENT_MUTEX(LOCK, this, 0); | |
| 130 | |
| 131 // Loop while the mutex is locked, sleeping each time around | |
| 132 // the loop. This copes with the possibility of a higher priority | |
| 133 // thread grabbing the mutex between the wakeup in unlock() and | |
| 134 // this thread actually starting. | |
| 135 | |
| 2 | 136 while( locked && result ) |
| 0 | 137 { |
| 138 CYG_ASSERT( self != owner, "Locking mutex I already own"); | |
| 139 | |
| 140 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 141 | |
| 142 self->sleep(); | |
| 143 | |
| 144 queue.enqueue( self ); | |
| 145 | |
| 146 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 147 | |
| 148 owner->inherit_priority(self); | |
|
28
18ee5a9c102e
Merge from eCos master repository on 1999-08-09-17:04:48-BST
jlarmour
parents:
2
diff
changeset
|
149 |
| 0 | 150 #endif |
| 151 | |
| 152 CYG_INSTRUMENT_MUTEX(WAIT, this, 0); | |
| 153 | |
| 154 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, "Called with non-zero scheduler lock"); | |
| 155 | |
| 156 // Unlock scheduler and allow other threads | |
| 157 // to run | |
| 158 Cyg_Scheduler::unlock(); | |
| 159 Cyg_Scheduler::lock(); | |
| 160 | |
| 161 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 162 | |
| 163 switch( self->get_wake_reason() ) | |
| 164 { | |
| 165 case Cyg_Thread::DESTRUCT: | |
| 166 case Cyg_Thread::BREAK: | |
| 167 result = false; | |
| 168 break; | |
| 169 | |
| 170 case Cyg_Thread::EXIT: | |
| 171 self->exit(); | |
| 172 break; | |
| 173 | |
| 174 default: | |
| 175 break; | |
| 176 } | |
| 177 | |
| 178 } | |
| 179 | |
| 180 if( result ) | |
| 181 { | |
| 182 locked = true; | |
| 183 owner = self; | |
| 184 | |
| 185 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 186 | |
| 187 self->count_mutex(); | |
| 188 | |
| 189 #endif | |
| 190 | |
| 191 CYG_INSTRUMENT_MUTEX(LOCKED, this, 0); | |
| 192 } | |
| 193 | |
| 194 // Unlock the scheduler and maybe switch threads | |
| 195 Cyg_Scheduler::unlock(); | |
| 196 | |
| 197 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 198 | |
| 2 | 199 CYG_REPORT_RETVAL(result); |
| 200 | |
| 0 | 201 return result; |
| 202 } | |
| 203 | |
| 204 // ------------------------------------------------------------------------- | |
| 205 // Try to lock and return success | |
| 206 | |
| 2 | 207 cyg_bool |
| 208 Cyg_Mutex::trylock(void) | |
| 0 | 209 { |
| 2 | 210 CYG_REPORT_FUNCTYPE("returning %d"); |
| 0 | 211 |
| 212 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 213 | |
| 214 cyg_bool result = true; | |
| 215 | |
| 216 // Prevent preemption | |
| 217 Cyg_Scheduler::lock(); | |
| 218 | |
| 219 // If the mutex is not locked, grab it | |
| 220 // for ourself. Otherwise return failure. | |
| 221 if( !locked ) | |
| 222 { | |
| 223 Cyg_Thread *self = Cyg_Thread::self(); | |
| 224 | |
| 225 locked = true; | |
| 226 owner = self; | |
| 227 | |
| 228 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 229 | |
| 230 self->count_mutex(); | |
| 231 | |
| 232 #endif | |
| 233 } | |
| 234 else result = false; | |
| 235 | |
| 236 CYG_INSTRUMENT_MUTEX(TRY, this, result); | |
| 237 | |
| 238 // Unlock the scheduler and maybe switch threads | |
| 239 Cyg_Scheduler::unlock(); | |
| 240 | |
| 2 | 241 CYG_REPORT_RETVAL(result); |
| 0 | 242 return result; |
| 243 } | |
| 244 | |
| 245 // ------------------------------------------------------------------------- | |
| 246 // unlock | |
| 247 | |
| 2 | 248 void |
| 249 Cyg_Mutex::unlock(void) | |
| 0 | 250 { |
| 251 CYG_REPORT_FUNCTION(); | |
| 252 | |
| 253 // Prevent preemption | |
| 254 Cyg_Scheduler::lock(); | |
| 255 | |
| 256 CYG_INSTRUMENT_MUTEX(UNLOCK, this, 0); | |
| 257 | |
| 258 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 259 CYG_ASSERT( locked, "Unlock mutex that is not locked"); | |
| 260 CYG_ASSERT( owner == Cyg_Thread::self(), "Unlock mutex I do not own"); | |
| 261 | |
| 262 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 263 | |
| 264 owner->uncount_mutex(); | |
| 265 owner->disinherit_priority(); | |
| 266 | |
| 267 #endif | |
| 268 | |
| 269 locked = false; | |
| 270 owner = NULL; | |
| 271 | |
| 272 if( !queue.empty() ) { | |
| 273 | |
| 274 // The queue is non-empty, so grab the next | |
| 275 // thread from it and wake it up. | |
| 276 | |
| 277 Cyg_Thread *thread = queue.dequeue(); | |
| 278 | |
| 279 CYG_ASSERTCLASS( thread, "Bad thread pointer"); | |
| 280 | |
| 281 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 282 | |
| 283 thread->wake(); | |
| 284 | |
| 285 CYG_INSTRUMENT_MUTEX(WAKE, this, thread); | |
| 286 | |
| 287 } | |
| 288 | |
| 289 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 290 | |
| 291 // Unlock the scheduler and maybe switch threads | |
| 292 Cyg_Scheduler::unlock(); | |
| 293 | |
| 2 | 294 CYG_REPORT_RETURN(); |
| 295 } | |
| 296 | |
| 297 // ------------------------------------------------------------------------- | |
| 298 // Release all waiting threads. | |
| 299 | |
| 300 void Cyg_Mutex::release() | |
| 301 { | |
| 302 CYG_REPORT_FUNCTION(); | |
| 303 | |
| 304 // Prevent preemption | |
| 305 Cyg_Scheduler::lock(); | |
| 306 | |
| 307 CYG_INSTRUMENT_MUTEX(RELEASE, this, 0); | |
| 308 | |
| 309 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 310 | |
| 311 while( !queue.empty() ) | |
| 312 { | |
| 313 // The queue is non-empty, so grab each | |
| 314 // thread from it and release it. | |
| 315 | |
| 316 Cyg_Thread *thread = queue.dequeue(); | |
| 317 | |
| 318 CYG_ASSERTCLASS( thread, "Bad thread pointer"); | |
| 319 | |
| 320 thread->release(); | |
| 321 | |
| 322 CYG_INSTRUMENT_MUTEX(RELEASED, this, thread); | |
| 323 | |
| 324 } | |
| 325 | |
| 326 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 327 | |
| 328 // Unlock the scheduler and maybe switch threads | |
| 329 Cyg_Scheduler::unlock(); | |
| 330 | |
| 331 CYG_REPORT_RETURN(); | |
| 0 | 332 } |
| 333 | |
| 334 //========================================================================== | |
| 335 // Condition variables | |
| 336 | |
| 337 Cyg_Condition_Variable::Cyg_Condition_Variable( | |
| 338 Cyg_Mutex &mx // linked mutex | |
| 339 ) | |
| 340 { | |
| 341 CYG_REPORT_FUNCTION(); | |
| 342 | |
| 343 mutex = &mx; | |
| 344 | |
| 345 CYG_ASSERTCLASS( mutex, "Invalid mutex argument"); | |
| 2 | 346 |
| 347 CYG_REPORT_RETURN(); | |
| 0 | 348 } |
| 349 | |
| 350 // ------------------------------------------------------------------------- | |
| 351 // Destructor | |
| 352 | |
| 353 Cyg_Condition_Variable::~Cyg_Condition_Variable() | |
| 354 { | |
| 355 CYG_REPORT_FUNCTION(); | |
| 356 | |
| 357 CYG_ASSERT( queue.empty(), "Deleting condvar with waiting threads"); | |
| 2 | 358 |
| 359 CYG_REPORT_RETURN(); | |
| 0 | 360 } |
| 361 | |
| 362 // ------------------------------------------------------------------------- | |
| 363 | |
| 364 #ifdef CYGDBG_USE_ASSERTS | |
| 365 | |
| 2 | 366 bool |
| 367 Cyg_Condition_Variable::check_this( cyg_assert_class_zeal zeal) const | |
| 0 | 368 { |
| 2 | 369 bool result = true; |
| 370 | |
| 371 CYG_REPORT_FUNCTYPE("returning %d"); | |
| 372 CYG_REPORT_FUNCARG1("zeal = %d", zeal); | |
| 0 | 373 |
| 374 // check that we have a non-NULL pointer first | |
| 2 | 375 if( this == NULL ) |
| 376 result = false; | |
| 377 else { | |
| 378 | |
| 379 switch( zeal ) | |
| 380 { | |
| 381 case cyg_system_test: | |
| 382 case cyg_extreme: | |
| 383 case cyg_thorough: | |
| 384 if( !mutex->check_this(zeal) ) | |
| 385 result = false; | |
| 386 case cyg_quick: | |
| 387 case cyg_trivial: | |
| 388 case cyg_none: | |
| 389 default: | |
| 390 break; | |
| 391 } | |
| 392 } | |
| 0 | 393 |
| 2 | 394 CYG_REPORT_RETVAL(result); |
| 395 return result; | |
| 0 | 396 } |
| 397 | |
| 398 #endif | |
| 399 | |
| 400 // ------------------------------------------------------------------------- | |
| 401 // Wait for condition to be true | |
| 2 | 402 // Note: if this function is entered with the scheduler locked (e.g. to |
| 403 // suspend DSR processing) then there is no need to take the lock. Also | |
| 404 // in this case, exit with the scheduler locked, which allows this function | |
| 405 // to be used in a totally thread-safe manner. | |
| 0 | 406 |
| 2 | 407 void |
| 408 Cyg_Condition_Variable::wait(void) | |
| 0 | 409 { |
| 410 CYG_REPORT_FUNCTION(); | |
| 411 | |
| 412 Cyg_Thread *self = Cyg_Thread::self(); | |
| 413 | |
| 2 | 414 cyg_int32 current_lock = Cyg_Scheduler::get_sched_lock(); |
| 415 | |
| 416 if (current_lock == 0) | |
| 417 // Prevent preemption | |
| 418 Cyg_Scheduler::lock(); | |
| 0 | 419 |
| 420 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 421 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 422 CYG_ASSERTCLASS( self, "Bad self thread"); | |
| 423 | |
| 424 CYG_INSTRUMENT_CONDVAR(WAIT, this, 0); | |
| 425 | |
| 426 mutex->unlock(); | |
| 427 | |
| 428 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 429 | |
| 430 self->sleep(); | |
| 431 | |
| 432 queue.enqueue( self ); | |
| 433 | |
| 434 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, "Called with non-zero scheduler lock"); | |
| 435 | |
| 436 // Unlock the scheduler and switch threads | |
| 437 Cyg_Scheduler::unlock(); | |
| 438 | |
| 439 CYG_INSTRUMENT_CONDVAR(WOKE, this, self->get_wake_reason()); | |
| 440 | |
| 441 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 442 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 443 | |
| 444 switch( self->get_wake_reason() ) | |
| 445 { | |
| 446 case Cyg_Thread::EXIT: | |
| 447 self->exit(); | |
| 448 break; | |
| 449 | |
| 450 default: | |
| 451 break; | |
| 452 } | |
| 453 | |
| 454 | |
| 455 // When we awake, we must re-acquire the mutex. Note that while | |
| 456 // it is essential to release the mutex and queue on the CV | |
| 457 // atomically relative to other threads, to avoid races, it is not | |
| 458 // necessary for us to re-acquire the mutex in the same atomic | |
| 459 // action. Hence we can do it after unlocking the scheduler. | |
| 2 | 460 // We need to loop here in case the thread is released while waiting |
| 461 // for the mutex. It is essential that we exit this function with the | |
| 462 // mutex claimed. | |
| 463 | |
| 464 while ( !mutex->lock() ) | |
| 465 continue; | |
| 0 | 466 |
| 467 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 468 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 469 CYG_ASSERT( mutex->owner == self, "Not mutex owner"); | |
| 2 | 470 |
| 471 CYG_REPORT_RETURN(); | |
| 472 | |
| 473 if (current_lock) | |
| 474 // Reacquire the DSR pseudo lock | |
| 475 Cyg_Scheduler::lock(); | |
| 0 | 476 } |
| 477 | |
| 478 // ------------------------------------------------------------------------- | |
| 479 // Wake one thread | |
| 480 | |
| 2 | 481 void |
| 482 Cyg_Condition_Variable::signal(void) | |
| 0 | 483 { |
| 484 CYG_REPORT_FUNCTION(); | |
| 485 | |
| 486 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 487 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 488 | |
| 489 // Prevent preemption | |
| 490 Cyg_Scheduler::lock(); | |
| 491 | |
| 492 CYG_INSTRUMENT_CONDVAR(SIGNAL, this, 0); | |
| 493 | |
| 494 if( !queue.empty() ) | |
| 495 { | |
| 496 // The queue is non-empty, so grab the next | |
| 497 // thread from it and wake it up. | |
| 498 | |
| 499 Cyg_Thread *thread = queue.dequeue(); | |
| 500 | |
| 501 CYG_ASSERTCLASS( thread, "Bad thread pointer"); | |
| 502 | |
| 503 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 504 | |
| 505 thread->wake(); | |
| 506 | |
| 507 CYG_INSTRUMENT_CONDVAR(WAKE, this, thread); | |
| 508 | |
| 509 } | |
| 510 | |
| 511 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 512 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 513 | |
| 514 // Unlock the scheduler and maybe switch threads | |
| 515 Cyg_Scheduler::unlock(); | |
| 516 | |
| 2 | 517 CYG_REPORT_RETURN(); |
| 0 | 518 } |
| 519 | |
| 520 // ------------------------------------------------------------------------- | |
| 521 // Set cond true, wake all threads | |
| 522 | |
| 2 | 523 void |
| 524 Cyg_Condition_Variable::broadcast(void) | |
| 0 | 525 { |
| 526 CYG_REPORT_FUNCTION(); | |
| 527 | |
| 528 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 529 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 530 | |
| 531 // Prevent preemption | |
| 532 Cyg_Scheduler::lock(); | |
| 533 | |
| 534 CYG_INSTRUMENT_CONDVAR(BROADCAST, this, 0); | |
| 535 | |
| 536 // Grab all the threads from the queue and let them | |
| 537 // go. | |
| 538 | |
| 539 while( !queue.empty() ) | |
| 540 { | |
| 541 Cyg_Thread *thread = queue.dequeue(); | |
| 542 | |
| 543 CYG_ASSERTCLASS( thread, "Bad thread pointer"); | |
| 544 | |
| 545 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 546 | |
| 547 thread->wake(); | |
| 548 | |
| 549 CYG_INSTRUMENT_CONDVAR(WAKE, this, thread); | |
| 550 } | |
| 551 | |
| 552 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 553 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 554 | |
| 555 // Unlock the scheduler and maybe switch threads | |
| 556 Cyg_Scheduler::unlock(); | |
| 2 | 557 |
| 558 CYG_REPORT_RETURN(); | |
| 0 | 559 } |
| 560 | |
| 561 // ------------------------------------------------------------------------- | |
| 562 // Optional timed wait on a CV | |
| 563 | |
| 2 | 564 #if defined(CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT) |
| 0 | 565 |
| 2 | 566 cyg_bool |
| 567 Cyg_Condition_Variable::wait( cyg_tick_count timeout ) | |
| 0 | 568 { |
| 2 | 569 CYG_REPORT_FUNCTYPE("returning %d"); |
| 570 CYG_REPORT_FUNCARG1("timeout = %d", timeout); | |
| 0 | 571 |
| 572 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 573 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 574 | |
| 575 cyg_bool result = true; | |
| 576 | |
| 577 Cyg_Thread *self = Cyg_Thread::self(); | |
| 578 | |
| 579 CYG_ASSERTCLASS( self, "Bad self thread"); | |
| 580 | |
| 581 // Prevent preemption | |
| 582 Cyg_Scheduler::lock(); | |
| 583 | |
| 584 CYG_INSTRUMENT_CONDVAR(TIMED_WAIT, this, 0 ); | |
| 585 | |
| 586 mutex->unlock(); | |
| 587 | |
| 588 // The ordering of sleep() and set_timer() here are | |
| 589 // important. If the timeout is in the past, the thread | |
| 590 // will be woken up immediately and will not sleep. | |
| 591 | |
| 592 self->sleep(); | |
| 593 | |
| 594 // Set the timer and sleep reason | |
| 595 self->set_timer( timeout, Cyg_Thread::TIMEOUT ); | |
| 596 | |
| 597 // Only enqueue if the timeout has not already fired. | |
| 598 if( self->get_wake_reason() == Cyg_Thread::NONE ) | |
| 599 queue.enqueue( self ); | |
| 600 | |
| 601 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, "Called with non-zero scheduler lock"); | |
| 602 | |
| 603 // Unlock the scheduler and switch threads | |
| 604 Cyg_Scheduler::unlock(); | |
| 605 | |
| 606 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 607 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 608 | |
| 609 self->clear_timer(); | |
| 610 | |
| 611 CYG_INSTRUMENT_CONDVAR(WOKE, this, self->get_wake_reason()); | |
| 612 | |
| 613 switch( self->get_wake_reason() ) | |
| 614 { | |
| 615 case Cyg_Thread::TIMEOUT: | |
| 616 case Cyg_Thread::DESTRUCT: // which, the cv or the mutex? | |
| 617 case Cyg_Thread::BREAK: | |
| 618 result = false; | |
| 619 break; | |
| 620 | |
| 621 case Cyg_Thread::EXIT: | |
| 622 self->exit(); | |
| 623 break; | |
| 624 | |
| 625 default: | |
| 626 break; | |
| 627 } | |
| 628 | |
| 629 | |
| 630 // When we awake, we must re-acquire the mutex. Note that while | |
| 631 // it is essential to release the mutex and queue on the CV | |
| 632 // atomically relative to other threads, to avoid races, it is not | |
| 633 // necessary for us to re-acquire the mutex in the same atomic | |
| 634 // action. Hence we can do it after unlocking the scheduler. | |
| 635 | |
| 2 | 636 // FIXME: what if we woke up above due to TIMEOUT/DESTRUCT/BREAK? |
| 637 // In that situation is it correct to not lock the mutex? | |
| 638 if (false != result) | |
| 639 result = mutex->lock(); | |
| 0 | 640 |
| 641 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 642 CYG_ASSERTCLASS( mutex, "Corrupt mutex"); | |
| 2 | 643 |
| 644 CYG_REPORT_RETVAL(result); | |
| 0 | 645 |
| 646 return result; | |
| 647 } | |
| 648 | |
| 649 #endif | |
| 650 | |
| 651 | |
| 652 // ------------------------------------------------------------------------- | |
| 653 // EOF sync/mutex.cxx |
