add atomic refcnt and locks.

It might be unnecessary, if open/close can be done without lock.
This commit is contained in:
Ryo Nakamura
2022-10-20 20:25:23 +09:00
parent 808069ab9d
commit ab0bf7e5dc
4 changed files with 131 additions and 13 deletions

58
src/atomic.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef _ATOMIC_H_
#define _ATOMIC_H_
#include <stdlib.h>
#include <pthread.h>
#include <util.h>
typedef int refcnt;
static inline void refcnt_inc(refcnt *cnt)
{
__sync_add_and_fetch(cnt, 1);
}
static inline void refcnt_dec(refcnt *cnt)
{
__sync_sub_and_fetch(cnt, 1);
}
typedef pthread_mutex_t lock;
static inline void lock_init(lock *l)
{
pthread_mutex_init(l, NULL);
}
static inline void lock_acquire(lock *l)
{
int ret = pthread_mutex_lock(l);
if (ret < 0) {
switch (ret) {
case EINVAL:
pr_err("invalid mutex\n");
exit(1);
case EDEADLK:
pr_err("a deadlock would occur\n");
exit(1);
}
}
}
static inline void lock_release(lock *l)
{
int ret = pthread_mutex_unlock(l);
if (ret < 0) {
switch (ret) {
case EINVAL:
pr_err("invalid mutex\n");
exit(1);
case EPERM:
pr_err("this thread does not hold this mutex\n");
exit(1);
}
}
}
#endif /* _ATOMIC_H_ */