fast_task_queue.h 5.1 KB
Newer Older
Y
YuQing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2020 YuQing <384681@qq.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the Lesser GNU General Public License, version 3
 * or later ("LGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
Y
yuqing 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28

//fast_task_queue.h

#ifndef _FAST_TASK_QUEUE_H
#define _FAST_TASK_QUEUE_H 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "common_define.h"
#include "ioevent.h"
#include "fast_timer.h"

Y
YuQing 已提交
29 30 31
#define FC_NOTIFY_READ_FD(tdata)  (tdata)->pipe_fds[0]
#define FC_NOTIFY_WRITE_FD(tdata) (tdata)->pipe_fds[1]

Y
yuqing 已提交
32 33
#define ALIGNED_TASK_INFO_SIZE  MEM_ALIGN(sizeof(struct fast_task_info))

34
struct nio_thread_data;
Y
yuqing 已提交
35 36
struct fast_task_info;

37 38 39
typedef int (*ThreadLoopCallback) (struct nio_thread_data *pThreadData);
typedef int (*TaskFinishCallback) (struct fast_task_info *pTask);
typedef void (*TaskCleanUpCallback) (struct fast_task_info *pTask);
Y
YuQing 已提交
40
typedef int (*TaskInitCallback)(struct fast_task_info *pTask);
Y
yuqing 已提交
41 42 43

typedef void (*IOEventCallback) (int sock, short event, void *arg);

44 45
struct fast_task_info;

Y
yuqing 已提交
46 47 48 49 50 51 52 53 54 55 56
typedef struct ioevent_entry
{
	int fd;
	FastTimerEntry timer;
	IOEventCallback callback;
} IOEventEntry;

struct nio_thread_data
{
	struct ioevent_puller ev_puller;
	struct fast_timer timer;
57
	int pipe_fds[2];   //for notify
58
	struct fast_task_info *deleted_list;   //tasks for cleanup
Y
format  
yuqing 已提交
59
	ThreadLoopCallback thread_loop_callback;
Y
yuqing 已提交
60
	void *arg;   //extra argument pointer
61 62 63 64
    struct {
        struct fast_task_info *head;
        struct fast_task_info *tail;
        pthread_mutex_t lock;
Y
YuQing 已提交
65 66 67 68 69 70
    } waiting_queue;  //task queue

    struct {
        bool enabled;
        volatile int64_t counter;
    } notify;  //for thread notify
Y
yuqing 已提交
71 72 73 74
};

struct fast_task_info
{
Y
yuqing 已提交
75
	IOEventEntry event;  //must first
76 77 78 79
    union {
        char server_ip[IP_ADDRESS_SIZE];
        char client_ip[IP_ADDRESS_SIZE];
    };
Y
yuqing 已提交
80 81 82 83 84
	void *arg;  //extra argument pointer
	char *data; //buffer for write or recv
	int size;   //alloc size
	int length; //data length
	int offset; //current offset
85
    uint16_t port; //peer port
Y
YuQing 已提交
86 87 88 89
    struct {
        uint8_t current;
        uint8_t notify;
    } nio_stages; //stages for network IO
90
    bool canceled;   //if task canceled
91 92
    int connect_timeout; //for client side
    int network_timeout;
Y
yuqing 已提交
93
	int64_t req_count; //request count
94
	TaskFinishCallback finish_callback;
Y
yuqing 已提交
95
	struct nio_thread_data *thread_data;
Y
YuQing 已提交
96
	void *ctx;  //context pointer for libserverframe nio
97
	struct fast_task_info *next;
Y
yuqing 已提交
98 99 100 101 102 103 104 105
};

struct fast_task_queue
{
	struct fast_task_info *head;
	struct fast_task_info *tail;
	pthread_mutex_t lock;
	int max_connections;
106
	int alloc_connections;
Y
format  
yuqing 已提交
107
	int alloc_task_once;
Y
yuqing 已提交
108 109 110
	int min_buff_size;
	int max_buff_size;
	int arg_size;
Y
yuqing 已提交
111
	int block_size;
Y
yuqing 已提交
112
	bool malloc_whole_block;
Y
YuQing 已提交
113
    TaskInitCallback init_callback;
Y
yuqing 已提交
114 115 116 117 118 119
};

#ifdef __cplusplus
extern "C" {
#endif

Y
YuQing 已提交
120
int free_queue_init_ex2(const int max_connections, const int init_connections,
Y
yuqing 已提交
121
        const int alloc_task_once, const int min_buff_size,
Y
YuQing 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        const int max_buff_size, const int arg_size,
        TaskInitCallback init_callback);

static inline int free_queue_init_ex(const int max_connections,
        const int init_connections, const int alloc_task_once,
        const int min_buff_size, const int max_buff_size, const int arg_size)
{
    return free_queue_init_ex2(max_connections, init_connections,
            alloc_task_once, min_buff_size, max_buff_size, arg_size, NULL);
}

static inline int free_queue_init(const int max_connections,
        const int min_buff_size, const int max_buff_size, const int arg_size)
{
    return free_queue_init_ex2(max_connections, max_connections,
            0, min_buff_size, max_buff_size, arg_size, NULL);
}
Y
yuqing 已提交
139 140 141 142 143 144

void free_queue_destroy();

int free_queue_push(struct fast_task_info *pTask);
struct fast_task_info *free_queue_pop();
int free_queue_count();
145
int free_queue_alloc_connections();
146 147 148 149
int free_queue_set_buffer_size(struct fast_task_info *pTask,
        const int expect_size);
int free_queue_realloc_buffer(struct fast_task_info *pTask,
        const int expect_size);
Y
yuqing 已提交
150

151 152 153 154
int free_queue_set_max_buffer_size(struct fast_task_info *pTask);

int free_queue_realloc_max_buffer(struct fast_task_info *pTask);

Y
yuqing 已提交
155 156 157 158 159
int task_queue_init(struct fast_task_queue *pQueue);
int task_queue_push(struct fast_task_queue *pQueue, \
		struct fast_task_info *pTask);
struct fast_task_info *task_queue_pop(struct fast_task_queue *pQueue);
int task_queue_count(struct fast_task_queue *pQueue);
160 161 162 163
int task_queue_set_buffer_size(struct fast_task_queue *pQueue,
        struct fast_task_info *pTask, const int expect_size);
int task_queue_realloc_buffer(struct fast_task_queue *pQueue,
        struct fast_task_info *pTask, const int expect_size);
Y
yuqing 已提交
164

165 166 167
int task_queue_get_new_buffer_size(const int min_buff_size,
        const int max_buff_size, const int expect_size, int *new_size);

Y
yuqing 已提交
168 169 170 171 172 173
#ifdef __cplusplus
}
#endif

#endif