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

Subversion Repositories openrisc_me

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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 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_Binary_Semaphore implementation
47
// Description: This file contains the implementations of the binary 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/sema.hxx>             // our header
64
 
65
#include <cyg/kernel/sched.inl>            // scheduler inlines
66
 
67
// -------------------------------------------------------------------------
68
 
69
Cyg_Binary_Semaphore::Cyg_Binary_Semaphore (
70
    cyg_bool    init_state
71
)
72
{
73
    state       = init_state;
74
}
75
 
76
// -------------------------------------------------------------------------
77
 
78
Cyg_Binary_Semaphore::~Cyg_Binary_Semaphore ( )
79
{
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
 
136
cyg_bool Cyg_Binary_Semaphore::trywait()
137
{
138
    cyg_bool result = true;
139
 
140
    // Prevent preemption
141
    Cyg_Scheduler::lock();
142
 
143
    if( state ) state = false;
144
    else        result = false;
145
 
146
    CYG_INSTRUMENT_BINSEM( TRY, this, result );
147
 
148
    // Unlock the scheduler and maybe switch threads
149
    Cyg_Scheduler::unlock();
150
 
151
    return result;
152
}
153
 
154
// -------------------------------------------------------------------------
155
 
156
void Cyg_Binary_Semaphore::post()
157
{
158
    // Prevent preemption
159
    Cyg_Scheduler::lock();
160
 
161
    CYG_INSTRUMENT_BINSEM( POST, this, 0 );
162
 
163
    state = true;
164
 
165
    if( !queue.empty() ) {
166
 
167
        // The queue is non-empty, so grab the next
168
        // thread from it and wake it up. The waiter
169
        // will clear the flag.
170
 
171
        Cyg_Thread *thread = queue.dequeue();
172
 
173
        thread->set_wake_reason( Cyg_Thread::DONE );
174
 
175
        thread->wake();
176
 
177
        CYG_INSTRUMENT_BINSEM( WAKE, this, thread );
178
    }
179
 
180
    // Unlock the scheduler and maybe switch threads
181
    Cyg_Scheduler::unlock();
182
 
183
}
184
 
185
// -------------------------------------------------------------------------
186
 
187
cyg_bool Cyg_Binary_Semaphore::posted()
188
{
189
    // This is a single read of the value of state.
190
    // This is already atomic, hence there is no need
191
    // to lock the scheduler.
192
 
193
    return state;
194
}
195
 
196
// -------------------------------------------------------------------------
197
// 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.