Add close_socket fucntions and rise the scope for mutex for accessing s_last_thread

This commit is contained in:
Grégory Soutadé 2016-02-04 20:39:50 +01:00
parent 54184da72c
commit 9b0bdae64e
3 changed files with 1218 additions and 327 deletions

View File

@ -1,7 +1,12 @@
.PHONY: data
all:
make -C src
data:
make -C data
clean:
make -C src clean

File diff suppressed because it is too large Load Diff

View File

@ -143,6 +143,9 @@ static void delete_thread(thread_ctx_t* thread_ctx)
{
int i;
pthread_mutex_lock(&s_fastmutex);
thread_ctx->nb_available_sockets = 0;
if (thread_ctx->quiet < 0)
syslog(LOG_DEBUG, "Delete thread %p", thread_ctx);
@ -156,19 +159,24 @@ static void delete_thread(thread_ctx_t* thread_ctx)
free(thread_ctx->sockets);
pthread_mutex_lock(&s_fastmutex);
if (thread_ctx->next)
thread_ctx->next->prev = thread_ctx->prev;
if (thread_ctx->prev)
thread_ctx->prev->next = thread_ctx->next;
if (thread_ctx == s_last_thread)
s_last_thread = NULL;
s_last_thread = thread_ctx->next;
pthread_mutex_unlock(&s_fastmutex);
free(thread_ctx);
}
static inline void close_socket(socket_ctx_t* socket)
{
socket->timeout = -1;
close(socket->socket);
}
static void* thread_loop(void* param)
{
thread_ctx_t* ctx = (thread_ctx_t*)param;
@ -217,10 +225,7 @@ static void* thread_loop(void* param)
for(i=0; i<ctx->nb_cur_sockets; i++)
{
if (ctx->sockets[i].timeout > 0)
{
close (ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
}
close_socket(&ctx->sockets[i]);
}
}
else if (ret < 0)
@ -235,8 +240,7 @@ static void* thread_loop(void* param)
if (ctx->sockets[i].timeout < 0) continue;
if (FD_ISSET(ctx->sockets[i].socket, &exc_set))
{
close (ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
close_socket(&ctx->sockets[i]);
continue;
}
// Someone is speaking
@ -246,14 +250,19 @@ static void* thread_loop(void* param)
ret = handle_request(ctx, ctx->sockets[i].socket);
if (ret == 1)
{
close(ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
if (ctx->quiet < 0)
syslog(LOG_DEBUG, "Client has closed socket %d",
ctx->sockets[i].socket);
close_socket(&ctx->sockets[i]);
}
// No more requests accepted
if (!ctx->sockets[i].nb_remaining_requests--)
{
close (ctx->sockets[i].socket);
ctx->sockets[i].timeout = -1;
if (ctx->quiet < 0)
syslog(LOG_DEBUG, "Max requests reached for socket %d",
ctx->sockets[i].socket);
syncfs(ctx->sockets[i].socket);
close_socket(&ctx->sockets[i]);
}
}
else
@ -290,28 +299,31 @@ static inline thread_ctx_t* create_thread_ctx(struct gengetopt_args_info* params
thread_ctx->prev = NULL;
pthread_mutex_init(&thread_ctx->mutex, NULL);
pthread_mutex_lock(&s_fastmutex);
thread_ctx->next = s_last_thread;
if (s_last_thread)
s_last_thread->prev = thread_ctx;
else
s_last_thread = thread_ctx;
pthread_mutex_unlock(&s_fastmutex);
return thread_ctx;
}
static void fill_new_socket(struct gengetopt_args_info* params, int socket)
{
thread_ctx_t* thread_ctx = s_last_thread;
thread_ctx_t* thread_ctx;
int launch_thread = 0;
pthread_mutex_lock(&s_fastmutex);
thread_ctx = s_last_thread;
if (!thread_ctx || !thread_ctx->nb_available_sockets)
{
thread_ctx = create_thread_ctx(params);
launch_thread = 1;
}
pthread_mutex_unlock(&s_fastmutex);
thread_ctx->sockets[thread_ctx->nb_cur_sockets].socket = socket;
thread_ctx->sockets[thread_ctx->nb_cur_sockets].timeout = thread_ctx->max_timeout;
thread_ctx->sockets[thread_ctx->nb_cur_sockets].nb_remaining_requests = params->client_max_requests_arg;