0023065: This is desirable to add general DRAW command to estimate visualization...
[occt.git] / src / OSD / OSD_Csemaphore.c
1 /*
2  Copyright (c) 1998-1999 Matra Datavision
3  Copyright (c) 1999-2012 OPEN CASCADE SAS
4
5  The content of this file is subject to the Open CASCADE Technology Public
6  License Version 6.5 (the "License"). You may not use the content of this file
7  except in compliance with the License. Please obtain a copy of the License
8  at http://www.opencascade.org and read it completely before using this file.
9
10  The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11  main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12
13  The Original Code and all software distributed under the License is
14  distributed on an "AS IS" basis, without warranty of any kind, and the
15  Initial Developer hereby disclaims all such warranties, including without
16  limitation, any warranties of merchantability, fitness for a particular
17  purpose or non-infringement. Please see the License for the specific terms
18  and conditions governing the rights and limitations under the License.
19
20 */
21
22 #ifndef WNT
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_SYS_IPC_H
36 #include <sys/ipc.h>
37 #endif
38
39 #ifdef HAVE_SYS_SEM_H
40 # include <sys/sem.h>
41 #endif
42
43 #ifdef HAVE_SIGNAL_H
44 # include <signal.h>
45 #endif
46
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50
51 /* Modified by Stephan GARNAUD (ARM) 1992 for Matra Datavision */
52
53 #if (!defined (__hpux)) && (!defined (HPUX))
54
55 #define CLUSTER_NUMBER 4 
56 #define SIGUSER SIGUSR1-1
57
58 static struct sembuf event_flag ;
59          
60 static void osd_signal_(int); 
61 int osd_getkey(char *);
62 #ifdef ANSI
63 int create_semaphore(char *);
64 int open_semaphore(char *);
65 int lock_semaphore(int *,int *);
66 int free_semaphore(int *,int *);
67 int reset_semaphore(int *,int *);
68 int remove_semaphore(int *,char *);
69 int osd_signl_(int *);
70 int osd_skill_(int *,int *);
71 int osd_alarm_(int,void (*)());
72
73 #endif
74
75 static void
76 osd_signal_(int i )
77 {
78 }
79
80
81 int
82 create_semaphore(char *name) 
83 /*=======================================================
84
85         CREATE a semaphore cluster of 32 events
86
87 =========================================================*/
88 {
89 int status ;
90
91
92
93     key_t key;
94
95
96         key = (key_t) osd_getkey (name);
97
98
99         status = semget(key,CLUSTER_NUMBER,0750 + IPC_CREAT) ;
100
101         if( status < 0 ) return (0) ;  /* semget failed */
102         return status ;
103 }
104
105
106 int
107 open_semaphore(char *name)
108 /*=========================================================
109
110         OPEN a semaphore cluster 
111
112 =========================================================================*/
113 {
114 int status ;
115
116
117     key_t key;
118
119
120         key = (key_t) osd_getkey (name);
121         status = semget(key,0,0) ;
122
123         if( status < 0 ) return (0) ;  /* semget failed */
124          else   return status;
125 }
126
127
128
129 int
130 lock_semaphore(int *semid, int *event) 
131 /*==================================================
132
133         WAIT & LOCK the semaphore attached to the cluster
134
135 ====================================================*/
136 {
137   int status ;
138
139   event_flag.sem_num = *event ;
140   event_flag.sem_op  = -1 ;
141   event_flag.sem_flg = SEM_UNDO;
142   
143   do 
144 #if defined(SEMOP_NO_REFERENCE) && !defined(DECOSF1) 
145     status = semop(*semid,event_flag,1) ;
146 #else 
147     status = semop(*semid,&event_flag,1) ;
148 #endif
149   while ( status < 0 && errno != EINTR );
150   
151   if( status < 0 )
152     return (0) ;
153   else
154     return (1) ;
155 }
156
157
158 int
159 free_semaphore(int *semid, int *event) 
160 /*==================================================
161
162         FREE the semaphore attached to the cluster
163
164 ====================================================*/
165 {
166   int status ;
167   int zero_value = 0;
168   
169   event_flag.sem_num = *event ;
170   event_flag.sem_op  = 1 ;
171   event_flag.sem_flg = IPC_NOWAIT ;
172
173 #ifdef SEMCTL_NO_REFERENCE
174   status = semctl(*semid,*event,GETVAL,zero_value);
175 #else 
176   status = semctl(*semid,*event,GETVAL,&zero_value);
177 #endif
178   if (status !=1) 
179 #if defined(SEMOP_NO_REFERENCE) && !defined(DECOSF1)
180     status = semop(*semid,event_flag,1) ;
181 #else 
182     status = semop(*semid,&event_flag,1) ;
183 #endif
184   else return 0;
185
186    if( status < 0 )
187     return (0) ;
188   else
189     return (1) ;
190 }
191
192
193
194 int
195 reset_semaphore(int *semid, int *event) 
196 /*===================================================
197
198       RESET semaphore to initial state
199
200 ======================================================*/
201 {
202  int status;
203  int zero_value=0;
204
205 #ifdef SEMCTL_NO_REFERENCE
206  status = semctl(*semid,*event,SETVAL,zero_value);
207 #else
208  status = semctl(*semid,*event,SETVAL,&zero_value);
209 #endif
210  if (status < 0) return(0);
211             else return(1);
212
213 }
214
215
216 int
217 set_semaphore(int *semid, int *event, int value) 
218 /*=================================================
219
220       SET semaphore counter to a fixed value
221
222 ====================================================*/
223 {
224  int status;
225
226 #ifdef SEMCTL_NO_REFERENCE
227  status = semctl((*semid),*event,SETVAL,value);
228 #else
229  status = semctl((*semid),*event,SETVAL,&value);
230 #endif
231  if (status < 0) return(0);
232             else return(1);
233
234 }
235
236
237 int
238 get_semaphore(int *semid, int *event, int *value)
239 /*=================================================
240  
241       GET semaphore counter value
242  
243 ==========================================================================*/
244 {
245  int status;
246  
247 #ifdef SEMCTL_NO_REFERENCE
248  status = semctl((*semid),*event,GETVAL,value);
249 #else
250  status = semctl((*semid),*event,GETVAL,&value);
251 #endif
252
253  if (status < 0) return(0);
254             else return(1);
255  
256 }
257
258
259
260 int
261 remove_semaphore(int *semid, char *name) 
262 /*======================================================
263
264           CLOSE semaphore attached to cluster
265
266 =============================================================================*/
267 {
268  int status;
269
270 /*  status = semctl(*semid,IPC_RMID,NULL);*/
271   status = semctl(*semid,IPC_RMID,0);
272   if (status < 0) return 0;
273              else return 1;
274
275 }
276
277
278 int
279 osd_signl_(int *event) 
280 /*====================================================
281
282         ARM the SIGNAL event (Must be 1 or 2 )
283         And WAIT after it was KILLED
284
285 =======================================================*/
286 {
287         signal(SIGUSER + *event ,  osd_signal_) ;
288         pause() ;
289         return (1) ;
290 }
291
292 int
293 osd_skill_(int *pid, int *event) 
294 /*==================================================
295
296         SEND a SIGNAL to the PROCESS ID specified
297
298  ====================================================*/
299 {
300        if( kill( *pid , SIGUSER  + *event ) < 0 ) return (0) ;
301        return (1) ;
302 }
303
304 int
305 osd_alarm_(int sec,RETSIGTYPE (*_alarm)())
306 /*====================================================
307
308         ARM the SIGNAL event (Must be 1 or 2 )
309         And WAIT after it was KILLED
310        
311 ==========================================================================*/
312 {
313   signal(SIGALRM, _alarm) ;
314   alarm(sec) ;
315   return (1) ;
316 }
317
318 #endif /* __hpux  */
319
320 #endif /* WNT  */