1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_atomic.h"
19 #ifdef USE_ATOMICS_PPC
22 # define PPC405_ERR77_SYNC " sync\n"
24 # define PPC405_ERR77_SYNC
27 APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
29 #if defined (NEED_ATOMICS_GENERIC64)
30 return apr__atomic_generic64_init(p);
36 APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
41 APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
46 APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
48 apr_uint32_t prev, temp;
50 asm volatile ("1:\n" /* lost reservation */
51 " lwarx %0,0,%3\n" /* load and reserve */
52 " add %1,%0,%4\n" /* add val and prev */
53 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
54 " stwcx. %1,0,%3\n" /* store new value */
55 " bne- 1b\n" /* loop if lost */
56 : "=&r" (prev), "=&r" (temp), "=m" (*mem)
57 : "b" (mem), "r" (val)
63 APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
67 asm volatile ("1:\n" /* lost reservation */
68 " lwarx %0,0,%2\n" /* load and reserve */
69 " subf %0,%3,%0\n" /* subtract val */
70 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
71 " stwcx. %0,0,%2\n" /* store new value */
72 " bne- 1b\n" /* loop if lost */
73 : "=&r" (temp), "=m" (*mem)
74 : "b" (mem), "r" (val)
78 APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
82 asm volatile ("1:\n" /* lost reservation */
83 " lwarx %0,0,%2\n" /* load and reserve */
84 " addi %0,%0,1\n" /* add immediate */
85 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
86 " stwcx. %0,0,%2\n" /* store new value */
87 " bne- 1b\n" /* loop if lost */
88 " subi %0,%0,1\n" /* return old value */
89 : "=&b" (prev), "=m" (*mem)
90 : "b" (mem), "m" (*mem)
96 APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
100 asm volatile ("1:\n" /* lost reservation */
101 " lwarx %0,0,%2\n" /* load and reserve */
102 " subi %0,%0,1\n" /* subtract immediate */
103 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
104 " stwcx. %0,0,%2\n" /* store new value */
105 " bne- 1b\n" /* loop if lost */
106 : "=&b" (prev), "=m" (*mem)
107 : "b" (mem), "m" (*mem)
113 APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
118 asm volatile ("1:\n" /* lost reservation */
119 " lwarx %0,0,%1\n" /* load and reserve */
120 " cmpw %0,%3\n" /* compare operands */
121 " bne- exit_%=\n" /* skip if not equal */
122 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
123 " stwcx. %2,0,%1\n" /* store new value */
124 " bne- 1b\n" /* loop if lost */
125 "exit_%=:\n" /* not equal */
127 : "b" (mem), "r" (with), "r" (cmp)
133 APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
137 asm volatile ("1:\n" /* lost reservation */
138 " lwarx %0,0,%1\n" /* load and reserve */
139 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
140 " stwcx. %2,0,%1\n" /* store new value */
141 " bne- 1b" /* loop if lost */
143 : "b" (mem), "r" (val)
149 APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
152 #if APR_SIZEOF_VOIDP == 4
153 asm volatile ("1:\n" /* lost reservation */
154 " lwarx %0,0,%1\n" /* load and reserve */
155 " cmpw %0,%3\n" /* compare operands */
156 " bne- 2f\n" /* skip if not equal */
157 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
158 " stwcx. %2,0,%1\n" /* store new value */
159 " bne- 1b\n" /* loop if lost */
160 "2:\n" /* not equal */
162 : "b" (mem), "r" (with), "r" (cmp)
164 #elif APR_SIZEOF_VOIDP == 8
165 asm volatile ("1:\n" /* lost reservation */
166 " ldarx %0,0,%1\n" /* load and reserve */
167 " cmpd %0,%3\n" /* compare operands */
168 " bne- 2f\n" /* skip if not equal */
169 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
170 " stdcx. %2,0,%1\n" /* store new value */
171 " bne- 1b\n" /* loop if lost */
172 "2:\n" /* not equal */
174 : "b" (mem), "r" (with), "r" (cmp)
177 #error APR_SIZEOF_VOIDP value not supported
182 APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
185 #if APR_SIZEOF_VOIDP == 4
186 asm volatile ("1:\n" /* lost reservation */
187 " lwarx %0,0,%1\n" /* load and reserve */
188 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
189 " stwcx. %2,0,%1\n" /* store new value */
190 " bne- 1b\n" /* loop if lost */
191 " isync\n" /* memory barrier */
193 : "b" (mem), "r" (with)
195 #elif APR_SIZEOF_VOIDP == 8
196 asm volatile ("1:\n" /* lost reservation */
197 " ldarx %0,0,%1\n" /* load and reserve */
198 PPC405_ERR77_SYNC /* ppc405 Erratum 77 */
199 " stdcx. %2,0,%1\n" /* store new value */
200 " bne- 1b\n" /* loop if lost */
201 " isync\n" /* memory barrier */
203 : "b" (mem), "r" (with)
206 #error APR_SIZEOF_VOIDP value not supported
211 #endif /* USE_ATOMICS_PPC */