Mercurial > flash_v2
annotate packages/kernel/current/src/sync/cnt_sem.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/cnt_sem.cxx |
| 0 | 4 // |
| 2 | 5 // Counting semaphore 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 |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
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 |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
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): nickg |
| 35 // Contributors: nickg | |
| 36 // Date: 1997-09-24 | |
| 37 // Purpose: Cyg_Counting_Semaphore implementation | |
| 38 // Description: This file contains the implementations of the counting semaphore | |
| 0 | 39 // class. |
| 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/thread.inl> // Cyg_Thread inlines | |
| 53 | |
| 54 #include <cyg/kernel/sema.hxx> // our header | |
| 55 | |
| 56 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 57 | |
| 58 // ------------------------------------------------------------------------- | |
| 59 // Constructor | |
| 60 | |
| 61 Cyg_Counting_Semaphore::Cyg_Counting_Semaphore( | |
| 62 cyg_count32 init_count // Initial count value | |
| 63 ) | |
| 64 { | |
| 65 count = init_count; | |
| 66 } | |
| 67 | |
| 68 // ------------------------------------------------------------------------- | |
| 69 // Destructor | |
| 70 | |
| 71 Cyg_Counting_Semaphore::~Cyg_Counting_Semaphore() | |
| 72 { | |
| 73 CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads"); | |
| 74 } | |
| 75 | |
| 76 // ------------------------------------------------------------------------- | |
| 77 // Wait until the count can be decremented without it becoming | |
| 78 // negative. | |
| 79 | |
| 80 cyg_bool Cyg_Counting_Semaphore::wait() | |
| 81 { | |
| 82 cyg_bool result = true; | |
| 83 Cyg_Thread *self = Cyg_Thread::self(); | |
| 84 | |
| 85 // Prevent preemption | |
| 86 Cyg_Scheduler::lock(); | |
| 87 | |
| 88 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 89 | |
| 90 while( count == 0 && result ) | |
| 91 { | |
| 92 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 93 | |
| 94 self->sleep(); | |
| 95 | |
| 96 queue.enqueue( self ); | |
| 97 | |
| 98 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 99 | |
| 100 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, "Called with non-zero scheduler lock"); | |
| 101 | |
| 102 Cyg_Scheduler::unlock(); | |
| 103 Cyg_Scheduler::lock(); | |
| 104 | |
| 105 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 106 | |
| 107 switch( self->get_wake_reason() ) | |
| 108 { | |
| 109 case Cyg_Thread::DESTRUCT: | |
| 110 case Cyg_Thread::BREAK: | |
| 111 result = false; | |
| 112 break; | |
| 113 | |
| 114 case Cyg_Thread::EXIT: | |
| 115 self->exit(); | |
| 116 break; | |
| 117 | |
| 118 default: | |
| 119 break; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 if( result ) count--; | |
| 124 | |
| 125 // Unlock the scheduler | |
| 126 Cyg_Scheduler::unlock(); | |
| 127 | |
| 128 return result; | |
| 129 } | |
| 130 | |
| 131 // ------------------------------------------------------------------------- | |
| 132 // Wait until the count can be decremented without it becoming | |
| 133 // negative. | |
| 134 | |
| 135 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 136 | |
| 137 cyg_bool | |
| 138 Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ) | |
| 139 { | |
| 140 cyg_bool result = true; | |
| 141 Cyg_Thread *self = Cyg_Thread::self(); | |
| 142 | |
| 143 // Prevent preemption | |
| 144 Cyg_Scheduler::lock(); | |
| 145 | |
| 146 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 147 | |
| 148 // Set the timer _once_ outside the loop. | |
| 149 self->set_timer( timeout, Cyg_Thread::TIMEOUT ); | |
| 150 | |
| 151 // If the timeout is in the past, the wake reason will have been | |
| 152 // set to something other than NONE already. Set the result false | |
| 153 // to force an immediate return. | |
| 154 | |
| 155 if( self->get_wake_reason() != Cyg_Thread::NONE ) | |
| 156 result = false; | |
| 157 | |
| 158 while ( 0 == count && result ) { | |
| 159 | |
| 160 // must reset the sleep reason every time | |
| 161 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 162 | |
| 163 self->sleep(); | |
| 164 | |
| 165 queue.enqueue( self ); | |
| 166 | |
| 167 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 168 | |
| 169 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, | |
| 170 "Called with non-zero scheduler lock"); | |
| 171 | |
| 172 Cyg_Scheduler::unlock(); | |
| 173 Cyg_Scheduler::lock(); | |
| 174 | |
| 175 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 176 | |
| 177 switch( self->get_wake_reason() ) | |
| 178 { | |
| 179 case Cyg_Thread::TIMEOUT: | |
| 180 result = false; | |
| 181 CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count); | |
| 182 break; | |
| 183 | |
| 184 case Cyg_Thread::DESTRUCT: | |
| 185 case Cyg_Thread::BREAK: | |
| 186 result = false; | |
| 187 break; | |
| 188 | |
| 189 case Cyg_Thread::EXIT: | |
| 190 self->exit(); | |
| 191 break; | |
| 192 | |
| 193 default: | |
| 194 break; | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 // Clear the timeout. It is irrelevant whether the alarm has | |
| 199 // actually gone off or not. | |
| 200 self->clear_timer(); | |
| 201 | |
| 202 if ( result ) count--; | |
| 203 | |
| 204 // Unlock the scheduler and maybe switch threads | |
| 205 Cyg_Scheduler::unlock(); | |
| 206 | |
| 207 return result; | |
| 208 } | |
| 209 | |
| 210 #endif // CYGFUN_KERNEL_THREADS_TIMER | |
| 211 | |
| 212 // ------------------------------------------------------------------------- | |
| 213 // Try to decrement, but fail if not possible | |
| 214 | |
| 215 cyg_bool Cyg_Counting_Semaphore::trywait() | |
| 216 { | |
| 217 cyg_bool result = true; | |
| 218 | |
| 219 // Prevent preemption | |
| 220 Cyg_Scheduler::lock(); | |
| 221 | |
| 222 if( count > 0 ) count--; | |
| 223 else result = false; | |
| 224 | |
| 225 CYG_INSTRUMENT_CNTSEM( TRY, this, result ); | |
| 226 | |
| 227 // Unlock the scheduler and maybe switch threads | |
| 228 Cyg_Scheduler::unlock(); | |
| 229 | |
| 230 return result; | |
| 231 } | |
| 232 | |
| 233 // ------------------------------------------------------------------------- | |
| 234 // Increment count | |
| 235 | |
| 236 void Cyg_Counting_Semaphore::post() | |
| 237 { | |
| 238 // Prevent preemption | |
| 239 Cyg_Scheduler::lock(); | |
| 240 | |
| 241 CYG_INSTRUMENT_CNTSEM( POST, this, 0 ); | |
| 242 | |
| 243 count++; | |
| 244 | |
| 245 if( !queue.empty() ) { | |
| 246 | |
| 247 // The queue is non-empty, so grab the next | |
| 248 // thread from it and wake it up. The waiter | |
| 249 // will decrement the count when he is awakened. | |
| 250 | |
| 251 Cyg_Thread *thread = queue.dequeue(); | |
| 252 | |
| 253 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 254 | |
| 255 thread->wake(); | |
| 256 | |
| 257 CYG_INSTRUMENT_CNTSEM( WAKE, this, thread ); | |
| 258 } | |
| 259 | |
| 260 // Unlock the scheduler and maybe switch threads | |
| 261 Cyg_Scheduler::unlock(); | |
| 262 } | |
| 263 | |
| 264 // ------------------------------------------------------------------------- | |
| 265 // Get current count value | |
| 266 | |
| 267 cyg_count32 Cyg_Counting_Semaphore::peek() | |
| 268 { | |
| 269 // This is a single read of the value of count. | |
| 270 // This is already atomic, hence there is no need | |
| 271 // to lock the scheduler. | |
| 272 | |
| 273 return count; | |
| 274 } | |
| 275 | |
| 276 // ------------------------------------------------------------------------- | |
| 277 // EOF sync/cnt_sem.cxx |
