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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [src/] [sync/] [cnt_sem2.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/cnt_sem2.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/sema2.hxx>        // our header
63
 
64
#include <cyg/kernel/sched.inl>        // scheduler inlines
65
 
66
// -------------------------------------------------------------------------
67
// Constructor
68
 
69
Cyg_Counting_Semaphore2::Cyg_Counting_Semaphore2(
70
    cyg_count32 init_count              // Initial count value
71
    )
72
{
73
    count       = init_count;
74
}
75
 
76
// -------------------------------------------------------------------------
77
// Destructor
78
 
79
Cyg_Counting_Semaphore2::~Cyg_Counting_Semaphore2()
80
{
81
    CYG_REPORT_FUNCTION();
82
#if 0
83
    CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads");
84
#endif
85
    // Prevent preemption
86
    Cyg_Scheduler::lock();
87
 
88
    while ( ! queue.empty() ) {
89
        Cyg_Thread *thread = queue.dequeue();
90
        thread->set_wake_reason( Cyg_Thread::DESTRUCT );
91
        thread->wake();
92
    }
93
 
94
    // Unlock the scheduler and maybe switch threads
95
    Cyg_Scheduler::unlock();
96
    CYG_REPORT_RETURN();
97
}
98
 
99
// -------------------------------------------------------------------------
100
// Wait until the count can be decremented without it becoming
101
// negative.
102
 
103
cyg_bool Cyg_Counting_Semaphore2::wait()
104
{
105
    CYG_REPORT_FUNCTION();
106
    Cyg_Thread *self = Cyg_Thread::self();
107
    cyg_bool result = true;
108
 
109
    // Prevent preemption
110
    Cyg_Scheduler::lock();
111
 
112
    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );
113
 
114
    if ( 0 < count ) {
115
        count--;
116
        Cyg_Scheduler::unlock();
117
    }
118
    else {
119
        self->set_sleep_reason( Cyg_Thread::WAIT );
120
        self->sleep();
121
        queue.enqueue( self );
122
 
123
        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );
124
 
125
        Cyg_Scheduler::unlock();
126
 
127
        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );
128
 
129
        switch( self->get_wake_reason() )
130
        {
131
        case Cyg_Thread::DESTRUCT:
132
        case Cyg_Thread::BREAK:
133
            result = false;
134
            break;
135
 
136
        case Cyg_Thread::EXIT:
137
            self->exit();
138
            break;
139
 
140
        default:
141
            break;
142
        }
143
    }
144
 
145
    CYG_REPORT_RETVAL( result );
146
    return result;
147
}
148
 
149
// -------------------------------------------------------------------------
150
// Wait until the count can be decremented without it becoming
151
// negative.
152
 
153
#ifdef CYGFUN_KERNEL_THREADS_TIMER
154
 
155
cyg_bool
156
Cyg_Counting_Semaphore2::wait( cyg_tick_count abs_timeout )
157
{
158
    CYG_REPORT_FUNCTION();
159
    Cyg_Thread *self = Cyg_Thread::self();
160
    cyg_bool result = true;
161
 
162
    // Prevent preemption
163
    Cyg_Scheduler::lock();
164
 
165
    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );
166
 
167
    if ( 0 < count ) {
168
        count--;
169
        Cyg_Scheduler::unlock();
170
    }
171
    else {
172
 
173
        // Put thread in sleep state before setting timer since if the
174
        // timeout is in the past, it will be re-awoken
175
        // immediately. If this happens then wake_reason will not be
176
        // NONE.
177
 
178
        self->sleep();
179
 
180
        self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT );
181
 
182
        // only enqueue if the timeout did not already happen
183
        if( Cyg_Thread::NONE == self->get_wake_reason() )
184
            queue.enqueue( self );
185
 
186
        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );
187
 
188
 
189
        Cyg_Scheduler::unlock();
190
 
191
        // Clear the timeout. It is irrelevant whether the alarm has
192
        // actually gone off or not.
193
        self->clear_timer();
194
 
195
        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );
196
 
197
        switch( self->get_wake_reason() )
198
        {
199
        case Cyg_Thread::TIMEOUT:
200
            result = false;
201
            CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count);
202
            break;
203
 
204
        case Cyg_Thread::DESTRUCT:
205
        case Cyg_Thread::BREAK:
206
            result = false;
207
            break;
208
 
209
        case Cyg_Thread::EXIT:
210
            self->exit();
211
            break;
212
 
213
        default:
214
            break;
215
        }
216
    }
217
 
218
    CYG_REPORT_RETVAL( result );
219
    return result;
220
}
221
 
222
#endif // CYGFUN_KERNEL_THREADS_TIMER
223
 
224
// -------------------------------------------------------------------------
225
// Try to decrement, but fail if not possible
226
 
227
cyg_bool Cyg_Counting_Semaphore2::trywait()
228
{
229
    CYG_REPORT_FUNCTION();
230
    cyg_bool result = true;
231
 
232
    // Prevent preemption
233
    Cyg_Scheduler::lock();
234
 
235
    if( 0 < count ) count--;
236
    else            result = false;
237
 
238
    CYG_INSTRUMENT_CNTSEM( TRY, this, result );
239
 
240
    // Unlock the scheduler and maybe switch threads
241
    Cyg_Scheduler::unlock();
242
 
243
    CYG_REPORT_RETVAL( result );
244
    return result;
245
}
246
 
247
// -------------------------------------------------------------------------
248
// Increment count
249
 
250
void Cyg_Counting_Semaphore2::post()
251
{
252
    CYG_REPORT_FUNCTION();
253
    // Prevent preemption
254
    Cyg_Scheduler::lock();
255
 
256
    CYG_INSTRUMENT_CNTSEM( POST, this, 0 );
257
 
258
    if( queue.empty() ) {
259
        count++;
260
    }
261
    else {
262
        // The queue is non-empty, so grab the next
263
        // thread from it and wake it up. The waiter
264
        // won't decrement the count when he is awakened,
265
        // for we never incremented it in the first place
266
 
267
        Cyg_Thread *thread = queue.dequeue();
268
 
269
        thread->set_wake_reason( Cyg_Thread::DONE );
270
 
271
        thread->wake();
272
 
273
        CYG_INSTRUMENT_CNTSEM( WAKE, this, thread );
274
    }
275
 
276
    // Unlock the scheduler and maybe switch threads
277
    Cyg_Scheduler::unlock();
278
    CYG_REPORT_RETURN();
279
}
280
 
281
// -------------------------------------------------------------------------
282
// Get current count value
283
 
284
cyg_count32 Cyg_Counting_Semaphore2::peek() const
285
{
286
    // This is a single read of the value of count.
287
    // This is already atomic, hence there is no need
288
    // to lock the scheduler.
289
 
290
    return count;
291
}
292
 
293
// -------------------------------------------------------------------------
294
// EOF sync/cnt_sem2.cxx

powered by: WebSVN 2.1.0

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