This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcgi_context.c
161 lines (131 loc) · 3.92 KB
/
fcgi_context.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Monkey HTTP Daemon
* ------------------
* Copyright (C) 2012, Sonny Karlsson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include "fcgi_context.h"
#include "dbg.h"
static void *(*mem_alloc)(const size_t) = &malloc;
static void (*mem_free)(void *) = &free;
void fcgi_context_module_init(void *(*mem_alloc_p)(const size_t),
void (*mem_free_p)(void *))
{
mem_alloc = mem_alloc_p;
mem_free = mem_free_p;
}
void fcgi_context_free(struct fcgi_context *tdata)
{
request_list_free(&tdata->rl);
fcgi_fd_list_free(&tdata->fdl);
chunk_list_free_chunks(&tdata->cl);
}
int fcgi_context_init(struct fcgi_context *tdata,
const struct fcgi_fd_matrix fdm,
unsigned int thread_id,
unsigned int request_capacity,
struct fcgi_config *config)
{
unsigned int request_offset = 1 + request_capacity * thread_id;
check(!request_list_init(&tdata->rl,
config->location_count,
request_offset,
request_capacity),
"Failed to init request list.");
check(!fcgi_fd_list_init(&tdata->fdl,
fdm,
thread_id,
config),
"Failed to init fd list.");
chunk_list_init(&tdata->cl);
return 0;
error:
fcgi_context_free(tdata);
return -1;
}
void fcgi_context_list_free(struct fcgi_context_list *tdlist)
{
int i;
pthread_mutex_destroy(&tdlist->thread_id_counter_mutex);
for (i = 0; i < tdlist->n; i++) {
if (!tdlist->tds[i]) {
continue;
}
fcgi_context_free(tdlist->tds[i]);
mem_free(tdlist->tds[i]);
}
mem_free(tdlist->tds);
tdlist->n = 0;
tdlist->tds = NULL;
}
int fcgi_context_list_init(struct fcgi_context_list *tdlist,
struct fcgi_config *config,
int workers,
int worker_capacity)
{
struct fcgi_context *tdata;
struct fcgi_fd_matrix fdm = fcgi_fd_matrix_create(config, workers);
const uint16_t capacity = next_power_of_2(worker_capacity);
int i;
check(capacity > 0, "No request capacity.");
check(capacity < UINT16_MAX, "Request capacity too large.");
tdlist->thread_id_counter = 0;
pthread_mutex_init(&tdlist->thread_id_counter_mutex, NULL);
tdlist->tds = mem_alloc(workers * sizeof(*tdlist->tds));
check_mem(tdlist->tds);
tdlist->n = workers;
for (i = 0; i < workers; i++) {
tdata = mem_alloc(sizeof(*tdata));
check_mem(tdata);
tdlist->tds[i] = tdata;
check(!fcgi_context_init(tdata, fdm, i, capacity, config),
"[THREAD_ID %d] Failed to init thread data.", i);
}
fcgi_fd_matrix_free(&fdm);
return 0;
error:
fcgi_fd_matrix_free(&fdm);
fcgi_context_list_free(tdlist);
return -1;
}
int fcgi_context_list_assign_thread_id(struct fcgi_context_list *tdlist)
{
int my_thread_id;
check(tdlist->thread_id_counter < tdlist->n,
"All thread id's have already assigned.");
pthread_mutex_lock(&tdlist->thread_id_counter_mutex);
my_thread_id = tdlist->thread_id_counter;
tdlist->thread_id_counter += 1;
pthread_mutex_unlock(&tdlist->thread_id_counter_mutex);
return my_thread_id;
error:
return -1;
}
struct fcgi_context *fcgi_context_list_get(
struct fcgi_context_list *tdlist,
int thread_id)
{
struct fcgi_context *tdata;
check(thread_id >= 0 && thread_id < tdlist->n,
"Thread id %d is out of range.", thread_id);
tdata = tdlist->tds[thread_id];
check(tdata, "Thread data is NULL for thread id %d.", thread_id);
return tdata;
error:
return NULL;
}