extern "C"
{
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "sem_utils.h"
#include <errno.h>
}
extern int errno;
#if HAVE_SEMUN
#else
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
#endif
#ifndef SEM_R
# define SEM_R 0444
#endif
#ifndef SEM_A
# define SEM_A 0222
#endif
int md4_sem_create(int semnum, unsigned short *start)
{
int semid;
int perms = 0777;
union semun arg;
key_t key = 12345;
int count = 0;
while((semid = semget(key, semnum, IPC_CREAT | IPC_EXCL | perms)) == -1) {
if(count++ > 2) {
return -1;
}
if (errno == EEXIST) {
semid = semget(key, 1, perms);
md4_sem_destroy(semid);
}
}
arg.array = start;
if(semctl(semid, 0, SETALL, arg) < 0) {
md4_sem_destroy(semid);
return -1;
}
return semid;
}
void md4_sem_destroy(int semid)
{
union semun dummy;
semctl(semid, 0, IPC_RMID, dummy);
}
void md4_sem_lock(int semid, int semnum)
{
struct sembuf sops;
sops.sem_num = semnum;
sops.sem_op = -1;
sops.sem_flg = SEM_UNDO;
semop(semid, &sops, 1);
}
void md4_sem_unlock(int semid, int semnum)
{
struct sembuf sops;
sops.sem_num = semnum;
sops.sem_op = 1;
sops.sem_flg = SEM_UNDO;
semop(semid, &sops, 1);
}
void md4_sem_wait_for_zero(int semid, int semnum)
{
struct sembuf sops;
sops.sem_num = semnum;
sops.sem_op = 0;
sops.sem_flg = SEM_UNDO;
semop(semid, &sops, 1);
}
void md4_sem_set(int semid, int semnum, int value)
{
union semun arg;
arg.val = value;
semctl(semid, semnum, SETALL, arg);
}
int md4_sem_get(int semid, int semnum)
{
union semun arg;
return semctl(semid, 0, GETVAL, arg);
}