/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SEMAPHOR_H_ #define SEMAPHOR_H_ /* * Interface to the sysv semaphore facility for the simple case of one * binary semaphore. Essentially provides the V(mutex) and P(mutex) * operations. */ #include #ifdef sun #include #include #endif #include #ifdef sun extern int semget(key_t key, int nsems, int semflg); extern int semctl(int semid, int semnum, int cmd, union semun arg); extern int semop(int semid, struct sembuf *sops, int nsops); #endif struct semaphor_s { int id; struct sembuf buf; }; typedef struct semaphor_s *semaphor; #ifdef __GNUC__ static __inline__ semaphor SEM_ALLOC(int flag) { semaphor mutex = (semaphor)malloc(sizeof(struct semaphor_s)); if(mutex == NULL) return NULL; mutex->id = semget(IPC_PRIVATE, 1, IPC_CREAT|0600); if(mutex->id == FAIL) return NULL; mutex->buf.sem_num = 0; mutex->buf.sem_flg = flag; if(semctl(mutex->id, 0, SETVAL, 1) == FAIL) return NULL; return mutex; } static __inline__ result SEM_FREE(semaphor mutex) { if(mutex != NULL) { result res = semctl(mutex->id, 0, IPC_RMID, 0); free(mutex); return res; } else return SUCCESS; } static __inline__ result SEM_OP(semaphor mutex, short op) { mutex->buf.sem_op = op; return semop(mutex->id, &mutex->buf, 1); } #else /* ! __GNUC__ */ semaphor _sem_alloc(int flag); result _sem_free(semaphor mutex); result _sem_op(semaphor mutex, short op); # define SEM_ALLOC _sem_alloc # define SEM_FREE _sem_free # define SEM_OP _sem_op #endif #define SEM_MODE(mutex, mode) ((mutex)->buf.sem_flg=mode) #define SEMOP_P -1 #define SEMOP_V +1 #define PROBEREN(mutex) SEM_OP(mutex, SEMOP_P) #define VERHOGEN(mutex) SEM_OP(mutex, SEMOP_V) #endif /* SEMAPHOR_H_ */