OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [src/] [sync/] [bin_sem.cxx] - Blame information for rev 851

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      sync/bin_sem.cxx
4
//
5
//      Binary semaphore implementation
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
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
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
{
79
    CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads");
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
 
104
        // Allow other threads to run
105
        Cyg_Scheduler::reschedule();
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
// -------------------------------------------------------------------------
135
// Wait until the state can be set false or timeout
136
 
137
#ifdef CYGFUN_KERNEL_THREADS_TIMER
138
 
139
cyg_bool
140
Cyg_Binary_Semaphore::wait( cyg_tick_count timeout )
141
{
142
    cyg_bool result = true;
143
    Cyg_Thread *self = Cyg_Thread::self();
144
 
145
    // Prevent preemption
146
    Cyg_Scheduler::lock();
147
 
148
    CYG_INSTRUMENT_BINSEM( CLAIM, this, state );
149
 
150
    // Set the timer _once_ outside the loop.
151
    self->set_timer( timeout, Cyg_Thread::TIMEOUT  );
152
 
153
    // If the timeout is in the past, the wake reason will have been
154
    // set to something other than NONE already. If the semaphore is
155
    // not available, set the result false to force an immediate
156
    // return. If it is available, then go ahead and claim it.
157
 
158
    if( self->get_wake_reason() != Cyg_Thread::NONE && !state )
159
        result = false;
160
 
161
    while ( !state && result ) {
162
 
163
        // must reset the sleep reason every time
164
        self->set_sleep_reason( Cyg_Thread::TIMEOUT );
165
 
166
        self->sleep();
167
 
168
        queue.enqueue( self );
169
 
170
        CYG_INSTRUMENT_BINSEM( WAIT, this, 0 );
171
 
172
        // Allow other threads to run
173
        Cyg_Scheduler::reschedule();
174
 
175
        CYG_INSTRUMENT_BINSEM( WOKE, this, state );
176
 
177
        switch( self->get_wake_reason() )
178
        {
179
        case Cyg_Thread::TIMEOUT:
180
            result = false;
181
            CYG_INSTRUMENT_BINSEM( TIMEOUT, this, state);
182
            break;
183
 
184
        case Cyg_Thread::DESTRUCT:
185
        case Cyg_Thread::BREAK:
186
            result = false;
187
            break;
188
 
189
        case Cyg_Thread::EXIT:
190
            self->exit();
191
            break;
192
 
193
        default:
194
            break;
195
        }
196
    }
197
 
198
    // Clear the timeout. It is irrelevant whether the alarm has
199
    // actually gone off or not.
200
    self->clear_timer();
201
 
202
    if( result ) state = false;
203
 
204
    // Unlock the scheduler and maybe switch threads
205
    Cyg_Scheduler::unlock();
206
 
207
    return result;
208
}
209
 
210
#endif // CYGFUN_KERNEL_THREADS_TIMER
211
 
212
// -------------------------------------------------------------------------
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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.