tizen 2.3.1 release
[external/protobuf.git] / src / google / protobuf / stubs / atomicops_internals_arm_qnx.h
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // This file is an internal atomic implementation, use atomicops.h instead.
32
33 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_QNX_H_
34 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_QNX_H_
35
36 // For _smp_cmpxchg()
37 #include <pthread.h>
38
39 namespace google {
40 namespace protobuf {
41 namespace internal {
42
43 inline Atomic32 QNXCmpxchg(Atomic32 old_value,
44                            Atomic32 new_value,
45                            volatile Atomic32* ptr) {
46   return static_cast<Atomic32>(
47       _smp_cmpxchg((volatile unsigned *)ptr,
48                    (unsigned)old_value,
49                    (unsigned)new_value));
50 }
51
52
53 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
54                                          Atomic32 old_value,
55                                          Atomic32 new_value) {
56   Atomic32 prev_value = *ptr;
57   do {
58     if (!QNXCmpxchg(old_value, new_value,
59                     const_cast<Atomic32*>(ptr))) {
60       return old_value;
61     }
62     prev_value = *ptr;
63   } while (prev_value == old_value);
64   return prev_value;
65 }
66
67 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
68                                          Atomic32 new_value) {
69   Atomic32 old_value;
70   do {
71     old_value = *ptr;
72   } while (QNXCmpxchg(old_value, new_value,
73                       const_cast<Atomic32*>(ptr)));
74   return old_value;
75 }
76
77 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
78                                           Atomic32 increment) {
79   return Barrier_AtomicIncrement(ptr, increment);
80 }
81
82 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
83                                         Atomic32 increment) {
84   for (;;) {
85     // Atomic exchange the old value with an incremented one.
86     Atomic32 old_value = *ptr;
87     Atomic32 new_value = old_value + increment;
88     if (QNXCmpxchg(old_value, new_value,
89                    const_cast<Atomic32*>(ptr)) == 0) {
90       // The exchange took place as expected.
91       return new_value;
92     }
93     // Otherwise, *ptr changed mid-loop and we need to retry.
94   }
95 }
96
97 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
98                                        Atomic32 old_value,
99                                        Atomic32 new_value) {
100   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
101 }
102
103 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
104                                        Atomic32 old_value,
105                                        Atomic32 new_value) {
106   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
107 }
108
109 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
110   *ptr = value;
111 }
112
113 inline void MemoryBarrier() {
114   __sync_synchronize();
115 }
116
117 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
118   *ptr = value;
119   MemoryBarrier();
120 }
121
122 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
123   MemoryBarrier();
124   *ptr = value;
125 }
126
127 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
128   return *ptr;
129 }
130
131 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
132   Atomic32 value = *ptr;
133   MemoryBarrier();
134   return value;
135 }
136
137 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
138   MemoryBarrier();
139   return *ptr;
140 }
141
142 }  // namespace internal
143 }  // namespace protobuf
144 }  // namespace google
145
146 #endif  // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_ARM_QNX_H_