From 5f34bc872bf0ddcd5c0584f0767bebb2f5ab1646 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 3 Jun 2022 15:30:49 +0800 Subject: [PATCH] add functions common_blocked_queue_empty/count --- src/common_blocked_queue.h | 30 ++++++++++++++++++++++++++++++ src/fc_queue.h | 17 +++++++++++++++++ src/shared_func.c | 8 ++++---- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/common_blocked_queue.h b/src/common_blocked_queue.h index e938816..1a0a18b 100644 --- a/src/common_blocked_queue.h +++ b/src/common_blocked_queue.h @@ -95,6 +95,36 @@ void common_blocked_queue_return_nodes(struct common_blocked_queue *queue, void *common_blocked_queue_pop_ex(struct common_blocked_queue *queue, const bool blocked); +static inline bool common_blocked_queue_empty( + struct common_blocked_queue *queue) +{ + bool empty; + + pthread_mutex_lock(&queue->lc_pair.lock); + empty = (queue->head == NULL); + pthread_mutex_unlock(&queue->lc_pair.lock); + return empty; +} + +static inline int common_blocked_queue_count( + struct common_blocked_queue *queue) +{ + int count; + struct common_blocked_node *node; + + count = 0; + pthread_mutex_lock(&queue->lc_pair.lock); + node = queue->head; + while (node != NULL) + { + ++count; + node = node->next; + } + pthread_mutex_unlock(&queue->lc_pair.lock); + return count; +} + + #define common_blocked_queue_pop(queue) \ common_blocked_queue_pop_ex(queue, true) diff --git a/src/fc_queue.h b/src/fc_queue.h index adfbe4d..42f2e25 100644 --- a/src/fc_queue.h +++ b/src/fc_queue.h @@ -154,6 +154,23 @@ static inline bool fc_queue_empty(struct fc_queue *queue) return empty; } +static inline int fc_queue_count(struct fc_queue *queue) +{ + int count; + void *data; + + count = 0; + pthread_mutex_lock(&queue->lc_pair.lock); + data = queue->head; + while (data != NULL) + { + ++count; + data = FC_QUEUE_NEXT_PTR(queue, data); + } + pthread_mutex_unlock(&queue->lc_pair.lock); + return count; +} + void *fc_queue_timedpop(struct fc_queue *queue, const int timeout, const int time_unit); diff --git a/src/shared_func.c b/src/shared_func.c index ce94955..c1fa180 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -711,7 +711,7 @@ int getOccurCount(const char *src, const char seperator) p = strchr(src, seperator); while (p != NULL) { - count++; + ++count; p = strchr(p + 1, seperator); } @@ -721,7 +721,7 @@ int getOccurCount(const char *src, const char seperator) int fc_get_file_line_count_ex(const char *filename, const int64_t until_offset, int64_t *line_count) { -#define READ_BUFFER_SIZE (256 * 1024) +#define READ_BUFFER_SIZE (256 * 1024 * 1024) int fd; int result; int read_bytes; @@ -752,8 +752,8 @@ int fc_get_file_line_count_ex(const char *filename, } while (remain_bytes > 0) { - read_bytes = remain_bytes >= READ_BUFFER_SIZE ? - (READ_BUFFER_SIZE - 1) : remain_bytes; + read_bytes = (remain_bytes >= READ_BUFFER_SIZE ? + (READ_BUFFER_SIZE - 1) : remain_bytes); read_bytes = read(fd, buff, read_bytes); if (read_bytes == 0) { break;