1 : /**********************************************************************
2 : * $Id: cpl_atomic_ops.cpp 19884 2010-06-17 22:01:19Z rouault $
3 : *
4 : * Name: cpl_atomic_ops.cpp
5 : * Project: CPL - Common Portability Library
6 : * Purpose: Atomic operation functions.
7 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
8 : *
9 : **********************************************************************
10 : * Copyright (c) 2009, Even Rouault, <even dot rouault at mines dash paris dot org>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "cpl_atomic_ops.h"
32 :
33 : #if defined(__MACH__) && defined(__APPLE__)
34 :
35 : #include <libkern/OSAtomic.h>
36 :
37 : int CPLAtomicAdd(volatile int* ptr, int increment)
38 : {
39 : return OSAtomicAdd32(increment, (int*)(ptr));
40 : }
41 :
42 : #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
43 :
44 : #include <windows.h>
45 :
46 : int CPLAtomicAdd(volatile int* ptr, int increment)
47 : {
48 : #if defined(_MSC_VER) && (_MSC_VER <= 1200)
49 : return InterlockedExchangeAdd((LONG*)(ptr), (LONG)(increment)) + increment;
50 : #else
51 : return InterlockedExchangeAdd((volatile LONG*)(ptr), (LONG)(increment)) + increment;
52 : #endif
53 : }
54 :
55 : #elif defined(__MINGW32__) && defined(__i386__)
56 :
57 : #include <windows.h>
58 :
59 : int CPLAtomicAdd(volatile int* ptr, int increment)
60 : {
61 : return InterlockedExchangeAdd((LONG*)(ptr), (LONG)(increment)) + increment;
62 : }
63 :
64 : #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
65 :
66 8338946 : int CPLAtomicAdd(volatile int* ptr, int increment)
67 : {
68 8338946 : int temp = increment;
69 : __asm__ __volatile__("lock; xaddl %0,%1"
70 : : "+r" (temp), "+m" (*ptr)
71 8338946 : : : "memory");
72 8338946 : return temp + increment;
73 : }
74 :
75 : #elif defined(HAVE_GCC_ATOMIC_BUILTINS)
76 : /* Starting with GCC 4.1.0, built-in functions for atomic memory access are provided. */
77 : /* see http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html */
78 : /* We use a ./configure test to determine whether this builtins are available */
79 : /* as it appears that the GCC 4.1 version used on debian etch is broken when linking */
80 : /* such instructions... */
81 : int CPLAtomicAdd(volatile int* ptr, int increment)
82 : {
83 : if (increment > 0)
84 : return __sync_add_and_fetch(ptr, increment);
85 : else
86 : return __sync_sub_and_fetch(ptr, -increment);
87 : }
88 :
89 : #elif !defined(CPL_MULTIPROC_PTHREAD)
90 : #warning "Needs real lock API to implement properly atomic increment"
91 :
92 : /* Dummy implementation */
93 : int CPLAtomicAdd(volatile int* ptr, int increment)
94 : {
95 : (*ptr) += increment;
96 : return *ptr;
97 : }
98 : #else
99 :
100 : #include "cpl_multiproc.h"
101 :
102 : static void *hAtomicOpMutex = NULL;
103 :
104 : /* Slow, but safe, implemenation using a mutex */
105 : int CPLAtomicAdd(volatile int* ptr, int increment)
106 : {
107 : CPLMutexHolder oMutex(&hAtomicOpMutex);
108 : (*ptr) += increment;
109 : return *ptr;
110 : }
111 :
112 : #endif
113 :
114 : #ifndef HAS_CPL_INLINE
115 :
116 : int CPLAtomicInc(volatile int* ptr)
117 : {
118 : return CPLAtomicAdd(ptr, 1);
119 : }
120 :
121 : int CPLAtomicDec(volatile int* ptr)
122 : {
123 : return CPLAtomicAdd(ptr, -1);
124 : }
125 :
126 : #endif
|