diff options
author | hongbotian <hongbo.tianhongbo@huawei.com> | 2015-11-30 01:45:08 -0500 |
---|---|---|
committer | hongbotian <hongbo.tianhongbo@huawei.com> | 2015-11-30 01:45:08 -0500 |
commit | e8ec7aa8e38a93f5b034ac74cebce5de23710317 (patch) | |
tree | aa031937bf856c1f8d6ad7877b8d2cb0224da5ef /rubbos/app/httpd-2.0.64/srclib/apr/locks/win32 | |
parent | cc40af334e619bb549038238507407866f774f8f (diff) |
upload http
JIRA: BOTTLENECK-10
Change-Id: I7598427ff904df438ce77c2819ee48ac75ffa8da
Signed-off-by: hongbotian <hongbo.tianhongbo@huawei.com>
Diffstat (limited to 'rubbos/app/httpd-2.0.64/srclib/apr/locks/win32')
4 files changed, 652 insertions, 0 deletions
diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/proc_mutex.c b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/proc_mutex.c new file mode 100644 index 00000000..3b249045 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/proc_mutex.c @@ -0,0 +1,223 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr.h" +#include "apr_private.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "apr_portable.h" +#include "apr_arch_file_io.h" +#include "apr_arch_proc_mutex.h" +#include "apr_arch_misc.h" + +static apr_status_t proc_mutex_cleanup(void *mutex_) +{ + apr_proc_mutex_t *mutex = mutex_; + + if (mutex->handle) { + if (CloseHandle(mutex->handle) == 0) { + return apr_get_os_error(); + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex, + const char *fname, + apr_lockmech_e mech, + apr_pool_t *pool) +{ + HANDLE hMutex; + void *mutexkey; + + /* res_name_from_filename turns fname into a pseduo-name + * without slashes or backslashes, and prepends the \global + * prefix on Win2K and later + */ + if (fname) { + mutexkey = res_name_from_filename(fname, 1, pool); + } + else { + mutexkey = NULL; + } + +#if APR_HAS_UNICODE_FS + IF_WIN_OS_IS_UNICODE + { + hMutex = CreateMutexW(NULL, FALSE, mutexkey); + } +#endif +#if APR_HAS_ANSI_FS + ELSE_WIN_OS_IS_ANSI + { + hMutex = CreateMutexA(NULL, FALSE, mutexkey); + } +#endif + + if (!hMutex) { + return apr_get_os_error(); + } + + *mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t)); + (*mutex)->pool = pool; + (*mutex)->handle = hMutex; + (*mutex)->fname = fname; + apr_pool_cleanup_register((*mutex)->pool, *mutex, + proc_mutex_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex, + const char *fname, + apr_pool_t *pool) +{ + HANDLE hMutex; + void *mutexkey; + + if (!fname) { + /* Reinitializing unnamed mutexes is a noop in the Unix code. */ + return APR_SUCCESS; + } + + /* res_name_from_filename turns file into a pseudo-name + * without slashes or backslashes, and prepends the \global + * prefix on Win2K and later + */ + mutexkey = res_name_from_filename(fname, 1, pool); + +#if defined(_WIN32_WCE) + hMutex = CreateMutex(NULL, FALSE, mutexkey); + if (hMutex && ERROR_ALREADY_EXISTS != GetLastError()) { + CloseHandle(hMutex); + hMutex = NULL; + SetLastError(ERROR_FILE_NOT_FOUND); + } +#else +#if APR_HAS_UNICODE_FS + IF_WIN_OS_IS_UNICODE + { + hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexkey); + } +#endif +#if APR_HAS_ANSI_FS + ELSE_WIN_OS_IS_ANSI + { + hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexkey); + } +#endif +#endif + + if (!hMutex) { + return apr_get_os_error(); + } + + *mutex = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t)); + (*mutex)->pool = pool; + (*mutex)->handle = hMutex; + (*mutex)->fname = fname; + apr_pool_cleanup_register((*mutex)->pool, *mutex, + proc_mutex_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex) +{ + DWORD rv; + + rv = WaitForSingleObject(mutex->handle, INFINITE); + + if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) { + return APR_SUCCESS; + } + return apr_get_os_error(); +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex) +{ + DWORD rv; + + rv = WaitForSingleObject(mutex->handle, 0); + + if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) { + return APR_SUCCESS; + } + else if (rv == WAIT_TIMEOUT) { + return APR_EBUSY; + } + return apr_get_os_error(); +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex) +{ + if (ReleaseMutex(mutex->handle) == 0) { + return apr_get_os_error(); + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex) +{ + apr_status_t stat; + + stat = proc_mutex_cleanup(mutex); + if (stat == APR_SUCCESS) { + apr_pool_cleanup_kill(mutex->pool, mutex, proc_mutex_cleanup); + } + return stat; +} + +APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex) +{ + return NULL; +} + +APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex) +{ + return mutex->fname; +} + +APR_DECLARE(const char *) apr_proc_mutex_defname(void) +{ + return "win32mutex"; +} + +APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex) + +/* Implement OS-specific accessors defined in apr_portable.h */ + +APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex, + apr_proc_mutex_t *mutex) +{ + *ospmutex = mutex->handle; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex, + apr_os_proc_mutex_t *ospmutex, + apr_pool_t *pool) +{ + if (pool == NULL) { + return APR_ENOPOOL; + } + if ((*pmutex) == NULL) { + (*pmutex) = (apr_proc_mutex_t *)apr_palloc(pool, + sizeof(apr_proc_mutex_t)); + (*pmutex)->pool = pool; + } + (*pmutex)->handle = *ospmutex; + return APR_SUCCESS; +} + diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_cond.c b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_cond.c new file mode 100644 index 00000000..ac71a419 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_cond.c @@ -0,0 +1,128 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr.h" +#include "apr_private.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "win32/apr_arch_thread_mutex.h" +#include "win32/apr_arch_thread_cond.h" +#include "apr_portable.h" + +static apr_status_t thread_cond_cleanup(void *data) +{ + apr_thread_cond_t *cond = data; + CloseHandle(cond->event); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond, + apr_pool_t *pool) +{ + *cond = apr_palloc(pool, sizeof(**cond)); + (*cond)->pool = pool; + (*cond)->event = CreateEvent(NULL, TRUE, FALSE, NULL); + (*cond)->signal_all = 0; + (*cond)->num_waiting = 0; + return APR_SUCCESS; +} + +static APR_INLINE apr_status_t _thread_cond_timedwait(apr_thread_cond_t *cond, + apr_thread_mutex_t *mutex, + DWORD timeout_ms ) +{ + DWORD res; + + while (1) { + cond->num_waiting++; + + apr_thread_mutex_unlock(mutex); + res = WaitForSingleObject(cond->event, timeout_ms); + apr_thread_mutex_lock(mutex); + cond->num_waiting--; + if (res != WAIT_OBJECT_0) { + apr_status_t rv = apr_get_os_error(); + if (res == WAIT_TIMEOUT) { + return APR_TIMEUP; + } + return apr_get_os_error(); + } + if (cond->signal_all) { + if (cond->num_waiting == 0) { + cond->signal_all = 0; + cond->signalled = 0; + ResetEvent(cond->event); + } + break; + } + else if (cond->signalled) { + cond->signalled = 0; + ResetEvent(cond->event); + break; + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond, + apr_thread_mutex_t *mutex) +{ + return _thread_cond_timedwait(cond, mutex, INFINITE); +} + +APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond, + apr_thread_mutex_t *mutex, + apr_interval_time_t timeout) +{ + DWORD timeout_ms = (DWORD) apr_time_as_msec(timeout); + + return _thread_cond_timedwait(cond, mutex, timeout_ms); +} + +APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond) +{ + apr_status_t rv = APR_SUCCESS; + DWORD res; + + cond->signalled = 1; + res = SetEvent(cond->event); + if (res == 0) { + rv = apr_get_os_error(); + } + return rv; +} + +APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond) +{ + apr_status_t rv = APR_SUCCESS; + DWORD res; + + cond->signalled = 1; + cond->signal_all = 1; + res = SetEvent(cond->event); + if (res == 0) { + rv = apr_get_os_error(); + } + return rv; +} + +APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond) +{ + return apr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup); +} + +APR_POOL_IMPLEMENT_ACCESSOR(thread_cond) + diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_mutex.c b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_mutex.c new file mode 100644 index 00000000..9b10d727 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_mutex.c @@ -0,0 +1,136 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr.h" +#include "apr_private.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "apr_arch_thread_mutex.h" +#include "apr_thread_mutex.h" +#include "apr_portable.h" +#include "apr_arch_misc.h" + +static apr_status_t thread_mutex_cleanup(void *data) +{ + apr_thread_mutex_t *lock = data; + + if (lock->type == thread_mutex_critical_section) { + lock->type = -1; + DeleteCriticalSection(&lock->section); + } + else { + if (!CloseHandle(lock->handle)) { + return apr_get_os_error(); + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex, + unsigned int flags, + apr_pool_t *pool) +{ + (*mutex) = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(**mutex)); + + (*mutex)->pool = pool; + + if (flags & APR_THREAD_MUTEX_UNNESTED) { + /* Use an auto-reset signaled event, ready to accept one + * waiting thread. + */ + (*mutex)->type = thread_mutex_unnested_event; + (*mutex)->handle = CreateEvent(NULL, FALSE, TRUE, NULL); + } + else { +#if APR_HAS_UNICODE_FS + /* Critical Sections are terrific, performance-wise, on NT. + * On Win9x, we cannot 'try' on a critical section, so we + * use a [slower] mutex object, instead. + */ + IF_WIN_OS_IS_UNICODE { + InitializeCriticalSection(&(*mutex)->section); + (*mutex)->type = thread_mutex_critical_section; + } +#endif +#if APR_HAS_ANSI_FS + ELSE_WIN_OS_IS_ANSI { + (*mutex)->type = thread_mutex_nested_mutex; + (*mutex)->handle = CreateMutex(NULL, FALSE, NULL); + + } +#endif + } + + apr_pool_cleanup_register((*mutex)->pool, (*mutex), thread_mutex_cleanup, + apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex) +{ + if (mutex->type == thread_mutex_critical_section) { + EnterCriticalSection(&mutex->section); + } + else { + DWORD rv = WaitForSingleObject(mutex->handle, INFINITE); + if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) { + return (rv == WAIT_TIMEOUT) ? APR_EBUSY : apr_get_os_error(); + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) +{ + if (mutex->type == thread_mutex_critical_section) { + if (!TryEnterCriticalSection(&mutex->section)) { + return APR_EBUSY; + } + } + else { + DWORD rv = WaitForSingleObject(mutex->handle, 0); + if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) { + return (rv == WAIT_TIMEOUT) ? APR_EBUSY : apr_get_os_error(); + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex) +{ + if (mutex->type == thread_mutex_critical_section) { + LeaveCriticalSection(&mutex->section); + } + else if (mutex->type == thread_mutex_unnested_event) { + if (!SetEvent(mutex->handle)) { + return apr_get_os_error(); + } + } + else if (mutex->type == thread_mutex_nested_mutex) { + if (!ReleaseMutex(mutex->handle)) { + return apr_get_os_error(); + } + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex) +{ + return apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup); +} + +APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex) + diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_rwlock.c b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_rwlock.c new file mode 100644 index 00000000..7099509f --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/locks/win32/thread_rwlock.c @@ -0,0 +1,165 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr.h" +#include "apr_private.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "win32/apr_arch_thread_rwlock.h" +#include "apr_portable.h" + +static apr_status_t thread_rwlock_cleanup(void *data) +{ + apr_thread_rwlock_t *rwlock = data; + + if (! CloseHandle(rwlock->read_event)) + return apr_get_os_error(); + + if (! CloseHandle(rwlock->write_mutex)) + return apr_get_os_error(); + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t)apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock, + apr_pool_t *pool) +{ + *rwlock = apr_palloc(pool, sizeof(**rwlock)); + + (*rwlock)->pool = pool; + (*rwlock)->readers = 0; + + if (! ((*rwlock)->read_event = CreateEvent(NULL, TRUE, FALSE, NULL))) { + *rwlock = NULL; + return apr_get_os_error(); + } + + if (! ((*rwlock)->write_mutex = CreateMutex(NULL, FALSE, NULL))) { + CloseHandle((*rwlock)->read_event); + *rwlock = NULL; + return apr_get_os_error(); + } + + apr_pool_cleanup_register(pool, *rwlock, thread_rwlock_cleanup, + apr_pool_cleanup_null); + + return APR_SUCCESS; +} + +static apr_status_t apr_thread_rwlock_rdlock_core(apr_thread_rwlock_t *rwlock, + DWORD milliseconds) +{ + DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds); + + if (code == WAIT_FAILED || code == WAIT_TIMEOUT) + return APR_FROM_OS_ERROR(code); + + /* We've successfully acquired the writer mutex, we can't be locked + * for write, so it's OK to add the reader lock. The writer mutex + * doubles as race condition protection for the readers counter. + */ + InterlockedIncrement(&rwlock->readers); + + if (! ResetEvent(rwlock->read_event)) + return apr_get_os_error(); + + if (! ReleaseMutex(rwlock->write_mutex)) + return apr_get_os_error(); + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock) +{ + return apr_thread_rwlock_rdlock_core(rwlock, INFINITE); +} + +APR_DECLARE(apr_status_t) +apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock) +{ + return apr_thread_rwlock_rdlock_core(rwlock, 0); +} + +static apr_status_t +apr_thread_rwlock_wrlock_core(apr_thread_rwlock_t *rwlock, DWORD milliseconds) +{ + DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds); + + if (code == WAIT_FAILED || code == WAIT_TIMEOUT) + return APR_FROM_OS_ERROR(code); + + /* We've got the writer lock but we have to wait for all readers to + * unlock before it's ok to use it. + */ + if (rwlock->readers) { + /* Must wait for readers to finish before returning, unless this + * is an trywrlock (milliseconds == 0): + */ + code = milliseconds + ? WaitForSingleObject(rwlock->read_event, milliseconds) + : WAIT_TIMEOUT; + + if (code == WAIT_FAILED || code == WAIT_TIMEOUT) { + /* Unable to wait for readers to finish, release write lock: */ + if (! ReleaseMutex(rwlock->write_mutex)) + return apr_get_os_error(); + + return APR_FROM_OS_ERROR(code); + } + } + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock) +{ + return apr_thread_rwlock_wrlock_core(rwlock, INFINITE); +} + +APR_DECLARE(apr_status_t)apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock) +{ + return apr_thread_rwlock_wrlock_core(rwlock, 0); +} + +APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock) +{ + apr_status_t rv = 0; + + /* First, guess that we're unlocking a writer */ + if (! ReleaseMutex(rwlock->write_mutex)) + rv = apr_get_os_error(); + + if (rv == APR_FROM_OS_ERROR(ERROR_NOT_OWNER)) { + /* Nope, we must have a read lock */ + if (rwlock->readers && + ! InterlockedDecrement(&rwlock->readers) && + ! SetEvent(rwlock->read_event)) { + rv = apr_get_os_error(); + } + else { + rv = 0; + } + } + + return rv; +} + +APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock) +{ + return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup); +} + +APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock) |