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