1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mutex.c,v $
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 ************************************************************************/
33 #include <osl/mutex.h>
34 #include <osl/diagnose.h>
38 The void* hidden by oslMutex points to a WIN32
39 CRITICAL_SECTION structure.
42 typedef struct _oslMutexImpl
{
43 CRITICAL_SECTION m_Mutex
;
49 static BOOL (WINAPI
*lpfTryEnterCriticalSection
)(LPCRITICAL_SECTION
)
50 = (BOOL (WINAPI
*)(LPCRITICAL_SECTION
))0xFFFFFFFF;
52 static CRITICAL_SECTION MutexLock
;
54 /*****************************************************************************/
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
),
77 /* ts: Window 98 does not support "TryEnterCriticalSection" but export the symbol !!!
78 calls to that symbol always returns FALSE */
80 GetVersionEx(&VersionInformation
) &&
81 (VersionInformation
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
84 lpfTryEnterCriticalSection
= (BOOL (WINAPI
*)(LPCRITICAL_SECTION
))
85 GetProcAddress(GetModuleHandle("KERNEL32"),
86 "TryEnterCriticalSection");
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
;
115 DeleteCriticalSection(&pMutexImpl
->m_Mutex
);
120 /*****************************************************************************/
121 /* osl_acquireMutex */
122 /*****************************************************************************/
123 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
125 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)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
);
144 EnterCriticalSection(&pMutexImpl
->m_Mutex
);
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
;
159 if (lpfTryEnterCriticalSection
!= NULL
)
160 return (sal_Bool
)(lpfTryEnterCriticalSection(&pMutexImpl
->m_Mutex
) != FALSE
);
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
);
175 /*****************************************************************************/
176 /* osl_releaseMutex */
177 /*****************************************************************************/
178 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
180 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)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
);
199 /*****************************************************************************/
200 /* osl_getGlobalMutex */
201 /*****************************************************************************/
203 /* initialized in dllentry.c */
206 oslMutex
* SAL_CALL
osl_getGlobalMutex(void)