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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [compat/] [posix/] [current/] [src/] [sem.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      sem.cxx
4
//
5
//      POSIX 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:                2000-03-27
45
// Purpose:             POSIX semaphore implementation
46
// Description:         This file contains the implementation of the POSIX semaphore
47
//                      functions.
48
//              
49
//              
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
#include <pkgconf/hal.h>
56
#include <pkgconf/kernel.h>
57
#include <pkgconf/posix.h>
58
 
59
#include <cyg/kernel/ktypes.h>          // base kernel types
60
#include <cyg/infra/cyg_trac.h>         // tracing macros
61
#include <cyg/infra/cyg_ass.h>          // assertion macros
62
 
63
#include <semaphore.h>                  // our header
64
 
65
#include "pprivate.h"                   // POSIX private header
66
 
67
#include <cyg/kernel/thread.hxx>        // Kernel threads
68
 
69
#include <cyg/kernel/thread.inl>        // Cyg_ThreadQueue::empty()
70
 
71
#include <cyg/kernel/sema.hxx>          // Kernel semaphores
72
 
73
// -------------------------------------------------------------------------
74
// Internal definitions
75
 
76
// Handle entry to a pthread package function. 
77
#define SEMA_ENTRY() CYG_REPORT_FUNCTYPE( "returning %d" );
78
 
79
// Do a semaphore package defined return. This requires the error code
80
// to be placed in errno, and if it is non-zero, -1 returned as the
81
// result of the function. This also gives us a place to put any
82
// generic tidyup handling needed for things like signal delivery and
83
// cancellation.
84
#define SEMA_RETURN(err)                        \
85
CYG_MACRO_START                                 \
86
    int __retval = 0;                           \
87
    if( err != 0 ) __retval = -1, errno = err;  \
88
    CYG_REPORT_RETVAL( __retval );              \
89
    return __retval;                            \
90
CYG_MACRO_END
91
 
92
//-----------------------------------------------------------------------------
93
// new operator to allow us to invoke the Cyg_Thread constructor on the
94
// user's semaphore object.
95
 
96
inline void *operator new(size_t size,  void *ptr) { return (void *)ptr; };
97
 
98
// -------------------------------------------------------------------------
99
// Initialize semaphore to value.
100
// pshared is not supported under eCos.
101
 
102
externC int sem_init  (sem_t *sem, int pshared, unsigned int value)
103
{
104
    SEMA_ENTRY();
105
 
106
    if( value > SEM_VALUE_MAX )
107
        SEMA_RETURN(EINVAL);
108
 
109
    Cyg_Counting_Semaphore *sema;
110
 
111
    sema = new((void *)sem) Cyg_Counting_Semaphore(value);
112
 
113
    sema=sema;
114
 
115
    SEMA_RETURN(0);
116
}
117
 
118
// -------------------------------------------------------------------------
119
// Destroy the semaphore.
120
 
121
externC int sem_destroy  (sem_t *sem)
122
{
123
    SEMA_ENTRY();
124
 
125
    Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
126
 
127
    // Check that the semaphore has no waiters
128
    if( sema->waiting() )
129
        SEMA_RETURN(EBUSY);
130
 
131
    // Call the destructor
132
    sema->~Cyg_Counting_Semaphore();
133
 
134
    SEMA_RETURN(0);
135
}
136
 
137
// -------------------------------------------------------------------------
138
// Decrement value if >0 or wait for a post.
139
 
140
externC int sem_wait  (sem_t *sem)
141
{
142
    int retval = 0;
143
 
144
    SEMA_ENTRY();
145
 
146
#ifdef CYGPKG_POSIX_PTHREAD
147
    // check for cancellation first.
148
    pthread_testcancel();
149
#endif
150
 
151
    Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
152
 
153
    if( !sema->wait() ) retval = EINTR;
154
 
155
#ifdef CYGPKG_POSIX_PTHREAD
156
    // check if we were woken because we were being cancelled
157
    pthread_testcancel();
158
#endif
159
 
160
    SEMA_RETURN(retval);
161
}
162
 
163
// -------------------------------------------------------------------------
164
// Decrement value if >0, return -1 if not.
165
 
166
externC int sem_trywait  (sem_t *sem)
167
{
168
    int retval = 0;
169
 
170
    SEMA_ENTRY();
171
 
172
    Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
173
 
174
    if( !sema->trywait() ) retval = EAGAIN;
175
 
176
    SEMA_RETURN(retval);
177
}
178
 
179
// -------------------------------------------------------------------------
180
// Increment value and wake a waiter if one is present.
181
 
182
externC int sem_post  (sem_t *sem)
183
{
184
    SEMA_ENTRY();
185
 
186
    Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
187
 
188
    sema->post();
189
 
190
    SEMA_RETURN(0);
191
}
192
 
193
 
194
// -------------------------------------------------------------------------
195
// Get current value
196
 
197
externC int sem_getvalue  (sem_t *sem, int *sval)
198
{
199
    SEMA_ENTRY();
200
 
201
    Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
202
 
203
    *sval = sema->peek();
204
 
205
    CYG_REPORT_RETVAL( 0 );
206
    return 0;
207
}
208
 
209
// -------------------------------------------------------------------------
210
// Open an existing named semaphore, or create it.
211
 
212
externC sem_t *sem_open  (const char *name, int oflag, ...)
213
{
214
    SEMA_ENTRY();
215
 
216
    errno = ENOSYS;
217
 
218
    CYG_REPORT_RETVAL( SEM_FAILED );
219
    return SEM_FAILED;
220
}
221
 
222
// -------------------------------------------------------------------------
223
// Close descriptor for semaphore.
224
 
225
externC int sem_close  (sem_t *sem)
226
{
227
    SEMA_ENTRY();
228
 
229
    SEMA_RETURN(ENOSYS);
230
}
231
 
232
// -------------------------------------------------------------------------
233
// Remove named semaphore
234
 
235
externC int sem_unlink  (const char *name)
236
{
237
    SEMA_ENTRY();
238
 
239
    SEMA_RETURN(ENOSYS);
240
}
241
 
242
// -------------------------------------------------------------------------
243
// EOF sem.cxx

powered by: WebSVN 2.1.0

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