From a6dc24e2f3e9a319fdfdd0575a10c6f4367c31b4 Mon Sep 17 00:00:00 2001
From: YuQing <384681@qq.com>
Date: Mon, 7 Jul 2025 17:30:43 +0800
Subject: [PATCH] add files: spinlock.[hc]
---
HISTORY | 3 +-
src/Makefile.in | 6 ++--
src/spinlock.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++
src/spinlock.h | 59 +++++++++++++++++++++++++++++++
4 files changed, 158 insertions(+), 4 deletions(-)
create mode 100644 src/spinlock.c
create mode 100644 src/spinlock.h
diff --git a/HISTORY b/HISTORY
index 0d98820..4b2ec62 100644
--- a/HISTORY
+++ b/HISTORY
@@ -1,6 +1,7 @@
-Version 1.78 2025-06-19
+Version 1.78 2025-07-07
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
+ * add files: spinlock.[hc]
Version 1.77 2025-03-18
* impl. shorten_path for /./ and /../
diff --git a/src/Makefile.in b/src/Makefile.in
index 2f84906..2d38f38 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -17,7 +17,7 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \
multi_socket_client.lo skiplist_set.lo uniq_skiplist.lo \
json_parser.lo buffered_file_writer.lo server_id_func.lo \
fc_queue.lo sorted_queue.lo fc_memory.lo shared_buffer.lo \
- thread_pool.lo array_allocator.lo sorted_array.lo
+ thread_pool.lo array_allocator.lo sorted_array.lo spinlock.lo
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
logger.o sockopt.o base64.o sched_thread.o \
@@ -31,7 +31,7 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
multi_socket_client.o skiplist_set.o uniq_skiplist.o \
json_parser.o buffered_file_writer.o server_id_func.o \
fc_queue.o sorted_queue.o fc_memory.o shared_buffer.o \
- thread_pool.o array_allocator.o sorted_array.o
+ thread_pool.o array_allocator.o sorted_array.o spinlock.o
HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
shared_func.h pthread_func.h ini_file_reader.h _os_define.h \
@@ -47,7 +47,7 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
fc_list.h locked_list.h json_parser.h buffered_file_writer.h \
server_id_func.h fc_queue.h sorted_queue.h fc_memory.h \
shared_buffer.h thread_pool.h fc_atomic.h array_allocator.h \
- sorted_array.h
+ sorted_array.h spinlock.h
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)
diff --git a/src/spinlock.c b/src/spinlock.c
new file mode 100644
index 0000000..1f4e855
--- /dev/null
+++ b/src/spinlock.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2025 YuQing <384681@qq.com>
+ *
+ * This program is free software: you can use, redistribute, and/or modify
+ * it under the terms of the Lesser GNU General Public License, version 3
+ * or later ("LGPL"), as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include "common_define.h"
+#ifdef OS_LINUX
+#include
+#include
+#define FUTEX(ptr, op, val) syscall(SYS_futex, ptr, op, val, NULL, NULL, 0)
+#else
+#include "pthread_func.h"
+#endif
+
+#include "logger.h"
+#include "spinlock.h"
+
+int fc_spinlock_init(FCSpinlock *lock, int *cond)
+{
+#ifdef OS_LINUX
+ lock->mutex = 0;
+ lock->cond = cond;
+ return 0;
+#else
+ return init_pthread_lock_cond_pair(&lock->lcp);
+#endif
+}
+
+void fc_spinlock_destroy(FCSpinlock *lock)
+{
+#ifdef OS_LINUX
+#else
+ destroy_pthread_lock_cond_pair(&lock->lcp);
+#endif
+}
+
+int fc_spinlock_lock(FCSpinlock *lock)
+{
+#ifdef OS_LINUX
+ return FUTEX(&lock->mutex, FUTEX_LOCK_PI_PRIVATE, 0);
+#else
+ return pthread_mutex_lock(&lock->lcp.lock);
+#endif
+}
+
+int fc_spinlock_trylock(FCSpinlock *lock)
+{
+#ifdef OS_LINUX
+ return FUTEX(&lock->mutex, FUTEX_TRYLOCK_PI_PRIVATE, 0);
+#else
+ return pthread_mutex_trylock(&lock->lcp.lock);
+#endif
+}
+
+int fc_spinlock_unlock(FCSpinlock *lock)
+{
+#ifdef OS_LINUX
+ return FUTEX(&lock->mutex, FUTEX_UNLOCK_PI_PRIVATE, 0);
+#else
+ return pthread_mutex_unlock(&lock->lcp.lock);
+#endif
+}
+
+int fc_spinlock_wait(FCSpinlock *lock, const int expected)
+{
+#ifdef OS_LINUX
+ return FUTEX(lock->cond, FUTEX_WAIT_PRIVATE, expected);
+#else
+ return pthread_cond_wait(&lock->lcp.cond, &lock->lcp.lock);
+#endif
+}
+
+int fc_spinlock_wake_ex(FCSpinlock *lock, const int count)
+{
+#ifdef OS_LINUX
+ return FUTEX(lock->cond, FUTEX_WAKE_PRIVATE, count);
+#else
+ if (count == 1) {
+ return pthread_cond_signal(&lock->lcp.cond);
+ } else {
+ return pthread_cond_broadcast(&lock->lcp.cond);
+ }
+#endif
+}
diff --git a/src/spinlock.h b/src/spinlock.h
new file mode 100644
index 0000000..9790a51
--- /dev/null
+++ b/src/spinlock.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2025 YuQing <384681@qq.com>
+ *
+ * This program is free software: you can use, redistribute, and/or modify
+ * it under the terms of the Lesser GNU General Public License, version 3
+ * or later ("LGPL"), as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#ifndef FC_SPINLOCK_H
+#define FC_SPINLOCK_H
+
+#include
+#include "common_define.h"
+
+typedef struct fc_spinlock_t {
+#ifdef OS_LINUX
+ int mutex;
+ int *cond;
+#else
+ pthread_lock_cond_pair_t lcp;
+#endif
+} FCSpinlock;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int fc_spinlock_init(FCSpinlock *lock, int *cond);
+
+void fc_spinlock_destroy(FCSpinlock *lock);
+
+int fc_spinlock_lock(FCSpinlock *lock);
+
+int fc_spinlock_trylock(FCSpinlock *lock);
+
+int fc_spinlock_unlock(FCSpinlock *lock);
+
+int fc_spinlock_wait(FCSpinlock *lock, const int expected);
+
+int fc_spinlock_wake_ex(FCSpinlock *lock, const int count);
+
+static inline int fc_spinlock_wake(FCSpinlock *lock)
+{
+ const int count = 1;
+ return fc_spinlock_wake_ex(lock, count);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif