void fio_sem_remove(struct fio_sem *sem)
{
- unlink(sem->sem_name);
+ close(sem->sem_fd);
munmap(sem, sizeof(*sem));
}
struct fio_sem *fio_sem_init(int value)
{
+ char sem_name[] = "/tmp/.fio_sem.XXXXXX";
+ struct fio_sem *sem = NULL;
pthread_mutexattr_t attr;
- struct fio_sem *sem;
- char sem_name[32];
int fd;
- sprintf(sem_name, "/tmp/.fio_lock.XXXXXX");
fd = mkstemp(sem_name);
if (fd < 0) {
perror("open sem");
if (ftruncate(fd, sizeof(struct fio_sem)) < 0) {
perror("ftruncate sem");
- return NULL;
+ goto err;
}
sem = mmap(NULL, sizeof(struct fio_sem), PROT_READ | PROT_WRITE,
if (sem == MAP_FAILED) {
perror("mmap sem");
close(fd);
- unlink(sem_name);
- return NULL;
+ sem = NULL;
+ goto err;
}
- close(fd);
+ unlink(sem_name);
+ sem->sem_fd = fd;
sem->value = value;
- strcpy(sem->sem_name, sem_name);
if (pthread_mutexattr_init(&attr)) {
perror("pthread_mutexattr_init");
return sem;
err:
- munmap(sem, sizeof(*sem));
+ if (sem)
+ fio_sem_remove(sem);
+
unlink(sem_name);
return NULL;
}