1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
7 * Purpose: Handle semaphores for shared memory operations.
8 ***************************************************************************
12 #define inline __inline__
18 #include "shm_semaph.h"
22 /* IMPORTANT: Make sure that killed process will not lock everything.
23 * If possible, restrict usage of these functions.
25 void shm_read_wait(shm_sem semid)
30 TRACE(sem,"(%d)\n",semid);
31 sop[0].sem_num=SEM_READ;
32 sop[0].sem_op=1; /* add this read instance */
33 sop[0].sem_flg=SEM_UNDO; /* undo in case process dies */
35 sop[1].sem_num=SEM_WRITE;
36 sop[1].sem_op=0; /* wait until no writing instance exists */
37 sop[1].sem_flg=SEM_UNDO;
40 ret=semop (semid,sop , 2);
41 } while (ret<0 && errno==EINTR); /* interrupted system call? */
44 WARN(sem,"(semid=%d,errno=%d): Failed semaphore lock for read\n",
47 void shm_write_wait(shm_sem semid)
52 TRACE(sem,"(%d)\n",semid);
53 sop[0].sem_num=SEM_READ;
54 sop[0].sem_op=0; /* wait until no reading instance exist */
55 sop[0].sem_flg=SEM_UNDO;
57 sop[1].sem_num=SEM_WRITE;
58 sop[1].sem_op=1; /* writing is in progress - disable read */
59 sop[1].sem_flg=SEM_UNDO; /* undo if process dies */
61 sop[2].sem_num=SEM_READ;
62 sop[2].sem_op=1; /* disable new writes */
63 sop[2].sem_flg=SEM_UNDO;
66 ret=semop (semid,sop , 3);
67 } while (ret<0 && errno==EINTR); /* interrupted system call? */
69 if (ret<0) /* test for the error */
70 WARN(sem,"(semid=%d,errno=%d): Failed semaphore lock for write\n",
73 void shm_write_signal(shm_sem semid)
78 TRACE(sem,"(%d)\n",semid);
79 sop[0].sem_num=SEM_READ;
81 sop[0].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
83 sop[1].sem_num=SEM_WRITE;
85 sop[1].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
88 ret=semop (semid,sop , 2);
89 } while (ret<0 && errno==EINTR); /* interrupted system call? */
91 if (ret<0) /* test for the error */
92 WARN(sem,"(semid=%d,errno=%d): Failed semaphore unlock for write\n",
96 void shm_read_signal(shm_sem semid)
101 TRACE(sem,"(%d)\n",semid);
102 sop[0].sem_num=SEM_READ;
104 sop[0].sem_flg=IPC_NOWAIT | SEM_UNDO; /* no reason to wait */
107 ret=semop (semid,sop , 1);
108 } while (ret<0 && errno==EINTR); /* interrupted system call? */
110 if (ret<0) /* test for the error */
111 WARN(sem,"(semid=%d,errno=%d): Failed semaphore unlock for read\n",
115 void shm_sem_init(shm_sem *sptr)
120 semid=semget (IPC_PRIVATE, 2, 0700 | IPC_CREAT);
123 semctl (semid, 0, SETVAL, arg);
124 semctl (semid, 1, SETVAL, arg);
128 void shm_sem_done(shm_sem *semptr)
132 semctl (*semptr, 0, IPC_RMID , arg);
133 semctl (*semptr, 1, IPC_RMID , arg);
138 #endif /* CONFIG_IPC */