merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / unx / interlck.c
blob0f4ad27c95c5cf0f8e7ca9f81dcd3a9a18f810cc
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: interlck.c,v $
10 * $Revision: 1.14 $
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 ************************************************************************/
32 #include "system.h"
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");
53 nCount = 1;
55 if ( osl_isSingleCPU ) {
56 __asm__ __volatile__ (
57 "xaddl %0, %1\n\t"
58 : "+r" (nCount), "+m" (*pCount)
59 : /* nothing */
60 : "memory");
62 else {
63 __asm__ __volatile__ (
64 "lock\n\t"
65 "xaddl %0, %1\n\t"
66 : "+r" (nCount), "+m" (*pCount)
67 : /* nothing */
68 : "memory");
71 return ++nCount;
74 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
76 register oslInterlockedCount nCount asm("%eax");
78 nCount = -1;
80 if ( osl_isSingleCPU ) {
81 __asm__ __volatile__ (
82 "xaddl %0, %1\n\t"
83 : "+r" (nCount), "+m" (*pCount)
84 : /* nothing */
85 : "memory");
87 else {
88 __asm__ __volatile__ (
89 "lock\n\t"
90 "xaddl %0, %1\n\t"
91 : "+r" (nCount), "+m" (*pCount)
92 : /* nothing */
93 : "memory");
96 return --nCount;
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"
111 " addi %0,%0,1\n\t"
112 " stwcx. %0,0,%2\n\t"
113 " bne- 1b\n\t"
114 " isync"
115 : "=&r" (nCount), "=m" (*pCount)
116 : "r" (pCount)
117 : "memory");
119 return nCount;
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"
129 " subi %0,%0,1\n\t"
130 " stwcx. %0,0,%2\n\t"
131 " bne- 1b\n\t"
132 " isync"
133 : "=&r" (nCount), "=m" (*pCount)
134 : "r" (pCount)
135 : "memory");
137 return nCount;
140 #else
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);
153 Count = ++(*pCount);
154 pthread_mutex_unlock(&InterLock);
156 return (Count);
159 /*****************************************************************************/
160 /* osl_decrementInterlockedCount */
161 /*****************************************************************************/
162 oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
164 oslInterlockedCount Count;
166 pthread_mutex_lock(&InterLock);
167 Count = --(*pCount);
168 pthread_mutex_unlock(&InterLock);
170 return (Count);
173 #endif /* default */