Mercurial > ecos
annotate packages/kernel/current/src/sync/cnt_sem.cxx @ 208:e0c0827131d1 ecos
Merge from eCos master repository on 2002-05-20-20:11:54-BST
| author | jlarmour |
|---|---|
| date | Mon, 20 May 2002 22:19:26 +0000 |
| parents | 6ed91473a1cd |
| children | d2c90368aeef |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/cnt_sem.cxx |
| 0 | 4 // |
| 2 | 5 // Counting semaphore implementation |
| 0 | 6 // |
| 7 //========================================================================== | |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
8 //####ECOSGPLCOPYRIGHTBEGIN#### |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
9 // ------------------------------------------- |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
12 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
13 // eCos is free software; you can redistribute it and/or modify it under |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
14 // the terms of the GNU General Public License as published by the Free |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
15 // Software Foundation; either version 2 or (at your option) any later version. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
16 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
20 // for more details. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
21 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
22 // You should have received a copy of the GNU General Public License along |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
23 // with eCos; if not, write to the Free Software Foundation, Inc., |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
25 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
26 // As a special exception, if other files instantiate templates or use macros |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
27 // or inline functions from this file, or you compile this file and link it |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
28 // with other works to produce a work based on this file, this file does not |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
29 // by itself cause the resulting work to be covered by the GNU General Public |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
30 // License. However the source code for this file must still be made available |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
31 // in accordance with section (3) of the GNU General Public License. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
32 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
33 // This exception does not invalidate any other reasons why a work based on |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
34 // this file might be covered by the GNU General Public License. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
35 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
37 // at http://sources.redhat.com/ecos/ecos-license |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
38 // ------------------------------------------- |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
115
diff
changeset
|
39 //####ECOSGPLCOPYRIGHTEND#### |
| 0 | 40 //========================================================================== |
| 41 //#####DESCRIPTIONBEGIN#### | |
| 42 // | |
| 2 | 43 // Author(s): nickg |
| 44 // Contributors: nickg | |
| 45 // Date: 1997-09-24 | |
| 46 // Purpose: Cyg_Counting_Semaphore implementation | |
| 47 // Description: This file contains the implementations of the counting semaphore | |
| 0 | 48 // class. |
| 49 // | |
| 50 //####DESCRIPTIONEND#### | |
| 51 // | |
| 52 //========================================================================== | |
| 53 | |
| 54 #include <pkgconf/kernel.h> | |
| 55 | |
| 56 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 57 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 58 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 59 #include <cyg/kernel/instrmnt.h> // instrumentation | |
| 60 | |
| 61 #include <cyg/kernel/thread.inl> // Cyg_Thread inlines | |
| 62 | |
| 63 #include <cyg/kernel/sema.hxx> // our header | |
| 64 | |
| 65 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 66 | |
| 67 // ------------------------------------------------------------------------- | |
| 68 // Constructor | |
| 69 | |
| 70 Cyg_Counting_Semaphore::Cyg_Counting_Semaphore( | |
| 71 cyg_count32 init_count // Initial count value | |
| 72 ) | |
| 73 { | |
| 74 count = init_count; | |
| 75 } | |
| 76 | |
| 77 // ------------------------------------------------------------------------- | |
| 78 // Destructor | |
| 79 | |
| 80 Cyg_Counting_Semaphore::~Cyg_Counting_Semaphore() | |
| 81 { | |
| 82 CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads"); | |
| 83 } | |
| 84 | |
| 85 // ------------------------------------------------------------------------- | |
| 86 // Wait until the count can be decremented without it becoming | |
| 87 // negative. | |
| 88 | |
| 89 cyg_bool Cyg_Counting_Semaphore::wait() | |
| 90 { | |
| 91 cyg_bool result = true; | |
| 92 Cyg_Thread *self = Cyg_Thread::self(); | |
| 93 | |
| 94 // Prevent preemption | |
| 95 Cyg_Scheduler::lock(); | |
| 96 | |
| 97 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 98 | |
| 99 while( count == 0 && result ) | |
| 100 { | |
| 101 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 102 | |
| 103 self->sleep(); | |
| 104 | |
| 105 queue.enqueue( self ); | |
| 106 | |
| 107 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 108 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
109 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
110 Cyg_Scheduler::reschedule(); |
| 0 | 111 |
| 112 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 113 | |
| 114 switch( self->get_wake_reason() ) | |
| 115 { | |
| 116 case Cyg_Thread::DESTRUCT: | |
| 117 case Cyg_Thread::BREAK: | |
| 118 result = false; | |
| 119 break; | |
| 120 | |
| 121 case Cyg_Thread::EXIT: | |
| 122 self->exit(); | |
| 123 break; | |
| 124 | |
| 125 default: | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 if( result ) count--; | |
| 131 | |
| 132 // Unlock the scheduler | |
| 133 Cyg_Scheduler::unlock(); | |
| 134 | |
| 135 return result; | |
| 136 } | |
| 137 | |
| 138 // ------------------------------------------------------------------------- | |
| 139 // Wait until the count can be decremented without it becoming | |
| 140 // negative. | |
| 141 | |
| 142 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 143 | |
| 144 cyg_bool | |
| 145 Cyg_Counting_Semaphore::wait( cyg_tick_count timeout ) | |
| 146 { | |
| 147 cyg_bool result = true; | |
| 148 Cyg_Thread *self = Cyg_Thread::self(); | |
| 149 | |
| 150 // Prevent preemption | |
| 151 Cyg_Scheduler::lock(); | |
| 152 | |
| 153 CYG_INSTRUMENT_CNTSEM( CLAIM, this, count ); | |
| 154 | |
| 155 // Set the timer _once_ outside the loop. | |
| 156 self->set_timer( timeout, Cyg_Thread::TIMEOUT ); | |
| 157 | |
| 158 // If the timeout is in the past, the wake reason will have been | |
| 159 // set to something other than NONE already. Set the result false | |
| 160 // to force an immediate return. | |
| 161 | |
| 162 if( self->get_wake_reason() != Cyg_Thread::NONE ) | |
| 163 result = false; | |
| 164 | |
| 165 while ( 0 == count && result ) { | |
| 166 | |
| 167 // must reset the sleep reason every time | |
| 168 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 169 | |
| 170 self->sleep(); | |
| 171 | |
| 172 queue.enqueue( self ); | |
| 173 | |
| 174 CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 ); | |
| 175 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
176 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
177 Cyg_Scheduler::reschedule(); |
| 0 | 178 |
| 179 CYG_INSTRUMENT_CNTSEM( WOKE, this, count ); | |
| 180 | |
| 181 switch( self->get_wake_reason() ) | |
| 182 { | |
| 183 case Cyg_Thread::TIMEOUT: | |
| 184 result = false; | |
| 185 CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count); | |
| 186 break; | |
| 187 | |
| 188 case Cyg_Thread::DESTRUCT: | |
| 189 case Cyg_Thread::BREAK: | |
| 190 result = false; | |
| 191 break; | |
| 192 | |
| 193 case Cyg_Thread::EXIT: | |
| 194 self->exit(); | |
| 195 break; | |
| 196 | |
| 197 default: | |
| 198 break; | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 // Clear the timeout. It is irrelevant whether the alarm has | |
| 203 // actually gone off or not. | |
| 204 self->clear_timer(); | |
| 205 | |
| 206 if ( result ) count--; | |
| 207 | |
| 208 // Unlock the scheduler and maybe switch threads | |
| 209 Cyg_Scheduler::unlock(); | |
| 210 | |
| 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_Semaphore::trywait() | |
| 220 { | |
| 221 cyg_bool result = true; | |
| 222 | |
| 223 // Prevent preemption | |
| 224 Cyg_Scheduler::lock(); | |
| 225 | |
| 226 if( count > 0 ) count--; | |
| 227 else result = false; | |
| 228 | |
| 229 CYG_INSTRUMENT_CNTSEM( TRY, this, result ); | |
| 230 | |
| 231 // Unlock the scheduler and maybe switch threads | |
| 232 Cyg_Scheduler::unlock(); | |
| 233 | |
| 234 return result; | |
| 235 } | |
| 236 | |
| 237 // ------------------------------------------------------------------------- | |
| 238 // Increment count | |
| 239 | |
| 240 void Cyg_Counting_Semaphore::post() | |
| 241 { | |
| 242 // Prevent preemption | |
| 243 Cyg_Scheduler::lock(); | |
| 244 | |
| 245 CYG_INSTRUMENT_CNTSEM( POST, this, 0 ); | |
| 246 | |
| 247 count++; | |
| 248 | |
| 249 if( !queue.empty() ) { | |
| 250 | |
| 251 // The queue is non-empty, so grab the next | |
| 252 // thread from it and wake it up. The waiter | |
| 253 // will decrement the count when he is awakened. | |
| 254 | |
| 255 Cyg_Thread *thread = queue.dequeue(); | |
| 256 | |
| 257 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 258 | |
| 259 thread->wake(); | |
| 260 | |
| 261 CYG_INSTRUMENT_CNTSEM( WAKE, this, thread ); | |
| 262 } | |
| 263 | |
| 264 // Unlock the scheduler and maybe switch threads | |
| 265 Cyg_Scheduler::unlock(); | |
| 266 } | |
| 267 | |
| 268 // ------------------------------------------------------------------------- | |
| 269 // Get current count value | |
| 270 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
271 cyg_count32 Cyg_Counting_Semaphore::peek() const |
| 0 | 272 { |
| 273 // This is a single read of the value of count. | |
| 274 // This is already atomic, hence there is no need | |
| 275 // to lock the scheduler. | |
| 276 | |
| 277 return count; | |
| 278 } | |
| 279 | |
| 280 // ------------------------------------------------------------------------- | |
| 281 // EOF sync/cnt_sem.cxx |
