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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      sync/cnt_sem.cxx
4
//
5
//      Counting 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_Counting_Semaphore implementation
46
// Description: This file contains the implementations of the counting 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
// 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
 
108
        // Allow other threads to run
109
        Cyg_Scheduler::reschedule();
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
158
    // set to something other than NONE already. If the count is zero,
159
    // set the result false to force an immediate return. If the count
160
    // is non-zero, then this wait will succeed anyway.
161
 
162
    if( self->get_wake_reason() != Cyg_Thread::NONE &&
163
 
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
 
177
        // Allow other threads to run
178
        Cyg_Scheduler::reschedule();
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
 
272
cyg_count32 Cyg_Counting_Semaphore::peek() const
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

powered by: WebSVN 2.1.0

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