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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [kernel/] [v2_0/] [src/] [sync/] [cnt_sem2.cxx] - Blame information for rev 528

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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 Red Hat, 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 version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):   nickg
44
// Contributors:        nickg
45
// Date:        1997-09-24
46
// Purpose:     Cyg_Counting_Semaphore implementation
47
// Description: This file contains the implementations of the counting semaphore
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/sema2.hxx>        // our header
64
 
65
#include <cyg/kernel/sched.inl>        // scheduler inlines
66
 
67
// -------------------------------------------------------------------------
68
// Constructor
69
 
70
Cyg_Counting_Semaphore2::Cyg_Counting_Semaphore2(
71
    cyg_count32 init_count              // Initial count value
72
    )
73
{
74
    count       = init_count;
75
}
76
 
77
// -------------------------------------------------------------------------
78
// Destructor
79
 
80
Cyg_Counting_Semaphore2::~Cyg_Counting_Semaphore2()
81
{
82
    CYG_REPORT_FUNCTION();
83
#if 0
84
    CYG_ASSERT( queue.empty(), "Destroying semaphore with waiting threads");
85
#endif
86
    // Prevent preemption
87
    Cyg_Scheduler::lock();
88
 
89
    while ( ! queue.empty() ) {
90
        Cyg_Thread *thread = queue.dequeue();
91
        thread->set_wake_reason( Cyg_Thread::DESTRUCT );
92
        thread->wake();
93
    }
94
 
95
    // Unlock the scheduler and maybe switch threads
96
    Cyg_Scheduler::unlock();
97
    CYG_REPORT_RETURN();
98
}
99
 
100
// -------------------------------------------------------------------------
101
// Wait until the count can be decremented without it becoming
102
// negative.
103
 
104
cyg_bool Cyg_Counting_Semaphore2::wait()
105
{
106
    CYG_REPORT_FUNCTION();
107
    Cyg_Thread *self = Cyg_Thread::self();
108
    cyg_bool result = true;
109
 
110
    // Prevent preemption
111
    Cyg_Scheduler::lock();
112
 
113
    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );
114
 
115
    if ( 0 < count ) {
116
        count--;
117
        Cyg_Scheduler::unlock();
118
    }
119
    else {
120
        self->set_sleep_reason( Cyg_Thread::WAIT );
121
        self->sleep();
122
        queue.enqueue( self );
123
 
124
        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );
125
 
126
        Cyg_Scheduler::unlock();
127
 
128
        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );
129
 
130
        switch( self->get_wake_reason() )
131
        {
132
        case Cyg_Thread::DESTRUCT:
133
        case Cyg_Thread::BREAK:
134
            result = false;
135
            break;
136
 
137
        case Cyg_Thread::EXIT:
138
            self->exit();
139
            break;
140
 
141
        default:
142
            break;
143
        }
144
    }
145
 
146
    CYG_REPORT_RETVAL( result );
147
    return result;
148
}
149
 
150
// -------------------------------------------------------------------------
151
// Wait until the count can be decremented without it becoming
152
// negative.
153
 
154
#ifdef CYGFUN_KERNEL_THREADS_TIMER
155
 
156
cyg_bool
157
Cyg_Counting_Semaphore2::wait( cyg_tick_count abs_timeout )
158
{
159
    CYG_REPORT_FUNCTION();
160
    Cyg_Thread *self = Cyg_Thread::self();
161
    cyg_bool result = true;
162
 
163
    // Prevent preemption
164
    Cyg_Scheduler::lock();
165
 
166
    CYG_INSTRUMENT_CNTSEM( CLAIM, this, count );
167
 
168
    if ( 0 < count ) {
169
        count--;
170
        Cyg_Scheduler::unlock();
171
    }
172
    else {
173
 
174
        // Put thread in sleep state before setting timer since if the
175
        // timeout is in the past, it will be re-awoken
176
        // immediately. If this happens then wake_reason will not be
177
        // NONE.
178
 
179
        self->sleep();
180
 
181
        self->set_timer( abs_timeout, Cyg_Thread::TIMEOUT );
182
 
183
        // only enqueue if the timeout did not already happen
184
        if( Cyg_Thread::NONE == self->get_wake_reason() )
185
            queue.enqueue( self );
186
 
187
        CYG_INSTRUMENT_CNTSEM( WAIT, this, 0 );
188
 
189
 
190
        Cyg_Scheduler::unlock();
191
 
192
        // Clear the timeout. It is irrelevant whether the alarm has
193
        // actually gone off or not.
194
        self->clear_timer();
195
 
196
        CYG_INSTRUMENT_CNTSEM( WOKE, this, count );
197
 
198
        switch( self->get_wake_reason() )
199
        {
200
        case Cyg_Thread::TIMEOUT:
201
            result = false;
202
            CYG_INSTRUMENT_CNTSEM( TIMEOUT, this, count);
203
            break;
204
 
205
        case Cyg_Thread::DESTRUCT:
206
        case Cyg_Thread::BREAK:
207
            result = false;
208
            break;
209
 
210
        case Cyg_Thread::EXIT:
211
            self->exit();
212
            break;
213
 
214
        default:
215
            break;
216
        }
217
    }
218
 
219
    CYG_REPORT_RETVAL( result );
220
    return result;
221
}
222
 
223
#endif // CYGFUN_KERNEL_THREADS_TIMER
224
 
225
// -------------------------------------------------------------------------
226
// Try to decrement, but fail if not possible
227
 
228
cyg_bool Cyg_Counting_Semaphore2::trywait()
229
{
230
    CYG_REPORT_FUNCTION();
231
    cyg_bool result = true;
232
 
233
    // Prevent preemption
234
    Cyg_Scheduler::lock();
235
 
236
    if( 0 < count ) count--;
237
    else            result = false;
238
 
239
    CYG_INSTRUMENT_CNTSEM( TRY, this, result );
240
 
241
    // Unlock the scheduler and maybe switch threads
242
    Cyg_Scheduler::unlock();
243
 
244
    CYG_REPORT_RETVAL( result );
245
    return result;
246
}
247
 
248
// -------------------------------------------------------------------------
249
// Increment count
250
 
251
void Cyg_Counting_Semaphore2::post()
252
{
253
    CYG_REPORT_FUNCTION();
254
    // Prevent preemption
255
    Cyg_Scheduler::lock();
256
 
257
    CYG_INSTRUMENT_CNTSEM( POST, this, 0 );
258
 
259
    if( queue.empty() ) {
260
        count++;
261
    }
262
    else {
263
        // The queue is non-empty, so grab the next
264
        // thread from it and wake it up. The waiter
265
        // won't decrement the count when he is awakened,
266
        // for we never incremented it in the first place
267
 
268
        Cyg_Thread *thread = queue.dequeue();
269
 
270
        thread->set_wake_reason( Cyg_Thread::DONE );
271
 
272
        thread->wake();
273
 
274
        CYG_INSTRUMENT_CNTSEM( WAKE, this, thread );
275
    }
276
 
277
    // Unlock the scheduler and maybe switch threads
278
    Cyg_Scheduler::unlock();
279
    CYG_REPORT_RETURN();
280
}
281
 
282
// -------------------------------------------------------------------------
283
// Get current count value
284
 
285
cyg_count32 Cyg_Counting_Semaphore2::peek() const
286
{
287
    // This is a single read of the value of count.
288
    // This is already atomic, hence there is no need
289
    // to lock the scheduler.
290
 
291
    return count;
292
}
293
 
294
// -------------------------------------------------------------------------
295
// 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.