From 66d769e1b2c4e15174e82b292f2595842f081fdb Mon Sep 17 00:00:00 2001 From: Ivan Zhakov Date: Sun, 26 May 2024 19:40:59 +0000 Subject: [PATCH] win32: Use SRW Lock instead of CRITICAL_SECTION: - The are the same in terms of performance. - The SRW Lock can be statically initialized and does not need to be freed. * include/arch/win32/apr_arch_threadproc.h (apr_threadproc_init): Remove declaration. * misc/win32/start.c (): Do not include apr_arch_threadproc.h. (apr_initialize): Do not call apr_threadproc_init() because it's not longer exists. * threadproc/win32/proc.c (proc_lock): Change type to SRW_LOCK and initialize it to SRWLOCK_INIT. (threadproc_global_cleanup, apr_threadproc_init): Remove. (apr_proc_create): Use AcquireSRWLockExclusive()/ReleaseSRWLockExclusive() instead of EnterCriticalSection()/LeaveCriticalSection(). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1917990 13f79535-47bb-0310-9956-ffa450edef68 --- include/arch/win32/apr_arch_threadproc.h | 2 -- misc/win32/start.c | 3 -- threadproc/win32/proc.c | 38 +++++------------------- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/include/arch/win32/apr_arch_threadproc.h b/include/arch/win32/apr_arch_threadproc.h index 23b0172ec69..6bcd90e7fa3 100644 --- a/include/arch/win32/apr_arch_threadproc.h +++ b/include/arch/win32/apr_arch_threadproc.h @@ -68,7 +68,5 @@ struct apr_thread_once_t { INIT_ONCE once; }; -extern apr_status_t apr_threadproc_init(apr_pool_t *pool); - #endif /* ! THREAD_PROC_H */ diff --git a/misc/win32/start.c b/misc/win32/start.c index 2524707c25b..bcb1ab631b2 100644 --- a/misc/win32/start.c +++ b/misc/win32/start.c @@ -23,7 +23,6 @@ #include "apr_arch_misc.h" /* for WSAHighByte / WSALowByte */ #include "wchar.h" #include "apr_arch_file_io.h" /* bring in unicode-ness */ -#include "apr_arch_threadproc.h" /* bring in apr_threadproc_init */ #include "assert.h" /* This symbol is _private_, although it must be exported. @@ -197,8 +196,6 @@ APR_DECLARE(apr_status_t) apr_initialize(void) apr_signal_init(pool); - apr_threadproc_init(pool); - return APR_SUCCESS; } diff --git a/threadproc/win32/proc.c b/threadproc/win32/proc.c index 84fe77ed587..3d12f6e3258 100644 --- a/threadproc/win32/proc.c +++ b/threadproc/win32/proc.c @@ -465,32 +465,10 @@ APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr, return APR_SUCCESS; } -/* Used only for the NT code path, a critical section is the fastest - * implementation available. +/* Used only for the NT code path. Use Slim RW lock because it does not + * require free. */ -static CRITICAL_SECTION proc_lock; - -static apr_status_t threadproc_global_cleanup(void *ignored) -{ - DeleteCriticalSection(&proc_lock); - return APR_SUCCESS; -} - -/* Called from apr_initialize, we need a critical section to handle - * the pipe inheritance on win32. This will mutex any process create - * so as we change our inherited pipes, we prevent another process from - * also inheriting those alternate handles, and prevent the other process - * from failing to inherit our standard handles. - */ -apr_status_t apr_threadproc_init(apr_pool_t *pool) -{ - InitializeCriticalSection(&proc_lock); - /* register the cleanup */ - apr_pool_cleanup_register(pool, &proc_lock, - threadproc_global_cleanup, - apr_pool_cleanup_null); - return APR_SUCCESS; -} +static SRWLOCK proc_lock = SRWLOCK_INIT; APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, const char *progname, @@ -794,10 +772,10 @@ APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, si.wShowWindow = SW_HIDE; } - /* LOCK CRITICAL SECTION + /* LOCK SRW Lock * before we begin to manipulate the inherited handles */ - EnterCriticalSection(&proc_lock); + AcquireSRWLockExclusive(&proc_lock); if ((attr->child_in && attr->child_in->filehand) || (attr->child_out && attr->child_out->filehand) @@ -862,7 +840,7 @@ APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, rv = apr_get_os_error(); CloseHandle(attr->user_token); attr->user_token = NULL; - LeaveCriticalSection(&proc_lock); + ReleaseSRWLockExclusive(&proc_lock); return rv; } if (!CreateProcessAsUserW(attr->user_token, @@ -932,10 +910,10 @@ APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, } } - /* RELEASE CRITICAL SECTION + /* RELEASE SRW Lock * The state of the inherited handles has been restored. */ - LeaveCriticalSection(&proc_lock); + ReleaseSRWLockExclusive(&proc_lock); }