Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "system.h"
31 : :
32 : : #include <osl/armarch.h>
33 : : #include <osl/interlck.h>
34 : : #include <osl/diagnose.h>
35 : :
36 : : #if ( defined ( SOLARIS ) || defined ( NETBSD ) ) && defined ( SPARC )
37 : : #error please use asm/interlck_sparc.s
38 : : #elif defined ( SOLARIS) && defined ( X86 )
39 : : #error please use asm/interlck_x86.s
40 : : #elif defined ( GCC ) && ( defined ( X86 ) || defined ( X86_64 ) )
41 : : /* That's possible on x86-64 too since oslInterlockedCount is a sal_Int32 */
42 : :
43 : : extern int osl_isSingleCPU;
44 : :
45 : : /*****************************************************************************/
46 : : /* osl_incrementInterlockedCount */
47 : : /*****************************************************************************/
48 : 1026312108 : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
49 : : {
50 : : // Fast case for old, slow, single CPU Intel machines for whom
51 : : // interlocking is a performance nightmare.
52 : : register oslInterlockedCount nCount asm("%eax");
53 : 1026312108 : nCount = 1;
54 : :
55 [ - + ]: 1026312108 : if ( osl_isSingleCPU ) {
56 : 0 : __asm__ __volatile__ (
57 : : "xaddl %0, %1\n\t"
58 : : : "+r" (nCount), "+m" (*pCount)
59 : : : /* nothing */
60 : : : "memory");
61 : 0 : return ++nCount;
62 : : }
63 : : #if ( __GNUC__ > 4 ) || (( __GNUC__ == 4) && ( __GNUC_MINOR__ >= 4 ))
64 : : else
65 : 1026312108 : return __sync_add_and_fetch (pCount, 1);
66 : : #else
67 : : else {
68 : : __asm__ __volatile__ (
69 : : "lock\n\t"
70 : : "xaddl %0, %1\n\t"
71 : : : "+r" (nCount), "+m" (*pCount)
72 : : : /* nothing */
73 : : : "memory");
74 : : }
75 : : return ++nCount;
76 : : #endif
77 : : }
78 : :
79 : 1036671687 : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
80 : : {
81 : : register oslInterlockedCount nCount asm("%eax");
82 : 1036671687 : nCount = -1;
83 : :
84 [ - + ]: 1036671687 : if ( osl_isSingleCPU ) {
85 : 0 : __asm__ __volatile__ (
86 : : "xaddl %0, %1\n\t"
87 : : : "+r" (nCount), "+m" (*pCount)
88 : : : /* nothing */
89 : : : "memory");
90 : 0 : return --nCount;
91 : : }
92 : : #if ( __GNUC__ > 4 ) || (( __GNUC__ == 4) && ( __GNUC_MINOR__ >= 4 ))
93 : : else
94 : 1036671687 : return __sync_sub_and_fetch (pCount, 1);
95 : : #else
96 : : else {
97 : : __asm__ __volatile__ (
98 : : "lock\n\t"
99 : : "xaddl %0, %1\n\t"
100 : : : "+r" (nCount), "+m" (*pCount)
101 : : : /* nothing */
102 : : : "memory");
103 : : }
104 : : return --nCount;
105 : : #endif
106 : : }
107 : : #elif ( __GNUC__ > 4 ) || (( __GNUC__ == 4) && ( __GNUC_MINOR__ >= 4 ))
108 : : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
109 : : {
110 : : return __sync_add_and_fetch(pCount, 1);
111 : : }
112 : : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
113 : : {
114 : : return __sync_sub_and_fetch(pCount, 1);
115 : : }
116 : : #else
117 : : /* use only if nothing else works, expensive due to single mutex for all reference counts */
118 : :
119 : : static pthread_mutex_t InterLock = PTHREAD_MUTEX_INITIALIZER;
120 : :
121 : : /*****************************************************************************/
122 : : /* osl_incrementInterlockedCount */
123 : : /*****************************************************************************/
124 : : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
125 : : {
126 : : oslInterlockedCount Count;
127 : :
128 : : pthread_mutex_lock(&InterLock);
129 : : Count = ++(*pCount);
130 : : pthread_mutex_unlock(&InterLock);
131 : :
132 : : return (Count);
133 : : }
134 : :
135 : : /*****************************************************************************/
136 : : /* osl_decrementInterlockedCount */
137 : : /*****************************************************************************/
138 : : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
139 : : {
140 : : oslInterlockedCount Count;
141 : :
142 : : pthread_mutex_lock(&InterLock);
143 : : Count = --(*pCount);
144 : : pthread_mutex_unlock(&InterLock);
145 : :
146 : : return (Count);
147 : : }
148 : :
149 : : #endif /* default */
150 : :
151 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|