/
mq_notify.c
147 lines (134 loc) · 3.51 KB
/
mq_notify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (C) 2014 zomoe
*
* zomoemq may be copied only under the terms of the GNU General Public License V3
* mail : xianszm007@gmail.com
* Author: zomoe
*/
#include <event.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <string.h>
#include "mq_types.h"
#include "mq_notify.h"
#include "mq_socket.h"
static int STACK_SIZE = 8;
int notify_pool_init(size_t m_size,notify_read_callback callbackfunc){
int ret=0;
assert(m_size!=0);
p_notify_t=(notify_t*)calloc(m_size,sizeof(notify_t));
assert(p_notify!=NULL);
do{
for(int i=0;i<m_size;i++){
(p_notify_t+i)->ev_base=event_base_new();
(p_notify_t+i)->pid=0;
(p_notify_t+i)->index=0;
if(pipe((p_notify_t+i)->pipe_fd)==-1){
ret=-1;
break;
}
(p_notify_t+i)->func=callbackfunc;
}
}while(0);
if(ret==-1){
free(p_notify_t);
}
return ret;
}
int notify_pool_destroy(){
free(p_notify_t);
p_notify_t=NULL;
return 0;
}
int notify_event_init(notify_t *p_notify){
int ret=0;
assert(p_notify!=NULL);
set_nonblocking(p_notify->pipe_fd[0]);
struct event event_notify;
do{
if(event_set(&event_notify,\
p_notify->pipe_fd[0],\
EV_READ|EV_PERSIST,\
p_notify->func,\
&(p_notify->index))!=0){
ret=-1;
break;
}
if(event_add(&event_notify,NULL)!=0){
ret=-1;
break;
}
while(isLoop){
event_base_loop(p_notify->ev_base,0);
}
}while(0);
return ret;
}
void notify_read_event(int sockfd,short event,void *arg){
assert(arg!=NULL || *((int*)arg) >0);
int index=*((int*)arg);
int client_sockfd,ret;
while(isLoop){
if((ret=read(sockfd,&client_sockfd,\
sizeof(client_sockfd))) > 0){
if(!(EAGAIN ==errno || EWOULDBLOCK == errno)){
//log_error
}
break;
}else if(ret==0){
break;
}
// here should be some_func to manage task
//.....not end!
}
}
static void *notify_thread_issue(void *arg)
{
assert(arg!=NULL);
notify_t *p_notify = (notify_t *)arg;
if(notify_event_init(p_notify)!=0){
return NULL;
}
return NULL;
}
int init_thread(notify_t* p_notify){
int ret=0;
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
do{
if(pthread_attr_setstacksize(&thread_attr,\
STACK_SIZE * 1024 * 1024)!=0){
ret=-1;
break;
}
if ((ret=pthread_create(&(p_notify->pid), &thread_attr,\
notify_thread_issue, p_notify))!=0){
ret = -1;
break;
}
}while(0);
pthread_attr_destroy(&thread_attr);
return ret;
}
int work_thread_init(size_t size,size_t stack_size){
int ret=0;
assert(size!=0 || stack_size!=0);
STACK_SIZE=stack_size;
for(int i=0;i<size;i++){
notify_t *p_notify=p_notify_t+i;
if(p_notify==NULL){
ret=-1;
break;
}
if(init_thread(p_notify)==-1){
ret=-1;
break;
}
}
return ret;
}