comparison packages/kernel/current/src/sync/bin_sem.cxx @ 1062:e8b69d9c90a6

* cdl/kernel.cdl: Added tests/bin_sem3 to list of kernel tests. * include/sema.hxx: Added declaration for wait with timeout for Cyg_Binary_Semaphore. * include/instrmnt.h: Added instrumentation point for binary semaphore timeout. * src/sync/bin_sem.cxx: Added wait with time-out function to Cyg_Binary_Semaphore class. * src/sync/cnt_sem.cxx: Modified semantics slightly to claim an available semaphore even with a timeout in the past. This is in line with the new timed wait in bin_sem.cxx. * tests/bin_sem3.cxx: Created new test for timed wait binary semaphore.
author nickg
date Mon, 23 Jun 2003 18:09:59 +0000
parents d2c90368aeef
children
comparison
equal deleted inserted replaced
1061:13900936a2c4 1062:e8b69d9c90a6
75 75
76 // ------------------------------------------------------------------------- 76 // -------------------------------------------------------------------------
77 77
78 Cyg_Binary_Semaphore::~Cyg_Binary_Semaphore ( ) 78 Cyg_Binary_Semaphore::~Cyg_Binary_Semaphore ( )
79 { 79 {
80 CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads");
80 } 81 }
81 82
82 // ------------------------------------------------------------------------- 83 // -------------------------------------------------------------------------
83 84
84 cyg_bool Cyg_Binary_Semaphore::wait() 85 cyg_bool Cyg_Binary_Semaphore::wait()
130 131
131 return result; 132 return result;
132 } 133 }
133 134
134 // ------------------------------------------------------------------------- 135 // -------------------------------------------------------------------------
136 // Wait until the state can be set false or timeout
137
138 #ifdef CYGFUN_KERNEL_THREADS_TIMER
139
140 cyg_bool
141 Cyg_Binary_Semaphore::wait( cyg_tick_count timeout )
142 {
143 cyg_bool result = true;
144 Cyg_Thread *self = Cyg_Thread::self();
145
146 // Prevent preemption
147 Cyg_Scheduler::lock();
148
149 CYG_INSTRUMENT_BINSEM( CLAIM, this, state );
150
151 // Set the timer _once_ outside the loop.
152 self->set_timer( timeout, Cyg_Thread::TIMEOUT );
153
154 // If the timeout is in the past, the wake reason will have been
155 // set to something other than NONE already. If the semaphore is
156 // not available, set the result false to force an immediate
157 // return. If it is available, then go ahead and claim it.
158
159 if( self->get_wake_reason() != Cyg_Thread::NONE && !state )
160 result = false;
161
162 while ( !state && result ) {
163
164 // must reset the sleep reason every time
165 self->set_sleep_reason( Cyg_Thread::TIMEOUT );
166
167 self->sleep();
168
169 queue.enqueue( self );
170
171 CYG_INSTRUMENT_BINSEM( WAIT, this, 0 );
172
173 // Allow other threads to run
174 Cyg_Scheduler::reschedule();
175
176 CYG_INSTRUMENT_BINSEM( WOKE, this, state );
177
178 switch( self->get_wake_reason() )
179 {
180 case Cyg_Thread::TIMEOUT:
181 result = false;
182 CYG_INSTRUMENT_BINSEM( TIMEOUT, this, state);
183 break;
184
185 case Cyg_Thread::DESTRUCT:
186 case Cyg_Thread::BREAK:
187 result = false;
188 break;
189
190 case Cyg_Thread::EXIT:
191 self->exit();
192 break;
193
194 default:
195 break;
196 }
197 }
198
199 // Clear the timeout. It is irrelevant whether the alarm has
200 // actually gone off or not.
201 self->clear_timer();
202
203 if( result ) state = false;
204
205 // Unlock the scheduler and maybe switch threads
206 Cyg_Scheduler::unlock();
207
208 return result;
209 }
210
211 #endif // CYGFUN_KERNEL_THREADS_TIMER
212
213 // -------------------------------------------------------------------------
135 214
136 cyg_bool Cyg_Binary_Semaphore::trywait() 215 cyg_bool Cyg_Binary_Semaphore::trywait()
137 { 216 {
138 cyg_bool result = true; 217 cyg_bool result = true;
139 218