Menu

[r349]: / framework / trunk / manager.cpp  Maximize  Restore  History

Download this file

268 lines (213 with data), 4.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "manager.h"
#include "cgicc_connection.h"
#include <poll.h>
#include <signal.h>
#include <errno.h>
namespace cppcms {
namespace details {
fast_cgi_application *fast_cgi_application::handlers_owner=NULL;
fast_cgi_application::fast_cgi_application(const char *socket,int backlog)
{
FCGX_Init();
pipe(signal_pipe);
if(socket==NULL || socket[0]==0){
main_fd=0;
}
else {
this->socket=socket;
main_fd=FCGX_OpenSocket(socket,backlog);
if(main_fd<0) {
throw cppcms_error(string("Failed to open socket ")
+socket);
}
}
}
fast_cgi_application::event_t fast_cgi_application::wait()
{
for(;;) {
struct pollfd fds[2];
fds[0].fd=main_fd;
fds[0].events=POLLIN | POLLERR;
fds[0].revents=0;
fds[1].fd=signal_pipe[0];
fds[1].events=POLLIN | POLLERR;
fds[1].revents=0;
/* Wait for two events:
* 1. New connection and then do accept
* 2. Exit message - exit and do clean up */
if(poll(fds,2,-1)<0) {
if(errno==EINTR)
continue;
return EXIT;
}
if(fds[1].revents) {
return EXIT;
}
else if(fds[0].revents) {
if(fds[0].revents & POLLERR)
return EXIT;
return ACCEPT;
}
}
}
void fast_cgi_application::shutdown()
{
// Rise exit signal on
// the selfpipe
write(signal_pipe[1],"0",1);
}
void fast_cgi_application::handler(int id)
{
fast_cgi_application *ptr=fast_cgi_application::get_instance();
if(ptr) {
ptr->shutdown();
}
}
void fast_cgi_application::set_signal_handlers()
{
handlers_owner=this;
/* Signals defined by standard */
signal(SIGTERM,handler);
signal(SIGUSR1,handler);
/* Additional signal */
signal(SIGINT,handler);
}
fast_cgi_single_threaded_app::fast_cgi_single_threaded_app(base_factory const &factory,char const *socket) :
fast_cgi_application(socket,1)
{
worker=factory();
FCGX_InitRequest(&request, main_fd, 0);
}
bool fast_cgi_single_threaded_app::run()
{
// Blocking loop
event_t event=wait();
if(event==EXIT) {
return false;
} // ELSE event==ACCEPT
int res;
res=FCGX_Accept_r(&request);
if(res>=0){
// Execute
cgicc_connection_fast_cgi cgi(request);
worker->run(cgi);
}
return true;
};
void fast_cgi_application::execute()
{
set_signal_handlers();
while(run()){
/* Do Continue */
}
}
fast_cgi_multiple_threaded_app::fast_cgi_multiple_threaded_app( int threads_num,
int buffer,
base_factory const &factory,
char const *socket) :
fast_cgi_application(socket,threads_num+1+buffer)
{
int i;
requests=NULL;
threads_info=NULL;
pids=NULL;
size=threads_num;
// Init Worker Threads
workers.resize(size);
for(i=0;i<size;i++) {
workers[i]=factory();
}
// Init FCGX Requests
requests = new FCGX_Request [buffer];
for(i=0;i<buffer;i++) {
FCGX_InitRequest(requests+i, main_fd, 0);
}
requests_queue.init(buffer);
for(i=0;i<buffer;i++)
requests_queue.push(requests+i);
// Setup Jobs Manager
jobs_queue.init(buffer);
start_threads();
}
void *fast_cgi_multiple_threaded_app::thread_func(void *p)
{
info_t *params=(info_t *)p;
int id=params->first;
fast_cgi_multiple_threaded_app *self=params->second;
FCGX_Request *req;
while((req=self->jobs_queue.pop())!=NULL) {
{
cgicc_connection_fast_cgi cgi(*req);
self->workers[id]->run(cgi);
}
self->requests_queue.push(req);
}
return NULL;
}
void fast_cgi_multiple_threaded_app::start_threads()
{
int i;
pids = new pthread_t [size];
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 fast_cgi_multiple_threaded_app::wait_threads()
{
int i;
for(i=0;i<size;i++) {
pthread_join(pids[i],NULL);
}
}
bool fast_cgi_multiple_threaded_app::run()
{
int res;
if(wait()==ACCEPT) {
FCGX_Request *req=requests_queue.pop();
res=FCGX_Accept_r(req);
if(res<0) {
requests_queue.push(req);
return true;
}
jobs_queue.push(req);
return true;
}
else {// Exit event
int i;
for(i=0;i<size;i++) {
jobs_queue.push(NULL);
}
wait_threads();
return false;
}
}
} // END oF Details
void run_application(int argc,char *argv[],base_factory const &factory)
{
int n,max;
global_config.load(argc,argv);
char const *socket=global_config.sval("server.socket","").c_str();
string mod=global_config.sval("server.mod");
if(mod=="process") {
details::fast_cgi_single_threaded_app app(factory,socket);
app.execute();
}
else if(mod=="thread") {
n=global_config.lval("server.threads",5);
max=global_config.lval("server.buffer",1);
details::fast_cgi_multiple_threaded_app app(n,max,factory,socket);
app.execute();
}
else if(mod=="cgi") {
shared_ptr<worker_thread> ptr=factory();
cgicc_connection_cgi cgi;
ptr->run(cgi);
}
else {
throw cppcms_error("Unknown mod:" + mod);
}
};
} // END OF CPPCMS
MongoDB Logo MongoDB