Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "system.h"
22 :
23 : #include <osl/interlck.h>
24 : #include <osl/diagnose.h>
25 :
26 : #if ( defined ( SOLARIS ) || defined ( NETBSD ) ) && defined ( SPARC )
27 : #error please use asm/interlck_sparc.s
28 : #elif defined ( SOLARIS) && defined ( X86 )
29 : #error please use asm/interlck_x86.s
30 : #elif defined ( GCC ) && ( defined ( X86 ) || defined ( X86_64 ) )
31 : /* That's possible on x86-64 too since oslInterlockedCount is a sal_Int32 */
32 :
33 : extern int osl_isSingleCPU;
34 :
35 : /*****************************************************************************/
36 : /* osl_incrementInterlockedCount */
37 : /*****************************************************************************/
38 0 : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
39 : {
40 : // Fast case for old, slow, single CPU Intel machines for whom
41 : // interlocking is a performance nightmare.
42 0 : if ( osl_isSingleCPU ) {
43 : register oslInterlockedCount nCount asm("%eax");
44 0 : nCount = 1;
45 0 : __asm__ __volatile__ (
46 : "xaddl %0, %1\n\t"
47 : : "+r" (nCount), "+m" (*pCount)
48 : : /* nothing */
49 : : "memory");
50 0 : return ++nCount;
51 : }
52 : #if defined( HAVE_GCC_BUILTIN_ATOMIC )
53 : else
54 0 : return __sync_add_and_fetch (pCount, 1);
55 : #else
56 : else {
57 : register oslInterlockedCount nCount asm("%eax");
58 : nCount = 1;
59 : __asm__ __volatile__ (
60 : "lock\n\t"
61 : "xaddl %0, %1\n\t"
62 : : "+r" (nCount), "+m" (*pCount)
63 : : /* nothing */
64 : : "memory");
65 : return ++nCount;
66 : }
67 : #endif
68 : }
69 :
70 0 : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
71 : {
72 0 : if ( osl_isSingleCPU ) {
73 : register oslInterlockedCount nCount asm("%eax");
74 0 : nCount = -1;
75 0 : __asm__ __volatile__ (
76 : "xaddl %0, %1\n\t"
77 : : "+r" (nCount), "+m" (*pCount)
78 : : /* nothing */
79 : : "memory");
80 0 : return --nCount;
81 : }
82 : #if defined( HAVE_GCC_BUILTIN_ATOMIC )
83 : else
84 0 : return __sync_sub_and_fetch (pCount, 1);
85 : #else
86 : else {
87 : register oslInterlockedCount nCount asm("%eax");
88 : nCount = -1;
89 : __asm__ __volatile__ (
90 : "lock\n\t"
91 : "xaddl %0, %1\n\t"
92 : : "+r" (nCount), "+m" (*pCount)
93 : : /* nothing */
94 : : "memory");
95 : return --nCount;
96 : }
97 : #endif
98 : }
99 : #elif defined( HAVE_GCC_BUILTIN_ATOMIC )
100 : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
101 : {
102 : return __sync_add_and_fetch(pCount, 1);
103 : }
104 : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
105 : {
106 : return __sync_sub_and_fetch(pCount, 1);
107 : }
108 : #else
109 : /* use only if nothing else works, expensive due to single mutex for all reference counts */
110 :
111 : static pthread_mutex_t InterLock = PTHREAD_MUTEX_INITIALIZER;
112 :
113 : /*****************************************************************************/
114 : /* osl_incrementInterlockedCount */
115 : /*****************************************************************************/
116 : oslInterlockedCount SAL_CALL osl_incrementInterlockedCount(oslInterlockedCount* pCount)
117 : {
118 : oslInterlockedCount Count;
119 :
120 : pthread_mutex_lock(&InterLock);
121 : Count = ++(*pCount);
122 : pthread_mutex_unlock(&InterLock);
123 :
124 : return (Count);
125 : }
126 :
127 : /*****************************************************************************/
128 : /* osl_decrementInterlockedCount */
129 : /*****************************************************************************/
130 : oslInterlockedCount SAL_CALL osl_decrementInterlockedCount(oslInterlockedCount* pCount)
131 : {
132 : oslInterlockedCount Count;
133 :
134 : pthread_mutex_lock(&InterLock);
135 : Count = --(*pCount);
136 : pthread_mutex_unlock(&InterLock);
137 :
138 : return (Count);
139 : }
140 :
141 : #endif /* default */
142 :
143 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|