Mercurial > flash_v2
annotate packages/kernel/current/src/sync/cnt_sem2.cxx @ 124:0ec04793409a ecos-sw-2000-09-11
Merge from eCos master repository on 2000-09-11-03:00:13-BST
| author | jlarmour |
|---|---|
| date | Mon, 11 Sep 2000 02:42:46 +0000 |
| parents | 6ed91473a1cd |
| children | e0c0827131d1 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/cnt_sem2.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 |
|
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): 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/sema2.hxx> // our header | |
| 55 | |
| 56 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 57 | |
| 58 // ------------------------------------------------------------------------- | |
| 59 // Constructor | |
| 60 | |
| 61 Cyg_Counting_Semaphore2::Cyg_Counting_Semaphore2( | |
| 62 cyg_count32 init_count // Initial count value | |
| 63 ) | |
| 64 { | |
| 65 count = init_count; | |
| 66 } | |
| 67 | |
| 68 // ------------------------------------------------------------------------- | |
| 69 // Destructor | |
| 70 | |
| 71 Cyg_Counting_Semaphore2::~Cyg_Counting_Semaphore2() | |
| 72 { | |
| 73 CYG_REPORT_FUNCTION(); | |
| 74 #if 0 | |
| 75 CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads"); | |
| 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 // Wait until the count can be decremented without it becoming | |
| 93 // negative. | |
| 94 | |
| 95 cyg_bool Cyg_Counting_Semaphore2::wait() | |
| 96 { | |
| 97 CYG_REPORT_FUNCTION(); | |
| 98 Cyg_Thread *self = Cyg_Thread::self(); | |
| 99 cyg_bool result = true; | |
| 100 | |
| 101 // Prevent preemption | |
| 102 Cyg_Scheduler::lock(); | |
| 103 | |
| 104 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 105 | |
| 106 if ( 0 < count ) { | |
| 107 count--; | |
| 108 Cyg_Scheduler::unlock(); | |
| 109 } | |
| 110 else { | |
| 111 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 112 self->sleep(); | |
| 113 queue.enqueue( self ); | |
| 114 | |
| 115 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 116 | |
| 117 Cyg_Scheduler::unlock(); | |
| 118 | |
| 119 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 120 | |
| 121 switch( self->get_wake_reason() ) | |
| 122 { | |
| 123 case Cyg_Thread::DESTRUCT: | |
| 124 case Cyg_Thread::BREAK: | |
| 125 result = false; | |
| 126 break; | |
| 127 | |
| 128 case Cyg_Thread::EXIT: | |
| 129 self->exit(); | |
| 130 break; | |
| 131 | |
| 132 default: | |
| 133 break; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 CYG_REPORT_RETVAL( result ); | |
| 138 return result; | |
| 139 } | |
| 140 | |
| 141 // ------------------------------------------------------------------------- | |
| 142 // Wait until the count can be decremented without it becoming | |
| 143 // negative. | |
| 144 | |
| 145 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 146 | |
| 147 cyg_bool | |
| 148 Cyg_Counting_Semaphore2::wait( cyg_tick_count abs_timeout ) | |
| 149 { | |
| 150 CYG_REPORT_FUNCTION(); | |
| 151 Cyg_Thread *self = Cyg_Thread::self(); | |
| 152 cyg_bool result = true; | |
| 153 | |
| 154 // Prevent preemption | |
| 155 Cyg_Scheduler::lock(); | |
| 156 | |
| 157 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 158 | |
| 159 if ( 0 < count ) { | |
| 160 count--; | |
| 161 Cyg_Scheduler::unlock(); | |
| 162 } | |
| 163 else { | |
| 164 | |
| 165 // Put thread in sleep state before setting timer since if the | |
| 166 // timeout is in the past, it will be re-awoken | |
| 167 // immediately. If this happens then wake_reason will not be | |
| 168 // NONE. | |
| 169 | |
| 170 self->sleep(); | |
| 171 | |
| 172 self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT ); | |
| 173 | |
| 174 // only enqueue if the timeout did not already happen | |
| 175 if( Cyg_Thread::NONE == self->get_wake_reason() ) | |
| 176 queue.enqueue( self ); | |
| 177 | |
| 178 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 179 | |
| 180 | |
| 181 Cyg_Scheduler::unlock(); | |
| 182 | |
| 183 // Clear the timeout. It is irrelevant whether the alarm has | |
| 184 // actually gone off or not. | |
| 185 self->clear_timer(); | |
| 186 | |
| 187 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 188 | |
| 189 switch( self->get_wake_reason() ) | |
| 190 { | |
| 191 case Cyg_Thread::TIMEOUT: | |
| 192 result = false; | |
| 193 CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count); | |
| 194 break; | |
| 195 | |
| 196 case Cyg_Thread::DESTRUCT: | |
| 197 case Cyg_Thread::BREAK: | |
| 198 result = false; | |
| 199 break; | |
| 200 | |
| 201 case Cyg_Thread::EXIT: | |
| 202 self->exit(); | |
| 203 break; | |
| 204 | |
| 205 default: | |
| 206 break; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 CYG_REPORT_RETVAL( result ); | |
| 211 return result; | |
| 212 } | |
| 213 | |
| 214 #endif // CYGFUN_KERNEL_THREADS_TIMER | |
| 215 | |
| 216 // ------------------------------------------------------------------------- | |
| 217 // Try to decrement, but fail if not possible | |
| 218 | |
| 219 cyg_bool Cyg_Counting_Semaphore2::trywait() | |
| 220 { | |
| 221 CYG_REPORT_FUNCTION(); | |
| 222 cyg_bool result = true; | |
| 223 | |
| 224 // Prevent preemption | |
| 225 Cyg_Scheduler::lock(); | |
| 226 | |
| 227 if( 0 < count ) count--; | |
| 228 else result = false; | |
| 229 | |
| 230 CYG_INSTRUMENT_CNTSEM( TRY, this, result ); | |
| 231 | |
| 232 // Unlock the scheduler and maybe switch threads | |
| 233 Cyg_Scheduler::unlock(); | |
| 234 | |
| 235 CYG_REPORT_RETVAL( result ); | |
| 236 return result; | |
| 237 } | |
| 238 | |
| 239 // ------------------------------------------------------------------------- | |
| 240 // Increment count | |
| 241 | |
| 242 void Cyg_Counting_Semaphore2::post() | |
| 243 { | |
| 244 CYG_REPORT_FUNCTION(); | |
| 245 // Prevent preemption | |
| 246 Cyg_Scheduler::lock(); | |
| 247 | |
| 248 CYG_INSTRUMENT_CNTSEM( POST, this, 0 ); | |
| 249 | |
| 250 if( queue.empty() ) { | |
| 251 count++; | |
| 252 } | |
| 253 else { | |
| 254 // The queue is non-empty, so grab the next | |
| 255 // thread from it and wake it up. The waiter | |
| 256 // won't decrement the count when he is awakened, | |
| 257 // for we never incremented it in the first place | |
| 258 | |
| 259 Cyg_Thread *thread = queue.dequeue(); | |
| 260 | |
| 261 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 262 | |
| 263 thread->wake(); | |
| 264 | |
| 265 CYG_INSTRUMENT_CNTSEM( WAKE, this, thread ); | |
| 266 } | |
| 267 | |
| 268 // Unlock the scheduler and maybe switch threads | |
| 269 Cyg_Scheduler::unlock(); | |
| 270 CYG_REPORT_RETURN(); | |
| 271 } | |
| 272 | |
| 273 // ------------------------------------------------------------------------- | |
| 274 // Get current count value | |
| 275 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
276 cyg_count32 Cyg_Counting_Semaphore2::peek() const |
| 0 | 277 { |
| 278 // This is a single read of the value of count. | |
| 279 // This is already atomic, hence there is no need | |
| 280 // to lock the scheduler. | |
| 281 | |
| 282 return count; | |
| 283 } | |
| 284 | |
| 285 // ------------------------------------------------------------------------- | |
| 286 // EOF sync/cnt_sem2.cxx |
