Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
atomichelpers.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 #include "commonincludes.hpp"
19 #include "atomichelpers.h"
20 
21 
22 
23 #if defined(i386) || defined(__i386) || defined(__i386__)
24 #define ATOMICS_USE_XADD
25 #endif
26 
27 
28 #ifdef ATOMICS_USE_XADD
29 
30 unsigned int xadd_4(volatile void* pVal, unsigned int inc)
31 {
32  unsigned int result;
33  unsigned int* pValInt = (unsigned int*)pVal;
34 
35  asm volatile(
36  "lock; xaddl %%eax, %2;"
37  :"=a" (result)
38  : "a" (inc), "m" (*pValInt)
39  :"memory" );
40 
41  return (result);
42 }
43 
44 int AtomicIncrement(int* pInt)
45 {
46  COMPILE_TIME_ASSERT(sizeof(int)==4);
47  // InterlockedIncrement
48  unsigned int result = xadd_4(pInt, 1) + 1;
49  return (int)result;
50 }
51 
52 int AtomicDecrement(int* pInt)
53 {
54  // InterlockedDecrement
55  unsigned int result = xadd_4(pInt, -1) - 1;
56  return (int)result;
57 }
58 
59 #else
60 
61 int AtomicIncrement(int* pInt)
62 {
63  COMPILE_TIME_ASSERT(sizeof(int)==4);
64  // InterlockedIncrement
65  return __sync_add_and_fetch(pInt, 1);
66 }
67 
68 int AtomicDecrement(int* pInt)
69 {
70  // InterlockedDecrement
71  return __sync_sub_and_fetch(pInt, 1);
72 }
73 
74 #endif
75 
76 
77 
78 
79 
int AtomicIncrement(int *pInt)
#define COMPILE_TIME_ASSERT(x)
int AtomicDecrement(int *pInt)