summaryrefslogtreecommitdiff
path: root/inc/Standard_Atomic.hxx
blob: 432e624207a3b5fc36ecab872b67e69e3368bcec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// File:    Standard_Atomic.hxx
// Created: Tue Sep 04 08:52:43 2007
// Author:  Andrey BETENEV

//! @file 
//! Implementation of some atomic operations (elementary operations 
//! with data that cannot be interrupted by parallel threads in the
//! multithread process) on various platforms
//!
//! By the moment, only operations necessary for reference counter 
//! in Standard_Transient objects are implemented.
//! 
//! This is preffered to use fixed size types "int32_t" / "int64_t" for
//! correct function declarations however we leave "int" assuming it is 32bits for now.

#ifndef _Standard_Atomic_HeaderFile
#define _Standard_Atomic_HeaderFile

#include <Standard_Macro.hxx>

#if defined(_MSC_VER) || defined(__BORLANDC__)
#ifdef __BORLANDC__
extern "C" {
  __declspec(dllimport) long __stdcall InterlockedIncrement ( long volatile *lpAddend);
  __declspec(dllimport) long __stdcall InterlockedDecrement ( long volatile *lpAddend);
 }
 #define _InterlockedIncrement InterlockedIncrement
 #define _InterlockedDecrement InterlockedDecrement
#elif defined(_MSC_VER)
 extern "C" {
  long _InterlockedIncrement(long volatile* lpAddend);
  long _InterlockedDecrement(long volatile* lpAddend);
 }
#endif
#endif

#if defined(_MSC_VER)
  // force intrinsic instead of WinAPI calls
  #pragma intrinsic (_InterlockedIncrement)
  #pragma intrinsic (_InterlockedDecrement)
#endif

//! Increments atomically integer variable pointed by theValue
//! and returns resulting incremented value.
static int Standard_Atomic_Increment (volatile int* theValue)
{
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  // mordern g++ compiler (gcc4.4+)
  // built-in functions available for appropriate CPUs (at least -march=i486 should be specified on x86 platform)
  return __sync_add_and_fetch (theValue, 1);
#elif (defined(_WIN32) || defined(__WIN32__))
  // WinAPI function or MSVC intrinsic
  return _InterlockedIncrement(reinterpret_cast<long volatile*>(theValue));
#elif defined(LIN)
  // use x86 / x86_64 inline assembly (compatibility with alien compilers / old GCC)
  int anIncResult;
  __asm__ __volatile__ (
  #if defined(_OCC64)
    "lock xaddl %%ebx, (%%rax) \n\t"
    "incl %%ebx                \n\t"
    : "=b" (anIncResult)
    : "a" (theValue), "b" (1)
    : "cc", "memory");
  #else
    "lock xaddl %%eax, (%%ecx) \n\t"
    "incl %%eax                \n\t"
    : "=a" (anIncResult)
    : "c" (theValue), "a" (1)
    : "memory");
  #endif
  return anIncResult;
#else
  //#error "Atomic operation doesn't implemented for current platform!"
  return ++(*theValue);
#endif
}

//! Decrements atomically integer variable pointed by theValue
//! and returns resulting decremented value.
static int Standard_Atomic_Decrement (volatile int* theValue)
{
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  // mordern g++ compiler (gcc4.4+)
  // built-in functions available for appropriate CPUs (at least -march=i486 should be specified on x86 platform)
  return __sync_sub_and_fetch (theValue, 1);
#elif (defined(_WIN32) || defined(__WIN32__))
  // WinAPI function or MSVC intrinsic
  return _InterlockedDecrement(reinterpret_cast<long volatile*>(theValue));
#elif defined(LIN)
  // use x86 / x86_64 inline assembly (compatibility with alien compilers / old GCC)
  int aDecResult;
  __asm__ __volatile__ (
  #if defined(_OCC64)
    "lock xaddl %%ebx, (%%rax) \n\t"
    "decl %%ebx                \n\t"
    : "=b" (aDecResult)
    : "a" (theValue), "b" (-1)
    : "cc", "memory");
  #else
    "lock xaddl %%eax, (%%ecx) \n\t"
    "decl %%eax                \n\t"
    : "=a" (aDecResult)
    : "c" (theValue), "a" (-1)
    : "memory");
  #endif
  return aDecResult;
#else
  //#error "Atomic operation doesn't implemented for current platform!"
  return --(*theValue);
#endif
}

#endif //_Standard_Atomic_HeaderFile