summaryrefslogtreecommitdiff
path: root/server/mpm/winnt/child.c
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2022-01-25 17:34:57 +0000
committerYann Ylavic <ylavic@apache.org>2022-01-25 17:34:57 +0000
commit604485905718803811a4b68cd2717a3712500d4a (patch)
treee4b7c189dacd6e94365d420edb5e59d01f4b965b /server/mpm/winnt/child.c
parenta6f105f7d4451f64b98cff16804a0158ddd29bff (diff)
downloadhttpd-604485905718803811a4b68cd2717a3712500d4a.tar.gz
core: Efficient ap_thread_current() when apr_thread_local() is missing.
#define ap_thread_create, ap_thread_current_create and ap_thread_current to their apr-1.8+ equivalent if available, or implement them using the compiler's thread_local mechanism if available, or finally provide stubs otherwise. #define AP_HAS_THREAD_LOCAL to 1 in the two former case or 0 otherwise, while AP_THREAD_LOCAL is defined to the compiler's keyword iff AP_HAS_THREAD_LOCAL. Replace all apr_thread_create() calls with ap_thread_create() so that httpd threads can use ap_thread_current()'s pool data as Thread Local Storage. Bump MMN minor. * include/httpd.h(): Define AP_HAS_THREAD_LOCAL, AP_THREAD_LOCAL (eventually), ap_thread_create(), ap_thread_current_create() and ap_thread_current(). * server/util.c: Implement ap_thread_create(), ap_thread_current_create() and ap_thread_current() when APR < 1.8. * modules/core/mod_watchdog.c, modules/http2/h2_workers.c, modules/ssl/mod_ssl_ct.c: Use ap_thread_create() instead of apr_thread_create. * server/main.c: Use AP_HAS_THREAD_LOCAL and ap_thread_current_create instead of APR's. * server/util_pcre.c: Use AP_HAS_THREAD_LOCAL and ap_thread_current instead of APR's. * server/mpm/event/event.c, server/mpm/worker/worker.c, server/mpm/prefork/prefork.c: Use ap_thread_create() instead of apr_thread_create. Create an apr_thread_t/ap_thread_current() for the main chaild thread usable at child_init(). * server/mpm/winnt/child.c: Use ap_thread_create() instead of CreateThread(). Create an apr_thread_t/ap_thread_current() for the main chaild thread usable git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1897460 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'server/mpm/winnt/child.c')
-rw-r--r--server/mpm/winnt/child.c107
1 files changed, 69 insertions, 38 deletions
diff --git a/server/mpm/winnt/child.c b/server/mpm/winnt/child.c
index fd63cfc666..52ff30ff51 100644
--- a/server/mpm/winnt/child.c
+++ b/server/mpm/winnt/child.c
@@ -778,24 +778,27 @@ static winnt_conn_ctx_t *winnt_get_connection(winnt_conn_ctx_t *context)
return context;
}
+struct worker_info {
+ apr_thread_t *thd;
+ HANDLE handle;
+ int num;
+};
+
/*
- * worker_main()
+ * worker_thread()
* Main entry point for the worker threads. Worker threads block in
* win*_get_connection() awaiting a connection to service.
*/
-static DWORD __stdcall worker_main(void *thread_num_val)
+static void *APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void *ctx)
{
- apr_thread_t *thd;
- apr_os_thread_t osthd;
+ struct worker_info *info = ctx;
static int requests_this_child = 0;
winnt_conn_ctx_t *context = NULL;
- int thread_num = (int)thread_num_val;
+ int thread_num = info->num;
ap_sb_handle_t *sbh;
conn_rec *c;
apr_int32_t disconnected;
- osthd = apr_os_thread_current();
-
while (1) {
ap_update_child_status_from_indexes(0, thread_num, SERVER_READY, NULL);
@@ -827,8 +830,6 @@ static DWORD __stdcall worker_main(void *thread_num_val)
continue;
}
- thd = NULL;
- apr_os_thread_put(&thd, &osthd, context->ptrans);
c->current_thread = thd;
ap_process_connection(c, context->sock);
@@ -843,6 +844,7 @@ static DWORD __stdcall worker_main(void *thread_num_val)
ap_update_child_status_from_indexes(0, thread_num, SERVER_DEAD, NULL);
+ SetEvent(info->handle);
return 0;
}
@@ -889,13 +891,21 @@ static void create_listener_thread(void)
}
+#if AP_HAS_THREAD_LOCAL
+static apr_status_t main_thread_cleanup(void *arg)
+{
+ apr_thread_t *thd = arg;
+ apr_pool_destroy(apr_thread_pool_get(thd));
+ return APR_SUCCESS;
+}
+#endif
+
void child_main(apr_pool_t *pconf, DWORD parent_pid)
{
apr_status_t status;
- apr_hash_t *ht;
ap_listen_rec *lr;
HANDLE child_events[3];
- HANDLE *child_handles;
+ struct worker_info *workers;
int listener_started = 0;
int threads_created = 0;
int time_remains;
@@ -911,8 +921,29 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
apr_pool_create(&pchild, pconf);
apr_pool_tag(pchild, "pchild");
+#if AP_HAS_THREAD_LOCAL
+ /* Create an apr_thread_t for the main child thread to set up its
+ * Thread Local Storage. Since it's detached and it won't
+ * apr_thread_exit(), destroy its pool before exiting via
+ * a pchild cleanup
+ */
+ {
+ apr_thread_t *main_thd = NULL;
+ apr_threadattr_t *main_thd_attr = NULL;
+ if (apr_threadattr_create(&main_thd_attr, pchild)
+ || apr_threadattr_detach_set(main_thd_attr, 1)
+ || ap_thread_current_create(&main_thd, main_thd_attr,
+ pchild)) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf, APLOGNO()
+ "Couldn't initialize child main thread");
+ exit(APEXIT_CHILDINIT);
+ }
+ apr_pool_cleanup_register(pchild, main_thd, main_thread_cleanup,
+ apr_pool_cleanup_null);
+ }
+#endif
+
ap_run_child_init(pchild, ap_server_conf);
- ht = apr_hash_make(pchild);
listener_shutdown_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!listener_shutdown_event) {
@@ -983,15 +1014,13 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
*/
ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf, APLOGNO(00354)
"Child: Starting %d worker threads.", ap_threads_per_child);
- child_handles = (HANDLE) apr_pcalloc(pchild, ap_threads_per_child
- * sizeof(HANDLE));
+ workers = apr_pcalloc(pchild, ap_threads_per_child * sizeof(*workers));
apr_thread_mutex_create(&child_lock, APR_THREAD_MUTEX_DEFAULT, pchild);
while (1) {
int from_previous_generation = 0, starting_up = 0;
for (i = 0; i < ap_threads_per_child; i++) {
- int *score_idx;
int status = ap_scoreboard_image->servers[0][i].status;
if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
if (ap_scoreboard_image->servers[0][i].generation != my_generation) {
@@ -1004,13 +1033,24 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
}
ap_update_child_status_from_indexes(0, i, SERVER_STARTING, NULL);
- child_handles[i] = CreateThread(NULL, ap_thread_stacksize,
- worker_main, (void *) i,
- stack_res_flag, &tid);
- if (child_handles[i] == 0) {
- ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(),
- ap_server_conf, APLOGNO(00355)
- "Child: CreateThread failed. Unable to "
+ workers[i].num = i;
+ workers[i].handle = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!workers[i].handle) {
+ rv = apr_get_os_error();
+ }
+ else {
+ apr_threadattr_t *thread_attr = NULL;
+ apr_threadattr_create(&thread_attr, pchild);
+ if (ap_thread_stacksize != 0) {
+ apr_threadattr_stacksize_set(thread_attr,
+ ap_thread_stacksize);
+ }
+ rv = ap_thread_create(&workers[i].thd, thread_attr,
+ worker_thread, &workers[i], pchild);
+ }
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00355)
+ "Child: thread creation failed. Unable to "
"create all worker threads. Created %d of the %d "
"threads requested with the ThreadsPerChild "
"configuration directive.",
@@ -1021,14 +1061,6 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
ap_scoreboard_image->servers[0][i].pid = my_pid;
ap_scoreboard_image->servers[0][i].generation = my_generation;
threads_created++;
- /* Save the score board index in ht keyed to the thread handle.
- * We need this when cleaning up threads down below...
- */
- apr_thread_mutex_lock(child_lock);
- score_idx = apr_pcalloc(pchild, sizeof(int));
- *score_idx = i;
- apr_hash_set(ht, &child_handles[i], sizeof(HANDLE), score_idx);
- apr_thread_mutex_unlock(child_lock);
}
/* Start the listener only when workers are available */
if (!listener_started && threads_created) {
@@ -1199,7 +1231,7 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
while (threads_created)
{
- HANDLE handle = child_handles[threads_created - 1];
+ struct worker_info *info = workers[threads_created - 1];
DWORD dwRet;
if (time_remains < 0)
@@ -1213,13 +1245,15 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
time_remains / 1000, threads_created);
}
- dwRet = WaitForSingleObject(handle, 100);
+ dwRet = WaitForSingleObject(info->handle, 100);
time_remains -= 100;
if (dwRet == WAIT_TIMEOUT) {
/* Keep waiting */
}
else if (dwRet == WAIT_OBJECT_0) {
- CloseHandle(handle);
+ apr_status_t thread_rv;
+ apr_thread_join(&thread_rv, info->thd);
+ CloseHandle(info->handle);
threads_created--;
}
else {
@@ -1232,11 +1266,8 @@ void child_main(apr_pool_t *pconf, DWORD parent_pid)
"Child: Waiting for %d threads timed out, terminating process.",
threads_created);
for (i = 0; i < threads_created; i++) {
- /* Reset the scoreboard entries for the threads. */
- int *idx = apr_hash_get(ht, &child_handles[i], sizeof(HANDLE));
- if (idx) {
- ap_update_child_status_from_indexes(0, *idx, SERVER_DEAD, NULL);
- }
+ struct worker_info *info = workers[i];
+ ap_update_child_status_from_indexes(0, info->num, SERVER_DEAD, NULL);
}
/* We can't wait for any longer, but still have some threads remaining.
*