Bladeren bron

Call init_thread for other threads as well

bel 9 jaren geleden
bovenliggende
commit
c419d42dd6
3 gewijzigde bestanden met toevoegingen van 33 en 8 verwijderingen
  1. 5 1
      include/civetweb.h
  2. 21 7
      src/civetweb.c
  3. 7 0
      src/timer.inl

+ 5 - 1
include/civetweb.h

@@ -209,7 +209,11 @@ struct mg_callbacks {
 	/* Called when a new worker thread is initialized.
 	/* Called when a new worker thread is initialized.
 	   Parameters:
 	   Parameters:
 	     ctx: context handle
 	     ctx: context handle
-	     thread_type: a value of 1 indicates a worker thread. */
+	     thread_type:
+	       0 indicates the master thread
+	       1 indicates a worker thread handling client connections
+	       2 indicates an internal helper thread (timer thread)
+	       */
 	void (*init_thread)(const struct mg_context *ctx, int thread_type);
 	void (*init_thread)(const struct mg_context *ctx, int thread_type);
 
 
 	/* Called when civetweb context is deleted.
 	/* Called when civetweb context is deleted.

+ 21 - 7
src/civetweb.c

@@ -11534,14 +11534,11 @@ mg_connect_client_impl(const struct mg_client_options *client_options,
 			fake_ctx.ssl_ctx = conn->client_ssl_ctx;
 			fake_ctx.ssl_ctx = conn->client_ssl_ctx;
 
 
 			/* TODO: Check ssl_verify_peer and ssl_ca_path here.
 			/* TODO: Check ssl_verify_peer and ssl_ca_path here.
-			   SSL_CTX_set_verify call is needed to switch off server
+			 * SSL_CTX_set_verify call is needed to switch off server
 			 * certificate checking, which is off by default in OpenSSL and
 			 * certificate checking, which is off by default in OpenSSL and
-			 on
-			 * in yaSSL. */
-
-			// TODO: SSL_CTX_set_verify(conn->client_ssl_ctx,
-			// SSL_VERIFY_PEER,
-			// verify_ssl_server);
+			 * on in yaSSL. */
+			/* TODO: SSL_CTX_set_verify(conn->client_ssl_ctx,
+			 * SSL_VERIFY_PEER, verify_ssl_server); */
 
 
 			if (client_options->client_cert) {
 			if (client_options->client_cert) {
 				if (!ssl_use_pem_file(&fake_ctx, client_options->client_cert)) {
 				if (!ssl_use_pem_file(&fake_ctx, client_options->client_cert)) {
@@ -11989,6 +11986,17 @@ websocket_client_thread(void *data)
 {
 {
 	struct websocket_client_thread_data *cdata =
 	struct websocket_client_thread_data *cdata =
 	    (struct websocket_client_thread_data *)data;
 	    (struct websocket_client_thread_data *)data;
+
+	mg_set_thread_name("ws-client");
+
+	if (cdata->conn->ctx) {
+		if (cdata->conn->ctx->callbacks.init_thread) {
+			/* 3 indicates a websocket client thread */
+			/* TODO: check if conn->ctx can be set */
+			cdata->conn->ctx->callbacks.init_thread(cdata->conn->ctx, 3);
+		}
+	}
+
 	read_websocket(cdata->conn, cdata->data_handler, cdata->callback_data);
 	read_websocket(cdata->conn, cdata->data_handler, cdata->callback_data);
 
 
 	DEBUG_TRACE("%s", "Websocket client thread exited\n");
 	DEBUG_TRACE("%s", "Websocket client thread exited\n");
@@ -12322,6 +12330,7 @@ worker_thread_run(void *thread_func_param)
 #endif
 #endif
 
 
 	if (ctx->callbacks.init_thread) {
 	if (ctx->callbacks.init_thread) {
+		/* call init_thread for a worker thread (type 1) */
 		ctx->callbacks.init_thread(ctx, 1);
 		ctx->callbacks.init_thread(ctx, 1);
 	}
 	}
 
 
@@ -12580,6 +12589,11 @@ master_thread_run(void *thread_func_param)
 	tls.is_master = 1;
 	tls.is_master = 1;
 	pthread_setspecific(sTlsKey, &tls);
 	pthread_setspecific(sTlsKey, &tls);
 
 
+	if (ctx->callbacks.init_thread) {
+		/* Callback for the master thread (type 0) */
+		ctx->callbacks.init_thread(ctx, 0);
+	}
+
 	/* Server starts *now* */
 	/* Server starts *now* */
 	ctx->start_time = time(NULL);
 	ctx->start_time = time(NULL);
 
 

+ 7 - 0
src/timer.inl

@@ -73,6 +73,13 @@ timer_thread_run(void *thread_func_param)
 	int re_schedule;
 	int re_schedule;
 	struct ttimer t;
 	struct ttimer t;
 
 
+	mg_set_thread_name("timer");
+
+	if (ctx->callbacks.init_thread) {
+		/* Timer thread */
+		ctx->callbacks.init_thread(ctx, 2);
+	}
+
 #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
 #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
 	/* TODO */
 	/* TODO */
 	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)
 	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)