Revert "smalloc: convert to spinlocks"
[fio.git] / spinlock.c
CommitLineData
8b4a7c86
JA
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
6#include <pthread.h>
7#include <sys/mman.h>
8
9#include "arch/arch.h"
10#include "spinlock.h"
11
12void fio_spinlock_remove(struct fio_spinlock *lock)
13{
14 close(lock->lock_fd);
15 munmap((void *) lock, sizeof(*lock));
16}
17
18struct fio_spinlock *fio_spinlock_init(void)
19{
20 char spinlock_name[] = "/tmp/.fio_spinlock.XXXXXX";
21 struct fio_spinlock *lock = NULL;
22 int fd;
23
24 fd = mkstemp(spinlock_name);
25 if (fd < 0) {
26 perror("open spinlock");
27 return NULL;
28 }
29
30 if (ftruncate(fd, sizeof(struct fio_spinlock)) < 0) {
31 perror("ftruncate spinlock");
32 goto err;
33 }
34
35 lock = (void *) mmap(NULL, sizeof(struct fio_spinlock),
36 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
37 if (lock == MAP_FAILED) {
38 perror("mmap spinlock");
39 close(fd);
40 lock = NULL;
41 goto err;
42 }
43
44 unlink(spinlock_name);
45 lock->lock_fd = fd;
46 spin_lock_init(&lock->slock);
47
48 return lock;
49err:
50 if (lock)
51 fio_spinlock_remove(lock);
52
53 unlink(spinlock_name);
54 return NULL;
55}