IP to Geo

IP to Geo Commit Details

Date:2016-02-04 20:39:50 (4 years 9 months ago)
Author:Grégory Soutadé
Branch:master
Commit:d110f892b46fbaa1a1d6c4914eb8955bd05c7cf0
Parents: a10ce763812541ebbd40cc95b98a562c87dc46cc
Message:Don't read thread_ctx nb_available_sockets and nb_cur_sockets as they may be changed. Use temporary variables. Use microseconds timeout instead of seconds

Changes:
Msrc/server.c (7 diffs)

File differences

src/server.c
180180
181181
182182
183
183
184184
185185
186186
......
190190
191191
192192
193
193
194
195
196
197
198
194199
195200
196201
197202
198203
199
200
204
205
201206
202207
203208
204209
205210
206
207
211
212
213
214
215
208216
209217
210218
......
214222
215223
216224
217
225
218226
219
220227
221228
222229
223230
224231
225
232
226233
227234
228235
......
235242
236243
237244
238
245
239246
240247
241248
242249
243250
244
245251
246252
247
253
248254
249255
250256
......
256262
257263
258264
259
265
260266
261267
262268
......
267273
268274
269275
270
276
271277
272278
273279
274
275280
276281
277282
......
291296
292297
293298
294
299
295300
296301
297302
static void* thread_loop(void* param)
{
thread_ctx_t* ctx = (thread_ctx_t*)param;
int i, ret, nfds;
int i, ret, nfds, nb_cur_sockets, nb_available_sockets;
fd_set read_set, exc_set;
struct timeval timeout;
FD_ZERO(&exc_set);
nfds = 0;
for(i=0; i<ctx->nb_cur_sockets; i++)
pthread_mutex_lock(&ctx->mutex);
nb_cur_sockets = ctx->nb_cur_sockets;
nb_available_sockets = ctx->nb_available_sockets;
pthread_mutex_unlock(&ctx->mutex);
for(i=0; i<nb_cur_sockets; i++)
{
if (ctx->sockets[i].timeout > 0)
{
FD_SET(ctx->sockets[i].socket, &read_set);
FD_SET(ctx->sockets[i].socket, &exc_set);
if (ctx->sockets[i].socket+1 > nfds)
nfds = ctx->sockets[i].socket+1;
if (ctx->sockets[i].socket > nfds)
nfds = ctx->sockets[i].socket;
}
}
if (!nfds)
{
// No more active socket for this thread
if (!ctx->nb_available_sockets)
/*
No more active socket for this thread
nor available slots
*/
if (!nb_available_sockets)
break;
usleep(100);
timeout.tv_sec = ctx->max_timeout;
timeout.tv_usec = 0;
ret = select(nfds, &read_set, NULL, &exc_set, &timeout);
ret = select(nfds+1, &read_set, NULL, &exc_set, &timeout);
pthread_mutex_lock(&ctx->mutex);
// Timeout, remove all current sockets
if (ret == 0)
{
if (ctx->quiet < 0)
syslog(LOG_DEBUG, "Timeout");
for(i=0; i<ctx->nb_cur_sockets; i++)
for(i=0; i<nb_cur_sockets; i++)
{
if (ctx->sockets[i].timeout > 0)
close_socket(&ctx->sockets[i]);
}
else
{
for(i=0; i<ctx->nb_cur_sockets; i++)
for(i=0; i<nb_cur_sockets; i++)
{
if (ctx->sockets[i].timeout < 0) continue;
if (FD_ISSET(ctx->sockets[i].socket, &exc_set))
{
close_socket(&ctx->sockets[i]);
continue;
}
// Someone is speaking
if (FD_ISSET(ctx->sockets[i].socket, &read_set))
else if (FD_ISSET(ctx->sockets[i].socket, &read_set))
{
ctx->sockets[i].timeout = ctx->max_timeout;
ret = handle_request(ctx, ctx->sockets[i].socket);
close_socket(&ctx->sockets[i]);
}
// No more requests accepted
if (!ctx->sockets[i].nb_remaining_requests--)
else if (!ctx->sockets[i].nb_remaining_requests--)
{
if (ctx->quiet < 0)
syslog(LOG_DEBUG, "Max requests reached for socket %d",
}
else
{
ctx->sockets[i].timeout -= timeout.tv_sec;
ctx->sockets[i].timeout -= timeout.tv_sec*1000000 + timeout.tv_usec;
}
}
}
pthread_mutex_unlock(&ctx->mutex);
};
delete_thread(ctx);
thread_ctx->sockets = malloc(sizeof(*thread_ctx->sockets)*params->sockets_per_thread_arg);
thread_ctx->nb_cur_sockets = 0;
thread_ctx->nb_available_sockets = params->sockets_per_thread_arg;
thread_ctx->max_timeout = params->sockets_timeout_arg;
thread_ctx->max_timeout = params->sockets_timeout_arg*1000000;
thread_ctx->stop = 0;
thread_ctx->quiet = params->quiet_flag;
if (params->verbose_flag)

Archive Download the corresponding diff file

Branches

Tags