xref: /aoo42x/main/sal/osl/w32/mutex.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "system.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/mutex.h>
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /*
34*cdf0e10cSrcweir 	Implementation notes:
35*cdf0e10cSrcweir 	The void* hidden by oslMutex points to a WIN32
36*cdf0e10cSrcweir 	CRITICAL_SECTION structure.
37*cdf0e10cSrcweir */
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir typedef struct _oslMutexImpl {
40*cdf0e10cSrcweir 	CRITICAL_SECTION	m_Mutex;
41*cdf0e10cSrcweir 	int 				m_Locks;
42*cdf0e10cSrcweir 	DWORD				m_Owner;
43*cdf0e10cSrcweir 	DWORD				m_Requests;
44*cdf0e10cSrcweir } oslMutexImpl;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir static BOOL (WINAPI *lpfTryEnterCriticalSection)(LPCRITICAL_SECTION)
47*cdf0e10cSrcweir 	= (BOOL (WINAPI *)(LPCRITICAL_SECTION))0xFFFFFFFF;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir static CRITICAL_SECTION MutexLock;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /*****************************************************************************/
52*cdf0e10cSrcweir /* osl_createMutex */
53*cdf0e10cSrcweir /*****************************************************************************/
54*cdf0e10cSrcweir oslMutex SAL_CALL osl_createMutex(void)
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	/* Window 95 does not support "TryEnterCriticalSection" */
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	if (lpfTryEnterCriticalSection ==
61*cdf0e10cSrcweir 				(BOOL (WINAPI *)(LPCRITICAL_SECTION))0xFFFFFFFF)
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir 		OSVERSIONINFO VersionInformation =
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 		{
66*cdf0e10cSrcweir 			sizeof(OSVERSIONINFO),
67*cdf0e10cSrcweir 			0,
68*cdf0e10cSrcweir 	    	0,
69*cdf0e10cSrcweir 	    	0,
70*cdf0e10cSrcweir 	    	0,
71*cdf0e10cSrcweir 	    	"",
72*cdf0e10cSrcweir 		};
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 		/* ts: Window 98 does not support "TryEnterCriticalSection" but export the symbol !!!
75*cdf0e10cSrcweir 		   calls to that symbol always returns FALSE */
76*cdf0e10cSrcweir 		if (
77*cdf0e10cSrcweir 			GetVersionEx(&VersionInformation) &&
78*cdf0e10cSrcweir 			(VersionInformation.dwPlatformId == VER_PLATFORM_WIN32_NT)
79*cdf0e10cSrcweir 		   )
80*cdf0e10cSrcweir 		{
81*cdf0e10cSrcweir 			lpfTryEnterCriticalSection = (BOOL (WINAPI *)(LPCRITICAL_SECTION))
82*cdf0e10cSrcweir 					GetProcAddress(GetModuleHandle("KERNEL32"),
83*cdf0e10cSrcweir 								   "TryEnterCriticalSection");
84*cdf0e10cSrcweir 		}
85*cdf0e10cSrcweir 		else
86*cdf0e10cSrcweir 		{
87*cdf0e10cSrcweir 			lpfTryEnterCriticalSection = (BOOL (WINAPI *)(LPCRITICAL_SECTION))NULL;
88*cdf0e10cSrcweir 		}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		InitializeCriticalSection(&MutexLock);
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	pMutexImpl= calloc(sizeof(oslMutexImpl), 1);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	OSL_ASSERT(pMutexImpl); /* alloc successful? */
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	InitializeCriticalSection(&pMutexImpl->m_Mutex);
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	return (oslMutex)pMutexImpl;
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /*****************************************************************************/
104*cdf0e10cSrcweir /* osl_destroyMutex */
105*cdf0e10cSrcweir /*****************************************************************************/
106*cdf0e10cSrcweir void SAL_CALL osl_destroyMutex(oslMutex Mutex)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	if (pMutexImpl)
111*cdf0e10cSrcweir 	{
112*cdf0e10cSrcweir 		DeleteCriticalSection(&pMutexImpl->m_Mutex);
113*cdf0e10cSrcweir 		free(pMutexImpl);
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir /*****************************************************************************/
118*cdf0e10cSrcweir /* osl_acquireMutex */
119*cdf0e10cSrcweir /*****************************************************************************/
120*cdf0e10cSrcweir sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	OSL_ASSERT(Mutex);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	if (lpfTryEnterCriticalSection == NULL)
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		EnterCriticalSection(&MutexLock);
129*cdf0e10cSrcweir 		pMutexImpl->m_Requests++;
130*cdf0e10cSrcweir 		LeaveCriticalSection(&MutexLock);
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		EnterCriticalSection(&pMutexImpl->m_Mutex);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		EnterCriticalSection(&MutexLock);
135*cdf0e10cSrcweir 		pMutexImpl->m_Requests--;
136*cdf0e10cSrcweir 		if (pMutexImpl->m_Locks++ == 0)
137*cdf0e10cSrcweir 			pMutexImpl->m_Owner = GetCurrentThreadId();
138*cdf0e10cSrcweir 		LeaveCriticalSection(&MutexLock);
139*cdf0e10cSrcweir 	}
140*cdf0e10cSrcweir 	else
141*cdf0e10cSrcweir 		EnterCriticalSection(&pMutexImpl->m_Mutex);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	return sal_True;
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir /*****************************************************************************/
147*cdf0e10cSrcweir /* osl_tryToAcquireMutex */
148*cdf0e10cSrcweir /*****************************************************************************/
149*cdf0e10cSrcweir sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	sal_Bool 	 ret = sal_False;
152*cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	OSL_ASSERT(Mutex);
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	if (lpfTryEnterCriticalSection != NULL)
157*cdf0e10cSrcweir 		return (sal_Bool)(lpfTryEnterCriticalSection(&pMutexImpl->m_Mutex) != FALSE);
158*cdf0e10cSrcweir 	else
159*cdf0e10cSrcweir 	{
160*cdf0e10cSrcweir 		EnterCriticalSection(&MutexLock);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 		if ( ((pMutexImpl->m_Requests == 0) && (pMutexImpl->m_Locks == 0)) ||
163*cdf0e10cSrcweir 			 (pMutexImpl->m_Owner == GetCurrentThreadId()) )
164*cdf0e10cSrcweir 			ret = osl_acquireMutex(Mutex);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 		LeaveCriticalSection(&MutexLock);
167*cdf0e10cSrcweir 	}
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	return ret;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir /*****************************************************************************/
173*cdf0e10cSrcweir /* osl_releaseMutex */
174*cdf0e10cSrcweir /*****************************************************************************/
175*cdf0e10cSrcweir sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	OSL_ASSERT(Mutex);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	if (lpfTryEnterCriticalSection == NULL)
182*cdf0e10cSrcweir 	{
183*cdf0e10cSrcweir 		EnterCriticalSection(&MutexLock);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 		if (--(pMutexImpl->m_Locks) == 0)
186*cdf0e10cSrcweir 			pMutexImpl->m_Owner = 0;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		LeaveCriticalSection(&MutexLock);
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 	LeaveCriticalSection(&pMutexImpl->m_Mutex);
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	return sal_True;
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir /*****************************************************************************/
197*cdf0e10cSrcweir /* osl_getGlobalMutex */
198*cdf0e10cSrcweir /*****************************************************************************/
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir /* initialized in dllentry.c */
201*cdf0e10cSrcweir oslMutex g_Mutex;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir oslMutex * SAL_CALL osl_getGlobalMutex(void)
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir 	return &g_Mutex;
206*cdf0e10cSrcweir }
207