Mercurial > ecos
annotate packages/kernel/current/src/sync/cnt_sem.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 // 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 |
|
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/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 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
100 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
101 Cyg_Scheduler::reschedule(); |
| 0 | 102 |
| 103 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 104 | |
| 105 switch( self->get_wake_reason() ) | |
| 106 { | |
| 107 case Cyg_Thread::DESTRUCT: | |
| 108 case Cyg_Thread::BREAK: | |
| 109 result = false; | |
| 110 break; | |
| 111 | |
| 112 case Cyg_Thread::EXIT: | |
| 113 self->exit(); | |
| 114 break; | |
| 115 | |
| 116 default: | |
| 117 break; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 if( result ) count--; | |
| 122 | |
| 123 // Unlock the scheduler | |
| 124 Cyg_Scheduler::unlock(); | |
| 125 | |
| 126 return result; | |
| 127 } | |
| 128 | |
| 129 // ------------------------------------------------------------------------- | |
| 130 // Wait until the count can be decremented without it becoming | |
| 131 // negative. | |
| 132 | |
| 133 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 134 | |
| 135 cyg_bool | |
| 136 Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ) | |
| 137 { | |
| 138 cyg_bool result = true; | |
| 139 Cyg_Thread *self = Cyg_Thread::self(); | |
| 140 | |
| 141 // Prevent preemption | |
| 142 Cyg_Scheduler::lock(); | |
| 143 | |
| 144 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 145 | |
| 146 // Set the timer _once_ outside the loop. | |
| 147 self->set_timer( timeout, Cyg_Thread::TIMEOUT ); | |
| 148 | |
| 149 // If the timeout is in the past, the wake reason will have been | |
| 150 // set to something other than NONE already. Set the result false | |
| 151 // to force an immediate return. | |
| 152 | |
| 153 if( self->get_wake_reason() != Cyg_Thread::NONE ) | |
| 154 result = false; | |
| 155 | |
| 156 while ( 0 == count && result ) { | |
| 157 | |
| 158 // must reset the sleep reason every time | |
| 159 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 160 | |
| 161 self->sleep(); | |
| 162 | |
| 163 queue.enqueue( self ); | |
| 164 | |
| 165 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 166 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
167 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
168 Cyg_Scheduler::reschedule(); |
| 0 | 169 |
| 170 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 171 | |
| 172 switch( self->get_wake_reason() ) | |
| 173 { | |
| 174 case Cyg_Thread::TIMEOUT: | |
| 175 result = false; | |
| 176 CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count); | |
| 177 break; | |
| 178 | |
| 179 case Cyg_Thread::DESTRUCT: | |
| 180 case Cyg_Thread::BREAK: | |
| 181 result = false; | |
| 182 break; | |
| 183 | |
| 184 case Cyg_Thread::EXIT: | |
| 185 self->exit(); | |
| 186 break; | |
| 187 | |
| 188 default: | |
| 189 break; | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 // Clear the timeout. It is irrelevant whether the alarm has | |
| 194 // actually gone off or not. | |
| 195 self->clear_timer(); | |
| 196 | |
| 197 if ( result ) count--; | |
| 198 | |
| 199 // Unlock the scheduler and maybe switch threads | |
| 200 Cyg_Scheduler::unlock(); | |
| 201 | |
| 202 return result; | |
| 203 } | |
| 204 | |
| 205 #endif // CYGFUN_KERNEL_THREADS_TIMER | |
| 206 | |
| 207 // ------------------------------------------------------------------------- | |
| 208 // Try to decrement, but fail if not possible | |
| 209 | |
| 210 cyg_bool Cyg_Counting_Semaphore::trywait() | |
| 211 { | |
| 212 cyg_bool result = true; | |
| 213 | |
| 214 // Prevent preemption | |
| 215 Cyg_Scheduler::lock(); | |
| 216 | |
| 217 if( count > 0 ) count--; | |
| 218 else result = false; | |
| 219 | |
| 220 CYG_INSTRUMENT_CNTSEM( TRY, this, result ); | |
| 221 | |
| 222 // Unlock the scheduler and maybe switch threads | |
| 223 Cyg_Scheduler::unlock(); | |
| 224 | |
| 225 return result; | |
| 226 } | |
| 227 | |
| 228 // ------------------------------------------------------------------------- | |
| 229 // Increment count | |
| 230 | |
| 231 void Cyg_Counting_Semaphore::post() | |
| 232 { | |
| 233 // Prevent preemption | |
| 234 Cyg_Scheduler::lock(); | |
| 235 | |
| 236 CYG_INSTRUMENT_CNTSEM( POST, this, 0 ); | |
| 237 | |
| 238 count++; | |
| 239 | |
| 240 if( !queue.empty() ) { | |
| 241 | |
| 242 // The queue is non-empty, so grab the next | |
| 243 // thread from it and wake it up. The waiter | |
| 244 // will decrement the count when he is awakened. | |
| 245 | |
| 246 Cyg_Thread *thread = queue.dequeue(); | |
| 247 | |
| 248 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 249 | |
| 250 thread->wake(); | |
| 251 | |
| 252 CYG_INSTRUMENT_CNTSEM( WAKE, this, thread ); | |
| 253 } | |
| 254 | |
| 255 // Unlock the scheduler and maybe switch threads | |
| 256 Cyg_Scheduler::unlock(); | |
| 257 } | |
| 258 | |
| 259 // ------------------------------------------------------------------------- | |
| 260 // Get current count value | |
| 261 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
262 cyg_count32 Cyg_Counting_Semaphore::peek() const |
| 0 | 263 { |
| 264 // This is a single read of the value of count. | |
| 265 // This is already atomic, hence there is no need | |
| 266 // to lock the scheduler. | |
| 267 | |
| 268 return count; | |
| 269 } | |
| 270 | |
| 271 // ------------------------------------------------------------------------- | |
| 272 // EOF sync/cnt_sem.cxx |
