Mercurial > nand-ecoscentric
annotate packages/kernel/current/src/sync/flag.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21
Merge from eCos master repository on 2000-08-21-22:40:54-BST
| author | jlarmour |
|---|---|
| date | Fri, 25 Aug 2000 17:32:38 +0000 |
| parents | bf00f99aec69 |
| children | e0c0827131d1 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // flag.cxx |
| 0 | 4 // |
| 2 | 5 // Flag class implementation |
| 0 | 6 // |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
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:
2
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:
2
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:
2
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:
2
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:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
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:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
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:
2
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:
2
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:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): hmt |
| 35 // Contributors: hmt | |
| 36 // Date: 1998-02-11 | |
| 37 // Purpose: Flag implementation | |
| 38 // Description: This file contains the implementations of the flag class | |
| 0 | 39 // |
| 40 //####DESCRIPTIONEND#### | |
| 41 // | |
| 42 //========================================================================== | |
| 43 | |
| 44 #include <pkgconf/kernel.h> | |
| 45 | |
| 46 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 47 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 48 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 49 #include <cyg/kernel/instrmnt.h> // instrumentation | |
| 50 | |
| 51 #include <cyg/kernel/thread.inl> // thread inlines | |
| 52 | |
| 53 #include <cyg/kernel/flag.hxx> // our own header | |
| 54 | |
| 55 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 56 #include <cyg/kernel/clock.inl> // clock inlines | |
| 57 | |
| 58 // ------------------------------------------------------------------------- | |
| 59 // Constructor | |
| 60 | |
| 61 Cyg_Flag::Cyg_Flag( Cyg_FlagValue init = 0 ) | |
| 62 { | |
| 63 CYG_REPORT_FUNCTION(); | |
| 64 value = init; | |
| 65 CYG_REPORT_RETURN(); | |
| 66 } | |
| 67 | |
| 68 // ------------------------------------------------------------------------- | |
| 69 // Destructor | |
| 70 | |
| 71 Cyg_Flag::~Cyg_Flag() | |
| 72 { | |
| 73 CYG_REPORT_FUNCTION(); | |
| 74 #if 0 | |
| 75 CYG_ASSERT( queue.empty(), "Deleting flag with threads waiting"); | |
| 76 #endif | |
| 77 // Prevent preemption | |
| 78 Cyg_Scheduler::lock(); | |
| 79 | |
| 80 while ( ! queue.empty() ) { | |
| 81 Cyg_Thread *thread = queue.dequeue(); | |
| 82 thread->set_wake_reason( Cyg_Thread::DESTRUCT ); | |
| 83 thread->wake(); | |
| 84 } | |
| 85 | |
| 86 // Unlock the scheduler and maybe switch threads | |
| 87 Cyg_Scheduler::unlock(); | |
| 88 CYG_REPORT_RETURN(); | |
| 89 } | |
| 90 | |
| 91 // ------------------------------------------------------------------------- | |
| 92 // debugging/assert function | |
| 93 | |
| 94 #ifdef CYGDBG_USE_ASSERTS | |
| 95 cyg_bool | |
| 2 | 96 Cyg_Flag::check_this(cyg_assert_class_zeal zeal) const |
| 0 | 97 { |
| 98 CYG_REPORT_FUNCTION(); | |
| 99 | |
| 100 if ( Cyg_Thread::DESTRUCT == Cyg_Thread::self()->get_wake_reason() ) | |
| 101 // then the whole thing is invalid, and we know it. | |
| 102 // so return OK, since this check should NOT make an error. | |
| 103 return true; | |
| 104 | |
| 105 // check that we have a non-NULL pointer first | |
| 106 if( this == NULL ) { | |
| 107 CYG_REPORT_RETVAL( false ); | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 // there ain't a lot to check here. | |
| 112 CYG_REPORT_RETVAL( true ); | |
| 113 return true; | |
| 114 } | |
| 115 #endif | |
| 116 | |
| 117 // ------------------------------------------------------------------------- | |
| 118 // now the members themselves: | |
| 119 | |
| 120 // clear some bits in the value (all of them by default) by ANDing with the | |
| 121 // argument. This cannot make a wait condition become true, so there's not | |
| 122 // much to it. | |
| 123 void | |
| 124 Cyg_Flag::maskbits( Cyg_FlagValue arg = 0 ) | |
| 125 { | |
| 126 CYG_REPORT_FUNCTION(); | |
| 127 | |
| 128 // Prevent preemption | |
| 129 Cyg_Scheduler::lock(); | |
| 130 | |
| 131 value &= arg; | |
| 132 // no need to wake anyone up; no waiter can become valid in | |
| 133 // consequence of this operation. | |
| 134 | |
| 135 // Unlock scheduler and allow other threads to run | |
| 136 Cyg_Scheduler::unlock(); | |
| 137 CYG_REPORT_RETURN(); | |
| 138 } | |
| 139 | |
| 140 | |
| 141 // ------------------------------------------------------------------------- | |
| 142 // set some bits in the value (all of them by default) and wake up any | |
| 143 // affected waiting threads; we do the decision making here so as to get | |
| 144 // atomicity wrt the other threads waking up - the value might have changed | |
| 145 // by the time they get to run. | |
| 146 | |
| 147 void | |
| 148 Cyg_Flag::setbits( Cyg_FlagValue arg = ~0 ) | |
| 149 { | |
| 150 CYG_REPORT_FUNCTION(); | |
| 151 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 152 | |
| 153 // Prevent preemption | |
| 154 Cyg_Scheduler::lock(); | |
| 155 | |
| 156 // OR in the argument to get a new flag value. | |
| 157 value |= arg; | |
| 158 | |
| 159 // anyone waiting? | |
| 160 if ( !(queue.empty()) ) { | |
| 161 FlagWaitInfo *p; | |
| 162 Cyg_Thread *thread; | |
| 163 Cyg_ThreadQueue holding; | |
| 164 | |
| 165 do { | |
| 166 thread = queue.dequeue(); | |
| 167 p = (FlagWaitInfo *)(thread->get_wait_info()); | |
| 168 | |
| 169 CYG_ASSERT( (p->allmask == 0) != (p->anymask == 0), | |
| 170 "Both masks set" ); | |
| 171 CYG_ASSERT( 0 == p->value_out, "Thread already awoken?" ); | |
| 172 | |
| 173 if ( ((p->allmask != 0) && (p->allmask & value) == p->allmask) || | |
| 174 ((p->anymask & value) != 0 ) ) { | |
| 175 // success! awaken the thread | |
| 176 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 177 thread->wake(); | |
| 178 // return the successful value to it | |
| 179 p->value_out = value; | |
| 180 // do we clear the value; is this the end? | |
| 181 if ( p->do_clear ) { | |
| 182 // we can break here but need to preserve ordering | |
| 183 value = 0; | |
| 184 // so let it cycle the whole queue regardless | |
| 185 } | |
| 186 } | |
| 187 else { | |
| 188 // preserve the entry on the holding queue | |
| 189 holding.enqueue( thread ); | |
| 190 } | |
| 191 } while ( !(queue.empty()) ); | |
| 192 | |
| 193 // Now re-queue the unaffected threads back into the flag queue | |
| 194 while ( !(holding.empty()) ) { | |
| 195 queue.enqueue( holding.dequeue() ); | |
| 196 } | |
| 197 } | |
| 198 // Unlock scheduler and allow other threads to run | |
| 199 Cyg_Scheduler::unlock(); | |
| 200 CYG_REPORT_RETURN(); | |
| 201 } | |
| 202 | |
| 203 // ------------------------------------------------------------------------- | |
| 204 // Wait for a match on our pattern, according to the flags given. | |
| 205 // Return the matching value. | |
| 206 Cyg_FlagValue | |
| 207 Cyg_Flag::wait( Cyg_FlagValue pattern, WaitMode mode ) | |
| 208 { | |
| 209 CYG_REPORT_FUNCTION(); | |
| 210 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 211 CYG_ASSERT( Cyg_Flag::MASK >= mode, "Bad mode" ); | |
| 212 | |
| 213 Cyg_FlagValue result; | |
| 214 | |
| 215 // Prevent preemption so that we compare atomically | |
| 216 Cyg_Scheduler::lock(); | |
| 217 | |
| 218 // try the current value | |
| 219 result = poll( pattern, mode ); | |
| 220 | |
| 221 if ( 0 != result ) { | |
| 222 Cyg_Scheduler::unlock(); | |
| 223 CYG_REPORT_RETVAL( result ); | |
| 224 return result; // all done | |
| 225 } | |
| 226 | |
| 227 // we have to wait until we are awoken | |
| 228 Cyg_Thread *self = Cyg_Thread::self(); | |
| 229 | |
| 230 FlagWaitInfo saveme; | |
| 231 saveme.allmask = (Cyg_Flag::OR & mode) ? 0 : pattern; | |
| 232 saveme.anymask = (Cyg_Flag::OR & mode) ? pattern : 0; | |
| 233 saveme.do_clear = (0 != (Cyg_Flag::CLR & mode)); | |
| 234 | |
| 235 self->set_wait_info( (CYG_ADDRWORD)&saveme ); | |
| 236 | |
| 237 result = true; // just being used as an early-out flag now | |
| 238 // this loop allows us to deal correctly with spurious wakeups | |
| 239 while ( result && (0 == saveme.value_out) ) { | |
| 240 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 241 self->sleep(); | |
| 242 // keep track of myself on the queue of waiting threads | |
| 243 queue.enqueue( self ); | |
| 244 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
245 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
246 Cyg_Scheduler::reschedule(); |
| 0 | 247 |
| 248 CYG_ASSERT( ((CYG_ADDRWORD)&saveme) == | |
| 249 Cyg_Thread::self()->get_wait_info(), | |
| 250 "Wait info lost" ); | |
| 251 | |
| 252 switch( self->get_wake_reason() ) | |
| 253 { | |
| 254 case Cyg_Thread::DESTRUCT: | |
| 255 case Cyg_Thread::BREAK: | |
| 256 result = false; | |
| 257 break; | |
| 258 | |
| 259 case Cyg_Thread::EXIT: | |
| 260 self->exit(); | |
| 261 break; | |
| 262 | |
| 263 default: | |
| 264 break; | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 CYG_ASSERT( (false == result) ^ (0 != saveme.value_out), | |
| 269 "Break out but also good result!" ); | |
| 270 | |
| 271 // Unlock scheduler and allow other threads to run | |
| 272 Cyg_Scheduler::unlock(); | |
| 273 CYG_REPORT_RETVAL( saveme.value_out ); | |
| 274 return saveme.value_out; | |
| 275 } | |
| 276 | |
| 277 // ------------------------------------------------------------------------- | |
| 278 // Wait for a match on our pattern, with a timeout. | |
| 279 // Return the matching value, or zero if timed out. | |
| 280 // (zero cannot match any pattern). | |
| 281 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 282 Cyg_FlagValue | |
| 283 Cyg_Flag::wait( Cyg_FlagValue pattern, WaitMode mode, | |
| 284 cyg_tick_count abs_timeout ) | |
| 285 { | |
| 286 CYG_REPORT_FUNCTION(); | |
| 287 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 288 CYG_ASSERT( Cyg_Flag::MASK >= mode, "Bad mode" ); | |
| 289 | |
| 290 Cyg_FlagValue result; | |
| 291 | |
| 292 // Prevent preemption so that we compare atomically | |
| 293 Cyg_Scheduler::lock(); | |
| 294 | |
| 295 // try the current value | |
| 296 result = poll( pattern, mode ); | |
| 297 | |
| 298 if ( 0 != result ) { | |
| 299 Cyg_Scheduler::unlock(); | |
| 300 CYG_REPORT_RETVAL( result ); | |
| 301 return result; // all done | |
| 302 } | |
| 303 | |
| 304 // we have to wait until we are awoken | |
| 305 Cyg_Thread *self = Cyg_Thread::self(); | |
| 306 | |
| 307 FlagWaitInfo saveme; | |
| 308 saveme.allmask = (Cyg_Flag::OR & mode) ? 0 : pattern; | |
| 309 saveme.anymask = (Cyg_Flag::OR & mode) ? pattern : 0; | |
| 310 saveme.do_clear = (0 != (Cyg_Flag::CLR & mode)); | |
| 311 | |
| 312 self->set_wait_info( (CYG_ADDRWORD)&saveme ); | |
| 313 | |
| 314 // Set the timer _once_ outside the loop. | |
| 315 self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT ); | |
| 316 | |
| 317 // If the timeout was in the past, it will have changed the value | |
| 318 // of wake_reason, so avoid going into the loop. | |
| 319 if( self->get_wake_reason() != Cyg_Thread::NONE ) | |
| 320 result = false; | |
| 321 else result = true; | |
| 322 | |
| 323 // Result is just being used as an early-out flag now. This loop | |
| 324 // allows us to deal correctly with spurious wakeups. | |
| 325 | |
| 326 while ( result && (0 == saveme.value_out) ) { | |
| 327 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 328 self->sleep(); | |
| 329 // keep track of myself on the queue of waiting threads | |
| 330 queue.enqueue( self ); | |
| 331 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
332 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
333 Cyg_Scheduler::reschedule(); |
| 0 | 334 |
| 335 CYG_ASSERT( ((CYG_ADDRWORD)&saveme) == | |
| 336 Cyg_Thread::self()->get_wait_info(), | |
| 337 "Wait info lost" ); | |
| 338 | |
| 339 switch( self->get_wake_reason() ) | |
| 340 { | |
| 341 case Cyg_Thread::TIMEOUT: | |
| 342 result = false; | |
| 343 break; | |
| 344 | |
| 345 case Cyg_Thread::DESTRUCT: | |
| 346 case Cyg_Thread::BREAK: | |
| 347 result = false; | |
| 348 break; | |
| 349 | |
| 350 case Cyg_Thread::EXIT: | |
| 351 self->exit(); | |
| 352 break; | |
| 353 | |
| 354 default: | |
| 355 break; | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 CYG_ASSERT( (false == result) ^ (0 != saveme.value_out), | |
| 360 "Break out but also good result!" ); | |
| 361 | |
| 362 // clear the timer; if it actually fired, no worries. | |
| 363 self->clear_timer(); | |
| 364 | |
| 365 // Unlock scheduler and allow other threads to run | |
| 366 Cyg_Scheduler::unlock(); | |
| 367 // in this version, value_out might be zero meaning timed out. | |
| 368 CYG_REPORT_RETVAL( saveme.value_out ); | |
| 369 return saveme.value_out; | |
| 370 } | |
| 371 #endif // CYGFUN_KERNEL_THREADS_TIMER | |
| 372 | |
| 373 // ------------------------------------------------------------------------- | |
| 374 // Test for a match on our pattern, according to the flags given. | |
| 375 // Return the matching value if success, else zero. | |
| 376 Cyg_FlagValue | |
| 377 Cyg_Flag::poll( Cyg_FlagValue pattern, WaitMode mode ) | |
| 378 { | |
| 379 CYG_REPORT_FUNCTION(); | |
| 380 CYG_ASSERTCLASS( this, "Bad this pointer"); | |
| 381 CYG_ASSERT( Cyg_Flag::MASK >= mode, "Bad mode" ); | |
| 382 | |
| 383 // Prevent preemption so that we compare atomically | |
| 384 Cyg_Scheduler::lock(); | |
| 385 | |
| 386 Cyg_FlagValue result = 0; | |
| 387 | |
| 388 if ( Cyg_Flag::OR & mode ) { | |
| 389 if ( 0 != (value & pattern) ) | |
| 390 result = value; | |
| 391 } | |
| 392 else { // Cyg_Flag::AND - all must be set | |
| 393 if ( (pattern != 0) && (pattern == (value & pattern)) ) | |
| 394 result = value; | |
| 395 } | |
| 396 | |
| 397 // result != 0 <=> test passed | |
| 398 if ( result && (Cyg_Flag::CLR & mode) ) | |
| 399 value = 0; | |
| 400 | |
| 401 Cyg_Scheduler::unlock(); | |
| 402 | |
| 403 CYG_REPORT_RETVAL( result ); | |
| 404 return result; | |
| 405 } | |
| 406 | |
| 407 | |
| 408 // ------------------------------------------------------------------------- | |
| 409 // EOF flag.cxx |
