1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <sys/fmutex.h>
32 #include <osl/mutex.h>
33 #include <osl/diagnose.h>
37 The void* hidden by oslMutex points to an OS/2 mutex semaphore.
39 typedef struct _oslMutexImpl
{
46 // static mutex to control access to private members of oslMutexImpl
47 static HMTX MutexLock
= 0;
49 /*****************************************************************************/
51 /*****************************************************************************/
52 oslMutex SAL_CALL
osl_createMutex()
54 oslMutexImpl
*pMutexImpl
;
58 pMutexImpl
= (oslMutexImpl
*)calloc(sizeof(oslMutexImpl
), 1);
59 OSL_ASSERT(pMutexImpl
); /* alloc successful? */
61 /* create semaphore */
62 rc
= DosCreateMutexSem( NULL
, &pMutexImpl
->m_Mutex
, 0, FALSE
);
69 // create static mutex for private members
71 DosCreateMutexSem( NULL
, &MutexLock
, 0, FALSE
);
73 return (oslMutex
)pMutexImpl
;
76 /*****************************************************************************/
77 /* osl_destroyMutex */
78 /*****************************************************************************/
79 void SAL_CALL
osl_destroyMutex(oslMutex Mutex
)
81 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)Mutex
;
84 DosCloseMutexSem( pMutexImpl
->m_Mutex
);
89 /*****************************************************************************/
90 /* osl_acquireMutex */
91 /*****************************************************************************/
92 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
94 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)Mutex
;
98 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
99 pMutexImpl
->m_Requests
++;
100 DosReleaseMutexSem( MutexLock
);
102 rc
= DosRequestMutexSem( pMutexImpl
->m_Mutex
, SEM_INDEFINITE_WAIT
);
104 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
105 pMutexImpl
->m_Requests
--;
106 if (pMutexImpl
->m_Locks
++ == 0)
107 pMutexImpl
->m_Owner
= _gettid();
108 DosReleaseMutexSem( MutexLock
);
113 /*****************************************************************************/
114 /* osl_tryToAcquireMutex */
115 /*****************************************************************************/
116 sal_Bool SAL_CALL
osl_tryToAcquireMutex(oslMutex Mutex
)
118 sal_Bool ret
= sal_False
;
119 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)Mutex
;
122 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
124 if ( ((pMutexImpl
->m_Requests
== 0) && (pMutexImpl
->m_Locks
== 0)) ||
125 (pMutexImpl
->m_Owner
== _gettid()) )
126 ret
= osl_acquireMutex(Mutex
);
128 DosReleaseMutexSem( MutexLock
);
133 /*****************************************************************************/
134 /* osl_releaseMutex */
135 /*****************************************************************************/
136 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
138 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)Mutex
;
142 DosRequestMutexSem( MutexLock
, SEM_INDEFINITE_WAIT
);
144 if (--(pMutexImpl
->m_Locks
) == 0)
145 pMutexImpl
->m_Owner
= 0;
147 DosReleaseMutexSem( MutexLock
);
149 rc
= DosReleaseMutexSem( pMutexImpl
->m_Mutex
);
156 /*****************************************************************************/
157 /* osl_getGlobalMutex */
158 /*****************************************************************************/
160 oslMutex g_Mutex
= NULL
;
162 oslMutex
* SAL_CALL
osl_getGlobalMutex(void)
165 g_Mutex
= osl_createMutex();