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