comparison packages/compat/posix/current/src/sem.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 25e238959bae
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // sem.cxx
4 //
5 // POSIX semaphore implementation
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): nickg
35 // Contributors: nickg
36 // Date: 2000-03-27
37 // Purpose: POSIX semaphore implementation
38 // Description: This file contains the implementation of the POSIX semaphore
39 // functions.
40 //
41 //
42 //
43 //####DESCRIPTIONEND####
44 //
45 //==========================================================================
46
47 #include <pkgconf/hal.h>
48 #include <pkgconf/kernel.h>
49 #include <pkgconf/posix.h>
50
51 #include <cyg/kernel/ktypes.h> // base kernel types
52 #include <cyg/infra/cyg_trac.h> // tracing macros
53 #include <cyg/infra/cyg_ass.h> // assertion macros
54
55 #include <semaphore.h> // our header
56
57 #include "pprivate.h" // POSIX private header
58
59 #include <cyg/kernel/thread.hxx> // Kernel threads
60
61 #include <cyg/kernel/thread.inl> // Cyg_ThreadQueue::empty()
62
63 #include <cyg/kernel/sema.hxx> // Kernel semaphores
64
65 // -------------------------------------------------------------------------
66 // Internal definitions
67
68 // Handle entry to a pthread package function.
69 #define SEMA_ENTRY() CYG_REPORT_FUNCTYPE( "returning %d" );
70
71 // Do a semaphore package defined return. This requires the error code
72 // to be placed in errno, and if it is non-zero, -1 returned as the
73 // result of the function. This also gives us a place to put any
74 // generic tidyup handling needed for things like signal delivery and
75 // cancellation.
76 #define SEMA_RETURN(err) \
77 CYG_MACRO_START \
78 int __retval = 0; \
79 if( err != 0 ) __retval = -1, errno = err; \
80 CYG_REPORT_RETVAL( __retval ); \
81 return __retval; \
82 CYG_MACRO_END
83
84 //-----------------------------------------------------------------------------
85 // new operator to allow us to invoke the Cyg_Thread constructor on the
86 // user's semaphore object.
87
88 inline void *operator new(size_t size, void *ptr) { return (void *)ptr; };
89
90 // -------------------------------------------------------------------------
91 // Initialize semaphore to value.
92 // pshared is not supported under eCos.
93
94 externC int sem_init (sem_t *sem, int pshared, unsigned int value)
95 {
96 SEMA_ENTRY();
97
98 if( value > SEM_VALUE_MAX )
99 SEMA_RETURN(EINVAL);
100
101 Cyg_Counting_Semaphore *sema;
102
103 sema = new((void *)sem) Cyg_Counting_Semaphore(value);
104
105 sema=sema;
106
107 SEMA_RETURN(0);
108 }
109
110 // -------------------------------------------------------------------------
111 // Destroy the semaphore.
112
113 externC int sem_destroy (sem_t *sem)
114 {
115 SEMA_ENTRY();
116
117 Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
118
119 // Check that the semaphore has no waiters
120 if( sema->waiting() )
121 SEMA_RETURN(EBUSY);
122
123 // Call the destructor
124 sema->~Cyg_Counting_Semaphore();
125
126 SEMA_RETURN(0);
127 }
128
129 // -------------------------------------------------------------------------
130 // Decrement value if >0 or wait for a post.
131
132 externC int sem_wait (sem_t *sem)
133 {
134 int retval = 0;
135
136 SEMA_ENTRY();
137
138 Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
139
140 if( !sema->wait() ) retval = EINTR;
141
142 SEMA_RETURN(retval);
143 }
144
145 // -------------------------------------------------------------------------
146 // Decrement value if >0, return -1 if not.
147
148 externC int sem_trywait (sem_t *sem)
149 {
150 int retval = 0;
151
152 SEMA_ENTRY();
153
154 Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
155
156 if( !sema->trywait() ) retval = EAGAIN;
157
158 SEMA_RETURN(0);
159 }
160
161 // -------------------------------------------------------------------------
162 // Increment value and wake a waiter if one is present.
163
164 externC int sem_post (sem_t *sem)
165 {
166 SEMA_ENTRY();
167
168 Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
169
170 sema->post();
171
172 SEMA_RETURN(0);
173 }
174
175
176 // -------------------------------------------------------------------------
177 // Get current value
178
179 externC int sem_getvalue (sem_t *sem, int *sval)
180 {
181 int retval;
182
183 SEMA_ENTRY();
184
185 Cyg_Counting_Semaphore *sema = (Cyg_Counting_Semaphore *)sem;
186
187 retval = sema->peek();
188
189 CYG_REPORT_RETVAL( retval );
190 return retval;
191 }
192
193 // -------------------------------------------------------------------------
194 // Open an existing named semaphore, or create it.
195
196 externC sem_t *sem_open (const char *name, int oflag, ...)
197 {
198 SEMA_ENTRY();
199
200 errno = ENOSYS;
201
202 CYG_REPORT_RETVAL( SEM_FAILED );
203 return SEM_FAILED;
204 }
205
206 // -------------------------------------------------------------------------
207 // Close descriptor for semaphore.
208
209 externC int sem_close (sem_t *sem)
210 {
211 SEMA_ENTRY();
212
213 SEMA_RETURN(ENOSYS);
214 }
215
216 // -------------------------------------------------------------------------
217 // Remove named semaphore
218
219 externC int sem_unlink (const char *name)
220 {
221 SEMA_ENTRY();
222
223 SEMA_RETURN(ENOSYS);
224 }
225
226 // -------------------------------------------------------------------------
227 // EOF sem.cxx