Commit 00b289a0 authored by Sergey Lyubka's avatar Sergey Lyubka

Added thread_start() and thread_stop()

parent 9818f984
...@@ -5134,6 +5134,11 @@ static void *worker_thread(void *thread_func_param) { ...@@ -5134,6 +5134,11 @@ static void *worker_thread(void *thread_func_param) {
conn->ctx = ctx; conn->ctx = ctx;
conn->request_info.user_data = ctx->user_data; conn->request_info.user_data = ctx->user_data;
if (ctx->callbacks.thread_start != NULL) {
ctx->callbacks.thread_start(&conn->request_info.user_data,
&conn->request_info.conn_data);
}
// Call consume_socket() even when ctx->stop_flag > 0, to let it signal // Call consume_socket() even when ctx->stop_flag > 0, to let it signal
// sq_empty condvar to wake up the master waiting in produce_socket() // sq_empty condvar to wake up the master waiting in produce_socket()
while (consume_socket(ctx, &conn->client)) { while (consume_socket(ctx, &conn->client)) {
...@@ -5160,6 +5165,11 @@ static void *worker_thread(void *thread_func_param) { ...@@ -5160,6 +5165,11 @@ static void *worker_thread(void *thread_func_param) {
close_connection(conn); close_connection(conn);
} }
free(conn); free(conn);
if (ctx->callbacks.thread_stop != NULL) {
ctx->callbacks.thread_stop(&conn->request_info.user_data,
&conn->request_info.conn_data);
}
} }
// Signal master that we're done with connection and exiting // Signal master that we're done with connection and exiting
...@@ -5253,6 +5263,10 @@ static void *master_thread(void *thread_func_param) { ...@@ -5253,6 +5263,10 @@ static void *master_thread(void *thread_func_param) {
pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param); pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param);
#endif #endif
if (ctx->callbacks.thread_start != NULL) {
ctx->callbacks.thread_start(&ctx->user_data, NULL);
}
pfd = (struct pollfd *) calloc(ctx->num_listening_sockets, sizeof(pfd[0])); pfd = (struct pollfd *) calloc(ctx->num_listening_sockets, sizeof(pfd[0]));
while (pfd != NULL && ctx->stop_flag == 0) { while (pfd != NULL && ctx->stop_flag == 0) {
for (i = 0; i < ctx->num_listening_sockets; i++) { for (i = 0; i < ctx->num_listening_sockets; i++) {
...@@ -5299,6 +5313,10 @@ static void *master_thread(void *thread_func_param) { ...@@ -5299,6 +5313,10 @@ static void *master_thread(void *thread_func_param) {
#endif #endif
DEBUG_TRACE(("exiting")); DEBUG_TRACE(("exiting"));
if (ctx->callbacks.thread_stop != NULL) {
ctx->callbacks.thread_stop(&ctx->user_data, NULL);
}
// Signal mg_stop() that we're done. // Signal mg_stop() that we're done.
// WARNING: This must be the very last thing this // WARNING: This must be the very last thing this
// thread does, as ctx becomes invalid after this line. // thread does, as ctx becomes invalid after this line.
......
...@@ -40,7 +40,7 @@ struct mg_request_info { ...@@ -40,7 +40,7 @@ struct mg_request_info {
int remote_port; // Client's port int remote_port; // Client's port
int is_ssl; // 1 if SSL-ed, 0 if not int is_ssl; // 1 if SSL-ed, 0 if not
void *user_data; // User data pointer passed to mg_start() void *user_data; // User data pointer passed to mg_start()
void *conn_data; // Connection-specific user data void *conn_data; // Connection-specific, per-thread user data.
int num_headers; // Number of HTTP headers int num_headers; // Number of HTTP headers
struct mg_header { struct mg_header {
...@@ -116,10 +116,23 @@ struct mg_callbacks { ...@@ -116,10 +116,23 @@ struct mg_callbacks {
// file_file: full path name to the uploaded file. // file_file: full path name to the uploaded file.
void (*upload)(struct mg_connection *, const char *file_name); void (*upload)(struct mg_connection *, const char *file_name);
// Called when mongoose is about to send HTTP error to the client. // Called at the beginning of mongoose's thread execution in the context of
// Implementing this callback allows to create custom error pages. // that thread. To be used to perform any extra per-thread initialization.
// Parameters: // Parameters:
// status: HTTP error status code. // user_data: pointer passed to mg_start
// conn_data: per-connection, i.e. per-thread pointer. Can be used to
// store per-thread data, for example, database connection
// handles. Persistent between connections handled by the
// same thread.
// NOTE: this parameter is NULL for master thread, and non-NULL
// for worker threads.
void (*thread_start)(void *user_data, void **conn_data);
// Called when mongoose's thread is about to terminate.
// Same as thread_setup() callback, but called when thread is about to be
// destroyed. Used to cleanup the state initialized by thread_setup().
// Parameters: see thread_start().
void (*thread_stop)(void *user_data, void **conn_data);
}; };
// Start web server. // Start web server.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment