|
| 1 | +/* |
| 2 | + * Copyright 2024-present MongoDB, Inc. |
| 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 | +/* Note: this file was derived from libbson's bson-atomic.c */ |
| 18 | + |
| 19 | +#include "bson/bson.h" |
| 20 | + |
| 21 | +#include "phongo_atomic.h" |
| 22 | + |
| 23 | +#ifdef BSON_OS_UNIX |
| 24 | +/* For sched_yield() */ |
| 25 | +#include <sched.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +static void |
| 29 | +_thrd_yield (void) |
| 30 | +{ |
| 31 | + BSON_IF_WINDOWS (SwitchToThread ();) |
| 32 | + BSON_IF_POSIX (sched_yield ();) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Some platforms do not support compiler intrinsics for atomic operations. |
| 37 | + * We emulate that here using a spin lock and regular arithmetic operations |
| 38 | + */ |
| 39 | +static int8_t gEmulAtomicLock = 0; |
| 40 | + |
| 41 | +static void |
| 42 | +_lock_emul_atomic (void) |
| 43 | +{ |
| 44 | + int i; |
| 45 | + if (phongo_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, phongo_memory_order_acquire) == 0) { |
| 46 | + /* Successfully took the spinlock */ |
| 47 | + return; |
| 48 | + } |
| 49 | + /* Failed. Try taking ten more times, then begin sleeping. */ |
| 50 | + for (i = 0; i < 10; ++i) { |
| 51 | + if (phongo_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, phongo_memory_order_acquire) == 0) { |
| 52 | + /* Succeeded in taking the lock */ |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + /* Still don't have the lock. Spin and yield */ |
| 57 | + while (phongo_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, phongo_memory_order_acquire) != 0) { |
| 58 | + _thrd_yield (); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +static void |
| 63 | +_unlock_emul_atomic (void) |
| 64 | +{ |
| 65 | + int64_t rv = phongo_atomic_int8_exchange (&gEmulAtomicLock, 0, phongo_memory_order_release); |
| 66 | + BSON_ASSERT (rv == 1 && "Released atomic lock while not holding it"); |
| 67 | +} |
| 68 | + |
| 69 | +int32_t |
| 70 | +_phongo_emul_atomic_int32_fetch_add (volatile int32_t *p, int32_t n, enum phongo_memory_order _unused) |
| 71 | +{ |
| 72 | + int32_t ret; |
| 73 | + |
| 74 | + BSON_UNUSED (_unused); |
| 75 | + |
| 76 | + _lock_emul_atomic (); |
| 77 | + ret = *p; |
| 78 | + *p += n; |
| 79 | + _unlock_emul_atomic (); |
| 80 | + return ret; |
| 81 | +} |
| 82 | + |
| 83 | +int32_t |
| 84 | +_phongo_emul_atomic_int32_exchange (volatile int32_t *p, int32_t n, enum phongo_memory_order _unused) |
| 85 | +{ |
| 86 | + int32_t ret; |
| 87 | + |
| 88 | + BSON_UNUSED (_unused); |
| 89 | + |
| 90 | + _lock_emul_atomic (); |
| 91 | + ret = *p; |
| 92 | + *p = n; |
| 93 | + _unlock_emul_atomic (); |
| 94 | + return ret; |
| 95 | +} |
| 96 | + |
| 97 | +int32_t |
| 98 | +_phongo_emul_atomic_int32_compare_exchange_strong (volatile int32_t *p, |
| 99 | + int32_t expect_value, |
| 100 | + int32_t new_value, |
| 101 | + enum phongo_memory_order _unused) |
| 102 | +{ |
| 103 | + int32_t ret; |
| 104 | + |
| 105 | + BSON_UNUSED (_unused); |
| 106 | + |
| 107 | + _lock_emul_atomic (); |
| 108 | + ret = *p; |
| 109 | + if (ret == expect_value) { |
| 110 | + *p = new_value; |
| 111 | + } |
| 112 | + _unlock_emul_atomic (); |
| 113 | + return ret; |
| 114 | +} |
| 115 | + |
| 116 | +int32_t |
| 117 | +_phongo_emul_atomic_int32_compare_exchange_weak (volatile int32_t *p, |
| 118 | + int32_t expect_value, |
| 119 | + int32_t new_value, |
| 120 | + enum phongo_memory_order order) |
| 121 | +{ |
| 122 | + /* We're emulating. We can't do a weak version. */ |
| 123 | + return _phongo_emul_atomic_int32_compare_exchange_strong (p, expect_value, new_value, order); |
| 124 | +} |
0 commit comments