blob: 5c480d51b2db5374ef68a92dc5f27e9c24183780 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_DETAIL_INTERLOCKED_HPP_INCLUDED
2#define NDNBOOST_DETAIL_INTERLOCKED_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070011// ndnboost/detail/interlocked.hpp
Jeff Thompsonf7d49942013-08-01 16:47:40 -070012//
13// Copyright 2005 Peter Dimov
14//
15// Distributed under the Boost Software License, Version 1.0. (See
16// accompanying file LICENSE_1_0.txt or copy at
17// http://www.boost.org/LICENSE_1_0.txt)
18//
19
Jeff Thompson2277ce52013-08-01 17:34:11 -070020#include <ndnboost/config.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070021
Jeff Thompson3d613fd2013-10-15 15:39:04 -070022#if defined( NDNBOOST_USE_WINDOWS_H )
Jeff Thompsonf7d49942013-08-01 16:47:40 -070023
24# include <windows.h>
25
Jeff Thompson3d613fd2013-10-15 15:39:04 -070026# define NDNBOOST_INTERLOCKED_INCREMENT InterlockedIncrement
27# define NDNBOOST_INTERLOCKED_DECREMENT InterlockedDecrement
28# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
29# define NDNBOOST_INTERLOCKED_EXCHANGE InterlockedExchange
30# define NDNBOOST_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
31# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER InterlockedCompareExchangePointer
32# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER InterlockedExchangePointer
Jeff Thompsonf7d49942013-08-01 16:47:40 -070033
34#elif defined(_WIN32_WCE)
35
36#if _WIN32_WCE >= 0x600
37
38extern "C" long __cdecl _InterlockedIncrement( long volatile * );
39extern "C" long __cdecl _InterlockedDecrement( long volatile * );
40extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
41extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
42extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
43
Jeff Thompson3d613fd2013-10-15 15:39:04 -070044# define NDNBOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
45# define NDNBOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
46# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
47# define NDNBOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
48# define NDNBOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
Jeff Thompsonf7d49942013-08-01 16:47:40 -070049
50#else
51// under Windows CE we still have old-style Interlocked* functions
52
53extern "C" long __cdecl InterlockedIncrement( long* );
54extern "C" long __cdecl InterlockedDecrement( long* );
55extern "C" long __cdecl InterlockedCompareExchange( long*, long, long );
56extern "C" long __cdecl InterlockedExchange( long*, long );
57extern "C" long __cdecl InterlockedExchangeAdd( long*, long );
58
Jeff Thompson3d613fd2013-10-15 15:39:04 -070059# define NDNBOOST_INTERLOCKED_INCREMENT InterlockedIncrement
60# define NDNBOOST_INTERLOCKED_DECREMENT InterlockedDecrement
61# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
62# define NDNBOOST_INTERLOCKED_EXCHANGE InterlockedExchange
63# define NDNBOOST_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
Jeff Thompsonf7d49942013-08-01 16:47:40 -070064
65#endif
66
Jeff Thompson3d613fd2013-10-15 15:39:04 -070067# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
68 ((void*)NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE((long*)(dest),(long)(exchange),(long)(compare)))
69# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
70 ((void*)NDNBOOST_INTERLOCKED_EXCHANGE((long*)(dest),(long)(exchange)))
Jeff Thompsonf7d49942013-08-01 16:47:40 -070071
Jeff Thompson3d613fd2013-10-15 15:39:04 -070072#elif defined( NDNBOOST_MSVC ) || defined( NDNBOOST_INTEL_WIN )
Jeff Thompsonf7d49942013-08-01 16:47:40 -070073
Jeff Thompson3d613fd2013-10-15 15:39:04 -070074#if defined( NDNBOOST_MSVC ) && NDNBOOST_MSVC >= 1600
Jeff Thompsonf7d49942013-08-01 16:47:40 -070075
76#include <intrin.h>
77
78#elif defined( __CLRCALL_PURE_OR_CDECL )
79
80extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedIncrement( long volatile * );
81extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedDecrement( long volatile * );
82extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange( long volatile *, long, long );
83extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchange( long volatile *, long );
84extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchangeAdd( long volatile *, long );
85
86#else
87
88extern "C" long __cdecl _InterlockedIncrement( long volatile * );
89extern "C" long __cdecl _InterlockedDecrement( long volatile * );
90extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
91extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
92extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
93
94#endif
95
96# pragma intrinsic( _InterlockedIncrement )
97# pragma intrinsic( _InterlockedDecrement )
98# pragma intrinsic( _InterlockedCompareExchange )
99# pragma intrinsic( _InterlockedExchange )
100# pragma intrinsic( _InterlockedExchangeAdd )
101
102# if defined(_M_IA64) || defined(_M_AMD64)
103
104extern "C" void* __cdecl _InterlockedCompareExchangePointer( void* volatile *, void*, void* );
105extern "C" void* __cdecl _InterlockedExchangePointer( void* volatile *, void* );
106
107# pragma intrinsic( _InterlockedCompareExchangePointer )
108# pragma intrinsic( _InterlockedExchangePointer )
109
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700110# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER _InterlockedCompareExchangePointer
111# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER _InterlockedExchangePointer
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700112
113# else
114
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700115# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
116 ((void*)NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
117# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
118 ((void*)NDNBOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700119
120# endif
121
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700122# define NDNBOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
123# define NDNBOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
124# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
125# define NDNBOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
126# define NDNBOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700127
128#elif defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
129
130#if defined(__MINGW64__)
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700131#define NDNBOOST_INTERLOCKED_IMPORT
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700132#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700133#define NDNBOOST_INTERLOCKED_IMPORT __declspec(dllimport)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700134#endif
135
136
137namespace ndnboost
138{
139
140namespace detail
141{
142
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700143extern "C" NDNBOOST_INTERLOCKED_IMPORT long __stdcall InterlockedIncrement( long volatile * );
144extern "C" NDNBOOST_INTERLOCKED_IMPORT long __stdcall InterlockedDecrement( long volatile * );
145extern "C" NDNBOOST_INTERLOCKED_IMPORT long __stdcall InterlockedCompareExchange( long volatile *, long, long );
146extern "C" NDNBOOST_INTERLOCKED_IMPORT long __stdcall InterlockedExchange( long volatile *, long );
147extern "C" NDNBOOST_INTERLOCKED_IMPORT long __stdcall InterlockedExchangeAdd( long volatile *, long );
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700148
149# if defined(_M_IA64) || defined(_M_AMD64)
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700150extern "C" NDNBOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedCompareExchangePointer( void* volatile *, void*, void* );
151extern "C" NDNBOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedExchangePointer( void* volatile *, void* );
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700152# endif
153
154} // namespace detail
155
156} // namespace ndnboost
157
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700158# define NDNBOOST_INTERLOCKED_INCREMENT ::ndnboost::detail::InterlockedIncrement
159# define NDNBOOST_INTERLOCKED_DECREMENT ::ndnboost::detail::InterlockedDecrement
160# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE ::ndnboost::detail::InterlockedCompareExchange
161# define NDNBOOST_INTERLOCKED_EXCHANGE ::ndnboost::detail::InterlockedExchange
162# define NDNBOOST_INTERLOCKED_EXCHANGE_ADD ::ndnboost::detail::InterlockedExchangeAdd
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700163
164# if defined(_M_IA64) || defined(_M_AMD64)
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700165# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER ::ndnboost::detail::InterlockedCompareExchangePointer
166# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER ::ndnboost::detail::InterlockedExchangePointer
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700167# else
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700168# define NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
169 ((void*)NDNBOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
170# define NDNBOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
171 ((void*)NDNBOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700172# endif
173
174#else
175
176# error "Interlocked intrinsics not available"
177
178#endif
179
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700180#endif // #ifndef NDNBOOST_DETAIL_INTERLOCKED_HPP_INCLUDED