Skip to content

Commit

Permalink
win32: Use SRW Lock instead of CRITICAL_SECTION:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Ivan Zhakov committed May 26, 2024
1 parent 2d727e1 commit 66d769e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 35 deletions.
2 changes: 0 additions & 2 deletions include/arch/win32/apr_arch_threadproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

3 changes: 0 additions & 3 deletions misc/win32/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -197,8 +196,6 @@ APR_DECLARE(apr_status_t) apr_initialize(void)

apr_signal_init(pool);

apr_threadproc_init(pool);

return APR_SUCCESS;
}

Expand Down
38 changes: 8 additions & 30 deletions threadproc/win32/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

}

Expand Down

0 comments on commit 66d769e

Please sign in to comment.