Got rid of THREAD_InitDone.
[wine/multimedia.git] / ipc / shm_semaph.c
blobf69319f5668a217c71ec6d57c762201aa11642dc
1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
6 * File: shm_semaph.c
7 * Purpose: Handle semaphores for shared memory operations.
8 ***************************************************************************
9 */
10 #ifdef CONFIG_IPC
12 #include <assert.h>
13 #include <unistd.h>
14 #include <sys/sem.h>
15 #include <errno.h>
16 #include "debugtools.h"
17 #include "shm_semaph.h"
19 DEFAULT_DEBUG_CHANNEL(sem)
20 #define SEM_READ 0
21 #define SEM_WRITE 1
23 /* IMPORTANT: Make sure that killed process will not lock everything.
24 * If possible, restrict usage of these functions.
26 void shm_read_wait(shm_sem semid)
28 struct sembuf sop[2];
29 int ret;
31 TRACE("(%d)\n",semid);
32 sop[0].sem_num=SEM_READ;
33 sop[0].sem_op=1; /* add this read instance */
34 sop[0].sem_flg=SEM_UNDO; /* undo in case process dies */
36 sop[1].sem_num=SEM_WRITE;
37 sop[1].sem_op=0; /* wait until no writing instance exists */
38 sop[1].sem_flg=SEM_UNDO;
40 do {
41 ret=semop (semid,sop , 2);
42 } while (ret<0 && errno==EINTR); /* interrupted system call? */
44 if (ret<0)
45 WARN("(semid=%d,errno=%d): Failed semaphore lock for read\n",
46 semid, errno);
48 void shm_write_wait(shm_sem semid)
50 struct sembuf sop[3];
51 int ret;
53 TRACE("(%d)\n",semid);
54 sop[0].sem_num=SEM_READ;
55 sop[0].sem_op=0; /* wait until no reading instance exist */
56 sop[0].sem_flg=SEM_UNDO;
58 sop[1].sem_num=SEM_WRITE;
59 sop[1].sem_op=1; /* writing is in progress - disable read */
60 sop[1].sem_flg=SEM_UNDO; /* undo if process dies */
62 sop[2].sem_num=SEM_READ;
63 sop[2].sem_op=1; /* disable new writes */
64 sop[2].sem_flg=SEM_UNDO;
66 do {
67 ret=semop (semid,sop , 3);
68 } while (ret<0 && errno==EINTR); /* interrupted system call? */
70 if (ret<0) /* test for the error */
71 WARN("(semid=%d,errno=%d): Failed semaphore lock for write\n",
72 semid, errno);
74 void shm_write_signal(shm_sem semid)
76 struct sembuf sop[2];
77 int ret;
79 TRACE("(%d)\n",semid);
80 sop[0].sem_num=SEM_READ;
81 sop[0].sem_op=-1;
82 sop[0].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
84 sop[1].sem_num=SEM_WRITE;
85 sop[1].sem_op=-1;
86 sop[1].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
88 do {
89 ret=semop (semid,sop , 2);
90 } while (ret<0 && errno==EINTR); /* interrupted system call? */
92 if (ret<0) /* test for the error */
93 WARN("(semid=%d,errno=%d): Failed semaphore unlock for write\n",
94 semid, errno);
97 void shm_read_signal(shm_sem semid)
99 struct sembuf sop[2];
100 int ret;
102 TRACE("(%d)\n",semid);
103 sop[0].sem_num=SEM_READ;
104 sop[0].sem_op=-1;
105 sop[0].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
107 do {
108 ret=semop (semid,sop , 1);
109 } while (ret<0 && errno==EINTR); /* interrupted system call? */
111 if (ret<0) /* test for the error */
112 WARN("(semid=%d,errno=%d): Failed semaphore unlock for read\n",
113 semid, errno);
116 void shm_sem_init(shm_sem *sptr)
118 shm_sem semid;
119 union semun arg;
121 semid=semget (IPC_PRIVATE, 2, 0700 | IPC_CREAT);
123 arg.val=0;
124 semctl (semid, 0, SETVAL, arg);
125 semctl (semid, 1, SETVAL, arg);
126 *sptr=semid;
129 void shm_sem_done(shm_sem *semptr)
131 union semun arg;
133 semctl (*semptr, 0, IPC_RMID , arg);
134 semctl (*semptr, 1, IPC_RMID , arg);
136 *semptr= -1;
139 #endif /* CONFIG_IPC */