merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / os2 / mutex.c
blob2ae7715096422a018c6f527acce148f547dfb77b
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 <sys/fmutex.h>
33 #include "system.h"
35 #include <osl/mutex.h>
36 #include <osl/diagnose.h>
39 Implementation notes:
40 The void* hidden by oslMutex points to an OS/2 mutex semaphore.
42 typedef struct _oslMutexImpl {
43 HMTX m_Mutex;
44 int m_Locks;
45 ULONG m_Owner;
46 ULONG m_Requests;
47 } oslMutexImpl;
49 // static mutex to control access to private members of oslMutexImpl
50 static HMTX MutexLock = 0;
52 /*****************************************************************************/
53 /* osl_createMutex */
54 /*****************************************************************************/
55 oslMutex SAL_CALL osl_createMutex()
57 oslMutexImpl *pMutexImpl;
58 HMTX hMutex;
59 APIRET rc;
61 pMutexImpl= (oslMutexImpl*)calloc(sizeof(oslMutexImpl), 1);
62 OSL_ASSERT(pMutexImpl); /* alloc successful? */
64 /* create semaphore */
65 rc = DosCreateMutexSem( NULL, &pMutexImpl->m_Mutex, 0, FALSE );
66 if( rc != 0 )
68 free(pMutexImpl);
69 return NULL;
72 // create static mutex for private members
73 if (MutexLock == 0)
74 DosCreateMutexSem( NULL, &MutexLock, 0, FALSE );
76 return (oslMutex)pMutexImpl;
79 /*****************************************************************************/
80 /* osl_destroyMutex */
81 /*****************************************************************************/
82 void SAL_CALL osl_destroyMutex(oslMutex Mutex)
84 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
85 if (pMutexImpl)
87 DosCloseMutexSem( pMutexImpl->m_Mutex);
88 free(pMutexImpl);
92 /*****************************************************************************/
93 /* osl_acquireMutex */
94 /*****************************************************************************/
95 sal_Bool SAL_CALL osl_acquireMutex(oslMutex Mutex)
97 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
98 APIRET rc = 0;
99 OSL_ASSERT(Mutex);
101 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
102 pMutexImpl->m_Requests++;
103 DosReleaseMutexSem( MutexLock);
105 rc = DosRequestMutexSem( pMutexImpl->m_Mutex, SEM_INDEFINITE_WAIT );
107 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
108 pMutexImpl->m_Requests--;
109 if (pMutexImpl->m_Locks++ == 0)
110 pMutexImpl->m_Owner = _gettid();
111 DosReleaseMutexSem( MutexLock);
113 return( rc == 0 );
116 /*****************************************************************************/
117 /* osl_tryToAcquireMutex */
118 /*****************************************************************************/
119 sal_Bool SAL_CALL osl_tryToAcquireMutex(oslMutex Mutex)
121 sal_Bool ret = sal_False;
122 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
123 OSL_ASSERT(Mutex);
125 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
127 if ( ((pMutexImpl->m_Requests == 0) && (pMutexImpl->m_Locks == 0)) ||
128 (pMutexImpl->m_Owner == _gettid()) )
129 ret = osl_acquireMutex(Mutex);
131 DosReleaseMutexSem( MutexLock);
133 return ret;
136 /*****************************************************************************/
137 /* osl_releaseMutex */
138 /*****************************************************************************/
139 sal_Bool SAL_CALL osl_releaseMutex(oslMutex Mutex)
141 oslMutexImpl *pMutexImpl = (oslMutexImpl *)Mutex;
142 APIRET rc;
143 OSL_ASSERT(Mutex);
145 DosRequestMutexSem( MutexLock, SEM_INDEFINITE_WAIT );
147 if (--(pMutexImpl->m_Locks) == 0)
148 pMutexImpl->m_Owner = 0;
150 DosReleaseMutexSem( MutexLock);
152 rc = DosReleaseMutexSem( pMutexImpl->m_Mutex);
154 return sal_True;
159 /*****************************************************************************/
160 /* osl_getGlobalMutex */
161 /*****************************************************************************/
163 oslMutex g_Mutex = NULL;
165 oslMutex * SAL_CALL osl_getGlobalMutex(void)
167 if (g_Mutex == NULL)
168 g_Mutex = osl_createMutex();
169 return &g_Mutex;