xref: /aoo41x/main/sal/osl/os2/mutex.c (revision 647f063d)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647f063dSAndrew Rist  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19*647f063dSAndrew Rist  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <sys/fmutex.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "system.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <osl/mutex.h>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /*
32cdf0e10cSrcweir     Implementation notes:
33cdf0e10cSrcweir     The void* hidden by oslMutex points to an OS/2 mutex semaphore.
34cdf0e10cSrcweir */
35cdf0e10cSrcweir typedef struct _oslMutexImpl {
36cdf0e10cSrcweir 	HMTX				m_Mutex;
37cdf0e10cSrcweir 	int 				m_Locks;
38cdf0e10cSrcweir 	ULONG				m_Owner;
39cdf0e10cSrcweir 	ULONG				m_Requests;
40cdf0e10cSrcweir } oslMutexImpl;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir // static mutex to control access to private members of oslMutexImpl
43cdf0e10cSrcweir static HMTX MutexLock = 0;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /*****************************************************************************/
46cdf0e10cSrcweir /* osl_createMutex */
47cdf0e10cSrcweir /*****************************************************************************/
osl_createMutex()48cdf0e10cSrcweir oslMutex SAL_CALL osl_createMutex()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl;
51cdf0e10cSrcweir     HMTX hMutex;
52cdf0e10cSrcweir     APIRET rc;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	pMutexImpl= (oslMutexImpl*)calloc(sizeof(oslMutexImpl), 1);
55cdf0e10cSrcweir 	OSL_ASSERT(pMutexImpl); /* alloc successful? */
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /* create semaphore */
58cdf0e10cSrcweir     rc = DosCreateMutexSem( NULL, &pMutexImpl->m_Mutex, 0, FALSE );
59cdf0e10cSrcweir     if( rc != 0 )
60cdf0e10cSrcweir 	{
61cdf0e10cSrcweir 		free(pMutexImpl);
62cdf0e10cSrcweir         return NULL;
63cdf0e10cSrcweir 	}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	// create static mutex for private members
66cdf0e10cSrcweir 	if (MutexLock == 0)
67cdf0e10cSrcweir 		DosCreateMutexSem( NULL, &MutexLock, 0, FALSE );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	return (oslMutex)pMutexImpl;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir /*****************************************************************************/
73cdf0e10cSrcweir /* osl_destroyMutex */
74cdf0e10cSrcweir /*****************************************************************************/
osl_destroyMutex(oslMutex Mutex)75cdf0e10cSrcweir void SAL_CALL osl_destroyMutex(oslMutex Mutex)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
78cdf0e10cSrcweir 	if (pMutexImpl)
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		DosCloseMutexSem( pMutexImpl->m_Mutex);
81cdf0e10cSrcweir 		free(pMutexImpl);
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir /*****************************************************************************/
86cdf0e10cSrcweir /* osl_acquireMutex */
87cdf0e10cSrcweir /*****************************************************************************/
osl_acquireMutex(oslMutex Mutex)88cdf0e10cSrcweir sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
91cdf0e10cSrcweir     APIRET rc = 0;
92cdf0e10cSrcweir     OSL_ASSERT(Mutex);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
95cdf0e10cSrcweir 	pMutexImpl->m_Requests++;
96cdf0e10cSrcweir 	DosReleaseMutexSem( MutexLock);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	rc = DosRequestMutexSem( pMutexImpl->m_Mutex, SEM_INDEFINITE_WAIT );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
101cdf0e10cSrcweir 	pMutexImpl->m_Requests--;
102cdf0e10cSrcweir 	if (pMutexImpl->m_Locks++ == 0)
103cdf0e10cSrcweir 		pMutexImpl->m_Owner = _gettid();
104cdf0e10cSrcweir 	DosReleaseMutexSem( MutexLock);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     return( rc == 0 );
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir /*****************************************************************************/
110cdf0e10cSrcweir /* osl_tryToAcquireMutex */
111cdf0e10cSrcweir /*****************************************************************************/
osl_tryToAcquireMutex(oslMutex Mutex)112cdf0e10cSrcweir sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir 	sal_Bool 	 ret = sal_False;
115cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
116cdf0e10cSrcweir     OSL_ASSERT(Mutex);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	if ( ((pMutexImpl->m_Requests == 0) && (pMutexImpl->m_Locks == 0)) ||
121cdf0e10cSrcweir 		 (pMutexImpl->m_Owner == _gettid()) )
122cdf0e10cSrcweir 		ret = osl_acquireMutex(Mutex);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	DosReleaseMutexSem( MutexLock);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     return ret;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir /*****************************************************************************/
130cdf0e10cSrcweir /* osl_releaseMutex */
131cdf0e10cSrcweir /*****************************************************************************/
osl_releaseMutex(oslMutex Mutex)132cdf0e10cSrcweir sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
135cdf0e10cSrcweir     APIRET rc;
136cdf0e10cSrcweir     OSL_ASSERT(Mutex);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	if (--(pMutexImpl->m_Locks) == 0)
141cdf0e10cSrcweir 		pMutexImpl->m_Owner = 0;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	DosReleaseMutexSem( MutexLock);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     rc = DosReleaseMutexSem( pMutexImpl->m_Mutex);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     return sal_True;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 
152cdf0e10cSrcweir /*****************************************************************************/
153cdf0e10cSrcweir /* osl_getGlobalMutex */
154cdf0e10cSrcweir /*****************************************************************************/
155cdf0e10cSrcweir 
156cdf0e10cSrcweir oslMutex g_Mutex = NULL;
157cdf0e10cSrcweir 
osl_getGlobalMutex(void)158cdf0e10cSrcweir oslMutex * SAL_CALL osl_getGlobalMutex(void)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	if (g_Mutex == NULL)
161cdf0e10cSrcweir 		g_Mutex = osl_createMutex();
162cdf0e10cSrcweir 	return &g_Mutex;
163cdf0e10cSrcweir }
164