Author: mturk Date: Fri Aug 7 12:45:12 2009 New Revision: 801978 URL: http://svn.apache.org/viewvc?rev=801978&view=rev Log: Implement windows mutex
Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c (with props) Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c?rev=801978&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c (added) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c Fri Aug 7 12:45:12 2009 @@ -0,0 +1,148 @@ +/* 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 "acr.h" +#include "acr_private.h" +#include "acr_arch.h" +#include "acr_error.h" +#include "acr_memory.h" +#include "acr_string.h" +#include "acr_descriptor.h" +#include "acr_procmutex.h" + +static int mutex_cleanup(void *mutex, int type, unsigned int flags) +{ + if (type != ACR_DT_MUTEX) { + if (!IS_INVALID_HANDLE(mutex)) + CloseHandle(mutex); + return ACR_SUCCESS; + } + return ACR_EINVAL; +} + +ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname) +{ + int rc; + HANDLE m; + wchar_t *reskey; + wchar_t keybuf[256]; + + if (fname == NULL) { + reskey = NULL; + } + /* Name-based shared memory */ + else { + /* res_name_from_filename turns file into a pseudo-name + * without slashes or backslashes, and prepends the \global + * prefix on Win2K and later + */ + reskey = res_name_from_filenamew(1, keybuf, fname); + } + m = CreateMutexW(NULL, FALSE, reskey); + if (!m) + return -1; + + rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup); + return rc; +} + +ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname) +{ + int rc; + HANDLE m; + wchar_t reskey[256]; + + 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 + */ + res_name_from_filenamew(1, reskey, fname); + + m = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, reskey); + if (!m) + return -1; + + rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup); + return rc; +} + +ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex) +{ + int rc; + HANDLE m = (HANDLE)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + rc = WaitForSingleObject(m, INFINITE); + + if (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED) { + return 0; + } + return ACR_GET_OS_ERROR(); +} + +ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex) +{ + int rc; + HANDLE m = (HANDLE)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + rc = WaitForSingleObject(m, 0); + + if (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED) { + return 0; + } + else if (rc == WAIT_TIMEOUT) { + return ACR_EBUSY; + } + return ACR_GET_OS_ERROR(); +} + +ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex) +{ + HANDLE m = (HANDLE)ACR_IOH(mutex); + + if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) { + return ACR_EINVAL; + } + + if (ReleaseMutex(m) == 0) { + return ACR_GET_OS_ERROR(); + } + return 0; +} + +ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms, + acr_uid_t uid, acr_uid_t gid) +{ + return ACR_ENOTIMPL; +} + +ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *_E, int mutex) +{ + + /* Close will call the cleanup function + */ + return acr_ioh_close(mutex); +} + Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/pmutex.c ------------------------------------------------------------------------------ svn:eol-style = native