update dev300-m58
[ooovba.git] / sal / osl / w32 / mutex.c
blob82f4e2bcf330d77be289823c396763276aa685e2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mutex.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "system.h"
33 #include <osl/mutex.h>
34 #include <osl/diagnose.h>
37 Implementation notes:
38 The void* hidden by oslMutex points to a WIN32
39 CRITICAL_SECTION structure.
42 typedef struct _oslMutexImpl {
43 CRITICAL_SECTION m_Mutex;
44 int m_Locks;
45 DWORD m_Owner;
46 DWORD m_Requests;
47 } oslMutexImpl;
49 static BOOL (WINAPI *lpfTryEnterCriticalSection)(LPCRITICAL_SECTION)
50 = (BOOL (WINAPI *)(LPCRITICAL_SECTION))0xFFFFFFFF;
52 static CRITICAL_SECTION MutexLock;
54 /*****************************************************************************/
55 /* osl_createMutex */
56 /*****************************************************************************/
57 oslMutex SAL_CALL osl_createMutex(void)
59 oslMutexImpl *pMutexImpl;
61 /* Window 95 does not support "TryEnterCriticalSection" */
63 if (lpfTryEnterCriticalSection ==
64 (BOOL (WINAPI *)(LPCRITICAL_SECTION))0xFFFFFFFF)
66 OSVERSIONINFO VersionInformation =
69 sizeof(OSVERSIONINFO),
70 0,
71 0,
73 0,
74 "",
77 /* ts: Window 98 does not support "TryEnterCriticalSection" but export the symbol !!!
78 calls to that symbol always returns FALSE */
79 if (
80 GetVersionEx(&VersionInformation) &&
81 (VersionInformation.dwPlatformId == VER_PLATFORM_WIN32_NT)
84 lpfTryEnterCriticalSection = (BOOL (WINAPI *)(LPCRITICAL_SECTION))
85 GetProcAddress(GetModuleHandle("KERNEL32"),
86 "TryEnterCriticalSection");
88 else
90 lpfTryEnterCriticalSection = (BOOL (WINAPI *)(LPCRITICAL_SECTION))NULL;
94 InitializeCriticalSection(&MutexLock);
97 pMutexImpl= calloc(sizeof(oslMutexImpl), 1);
99 OSL_ASSERT(pMutexImpl); /* alloc successful? */
101 InitializeCriticalSection(&pMutexImpl->m_Mutex);
103 return (oslMutex)pMutexImpl;
106 /*****************************************************************************/
107 /* osl_destroyMutex */
108 /*****************************************************************************/
109 void SAL_CALL osl_destroyMutex(oslMutex Mutex)
111 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
113 if (pMutexImpl)
115 DeleteCriticalSection(&pMutexImpl->m_Mutex);
116 free(pMutexImpl);
120 /*****************************************************************************/
121 /* osl_acquireMutex */
122 /*****************************************************************************/
123 sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
125 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
127 OSL_ASSERT(Mutex);
129 if (lpfTryEnterCriticalSection == NULL)
131 EnterCriticalSection(&MutexLock);
132 pMutexImpl->m_Requests++;
133 LeaveCriticalSection(&MutexLock);
135 EnterCriticalSection(&pMutexImpl->m_Mutex);
137 EnterCriticalSection(&MutexLock);
138 pMutexImpl->m_Requests--;
139 if (pMutexImpl->m_Locks++ == 0)
140 pMutexImpl->m_Owner = GetCurrentThreadId();
141 LeaveCriticalSection(&MutexLock);
143 else
144 EnterCriticalSection(&pMutexImpl->m_Mutex);
146 return sal_True;
149 /*****************************************************************************/
150 /* osl_tryToAcquireMutex */
151 /*****************************************************************************/
152 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
154 sal_Bool ret = sal_False;
155 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
157 OSL_ASSERT(Mutex);
159 if (lpfTryEnterCriticalSection != NULL)
160 return (sal_Bool)(lpfTryEnterCriticalSection(&pMutexImpl->m_Mutex) != FALSE);
161 else
163 EnterCriticalSection(&MutexLock);
165 if ( ((pMutexImpl->m_Requests == 0) && (pMutexImpl->m_Locks == 0)) ||
166 (pMutexImpl->m_Owner == GetCurrentThreadId()) )
167 ret = osl_acquireMutex(Mutex);
169 LeaveCriticalSection(&MutexLock);
172 return ret;
175 /*****************************************************************************/
176 /* osl_releaseMutex */
177 /*****************************************************************************/
178 sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
180 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
182 OSL_ASSERT(Mutex);
184 if (lpfTryEnterCriticalSection == NULL)
186 EnterCriticalSection(&MutexLock);
188 if (--(pMutexImpl->m_Locks) == 0)
189 pMutexImpl->m_Owner = 0;
191 LeaveCriticalSection(&MutexLock);
194 LeaveCriticalSection(&pMutexImpl->m_Mutex);
196 return sal_True;
199 /*****************************************************************************/
200 /* osl_getGlobalMutex */
201 /*****************************************************************************/
203 /* initialized in dllentry.c */
204 oslMutex g_Mutex;
206 oslMutex * SAL_CALL osl_getGlobalMutex(void)
208 return &g_Mutex;