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 ************************************************************************/
31 #include <sys/fmutex.h>
35 #include <osl/mutex.h>
36 #include <osl/diagnose.h>
40 The void* hidden by oslMutex points to an OS/2 mutex semaphore.
42 typedef struct _oslMutexImpl
{
49 // static mutex to control access to private members of oslMutexImpl
50 static HMTX MutexLock
= 0;
52 /*****************************************************************************/
54 /*****************************************************************************/
55 oslMutex SAL_CALL
osl_createMutex()
57 oslMutexImpl
*pMutexImpl
;
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
);
72 // create static mutex for private members
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
;
87 DosCloseMutexSem( pMutexImpl
->m_Mutex
);
92 /*****************************************************************************/
93 /* osl_acquireMutex */
94 /*****************************************************************************/
95 sal_Bool SAL_CALL
osl_acquireMutex(oslMutex Mutex
)
97 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)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
);
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
;
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
);
136 /*****************************************************************************/
137 /* osl_releaseMutex */
138 /*****************************************************************************/
139 sal_Bool SAL_CALL
osl_releaseMutex(oslMutex Mutex
)
141 oslMutexImpl
*pMutexImpl
= (oslMutexImpl
*)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
);
159 /*****************************************************************************/
160 /* osl_getGlobalMutex */
161 /*****************************************************************************/
163 oslMutex g_Mutex
= NULL
;
165 oslMutex
* SAL_CALL
osl_getGlobalMutex(void)
168 g_Mutex
= osl_createMutex();