add files: spinlock.[hc]
parent
70f6ad56ed
commit
a6dc24e2f3
3
HISTORY
3
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
|
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
||||||
|
* add files: spinlock.[hc]
|
||||||
|
|
||||||
Version 1.77 2025-03-18
|
Version 1.77 2025-03-18
|
||||||
* impl. shorten_path for /./ and /../
|
* impl. shorten_path for /./ and /../
|
||||||
|
|
|
||||||
|
|
@ -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 \
|
multi_socket_client.lo skiplist_set.lo uniq_skiplist.lo \
|
||||||
json_parser.lo buffered_file_writer.lo server_id_func.lo \
|
json_parser.lo buffered_file_writer.lo server_id_func.lo \
|
||||||
fc_queue.lo sorted_queue.lo fc_memory.lo shared_buffer.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 \
|
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
|
||||||
logger.o sockopt.o base64.o sched_thread.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 \
|
multi_socket_client.o skiplist_set.o uniq_skiplist.o \
|
||||||
json_parser.o buffered_file_writer.o server_id_func.o \
|
json_parser.o buffered_file_writer.o server_id_func.o \
|
||||||
fc_queue.o sorted_queue.o fc_memory.o shared_buffer.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 \
|
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 \
|
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 \
|
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 \
|
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 \
|
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)
|
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "common_define.h"
|
||||||
|
#ifdef OS_LINUX
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
#include <linux/futex.h>
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FC_SPINLOCK_H
|
||||||
|
#define FC_SPINLOCK_H
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#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
|
||||||
Loading…
Reference in New Issue