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