diff --git a/HISTORY b/HISTORY index 33054c4..5b37c9b 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-03-06 +Version 1.44 2020-03-08 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -16,6 +16,7 @@ Version 1.44 2020-03-06 * shared_func.[hc]: add function getFileSize * char_converter.[hc]: add function fast_char_unescape * struct fast_task_info add ctx pointer for libserverframe nio + * struct thread_data add waiting_queue for Linux eventfd notify Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/fast_task_queue.h b/src/fast_task_queue.h index c274f80..d98e33f 100644 --- a/src/fast_task_queue.h +++ b/src/fast_task_queue.h @@ -30,6 +30,8 @@ typedef void (*TaskCleanUpCallback) (struct fast_task_info *pTask); typedef void (*IOEventCallback) (int sock, short event, void *arg); +struct fast_task_info; + typedef struct ioevent_entry { int fd; @@ -41,10 +43,15 @@ struct nio_thread_data { struct ioevent_puller ev_puller; struct fast_timer timer; - int pipe_fds[2]; + int pipe_fds[2]; //for notify struct fast_task_info *deleted_list; ThreadLoopCallback thread_loop_callback; void *arg; //extra argument pointer + struct { + struct fast_task_info *head; + struct fast_task_info *tail; + pthread_mutex_t lock; + } waiting_queue; }; struct fast_task_info diff --git a/src/ioevent_loop.c b/src/ioevent_loop.c index fd1ade9..bb0ce40 100644 --- a/src/ioevent_loop.c +++ b/src/ioevent_loop.c @@ -7,8 +7,8 @@ static void deal_ioevents(IOEventPoller *ioevent) int event; IOEventEntry *pEntry; - for (ioevent->iterator.index=0; ioevent->iterator.index < ioevent->iterator. - count; ioevent->iterator.index++) + for (ioevent->iterator.index=0; ioevent->iterator.index < ioevent-> + iterator.count; ioevent->iterator.index++) { event = IOEVENT_GET_EVENTS(ioevent, ioevent->iterator.index); pEntry = (IOEventEntry *)IOEVENT_GET_DATA(ioevent, @@ -18,7 +18,8 @@ static void deal_ioevents(IOEventPoller *ioevent) } else { logDebug("file: "__FILE__", line: %d, " - "ignore iovent : %d, index: %d", __LINE__, event, ioevent->iterator.index); + "ignore iovent : %d, index: %d", + __LINE__, event, ioevent->iterator.index); } } } @@ -100,6 +101,7 @@ int ioevent_loop(struct nio_thread_data *pThreadData, memset(&ev_notify, 0, sizeof(ev_notify)); ev_notify.fd = pThreadData->pipe_fds[0]; ev_notify.callback = recv_notify_callback; + ev_notify.timer.data = pThreadData; if (ioevent_attach(&pThreadData->ev_puller, pThreadData->pipe_fds[0], IOEVENT_READ, &ev_notify) != 0) @@ -116,7 +118,8 @@ int ioevent_loop(struct nio_thread_data *pThreadData, last_check_time = g_current_time; while (*continue_flag) { - pThreadData->ev_puller.iterator.count = ioevent_poll(&pThreadData->ev_puller); + pThreadData->ev_puller.iterator.count = ioevent_poll( + &pThreadData->ev_puller); if (pThreadData->ev_puller.iterator.count > 0) { deal_ioevents(&pThreadData->ev_puller); diff --git a/src/tests/test_pipe.c b/src/tests/test_pipe.c index b6a7fc7..7149d51 100644 --- a/src/tests/test_pipe.c +++ b/src/tests/test_pipe.c @@ -8,7 +8,7 @@ #include "fastcommon/logger.h" #include "fastcommon/shared_func.h" -#define LOOP (200 * 1000) +#define LOOP (2000 * 1000) int main(int argc, char *argv[]) { diff --git a/src/tests/test_pthread_lock.c b/src/tests/test_pthread_lock.c index b30523e..412f09d 100644 --- a/src/tests/test_pthread_lock.c +++ b/src/tests/test_pthread_lock.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) start_time = get_current_time_ms(); sum = 0; for (k=1; k<=LOOP_COUNT; k++) { - __sync_synchronize(); + //__sync_synchronize(); //barrier(); __sync_add_and_fetch(&sum, k); } diff --git a/src/tests/test_server_id_func.c b/src/tests/test_server_id_func.c index 285b04d..ff0d59f 100644 --- a/src/tests/test_server_id_func.c +++ b/src/tests/test_server_id_func.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,54 @@ #include "fastcommon/shared_func.h" #include "fastcommon/server_id_func.h" +static int test_open_lseek(const char *filename) +{ + int result; + int fd; + int bytes; + char buff[1024]; + int64_t offset = 1024 * 1024; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + result = errno != 0 ? errno : EACCES; + logError("file: "__FILE__", line: %d, " + "open file \"%s\" fail, " + "errno: %d, error info: %s", + __LINE__, filename, + result, STRERROR(result)); + return result; + } + + if (offset > 0) { + if (lseek(fd, offset, SEEK_SET) < 0) { + result = errno != 0 ? errno : EACCES; + logError("file: "__FILE__", line: %d, " + "lseek file \"%s\" fail, offset: %"PRId64", " + "errno: %d, error info: %s", __LINE__, + filename, offset, + result, STRERROR(result)); + return result; + } else { + logInfo("lseek %"PRId64" successfully.", offset); + } + } + + if ((bytes=read(fd, buff, sizeof(buff))) < 0) { + result = errno != 0 ? errno : EACCES; + logError("file: "__FILE__", line: %d, " + "read file \"%s\" fail, offset: %"PRId64", " + "errno: %d, error info: %s", __LINE__, + filename, offset, result, STRERROR(result)); + return result; + } + + printf("read bytes: %d\n", bytes); + + close(fd); + return 0; +} + int main(int argc, char *argv[]) { int result; @@ -26,6 +75,48 @@ int main(int argc, char *argv[]) } log_init(); + + { + union { + int64_t flags; + struct { + union { + int flags: 4; + struct { + bool ns: 1; //namespace + bool pt: 1; //path + bool hc: 1; //hash code + }; + } path_info; + bool user_data : 1; + bool extra_data: 1; + bool mode : 1; + bool ctime: 1; + bool mtime: 1; + bool size : 1; + }; + } options; + + char *endptr; + int64_t n; + endptr = NULL; + n = strtoll(argv[1], &endptr, 10); + printf("sizeof(mode_t): %d\n", (int)sizeof(mode_t)); + + printf("sizeof(options): %d\n", (int)sizeof(options)); + + options.path_info.ns = options.path_info.pt = options.path_info.hc = 1; + printf("union flags: %d\n", options.path_info.flags); + + printf("n: %"PRId64", endptr: %s(%d)\n", n, endptr, (int)strlen(endptr)); + + n = snprintf(NULL, 0, "%"PRId64, n); + printf("expect len: %d\n", (int)n); + + test_open_lseek(config_filename); + return 1; + } + if ((result=fc_server_load_from_file_ex(&ctx, config_filename, default_port, min_hosts_each_group, share_between_groups)) != 0)