split up constants.h some
[trinity.git] / locks.c
blobb51e53fee968f8faf0e5347207dba8652dda5276
1 #include <signal.h>
2 #include <unistd.h>
3 #include "locks.h"
4 #include "log.h"
5 #include "pids.h"
7 #define STEAL_THRESHOLD 100000
9 void lock(lock_t *_lock)
11 while (_lock->lock == LOCKED) {
12 _lock->contention++;
13 usleep(1);
14 if (_lock->contention > STEAL_THRESHOLD) {
15 pid_t pid = _lock->owner;
17 if (pid_alive(pid) == FALSE) {
18 output(0, "[%d] more than %d attempts to get lock. pid %d looks dead, stealing.\n",
19 getpid(), STEAL_THRESHOLD, pid);
20 goto steal;
25 steal:
26 _lock->contention = 0;
27 _lock->lock = LOCKED;
28 _lock->owner = getpid();
31 void unlock(lock_t *_lock)
33 asm volatile("" ::: "memory");
34 _lock->contention = 0;
35 _lock->lock = UNLOCKED;
36 _lock->owner = 0;