Repository navigation
Expand file tree
/
Copy pathmanager.h
More file actions
244 lines (208 loc) · 5.47 KB
/
Copy pathmanager.h
File metadata and controls
244 lines (208 loc) · 5.47 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
#ifndef CPPCMS_MANAGER_H
#define CPPCMS_MANAGER_H
#include <pthread.h>
#include <string>
#include <memory>
#include "worker_thread.h"
#include "global_config.h"
#include "cache_interface.h"
#include "cgi_api.h"
#include "posix_mutex.h"
#include "transtext.h"
#include "session_backend_factory.h"
#include <boost/shared_ptr.hpp>
namespace cppcms {
class manager;
using boost::shared_ptr;
class base_factory {
public:
virtual shared_ptr<worker_thread> operator()(manager const &cf) const = 0;
virtual ~base_factory() {};
};
template<typename T>
class simple_factory : public base_factory {
public:
virtual shared_ptr<worker_thread> operator()(manager const &cf) const
{ return shared_ptr<worker_thread>(new T(cf)); };
};
template<typename T,typename P>
class one_param_factory : public base_factory {
P const &P1;
public:
one_param_factory(P const &p) : P1(p) {};
virtual shared_ptr<worker_thread> operator()(manager const &cf) const
{ return shared_ptr<worker_thread>(new T(cf,P1)); };
};
template<typename T,typename Pa,typename Pb>
class two_params_factory : public base_factory {
Pa const &P1;
Pb const &P2;
public:
two_params_factory(Pa const &p1,Pb const &p2) : P1(p1), P2(p2) {};
virtual shared_ptr<worker_thread> operator()(manager const &cf) const
{ return shared_ptr<worker_thread>(new T(cf,P1,P2)); };
};
class web_application {
public:
manager &app;
web_application(manager &m) : app(m) {};
virtual void execute() = 0;
virtual ~web_application() {};
};
namespace details {
#if !defined(CPPCMS_EMBEDDED) || defined(CPPCMS_EMBEDDED_THREAD)
class fast_cgi_application :public web_application {
static fast_cgi_application *handlers_owner;
protected:
// General control
bool the_end;
static void handler(int id);
typedef enum { EXIT , ACCEPT } event_t;
virtual event_t wait();
void set_signal_handlers();
static fast_cgi_application *get_instance() { return handlers_owner; };
public:
fast_cgi_application(manager &m) : web_application(m) {};
virtual ~fast_cgi_application() {};
void shutdown();
virtual bool run() { return false; };
virtual void execute();
};
class fast_cgi_single_threaded_app : public fast_cgi_application {
shared_ptr<worker_thread> worker;
void setup();
public:
virtual bool run();
fast_cgi_single_threaded_app(manager &m);
virtual ~fast_cgi_single_threaded_app(){};
};
template <class T>
class sefe_set {
pthread_mutex_t access_mutex;
pthread_cond_t new_data_availible;
pthread_cond_t new_space_availible;
protected:
int max;
int size;
virtual T &get_int() = 0;
virtual void put_int(T &val) = 0;
public:
void init(int size){
pthread_mutex_init(&access_mutex,NULL);
pthread_cond_init(&new_data_availible,NULL);
pthread_cond_init(&new_space_availible,NULL);
max=size;
this->size=0;
};
sefe_set() {};
virtual ~sefe_set() {};
virtual void push(T val) {
mutex_lock lock(access_mutex);
while(size>=max) {
pthread_cond_wait(&new_space_availible,&access_mutex);
}
put_int(val);
pthread_cond_signal(&new_data_availible);
};
T pop() {
mutex_lock lock(access_mutex);
while(size==0) {
pthread_cond_wait(&new_data_availible,&access_mutex);
}
T data=get_int();
pthread_cond_signal(&new_space_availible);
return data;
};
};
template <class T>
class sefe_queue : public sefe_set<T>{
T *queue;
int head;
int tail;
int next(int x) { return (x+1)%this->max; };
protected:
virtual void put_int(T &val) {
queue[head]=val;
head=next(head);
this->size++;
}
virtual T &get_int() {
this->size--;
int ptr=tail;
tail=next(tail);
return queue[ptr];
}
public:
void init(int size) {
if(queue) return;
queue=new T [size];
sefe_set<T>::init(size);
}
sefe_queue() { queue = NULL; head=tail=0; };
virtual ~sefe_queue() { delete [] queue; };
};
class fast_cgi_multiple_threaded_app : public fast_cgi_application {
int size;
vector<shared_ptr<worker_thread> > workers;
sefe_queue<cgi_session *> jobs;
typedef pair<int,fast_cgi_multiple_threaded_app*> info_t;
info_t *threads_info;
pthread_t *pids;
static void *thread_func(void *p);
void start_threads();
void wait_threads();
public:
fast_cgi_multiple_threaded_app(manager &m);
virtual bool run();
virtual ~fast_cgi_multiple_threaded_app() {
delete [] pids;
delete [] threads_info;
};
};
#endif
#if !defined(CPPCMS_EMBEDDED)
class prefork : public web_application {
vector<pid_t> pids;
int procs;
int exit_flag;
static prefork *self;
static void parent_handler(int s);
static void chaild_handler(int s);
void run();
public:
prefork(manager &m);
virtual void execute();
};
#endif
} // END OF DETAILS
void run_application(int argc,char *argv[],base_factory const &factory);
class manager : private boost::noncopyable {
cache_factory *get_cache_factory();
cgi_api *get_api();
web_application *get_mod();
session_backend_factory get_sessions();
transtext::trans_factory *get_gettext();
list<void *> templates_list;
void load_templates();
public:
cppcms_config config;
auto_ptr<cache_factory> cache;
auto_ptr<cgi_api> api;
auto_ptr<base_factory> workers;
auto_ptr<web_application> web_app;
session_backend_factory sessions;
auto_ptr<transtext::trans_factory> gettext;
void set_worker(base_factory *w);
void set_cache(cache_factory *c);
void set_api(cgi_api *a);
void set_mod(web_application *m);
void set_gettext(transtext::trans_factory *);
void set_sessions(session_backend_factory);
manager(char const *file);
manager(int argc, char **argv);
manager(int argc, char **argv,char const *file);
~manager();
void execute();
};
}
#endif /* CPPCMS_MANAGER_H */