Compare commits

...

12 Commits

Author SHA1 Message Date
YuQing cb014e58e7 test_pthread_lock.c changed 2026-03-13 15:06:01 +08:00
YuQing 82f66af3e2 base64_decode check dummies count for safety 2026-01-22 09:11:23 +08:00
YuQing 128e04b2ec upgrade version to 1.0.84 2026-01-16 14:45:48 +08:00
YuQing 233b644ce5 add function double2str and double_to_comma_str 2026-01-16 09:31:12 +08:00
YuQing e009d750a5 fast_task_queue.h: add function free_queue_task_arg_offset 2025-12-23 11:52:42 +08:00
vazmin 795b328bd6 gh actions: upgrade to 1.0.83-1 2025-11-23 10:47:37 +00:00
vazmin ac5f4a584f gh actions: upgrade to 1.0.83-1 2025-11-23 10:00:00 +00:00
vazmin c044906e63 gh actions: upgrade to 1.0.83-1 2025-11-23 09:05:57 +00:00
YuQing ccc84945d3 INSTALL changed 2025-11-18 14:47:17 +08:00
YuQing c4b6f1fcb5 upgrade version to 1.0.83 2025-11-16 17:00:25 +08:00
YuQing 017ca1efe7 comment out useless codes 2025-11-16 16:54:16 +08:00
YuQing 71b2229427 fast_task_queue.h: remove field finish_callback 2025-11-15 11:13:15 +08:00
11 changed files with 160 additions and 49 deletions

View File

@ -1,4 +1,11 @@
Version 1.84 2026-01-16
* fast_task_queue.h: add function free_queue_task_arg_offset
* add function double2str and double_to_comma_str
Version 1.83 2025-11-15
* fast_task_queue.h: remove field finish_callback
Version 1.82 2025-11-04
* set use_io_uring explicitly

View File

@ -21,5 +21,5 @@ Debian, Ubuntu etc.:
# the command lines as:
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon; git checkout V1.0.82
cd libfastcommon; git checkout V1.0.84
./make.sh clean && ./make.sh && sudo ./make.sh install

18
debian/changelog vendored
View File

@ -1,3 +1,21 @@
libfastcommon (1.0.83-1) unstable; urgency=medium
* upgrade to 1.0.83-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:47:37 +0000
libfastcommon (1.0.83-1) unstable; urgency=medium
* upgrade to 1.0.83-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:00:00 +0000
libfastcommon (1.0.83-1) unstable; urgency=medium
* upgrade to 1.0.83-1
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 09:05:57 +0000
libfastcommon (1.0.78-1) unstable; urgency=medium
* upgrade to 1.0.78-1

View File

@ -3,7 +3,7 @@
%define CommitVersion %(echo $COMMIT_VERSION)
Name: libfastcommon
Version: 1.0.82
Version: 1.0.84
Release: 1%{?dist}
Summary: c common functions library extracted from my open source projects FastDFS
License: LGPL

View File

@ -297,9 +297,7 @@ char *base64_decode_auto(struct base64_context *context, const char *src, \
memcpy(pBuff, src, nSrcLen);
memset(pBuff + nSrcLen, context->pad_ch, nPadLen);
base64_decode(context, pBuff, nNewLen, dest, dest_len);
if (pBuff != tmpBuff)
{
free(pBuff);
@ -385,16 +383,23 @@ char *base64_decode(struct base64_context *context, const char *src, \
if (cycle != 0)
{
*dest = '\0';
*dest_len = 0;
fprintf(stderr, "Input to decode not an even multiple of " \
"4 characters; pad with %c\n", context->pad_ch);
return dest;
*dest = '\0';
*dest_len = 0;
fprintf(stderr, "Input to decode not an even multiple of "
"4 characters; pad with %c\n", context->pad_ch);
return dest;
}
if (dummies > 2)
{
*dest = '\0';
*dest_len = 0;
fprintf(stderr, "pad char: %c count %d exceeds 2\n",
context->pad_ch, dummies);
return dest;
}
*dest_len = (pDest - dest) - dummies;
*(dest + (*dest_len)) = '\0';
return dest;
}

View File

@ -28,7 +28,7 @@
static int task_alloc_init(struct fast_task_info *task,
struct fast_task_queue *queue)
{
task->arg = (char *)task + ALIGNED_TASK_INFO_SIZE + queue->padding_size;
task->arg = (char *)task + free_queue_task_arg_offset(queue);
task->send.ptr = &task->send.holder;
task->send.ptr->size = queue->min_buff_size;
if (queue->malloc_whole_block) {

View File

@ -37,7 +37,6 @@ struct nio_thread_data;
struct fast_task_info;
typedef int (*ThreadLoopCallback) (struct nio_thread_data *pThreadData);
typedef int (*TaskFinishCallback) (struct fast_task_info *task);
typedef void (*TaskCleanUpCallback) (struct fast_task_info *task);
typedef int (*TaskInitCallback)(struct fast_task_info *task, void *arg);
typedef void (*TaskReleaseCallback)(struct fast_task_info *task);
@ -149,7 +148,6 @@ struct fast_task_info
struct fc_list_head dlink; //for polling queue
} polling; //for RDMA busy polling
TaskContinueCallback continue_callback; //for continue stage
TaskFinishCallback finish_callback;
struct nio_thread_data *thread_data;
struct sf_network_handler *handler; //network handler for libserverframe nio
struct fast_task_info *next; //for free queue and deleted list
@ -238,6 +236,11 @@ static inline int free_queue_alloc_connections(struct fast_task_queue *queue)
return queue->allocator.info.element_total_count;
}
static inline int free_queue_task_arg_offset(struct fast_task_queue *queue)
{
return ALIGNED_TASK_INFO_SIZE + queue->padding_size;
}
int free_queue_get_new_buffer_size(const int min_buff_size,
const int max_buff_size, const int expect_size, int *new_size);

View File

@ -145,10 +145,7 @@ static void deal_timeouts(FastTimerEntry *head)
/* must set NULL because NOT in time wheel */
current->prev = current->next = NULL;
pEventEntry = (IOEventEntry *)current;
if (pEventEntry != NULL)
{
pEventEntry->callback(pEventEntry->fd, IOEVENT_TIMEOUT, current);
}
pEventEntry->callback(pEventEntry->fd, IOEVENT_TIMEOUT, current);
}
}
@ -243,7 +240,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
}
if (thread_data->deleted_list != NULL) {
count = 0;
//count = 0;
while (thread_data->deleted_list != NULL) {
task = thread_data->deleted_list;
thread_data->deleted_list = task->next;
@ -257,7 +254,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
}
}
clean_up_callback(task);
count++;
//count++;
}
//logInfo("cleanup task count: %d", count);
}

View File

@ -3178,7 +3178,7 @@ key_t fc_ftok(const char *path, const int proj_id)
return (((proj_id & 0xFF) << 24) | (hash_code & 0xFFFFFF));
}
static void add_thousands_separator(char *str, const int len)
static int add_thousands_separator(char *str, const int len)
{
int new_len;
int addings;
@ -3191,7 +3191,7 @@ static void add_thousands_separator(char *str, const int len)
if (len <= 3)
{
return;
return len;
}
if (*str == '-')
@ -3227,6 +3227,8 @@ static void add_thousands_separator(char *str, const int len)
add_count++;
}
}
return new_len;
}
const char *int2str(const int n, char *buff, const bool thousands_separator)
@ -4523,3 +4525,33 @@ int fc_compare_int64_ptr(const int64_t *n1, const int64_t *n2)
{
return fc_compare_int64(*n1, *n2);
}
const char *double2str(const double d, const int scale,
char *buff, const bool thousands_separator)
{
int len;
int front_len;
int tail_len;
int new_len;
char *point;
char fragment[32];
len = fc_ftoa(d, scale, buff);
*(buff + len) = '\0';
if (!thousands_separator) {
return buff;
}
if (scale <= 0) {
add_thousands_separator(buff, len);
return buff;
}
tail_len = 1 + scale;
front_len = len - tail_len;
point = buff + front_len;
memcpy(fragment, point, tail_len + 1);
new_len = add_thousands_separator(buff, front_len);
memcpy(buff + new_len, fragment, tail_len + 1);
return buff;
}

View File

@ -1191,6 +1191,23 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff)
return long2str(n, buff, true);
}
/** convert double to string
* parameters:
* d: the double number
* scale: number of decimal places (round off)
* buff: output buffer
* thousands_separator: if add thousands separator
* return: string buffer
*/
const char *double2str(const double d, const int scale,
char *buff, const bool thousands_separator);
static inline const char *double_to_comma_str(const double d,
const int scale, char *buff)
{
return double2str(d, scale, buff, true);
}
const char *bytes_to_human_str(const int64_t bytes, char *buff);
/** if the string starts with the needle string

View File

@ -28,16 +28,68 @@
#include "fastcommon/fast_allocator.h"
#define LOOP_COUNT (30 * 1000 * 1000)
#define THREAD_COUNT 2
#define barrier() __asm__ __volatile__("" ::: "memory")
static volatile int64_t sum;
static pthread_mutex_t lock;
static void *atomic_thread_func(void *arg)
{
int k;
for (k=1; k<=LOOP_COUNT; k++) {
//__sync_synchronize();
//barrier();
__sync_add_and_fetch(&sum, k);
}
return NULL;
}
static void *mutex_thread_func(void *arg)
{
int k;
for (k=1; k<=LOOP_COUNT; k++) {
pthread_mutex_lock(&lock);
sum += k;
pthread_mutex_unlock(&lock);
}
return NULL;
}
typedef void *(*thread_func)(void *arg);
static int test(const char *caption, thread_func thread_run)
{
int64_t start_time;
char time_buff[32];
pthread_t tids[THREAD_COUNT];
int i;
int result;
start_time = get_current_time_ms();
sum = 0;
for (i=0; i<THREAD_COUNT; i++) {
if ((result=pthread_create(tids + i, NULL, thread_run, NULL)) != 0) {
return result;
}
}
for (i=0; i<THREAD_COUNT; i++) {
pthread_join(tids[i], NULL);
}
printf("%s add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
"%"PRId64" ms\n", caption, int_to_comma_str(LOOP_COUNT, time_buff),
sum, get_current_time_ms() - start_time);
return 0;
}
int main(int argc, char *argv[])
{
int result;
int k;
int64_t sum;
int64_t start_time;
char time_buff[32];
pthread_mutex_t lock;
log_init();
srand(time(NULL));
@ -49,33 +101,13 @@ int main(int argc, char *argv[])
return result;
}
start_time = get_current_time_ms();
sum = 0;
for (k=1; k<=LOOP_COUNT; k++) {
//__sync_synchronize();
//barrier();
__sync_add_and_fetch(&sum, k);
}
printf("atom add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
"%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff),
sum, get_current_time_ms() - start_time);
printf("lock 1: %d\n", pthread_mutex_lock(&lock));
printf("lock 2: %d\n", pthread_mutex_lock(&lock));
printf("unlock 1: %d\n", pthread_mutex_unlock(&lock));
printf("unlock 2: %d\n", pthread_mutex_unlock(&lock));
start_time = get_current_time_ms();
sum = 0;
for (k=1; k<=LOOP_COUNT; k++) {
pthread_mutex_lock(&lock);
sum += k;
pthread_mutex_unlock(&lock);
}
test("atom", atomic_thread_func);
test("lock", mutex_thread_func);
printf("locked add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
"%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff),
sum, get_current_time_ms() - start_time);
return 0;
}