/* * 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. */ #include "pg_config.h" IDENT("@(#) semaphor.c (Gunther Schadow) 12/19/96"); #include "semaphor.h" #include #include 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; } result _sem_free(semaphor mutex) { if(mutex != NULL) { result res = semctl(mutex->id, 0, IPC_RMID, 0); free(mutex); return res; } else return SUCCESS; } result _sem_op(semaphor mutex, short op) { mutex->buf.sem_op = op; return semop(mutex->id, &mutex->buf, 1); }