Skip to content

Commit

Permalink
refactor(iothread): not using attribute structures anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Feb 9, 2024
1 parent bbe6ad6 commit ca80693
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 45 deletions.
4 changes: 2 additions & 2 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 8 additions & 15 deletions docs/book/cn/iothread.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 7 additions & 14 deletions docs/book/en/iothread.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ 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
```
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`
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 1 addition & 8 deletions include/mln_iothread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/mln_iothread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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]);
Expand Down

0 comments on commit ca80693

Please sign in to comment.