Mercurial > ecos-v3_0-branch
annotate packages/kernel/current/src/sync/bin_sem.cxx @ 2863:7d8c61e6225c default tip
* Added execute permissions to files missed in conversion from CVS
| author | alexs |
|---|---|
| date | Thu, 26 Mar 2009 20:39:18 +0000 |
| parents | 74dbf4c3f2e1 |
| children |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sync/bin_sem.cxx |
| 0 | 4 // |
| 2 | 5 // Binary 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_Binary_Semaphore implementation | |
| 46 // Description: This file contains the implementations of the binary 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 | |
| 68 Cyg_Binary_Semaphore::Cyg_Binary_Semaphore ( | |
| 69 cyg_bool init_state | |
| 70 ) | |
| 71 { | |
| 72 state = init_state; | |
| 73 } | |
| 74 | |
| 75 // ------------------------------------------------------------------------- | |
| 76 | |
| 77 Cyg_Binary_Semaphore::~Cyg_Binary_Semaphore ( ) | |
| 78 { | |
|
1062
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
79 CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads"); |
| 0 | 80 } |
| 81 | |
| 82 // ------------------------------------------------------------------------- | |
| 83 | |
| 84 cyg_bool Cyg_Binary_Semaphore::wait() | |
| 85 { | |
| 86 cyg_bool result = true; | |
| 87 Cyg_Thread *self = Cyg_Thread::self(); | |
| 88 | |
| 89 // Prevent preemption | |
| 90 Cyg_Scheduler::lock(); | |
| 91 | |
| 92 CYG_INSTRUMENT_BINSEM( CLAIM, this, state ); | |
| 93 | |
| 94 while( !state && result ) | |
| 95 { | |
| 96 self->set_sleep_reason( Cyg_Thread::WAIT ); | |
| 97 | |
| 98 self->sleep(); | |
| 99 | |
| 100 queue.enqueue( self ); | |
| 101 | |
| 102 CYG_INSTRUMENT_BINSEM( WAIT, this, 0 ); | |
| 103 | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
104 // Allow other threads to run |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
66
diff
changeset
|
105 Cyg_Scheduler::reschedule(); |
| 0 | 106 |
| 107 CYG_INSTRUMENT_BINSEM( WOKE, this, state ); | |
| 108 | |
| 109 switch( self->get_wake_reason() ) | |
| 110 { | |
| 111 case Cyg_Thread::DESTRUCT: | |
| 112 case Cyg_Thread::BREAK: | |
| 113 result = false; | |
| 114 break; | |
| 115 | |
| 116 case Cyg_Thread::EXIT: | |
| 117 self->exit(); | |
| 118 break; | |
| 119 | |
| 120 default: | |
| 121 break; | |
| 122 } | |
| 123 | |
| 124 } | |
| 125 | |
| 126 if( result ) state = false; | |
| 127 | |
| 128 // Unlock the scheduler and maybe switch threads | |
| 129 Cyg_Scheduler::unlock(); | |
| 130 | |
| 131 return result; | |
| 132 } | |
| 133 | |
| 134 // ------------------------------------------------------------------------- | |
|
1062
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
135 // Wait until the state can be set false or timeout |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
136 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
137 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
138 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
139 cyg_bool |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
140 Cyg_Binary_Semaphore::wait( cyg_tick_count timeout ) |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
141 { |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
142 cyg_bool result = true; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
143 Cyg_Thread *self = Cyg_Thread::self(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
144 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
145 // Prevent preemption |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
146 Cyg_Scheduler::lock(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
147 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
148 CYG_INSTRUMENT_BINSEM( CLAIM, this, state ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
149 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
150 // Set the timer _once_ outside the loop. |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
151 self->set_timer( timeout, Cyg_Thread::TIMEOUT ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
152 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
153 // If the timeout is in the past, the wake reason will have been |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
154 // set to something other than NONE already. If the semaphore is |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
155 // not available, set the result false to force an immediate |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
156 // return. If it is available, then go ahead and claim it. |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
157 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
158 if( self->get_wake_reason() != Cyg_Thread::NONE && !state ) |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
159 result = false; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
160 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
161 while ( !state && result ) { |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
162 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
163 // must reset the sleep reason every time |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
164 self->set_sleep_reason( Cyg_Thread::TIMEOUT ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
165 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
166 self->sleep(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
167 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
168 queue.enqueue( self ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
169 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
170 CYG_INSTRUMENT_BINSEM( WAIT, this, 0 ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
171 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
172 // Allow other threads to run |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
173 Cyg_Scheduler::reschedule(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
174 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
175 CYG_INSTRUMENT_BINSEM( WOKE, this, state ); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
176 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
177 switch( self->get_wake_reason() ) |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
178 { |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
179 case Cyg_Thread::TIMEOUT: |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
180 result = false; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
181 CYG_INSTRUMENT_BINSEM( TIMEOUT, this, state); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
182 break; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
183 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
184 case Cyg_Thread::DESTRUCT: |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
185 case Cyg_Thread::BREAK: |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
186 result = false; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
187 break; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
188 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
189 case Cyg_Thread::EXIT: |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
190 self->exit(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
191 break; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
192 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
193 default: |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
194 break; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
195 } |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
196 } |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
197 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
198 // Clear the timeout. It is irrelevant whether the alarm has |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
199 // actually gone off or not. |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
200 self->clear_timer(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
201 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
202 if( result ) state = false; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
203 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
204 // Unlock the scheduler and maybe switch threads |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
205 Cyg_Scheduler::unlock(); |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
206 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
207 return result; |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
208 } |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
209 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
210 #endif // CYGFUN_KERNEL_THREADS_TIMER |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
211 |
|
e8b69d9c90a6
* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests.
nickg
parents:
210
diff
changeset
|
212 // ------------------------------------------------------------------------- |
| 0 | 213 |
| 214 cyg_bool Cyg_Binary_Semaphore::trywait() | |
| 215 { | |
| 216 cyg_bool result = true; | |
| 217 | |
| 218 // Prevent preemption | |
| 219 Cyg_Scheduler::lock(); | |
| 220 | |
| 221 if( state ) state = false; | |
| 222 else result = false; | |
| 223 | |
| 224 CYG_INSTRUMENT_BINSEM( TRY, this, result ); | |
| 225 | |
| 226 // Unlock the scheduler and maybe switch threads | |
| 227 Cyg_Scheduler::unlock(); | |
| 228 | |
| 229 return result; | |
| 230 } | |
| 231 | |
| 232 // ------------------------------------------------------------------------- | |
| 233 | |
| 234 void Cyg_Binary_Semaphore::post() | |
| 235 { | |
| 236 // Prevent preemption | |
| 237 Cyg_Scheduler::lock(); | |
| 238 | |
| 239 CYG_INSTRUMENT_BINSEM( POST, this, 0 ); | |
| 240 | |
| 241 state = true; | |
| 242 | |
| 243 if( !queue.empty() ) { | |
| 244 | |
| 245 // The queue is non-empty, so grab the next | |
| 246 // thread from it and wake it up. The waiter | |
| 247 // will clear the flag. | |
| 248 | |
| 249 Cyg_Thread *thread = queue.dequeue(); | |
| 250 | |
| 251 thread->set_wake_reason( Cyg_Thread::DONE ); | |
| 252 | |
| 253 thread->wake(); | |
| 254 | |
| 255 CYG_INSTRUMENT_BINSEM( WAKE, this, thread ); | |
| 256 } | |
| 257 | |
| 258 // Unlock the scheduler and maybe switch threads | |
| 259 Cyg_Scheduler::unlock(); | |
| 260 | |
| 261 } | |
| 262 | |
| 263 // ------------------------------------------------------------------------- | |
| 264 | |
| 265 cyg_bool Cyg_Binary_Semaphore::posted() | |
| 266 { | |
| 267 // This is a single read of the value of state. | |
| 268 // This is already atomic, hence there is no need | |
| 269 // to lock the scheduler. | |
| 270 | |
| 271 return state; | |
| 272 } | |
| 273 | |
| 274 // ------------------------------------------------------------------------- | |
| 275 // EOF sync/bin_sem.cxx |
