summaryrefslogtreecommitdiff
path: root/src/atomic.h
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2023-05-07 21:05:05 +0900
committerRyo Nakamura <upa@haeena.net>2023-05-07 21:05:05 +0900
commit24e86f58d87d48864e6ae33f1953124e467753ea (patch)
tree5bd65f8dd40209b6aaa08ca22c5224c059a22495 /src/atomic.h
parent1d3b3a2261153126832bab4276677cab6e262ed2 (diff)
mscp: maintain mscp_thread structs in list
Instead of m->threads array, struct mscp_thread instanes are maintained in m->thread_list. This enables stable counter access via mscp_get_stats().
Diffstat (limited to 'src/atomic.h')
-rw-r--r--src/atomic.h56
1 files changed, 52 insertions, 4 deletions
diff --git a/src/atomic.h b/src/atomic.h
index 87ba20d..09f9f57 100644
--- a/src/atomic.h
+++ b/src/atomic.h
@@ -20,6 +20,8 @@ static inline refcnt refcnt_dec(refcnt *cnt)
}
+/* mutex */
+
typedef pthread_mutex_t lock;
static inline void lock_init(lock *l)
@@ -44,12 +46,58 @@ static inline void lock_release_via_cleanup(void *l)
lock_release(l);
}
-#define LOCK_ACQUIRE_THREAD(l) \
- lock_acquire(l); \
- pthread_cleanup_push(lock_release_via_cleanup, l)
+#define LOCK_ACQUIRE(l) \
+ lock_acquire(l); \
+ pthread_cleanup_push(lock_release_via_cleanup, l)
+
+#define LOCK_RELEASE() \
+ pthread_cleanup_pop(1)
+
+
+
+/* read/write lock */
+typedef pthread_rwlock_t rwlock;
+
+static inline void rwlock_init(rwlock *rw)
+{
+ pthread_rwlock_init(rw, NULL);
+}
+
+static inline void rwlock_read_acquire(rwlock *rw)
+{
+ int ret = pthread_rwlock_rdlock(rw);
+ assert(ret == 0);
+}
+
+static inline void rwlock_write_acquire(rwlock *rw)
+{
+ int ret = pthread_rwlock_wrlock(rw);
+ assert(ret == 0);
+}
+
+static inline void rwlock_release(rwlock *rw)
+{
+ int ret = pthread_rwlock_unlock(rw);
+ assert(ret == 0);
+}
+static inline void rwlock_release_via_cleanup(void *rw)
+{
+ rwlock_release(rw);
+}
+
+#define RWLOCK_READ_ACQUIRE(rw) \
+ rwlock_read_acquire(rw); \
+ pthread_cleanup_push(rwlock_release_via_cleanup, rw)
+
+#define RWLOCK_WRITE_ACQUIRE(rw) \
+ rwlock_write_acquire(rw); \
+ pthread_cleanup_push(rwlock_release_via_cleanup, rw)
-#define LOCK_RELEASE_THREAD() \
+
+#define RWLOCK_RELEASE() \
pthread_cleanup_pop(1)
+
+
#endif /* _ATOMIC_H_ */