- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / protobuf / src / google / protobuf / stubs / atomicops_internals_mips_gcc.h
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc.  All rights reserved.
3 // http://code.google.com/p/protobuf/
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_MIPS_GCC_H_
34 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_MIPS_GCC_H_
35
36 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory")
37
38 namespace google {
39 namespace protobuf {
40 namespace internal {
41
42 // Atomically execute:
43 //      result = *ptr;
44 //      if (*ptr == old_value)
45 //        *ptr = new_value;
46 //      return result;
47 //
48 // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
49 // Always return the old value of "*ptr"
50 //
51 // This routine implies no memory barriers.
52 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
53                                          Atomic32 old_value,
54                                          Atomic32 new_value) {
55   Atomic32 prev, tmp;
56   __asm__ __volatile__(".set push\n"
57                        ".set noreorder\n"
58                        "1:\n"
59                        "ll %0, %5\n"  // prev = *ptr
60                        "bne %0, %3, 2f\n"  // if (prev != old_value) goto 2
61                        "move %2, %4\n"  // tmp = new_value
62                        "sc %2, %1\n"  // *ptr = tmp (with atomic check)
63                        "beqz %2, 1b\n"  // start again on atomic error
64                        "nop\n"  // delay slot nop
65                        "2:\n"
66                        ".set pop\n"
67                        : "=&r" (prev), "=m" (*ptr), "=&r" (tmp)
68                        : "Ir" (old_value), "r" (new_value), "m" (*ptr)
69                        : "memory");
70   return prev;
71 }
72
73 // Atomically store new_value into *ptr, returning the previous value held in
74 // *ptr.  This routine implies no memory barriers.
75 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
76                                          Atomic32 new_value) {
77   Atomic32 temp, old;
78   __asm__ __volatile__(".set push\n"
79                        ".set noreorder\n"
80                        "1:\n"
81                        "ll %1, %2\n"  // old = *ptr
82                        "move %0, %3\n"  // temp = new_value
83                        "sc %0, %2\n"  // *ptr = temp (with atomic check)
84                        "beqz %0, 1b\n"  // start again on atomic error
85                        "nop\n"  // delay slot nop
86                        ".set pop\n"
87                        : "=&r" (temp), "=&r" (old), "=m" (*ptr)
88                        : "r" (new_value), "m" (*ptr)
89                        : "memory");
90
91   return old;
92 }
93
94 // Atomically increment *ptr by "increment".  Returns the new value of
95 // *ptr with the increment applied.  This routine implies no memory barriers.
96 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
97                                           Atomic32 increment) {
98   Atomic32 temp, temp2;
99
100   __asm__ __volatile__(".set push\n"
101                        ".set noreorder\n"
102                        "1:\n"
103                        "ll %0, %2\n"  // temp = *ptr
104                        "addu %1, %0, %3\n"  // temp2 = temp + increment
105                        "sc %1, %2\n"  // *ptr = temp2 (with atomic check)
106                        "beqz %1, 1b\n"  // start again on atomic error
107                        "addu %1, %0, %3\n"  // temp2 = temp + increment
108                        ".set pop\n"
109                        : "=&r" (temp), "=&r" (temp2), "=m" (*ptr)
110                        : "Ir" (increment), "m" (*ptr)
111                        : "memory");
112   // temp2 now holds the final value.
113   return temp2;
114 }
115
116 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
117                                         Atomic32 increment) {
118   ATOMICOPS_COMPILER_BARRIER();
119   Atomic32 res = NoBarrier_AtomicIncrement(ptr, increment);
120   ATOMICOPS_COMPILER_BARRIER();
121   return res;
122 }
123
124 // "Acquire" operations
125 // ensure that no later memory access can be reordered ahead of the operation.
126 // "Release" operations ensure that no previous memory access can be reordered
127 // after the operation.  "Barrier" operations have both "Acquire" and "Release"
128 // semantics.   A MemoryBarrier() has "Barrier" semantics, but does no memory
129 // access.
130 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
131                                        Atomic32 old_value,
132                                        Atomic32 new_value) {
133   ATOMICOPS_COMPILER_BARRIER();
134   Atomic32 res = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
135   ATOMICOPS_COMPILER_BARRIER();
136   return res;
137 }
138
139 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
140                                        Atomic32 old_value,
141                                        Atomic32 new_value) {
142   ATOMICOPS_COMPILER_BARRIER();
143   Atomic32 res = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
144   ATOMICOPS_COMPILER_BARRIER();
145   return res;
146 }
147
148 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
149   *ptr = value;
150 }
151
152 inline void MemoryBarrier() {
153   __asm__ __volatile__("sync" : : : "memory");
154 }
155
156 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
157   *ptr = value;
158   MemoryBarrier();
159 }
160
161 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
162   MemoryBarrier();
163   *ptr = value;
164 }
165
166 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
167   return *ptr;
168 }
169
170 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
171   Atomic32 value = *ptr;
172   MemoryBarrier();
173   return value;
174 }
175
176 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
177   MemoryBarrier();
178   return *ptr;
179 }
180
181 }  // namespace internal
182 }  // namespace protobuf
183 }  // namespace google
184
185 #undef ATOMICOPS_COMPILER_BARRIER
186
187 #endif  // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_MIPS_GCC_H_