diff --git a/docs/Melon Developer Guide.txt b/docs/Melon Developer Guide.txt index 5ac26768..be69eee9 100644 --- a/docs/Melon Developer Guide.txt +++ b/docs/Melon Developer Guide.txt @@ -2984,8 +2984,8 @@ Their definitions can be found in melon/include/mln_types.h. It will return the next timestamp depending on 'exp'. If failed, 0 will be returned. 41) I/O Thread - a) int mln_iothread_init(mln_iothread_t *t, struct mln_iothread_attr *attr); - Initialize an iothread instance 't' with attribute 'attr'. + a) int mln_iothread_init(mln_iothread_t *t, mln_u32_t nthread, mln_iothread_entry_t entry, void *args, mln_iothread_msg_process_t handler); + Initialize an iothread instance 't' with attributes. Return value: 0 on success, otherwise -1. b) void mln_iothread_destroy(mln_iothread_t *t); diff --git a/docs/book/cn/iothread.md b/docs/book/cn/iothread.md index 76e8d7bc..d38d89c9 100644 --- a/docs/book/cn/iothread.md +++ b/docs/book/cn/iothread.md @@ -31,20 +31,18 @@ I/O线程算是一种另类线程池结构。但是这个组件主要用于图 #### mln_iothread_init ```c -int mln_iothread_init(mln_iothread_t *t, struct mln_iothread_attr *attr); - -struct mln_iothread_attr { - mln_u32_t nthread; //几个I/O线程 - mln_iothread_entry_t entry; //I/O线程入口函数 - void *args; //I/O线程入口参数 - mln_iothread_msg_process_t handler; //消息处理函数 -}; +int mln_iothread_init(mln_iothread_t *t, mln_u32_t nthread, mln_iothread_entry_t entry, void *args, mln_iothread_msg_process_t handler); typedef void *(*mln_iothread_entry_t)(void *); //线程入口 typedef void (*mln_iothread_msg_process_t)(mln_iothread_t *t, mln_iothread_ep_type_t from, mln_iothread_msg_t *msg);//消息处理函数 ``` -描述:依据`attr`对`t`进行初始化。 +描述:依据`attr`对`t`进行初始化,参数: + +- `nthread` I/O线程数量 +- `entry` I/O线程入口函数 +- `args` I/O线程入口参数 +- `handler` 消息处理函数 返回值:成功返回`0`,否则返回`-1` @@ -181,13 +179,8 @@ int main(void) { int i, rc; mln_iothread_t t; - struct mln_iothread_attr tattr; - tattr.nthread = 1; - tattr.entry = (mln_iothread_entry_t)entry; - tattr.args = &t; - tattr.handler = (mln_iothread_msg_process_t)msg_handler; - if (mln_iothread_init(&t, &tattr) < 0) { + if (mln_iothread_init(&t, 1, (mln_iothread_entry_t)entry, &t, (mln_iothread_msg_process_t)msg_handler) < 0) { fprintf(stderr, "iothread init failed\n"); return -1; } diff --git a/docs/book/en/iothread.md b/docs/book/en/iothread.md index 19c0efd7..0adbe47b 100644 --- a/docs/book/en/iothread.md +++ b/docs/book/en/iothread.md @@ -25,14 +25,7 @@ I/O thread is another kind of thread pool. But this component is mainly used for #### mln_iothread_init ```c -int mln_iothread_init(mln_iothread_t *t, struct mln_iothread_attr *attr); - -struct mln_iothread_attr { - mln_u32_t nthread; //Total number of I/O threads - mln_iothread_entry_t entry; //I/O thread entry function - void *args; //I/O thread entry parameters - mln_iothread_msg_process_t handler; //message handler -}; +int mln_iothread_init(mln_iothread_t *t, mln_u32_t nthread, mln_iothread_entry_t entry, void *args, mln_iothread_msg_process_t handler); typedef void *(*mln_iothread_entry_t)(void *); //I/O thread entry function typedef void (*mln_iothread_msg_process_t)(mln_iothread_t *t, mln_iothread_ep_type_t from, mln_iothread_msg_t *msg);//message handler @@ -40,6 +33,11 @@ typedef void (*mln_iothread_msg_process_t)(mln_iothread_t *t, mln_iothread_ep_ty Description: Initialize `t` according to `attr`. +- `nthread` Total number of I/O threads. +- `entry` I/O thread entry function. +- `args` I/O thread entry parameters. +- `handler` message handler. + Return value: return `0` on success, otherwise return `-1` @@ -181,13 +179,8 @@ int main(void) { int i, rc; mln_iothread_t t; - struct mln_iothread_attr tattr; - tattr.nthread = 1; - tattr.entry = (mln_iothread_entry_t)entry; - tattr.args = &t; - tattr.handler = (mln_iothread_msg_process_t)msg_handler; - if (mln_iothread_init(&t, &tattr) < 0) { + if (mln_iothread_init(&t, 1, (mln_iothread_entry_t)entry, &t, (mln_iothread_msg_process_t)msg_handler) < 0) { fprintf(stderr, "iothread init failed\n"); return -1; } diff --git a/include/mln_iothread.h b/include/mln_iothread.h index aeb62e31..cf71040e 100644 --- a/include/mln_iothread.h +++ b/include/mln_iothread.h @@ -30,13 +30,6 @@ struct mln_iothread_msg_s { pthread_mutex_t mutex; }; -struct mln_iothread_attr { - mln_u32_t nthread; - mln_iothread_entry_t entry; - void *args; - mln_iothread_msg_process_t handler; -}; - struct mln_iothread_s { pthread_mutex_t io_lock; pthread_mutex_t user_lock; @@ -59,7 +52,7 @@ struct mln_iothread_s { #define mln_iothread_msg_type(m) ((m)->type) #define mln_iothread_msg_data(m) ((m)->data) -extern int mln_iothread_init(mln_iothread_t *t, struct mln_iothread_attr *attr); +extern int mln_iothread_init(mln_iothread_t *t, mln_u32_t nthread, mln_iothread_entry_t entry, void *args, mln_iothread_msg_process_t handler); extern void mln_iothread_destroy(mln_iothread_t *t); extern int mln_iothread_send(mln_iothread_t *t, mln_u32_t type, void *data, mln_iothread_ep_type_t to, mln_u32_t feedback); extern int mln_iothread_recv(mln_iothread_t *t, mln_iothread_ep_type_t from); diff --git a/src/mln_iothread.c b/src/mln_iothread.c index 36631d9c..5c3b3ee3 100644 --- a/src/mln_iothread.c +++ b/src/mln_iothread.c @@ -23,12 +23,13 @@ MLN_CHAIN_FUNC_DECLARE(mln_iothread_msg, mln_iothread_msg_t, static inline void, MLN_CHAIN_FUNC_DEFINE(mln_iothread_msg, mln_iothread_msg_t, static inline void, prev, next); MLN_FUNC(, int, mln_iothread_init, \ - (mln_iothread_t *t, struct mln_iothread_attr *attr), (t, attr), \ + (mln_iothread_t *t, mln_u32_t nthread, mln_iothread_entry_t entry, void *args, mln_iothread_msg_process_t handler), \ + (t, nthread, entry, args, handler), \ { mln_u32_t i; int fds[2]; - if (!attr->nthread || attr->entry == NULL) { + if (!nthread || entry == NULL) { return -1; } @@ -39,14 +40,14 @@ MLN_FUNC(, int, mln_iothread_init, \ t->user_fd = fds[1]; mln_iothread_fd_nonblock_set(t->io_fd); mln_iothread_fd_nonblock_set(t->user_fd); - t->entry = attr->entry; - t->args = attr->args; - t->handler = attr->handler; + t->entry = entry; + t->args = args; + t->handler = handler; pthread_mutex_init(&(t->io_lock), NULL); pthread_mutex_init(&(t->user_lock), NULL); t->io_head = t->io_tail = NULL; t->user_head = t->user_tail = NULL; - t->nthread = attr->nthread; + t->nthread = nthread; if ((t->tids = (pthread_t *)calloc(t->nthread, sizeof(pthread_t))) == NULL) { mln_socket_close(fds[0]);