Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Standard / Standard_Atomic.hxx
CommitLineData
7fd59977 1// File: Standard_Atomic.hxx
2// Created: Tue Sep 04 08:52:43 2007
3// Author: Andrey BETENEV
4
5//! @file
6//! Implementation of some atomic operations (elementary operations
7//! with data that cannot be interrupted by parallel threads in the
8//! multithread process) on various platforms
9//!
10//! By the moment, only operations necessary for reference counter
11//! in Standard_Transient objects are implemented.
12//!
13//! Currently only two x86-based configurations (Windows NT with
14//! MS VC++ compiler and Linix with GCC) are really supported.
15//! Other configurations use non-atomic C equivalent.
16
17//! @fn void Standard_Atomic_Increment (int volatile* var)
18//! @brief Increments atomically integer variable pointed by var
19
20//! @fn int Standard_Atomic_DecrementTest (int volatile* var)
21//! @brief Decrements atomically integer variable pointed by var;
22//! returns 1 if result is zero, 0 otherwise
23
24//===================================================
25// Windows NT, MSVC++ compiler
26//===================================================
27#if defined(WNT)
28
29extern "C" {
30long _InterlockedIncrement(long volatile* lpAddend);
31long _InterlockedDecrement(long volatile* lpAddend);
32}
33
34#pragma intrinsic (_InterlockedIncrement)
35#pragma intrinsic (_InterlockedDecrement)
36
37inline void Standard_Atomic_Increment (int volatile* var)
38{
39 _InterlockedIncrement (reinterpret_cast<long volatile*>(var));
40}
41
42inline int Standard_Atomic_DecrementTest (int volatile* var)
43{
44 return _InterlockedDecrement (reinterpret_cast<long volatile*>(var)) == 0;
45}
46
47//===================================================
48// Linux, GCC compiler
49// Note: Linux kernel 2.6x provides definitions for atomic operators
50// in the header file /usr/include/asm/atomic.h,
51// however these definitions involve specific type atomic_t
52// Note: The same code probably would work for Intel compiler
53//===================================================
54#elif defined(LIN)
55
56inline void Standard_Atomic_Increment (int volatile* var)
57{
58 // C equivalent:
59 // ++(*var);
60
61 __asm__ __volatile__
62 (
63 "lock incl %0"
64 : "=m"(*var) // out
65 : "m" (*var) // in
66 );
67}
68
69inline int Standard_Atomic_DecrementTest (int volatile* var)
70{
71 // C equivalent:
72 // return --(*var) == 0;
73
74 unsigned char c;
75 __asm__ __volatile__
76 (
77 "lock decl %0; sete %1"
78 : "=m"(*var), "=qm"(c) // out
79 : "m" (*var) // in
80 : "memory"
81 );
82 return c != 0;
83}
84
85//===================================================
86// Default stub implementation, not atomic actually
87//===================================================
88#else
89
90inline void Standard_Atomic_Increment (int volatile* var)
91{
92 ++(*var);
93}
94
95inline int Standard_Atomic_DecrementTest (int volatile* var)
96{
97 return --(*var) == 0;
98}
99
100#endif