Skip to content

Commit 71b6810

Browse files
committed
rewrite code to make use of private event base instead of the global one
1 parent 918731b commit 71b6810

13 files changed

+81
-70
lines changed

sapi/fpm/fpm/fpm.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
struct fpm_globals_s fpm_globals;
2525

26-
int fpm_init(int argc, char **argv, char *config) /* {{{ */
26+
int fpm_init(int argc, char **argv, char *config, struct event_base **base) /* {{{ */
2727
{
2828
fpm_globals.argc = argc;
2929
fpm_globals.argv = argv;
@@ -39,7 +39,7 @@ int fpm_init(int argc, char **argv, char *config) /* {{{ */
3939
0 > fpm_children_init_main() ||
4040
0 > fpm_sockets_init_main() ||
4141
0 > fpm_worker_pool_init_main() ||
42-
0 > fpm_event_init_main()) {
42+
0 > fpm_event_init_main(base)) {
4343
return -1;
4444
}
4545

@@ -55,23 +55,23 @@ int fpm_init(int argc, char **argv, char *config) /* {{{ */
5555

5656
/* children: return listening socket
5757
parent: never return */
58-
int fpm_run(int *max_requests) /* {{{ */
58+
int fpm_run(int *max_requests, struct event_base *base) /* {{{ */
5959
{
6060
struct fpm_worker_pool_s *wp;
6161

6262
/* create initial children in all pools */
6363
for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6464
int is_parent;
6565

66-
is_parent = fpm_children_create_initial(wp);
66+
is_parent = fpm_children_create_initial(wp, base);
6767

6868
if (!is_parent) {
6969
goto run_child;
7070
}
7171
}
7272

7373
/* run event loop forever */
74-
fpm_event_loop();
74+
fpm_event_loop(base);
7575

7676
run_child: /* only workers reach this point */
7777

sapi/fpm/fpm/fpm.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#define FPM_H 1
77

88
#include <unistd.h>
9+
#include <sys/types.h> /* for event.h below */
10+
#include <event.h>
911

10-
int fpm_run(int *max_requests);
11-
int fpm_init(int argc, char **argv, char *config);
12+
int fpm_run(int *max_requests, struct event_base *base);
13+
int fpm_init(int argc, char **argv, char *config, struct event_base **base);
1214

1315
struct fpm_globals_s {
1416
pid_t parent_pid;

sapi/fpm/fpm/fpm_children.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int fpm_children_free(struct fpm_child_s *child) /* {{{ */
171171
}
172172
/* }}} */
173173

174-
void fpm_children_bury() /* {{{ */
174+
void fpm_children_bury(struct event_base *base) /* {{{ */
175175
{
176176
int status;
177177
pid_t pid;
@@ -277,12 +277,12 @@ void fpm_children_bury() /* {{{ */
277277

278278
zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload", fpm_global_config.emergency_restart_threshold, fpm_global_config.emergency_restart_interval);
279279

280-
fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
280+
fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET, base);
281281
}
282282
}
283283

284284
if (restart_child) {
285-
fpm_children_make(wp, 1 /* in event loop */, 1, 0);
285+
fpm_children_make(wp, 1 /* in event loop */, 1, 0, base);
286286

287287
if (fpm_globals.is_child) {
288288
break;
@@ -340,15 +340,15 @@ static void fpm_child_resources_use(struct fpm_child_s *child) /* {{{ */
340340
}
341341
/* }}} */
342342

343-
static void fpm_parent_resources_use(struct fpm_child_s *child) /* {{{ */
343+
static void fpm_parent_resources_use(struct fpm_child_s *child, struct event_base *base) /* {{{ */
344344
{
345345
fpm_shm_slots_parent_use_slot(child);
346-
fpm_stdio_parent_use_pipes(child);
346+
fpm_stdio_parent_use_pipes(child, base);
347347
fpm_child_link(child);
348348
}
349349
/* }}} */
350350

351-
int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to_spawn, int is_debug) /* {{{ */
351+
int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to_spawn, int is_debug, struct event_base *base) /* {{{ */
352352
{
353353
int enough = 0;
354354
pid_t pid;
@@ -378,12 +378,12 @@ int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to
378378
switch (pid) {
379379

380380
case 0 :
381+
event_reinit(base); /* reinitialize event base after fork() */
381382
fpm_child_resources_use(child);
382383
fpm_globals.is_child = 1;
383384
if (in_event_loop) {
384-
fpm_event_exit_loop();
385+
fpm_event_exit_loop(base);
385386
}
386-
event_init(); /* reopen epoll descriptor */
387387
fpm_child_init(wp);
388388
return 0;
389389

@@ -398,7 +398,7 @@ int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to
398398
default :
399399
child->pid = pid;
400400
fpm_clock_get(&child->started);
401-
fpm_parent_resources_use(child);
401+
fpm_parent_resources_use(child, base);
402402

403403
zlog(ZLOG_STUFF, is_debug ? ZLOG_DEBUG : ZLOG_NOTICE, "[pool %s] child %d started", wp->config->name, (int) pid);
404404
}
@@ -409,9 +409,9 @@ int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to
409409
}
410410
/* }}} */
411411

412-
int fpm_children_create_initial(struct fpm_worker_pool_s *wp) /* {{{ */
412+
int fpm_children_create_initial(struct fpm_worker_pool_s *wp, struct event_base *base) /* {{{ */
413413
{
414-
return fpm_children_make(wp, 0 /* not in event loop yet */, 0, 1);
414+
return fpm_children_make(wp, 0 /* not in event loop yet */, 0, 1, base);
415415
}
416416
/* }}} */
417417

sapi/fpm/fpm/fpm_children.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
#include "fpm_worker_pool.h"
1313

14-
int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
14+
int fpm_children_create_initial(struct fpm_worker_pool_s *wp, struct event_base *base);
1515
int fpm_children_free(struct fpm_child_s *child);
16-
void fpm_children_bury();
16+
void fpm_children_bury(struct event_base *base);
1717
int fpm_children_init_main();
18-
int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to_spawn, int is_debug);
18+
int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop, int nb_to_spawn, int is_debug, struct event_base *base);
1919

2020
struct fpm_child_s;
2121

sapi/fpm/fpm/fpm_conf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ static int fpm_conf_process_all_pools() /* {{{ */
515515
if (wp->config->pm->status && *wp->config->pm->status) {
516516
int i;
517517
char *status = wp->config->pm->status;
518-
struct fpm_status_s fpm_status;
518+
/* struct fpm_status_s fpm_status; */
519519

520520
if (*status != '/') {
521521
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] the status page '%s' must start with a '/'", wp->config->name, status);
@@ -541,7 +541,7 @@ static int fpm_conf_process_all_pools() /* {{{ */
541541
fpm_status_update_accepted_conn(wp->shm_status, 0);
542542
fpm_status_update_activity(wp->shm_status, -1, -1, -1, 1);
543543
fpm_status_set_pm(wp->shm_status, wp->config->pm->style);
544-
//memset(&fpm_status.last_update, 0, sizeof(fpm_status.last_update));
544+
/* memset(&fpm_status.last_update, 0, sizeof(fpm_status.last_update)); */
545545
}
546546
}
547547
return 0;

sapi/fpm/fpm/fpm_events.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <errno.h>
99
#include <stdlib.h> /* for putenv */
1010
#include <string.h>
11-
#include <sys/types.h> /* for event.h below */
12-
#include <event.h>
1311

1412
#include "fpm.h"
1513
#include "fpm_process_ctl.h"
@@ -22,14 +20,16 @@
2220

2321
static void fpm_event_cleanup(int which, void *arg) /* {{{ */
2422
{
25-
event_base_free(0);
23+
struct event_base *base = (struct event_base *)arg;
24+
event_base_free(base);
2625
}
2726
/* }}} */
2827

2928
static void fpm_got_signal(int fd, short ev, void *arg) /* {{{ */
3029
{
3130
char c;
3231
int res;
32+
struct event_base *base = (struct event_base *)arg;
3333

3434
do {
3535
do {
@@ -46,22 +46,22 @@ static void fpm_got_signal(int fd, short ev, void *arg) /* {{{ */
4646
switch (c) {
4747
case 'C' : /* SIGCHLD */
4848
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGCHLD");
49-
fpm_children_bury();
49+
fpm_children_bury(base);
5050
break;
5151
case 'I' : /* SIGINT */
5252
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGINT");
5353
zlog(ZLOG_STUFF, ZLOG_NOTICE, "Terminating ...");
54-
fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
54+
fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET, base);
5555
break;
5656
case 'T' : /* SIGTERM */
5757
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGTERM");
5858
zlog(ZLOG_STUFF, ZLOG_NOTICE, "Terminating ...");
59-
fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
59+
fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET, base);
6060
break;
6161
case 'Q' : /* SIGQUIT */
6262
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGQUIT");
6363
zlog(ZLOG_STUFF, ZLOG_NOTICE, "Finishing ...");
64-
fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
64+
fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET, base);
6565
break;
6666
case '1' : /* SIGUSR1 */
6767
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGUSR1");
@@ -74,7 +74,7 @@ static void fpm_got_signal(int fd, short ev, void *arg) /* {{{ */
7474
case '2' : /* SIGUSR2 */
7575
zlog(ZLOG_STUFF, ZLOG_DEBUG, "received SIGUSR2");
7676
zlog(ZLOG_STUFF, ZLOG_NOTICE, "Reloading in progress ...");
77-
fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
77+
fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET, base);
7878
break;
7979
}
8080

@@ -86,36 +86,38 @@ static void fpm_got_signal(int fd, short ev, void *arg) /* {{{ */
8686
}
8787
/* }}} */
8888

89-
int fpm_event_init_main() /* {{{ */
89+
int fpm_event_init_main(struct event_base **base) /* {{{ */
9090
{
91-
event_init();
91+
*base = event_base_new();
9292

93-
zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
93+
zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_base_get_method(*base));
9494

95-
if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
95+
if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, *base)) {
9696
return -1;
9797
}
9898
return 0;
9999
}
100100
/* }}} */
101101

102-
int fpm_event_loop() /* {{{ */
102+
int fpm_event_loop(struct event_base *base) /* {{{ */
103103
{
104104
static struct event signal_fd_event;
105105

106-
event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
106+
event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, base);
107+
event_base_set(base, &signal_fd_event);
107108
event_add(&signal_fd_event, 0);
108-
fpm_pctl_heartbeat(-1, 0, 0);
109-
fpm_pctl_perform_idle_server_maintenance_heartbeat(-1, 0, 0);
109+
fpm_pctl_heartbeat(-1, 0, base);
110+
fpm_pctl_perform_idle_server_maintenance_heartbeat(-1, 0, base);
110111
zlog(ZLOG_STUFF, ZLOG_NOTICE, "ready to handle connections");
111-
event_loop(0);
112+
event_base_dispatch(base);
112113
return 0;
113114
}
114115
/* }}} */
115116

116-
int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg) /* {{{ */
117+
int fpm_event_add(int fd, struct event_base *base, struct event *ev, void (*callback)(int, short, void *), void *arg) /* {{{ */
117118
{
118119
event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
120+
event_base_set(base, ev);
119121
return event_add(ev, 0);
120122
}
121123
/* }}} */
@@ -126,9 +128,9 @@ int fpm_event_del(struct event *ev) /* {{{ */
126128
}
127129
/* }}} */
128130

129-
void fpm_event_exit_loop() /* {{{ */
131+
void fpm_event_exit_loop(struct event_base *base) /* {{{ */
130132
{
131-
event_loopbreak();
133+
event_base_loopbreak(base);
132134
}
133135
/* }}} */
134136

sapi/fpm/fpm/fpm_events.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#ifndef FPM_EVENTS_H
66
#define FPM_EVENTS_H 1
77

8-
void fpm_event_exit_loop();
9-
int fpm_event_loop();
10-
int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
8+
void fpm_event_exit_loop(struct event_base *base);
9+
int fpm_event_loop(struct event_base *base);
10+
int fpm_event_add(int fd, struct event_base *base, struct event *ev, void (*callback)(int, short, void *), void *arg);
1111
int fpm_event_del(struct event *ev);
1212
void fpm_event_fire(struct event *ev);
13-
int fpm_event_init_main();
13+
int fpm_event_init_main(struct event_base **base);
1414

1515

1616
#endif

sapi/fpm/fpm/fpm_main.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ typedef struct _php_cgi_globals_struct {
173173
#endif
174174
HashTable user_config_cache;
175175
char *error_header;
176+
struct event_base *event_base;
176177
} php_cgi_globals_struct;
177178

178179
/* {{{ user_config_cache
@@ -1744,11 +1745,11 @@ consult the installation file that came with this distribution, or visit \n\
17441745
}
17451746
}
17461747

1747-
if (0 > fpm_init(argc, argv, fpm_config)) {
1748+
if (0 > fpm_init(argc, argv, fpm_config, &CGIG(event_base))) {
17481749
return FAILURE;
17491750
}
17501751

1751-
fcgi_fd = fpm_run(&max_requests);
1752+
fcgi_fd = fpm_run(&max_requests, CGIG(event_base));
17521753
parent = 0;
17531754
fcgi_set_is_fastcgi(1);
17541755

@@ -1780,8 +1781,6 @@ consult the installation file that came with this distribution, or visit \n\
17801781

17811782
if (!strcasecmp(SG(request_info).request_method, "GET") && fpm_status_handle_status(SG(request_info).request_uri, SG(request_info).query_string, &status_buffer, &status_content_type)) {
17821783
if (status_buffer) {
1783-
int i;
1784-
17851784
if (status_content_type) {
17861785
sapi_add_header_ex(status_content_type, strlen(status_content_type), 1, 1 TSRMLS_CC);
17871786
} else {

0 commit comments

Comments
 (0)