summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_Csemaphore.c
blob: da82104d71a03adc613c0c7c88cb2306713db14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef WNT

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#include <errno.h>
#include <stdio.h>

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h>
#endif

#ifdef HAVE_SYS_SEM_H
# include <sys/sem.h>
#endif

#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

/* Modified by Stephan GARNAUD (ARM) 1992 for Matra Datavision */

#if (!defined (__hpux)) && (!defined (HPUX))

#define CLUSTER_NUMBER 4 
#define SIGUSER SIGUSR1-1

static struct sembuf event_flag ;
	 
static void osd_signal_(int); 
int osd_getkey(char *);
#ifdef ANSI
int create_semaphore(char *);
int open_semaphore(char *);
int lock_semaphore(int *,int *);
int free_semaphore(int *,int *);
int reset_semaphore(int *,int *);
int remove_semaphore(int *,char *);
int osd_signl_(int *);
int osd_skill_(int *,int *);
int osd_alarm_(int,void (*)());

#endif

static void
osd_signal_(int i )
{
}


int
create_semaphore(char *name) 
/*=======================================================

	CREATE a semaphore cluster of 32 events

=========================================================*/
{
int status ;



    key_t key;


        key = (key_t) osd_getkey (name);


	status = semget(key,CLUSTER_NUMBER,0750 + IPC_CREAT) ;

	if( status < 0 ) return (0) ;  /* semget failed */
        return status ;
}


int
open_semaphore(char *name)
/*=========================================================

	OPEN a semaphore cluster 

=========================================================================*/
{
int status ;


    key_t key;


        key = (key_t) osd_getkey (name);
	status = semget(key,0,0) ;

	if( status < 0 ) return (0) ;  /* semget failed */
         else   return status;
}



int
lock_semaphore(int *semid, int *event) 
/*==================================================

	WAIT & LOCK the semaphore attached to the cluster

====================================================*/
{
  int status ;

  event_flag.sem_num = *event ;
  event_flag.sem_op  = -1 ;
  event_flag.sem_flg = SEM_UNDO;
  
  do 
#if defined(SEMOP_NO_REFERENCE) && !defined(DECOSF1) 
    status = semop(*semid,event_flag,1) ;
#else 
    status = semop(*semid,&event_flag,1) ;
#endif
  while ( status < 0 && errno != EINTR );
  
  if( status < 0 )
    return (0) ;
  else
    return (1) ;
}


int
free_semaphore(int *semid, int *event) 
/*==================================================

	FREE the semaphore attached to the cluster

====================================================*/
{
  int status ;
  int zero_value = 0;
  
  event_flag.sem_num = *event ;
  event_flag.sem_op  = 1 ;
  event_flag.sem_flg = IPC_NOWAIT ;

#ifdef SEMCTL_NO_REFERENCE
  status = semctl(*semid,*event,GETVAL,zero_value);
#else 
  status = semctl(*semid,*event,GETVAL,&zero_value);
#endif
  if (status !=1) 
#if defined(SEMOP_NO_REFERENCE) && !defined(DECOSF1)
    status = semop(*semid,event_flag,1) ;
#else 
    status = semop(*semid,&event_flag,1) ;
#endif
  else return 0;

   if( status < 0 )
    return (0) ;
  else
    return (1) ;
}



int
reset_semaphore(int *semid, int *event) 
/*===================================================

      RESET semaphore to initial state

======================================================*/
{
 int status;
 int zero_value=0;

#ifdef SEMCTL_NO_REFERENCE
 status = semctl(*semid,*event,SETVAL,zero_value);
#else
 status = semctl(*semid,*event,SETVAL,&zero_value);
#endif
 if (status < 0) return(0);
            else return(1);

}


int
set_semaphore(int *semid, int *event, int value) 
/*=================================================

      SET semaphore counter to a fixed value

====================================================*/
{
 int status;

#ifdef SEMCTL_NO_REFERENCE
 status = semctl((*semid),*event,SETVAL,value);
#else
 status = semctl((*semid),*event,SETVAL,&value);
#endif
 if (status < 0) return(0);
            else return(1);

}


int
get_semaphore(int *semid, int *event, int *value)
/*=================================================
 
      GET semaphore counter value
 
==========================================================================*/
{
 int status;
 
#ifdef SEMCTL_NO_REFERENCE
 status = semctl((*semid),*event,GETVAL,value);
#else
 status = semctl((*semid),*event,GETVAL,&value);
#endif

 if (status < 0) return(0);
            else return(1);
 
}



int
remove_semaphore(int *semid, char *name) 
/*======================================================

          CLOSE semaphore attached to cluster

=============================================================================*/
{
 int status;

/*  status = semctl(*semid,IPC_RMID,NULL);*/
  status = semctl(*semid,IPC_RMID,0);
  if (status < 0) return 0;
             else return 1;

}


int
osd_signl_(int *event) 
/*====================================================

	ARM the SIGNAL event (Must be 1 or 2 )
	And WAIT after it was KILLED

=======================================================*/
{
	signal(SIGUSER + *event ,  osd_signal_) ;
	pause() ;
	return (1) ;
}

int
osd_skill_(int *pid, int *event) 
/*==================================================

	SEND a SIGNAL to the PROCESS ID specified

 ====================================================*/
{
       if( kill( *pid , SIGUSER  + *event ) < 0 ) return (0) ;
       return (1) ;
}

int
osd_alarm_(int sec, void (*_alarm)())
/*====================================================

	ARM the SIGNAL event (Must be 1 or 2 )
	And WAIT after it was KILLED
       
==========================================================================*/
{
  signal(SIGALRM, _alarm) ;
  alarm(sec) ;
  return (1) ;
}

#endif /* __hpux  */

#endif /* WNT  */