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: interlck.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 ************************************************************************/
34 #include <osl/interlck.h>
35 #include <osl/diagnose.h>
37 #if ( defined ( SOLARIS ) || defined ( NETBSD ) ) && defined ( SPARC )
38 #error please use asm/interlck_sparc.s
39 #elif defined ( SOLARIS) && defined ( X86 )
40 #error please use asm/interlck_x86.s
41 #elif defined ( GCC ) && ( defined ( X86 ) || defined ( X86_64 ) )
42 /* That's possible on x86-64 too since oslInterlockedCount is a sal_Int32 */
44 extern int osl_isSingleCPU
;
46 /*****************************************************************************/
47 /* osl_incrementInterlockedCount */
48 /*****************************************************************************/
49 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
51 register oslInterlockedCount nCount
asm("%eax");
55 if ( osl_isSingleCPU
) {
56 __asm__
__volatile__ (
58 : "+r" (nCount
), "+m" (*pCount
)
63 __asm__
__volatile__ (
66 : "+r" (nCount
), "+m" (*pCount
)
74 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
76 register oslInterlockedCount nCount
asm("%eax");
80 if ( osl_isSingleCPU
) {
81 __asm__
__volatile__ (
83 : "+r" (nCount
), "+m" (*pCount
)
88 __asm__
__volatile__ (
91 : "+r" (nCount
), "+m" (*pCount
)
99 #elif defined ( GCC ) && defined ( POWERPC )
101 /*****************************************************************************/
102 /* osl_incrementInterlockedCount */
103 /*****************************************************************************/
104 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
106 /* "addi" doesn't work with r0 as second parameter */
107 register oslInterlockedCount nCount
__asm__ ("r4");
109 __asm__
__volatile__ (
110 "1: lwarx %0,0,%2\n\t"
112 " stwcx. %0,0,%2\n\t"
115 : "=&r" (nCount
), "=m" (*pCount
)
122 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
124 /* "subi" doesn't work with r0 as second parameter */
125 register oslInterlockedCount nCount
__asm__ ("r4");
127 __asm__
__volatile__ (
128 "1: lwarx %0,0,%2\n\t"
130 " stwcx. %0,0,%2\n\t"
133 : "=&r" (nCount
), "=m" (*pCount
)
141 /* use only if nothing else works, expensive due to single mutex for all reference counts */
143 static pthread_mutex_t InterLock
= PTHREAD_MUTEX_INITIALIZER
;
145 /*****************************************************************************/
146 /* osl_incrementInterlockedCount */
147 /*****************************************************************************/
148 oslInterlockedCount SAL_CALL
osl_incrementInterlockedCount(oslInterlockedCount
* pCount
)
150 oslInterlockedCount Count
;
152 pthread_mutex_lock(&InterLock
);
154 pthread_mutex_unlock(&InterLock
);
159 /*****************************************************************************/
160 /* osl_decrementInterlockedCount */
161 /*****************************************************************************/
162 oslInterlockedCount SAL_CALL
osl_decrementInterlockedCount(oslInterlockedCount
* pCount
)
164 oslInterlockedCount Count
;
166 pthread_mutex_lock(&InterLock
);
168 pthread_mutex_unlock(&InterLock
);