Menu

[r3]: / framework / trunk / thread_pool.cpp  Maximize  Restore  History

Download this file

194 lines (151 with data), 3.3 kB

  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "thread_pool.h"
#include <poll.h>
#include <signal.h>
FastCGI_Application *FastCGI_Application::handlers_owner=NULL;
FastCGI_Application::FastCGI_Application(const char *socket)
{
FCGX_Init();
pipe(signal_pipe);
if(socket==NULL){
main_fd=0;
}
else {
this->socket=socket;
main_fd=FCGX_OpenSocket(socket,1);
if(main_fd<0) {
throw HTTP_Error(string("Fauled to open socket ")
+socket);
}
}
}
FastCGI_Application::event_t FastCGI_Application::wait()
{
for(;;) {
struct pollfd fds[2];
fds[0].fd=main_fd;
fds[0].events=POLLIN;
fds[0].revents=0;
fds[1].fd=signal_pipe[0];
fds[1].events=POLLIN;
fds[1].revents=0;
/* Wait for two events:
* 1. New connection and then do accept
* 2. Exit message - exit and do clean up */
poll(fds,2,-1);
if(fds[1].revents) {
return EXIT;
}
else if(fds[0].revents) {
return ACCEPT;
}
}
}
void FastCGI_Application::shutdown()
{
// Rise exit signal on
// the selfpipe
write(signal_pipe[1],"0",1);
}
void FastCGI_Application::handler(int id)
{
FastCGI_Application *ptr=FastCGI_Application::get_instance();
if(ptr) {
ptr->shutdown();
}
}
void FastCGI_Application::set_signal_handlers()
{
handlers_owner=this;
/* Signals defined by standard */
signal(SIGTERM,handler);
signal(SIGUSR1,handler);
/* Additional signal */
signal(SIGINT,handler);
}
void FastCGI_Single_Threaded_App::setup(Worker_Thread *worker)
{
this->worker=worker;
worker->init();
FCGX_InitRequest(&request, main_fd, 0);
}
bool FastCGI_Single_Threaded_App::run()
{
// Blocking loop
event_t event=wait();
if(event==EXIT) {
return false;
} // ELSE event==ACCEPT
int res;
#ifdef FCGX_API_ACCEPT_ONLY_EXISTS
res=FCGX_Accept_Only_r(&request);
#else
res=FCGX_Accept_r(&request);
#endif
if(res>=0){
// Execute
worker->run(&request);
}
return true;
};
void FastCGI_Single_Threaded_App::execute()
{
set_signal_handlers();
while(run()){
/* Do Continue */
}
}
void FastCGI_Mutiple_Threaded_App::thread_func(void *p)
{
info_t *params=(info_t *)p;
int id=params->first;
FastCGI_Mutiple_Threaded_App *me=params->second;
FCGX_Request *req;
while((req=me->jobs_stack.pop())!=NULL) {
me->workers[id]->run(req);
jobs_stack.push(req);
}
}
void FastCGI_Mutiple_Threaded_App::start_threads()
{
int i;
threads_info = new info_t [size];
for(i=0;i<size;i++) {
threads_info[i].first=i;
threads_info[i].second=this;
pthread_create(pids+i,NULL,thread_func,threads_info+i);
}
}
void FastCGI_Mutiple_Threaded_App::setup(int num,Worker_Thread **workers)
{
int i;
this->workers=workers;
size=num;
requests = new FCGX_Request [size+1];
pids = new pthread_t [size];
requests_stack.init(size+1);
for(i=0;i<size+i;i++)
requests_stack.push(requests+i);
jobs_stack.init(size);
start_threads();
}
void FastCGI_Mutiple_Threaded_App::execute()
{
while(wait()==ACCEPT) {
FCGX_Request *req=requests_stack.pop()
#ifdef FCGX_API_ACCEPT_ONLY_EXISTS
res=FCGX_Accept_Only_r(&request);
#else
res=FCGX_Accept_r(&request);
#endif
if(res<0) {
requests_stack.push(req);
continue;
}
jobs_stack.push(request);
}
// Exit event
int i;
for(i=0;i<size;i++) {
jobs_stack.push(NULL);
}
}
MongoDB Logo MongoDB