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