Skip to content

Commit a1c99c4

Browse files
CDRIVER-5702 deprecate bson-cmp.h and bson-atomic.h (#1762)
* copy `bson-cmp.h` to internal `common-cmp-private.h` * copy `bson-atomic.(h|c)` to internal `common-atomic-private.(h|c)` --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 1e754dc commit a1c99c4

File tree

89 files changed

+1944
-342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+1944
-342
lines changed

NEWS

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
libmongoc 1.29.0 (unreleased)
22
=============================
33

4-
Deprecated:
5-
6-
* Compiling with `BSON_MEMCHECK` defined is deprecated.
7-
* `bson_string_t` and associated functions.
84

95
Platform Support:
106

src/common/bson-dsl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "bson/bson.h"
17+
#include <common-cmp-private.h>
1718

1819
enum {
1920
/// Toggle this value to enable/disable debug output for all bsonDSL
@@ -118,7 +119,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
118119
_bsonDSL_begin ("\"%s\" => [%s]", String, _bsonDSL_strElide (30, Element)); \
119120
const char *_bbString = (String); \
120121
const uint64_t length = (Len); \
121-
if (bson_in_range_unsigned (int, length)) { \
122+
if (mcommon_in_range_unsigned (int, length)) { \
122123
_bbCtx.key = _bbString; \
123124
_bbCtx.key_len = (int) length; \
124125
_bsonValueOperation (Element); \

src/common/common-atomic-private.h

Lines changed: 621 additions & 0 deletions
Large diffs are not rendered by default.

src/common/common-atomic.c

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/*
2+
* Copyright 2009-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+
18+
#include <common-atomic-private.h>
19+
20+
#ifdef BSON_OS_UNIX
21+
/* For sched_yield() */
22+
#include <sched.h>
23+
#endif
24+
25+
void
26+
mcommon_thrd_yield (void)
27+
{
28+
BSON_IF_WINDOWS (SwitchToThread ();)
29+
BSON_IF_POSIX (sched_yield ();)
30+
}
31+
32+
/**
33+
* Some platforms do not support compiler intrinsics for atomic operations.
34+
* We emulate that here using a spin lock and regular arithmetic operations
35+
*/
36+
static int8_t gEmulAtomicLock = 0;
37+
38+
static void
39+
_lock_emul_atomic (void)
40+
{
41+
int i;
42+
if (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) == 0) {
43+
/* Successfully took the spinlock */
44+
return;
45+
}
46+
/* Failed. Try taking ten more times, then begin sleeping. */
47+
for (i = 0; i < 10; ++i) {
48+
if (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) == 0) {
49+
/* Succeeded in taking the lock */
50+
return;
51+
}
52+
}
53+
/* Still don't have the lock. Spin and yield */
54+
while (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) != 0) {
55+
mcommon_thrd_yield ();
56+
}
57+
}
58+
59+
static void
60+
_unlock_emul_atomic (void)
61+
{
62+
int64_t rv = mcommon_atomic_int8_exchange (&gEmulAtomicLock, 0, mcommon_memory_order_release);
63+
BSON_ASSERT (rv == 1 && "Released atomic lock while not holding it");
64+
}
65+
66+
int64_t
67+
_mcommon_emul_atomic_int64_fetch_add (volatile int64_t *p, int64_t n, enum mcommon_memory_order _unused)
68+
{
69+
int64_t ret;
70+
71+
BSON_UNUSED (_unused);
72+
73+
_lock_emul_atomic ();
74+
ret = *p;
75+
*p += n;
76+
_unlock_emul_atomic ();
77+
return ret;
78+
}
79+
80+
int64_t
81+
_mcommon_emul_atomic_int64_exchange (volatile int64_t *p, int64_t n, enum mcommon_memory_order _unused)
82+
{
83+
int64_t ret;
84+
85+
BSON_UNUSED (_unused);
86+
87+
_lock_emul_atomic ();
88+
ret = *p;
89+
*p = n;
90+
_unlock_emul_atomic ();
91+
return ret;
92+
}
93+
94+
int64_t
95+
_mcommon_emul_atomic_int64_compare_exchange_strong (volatile int64_t *p,
96+
int64_t expect_value,
97+
int64_t new_value,
98+
enum mcommon_memory_order _unused)
99+
{
100+
int64_t ret;
101+
102+
BSON_UNUSED (_unused);
103+
104+
_lock_emul_atomic ();
105+
ret = *p;
106+
if (ret == expect_value) {
107+
*p = new_value;
108+
}
109+
_unlock_emul_atomic ();
110+
return ret;
111+
}
112+
113+
int64_t
114+
_mcommon_emul_atomic_int64_compare_exchange_weak (volatile int64_t *p,
115+
int64_t expect_value,
116+
int64_t new_value,
117+
enum mcommon_memory_order order)
118+
{
119+
/* We're emulating. We can't do a weak version. */
120+
return _mcommon_emul_atomic_int64_compare_exchange_strong (p, expect_value, new_value, order);
121+
}
122+
123+
124+
int32_t
125+
_mcommon_emul_atomic_int32_fetch_add (volatile int32_t *p, int32_t n, enum mcommon_memory_order _unused)
126+
{
127+
int32_t ret;
128+
129+
BSON_UNUSED (_unused);
130+
131+
_lock_emul_atomic ();
132+
ret = *p;
133+
*p += n;
134+
_unlock_emul_atomic ();
135+
return ret;
136+
}
137+
138+
int32_t
139+
_mcommon_emul_atomic_int32_exchange (volatile int32_t *p, int32_t n, enum mcommon_memory_order _unused)
140+
{
141+
int32_t ret;
142+
143+
BSON_UNUSED (_unused);
144+
145+
_lock_emul_atomic ();
146+
ret = *p;
147+
*p = n;
148+
_unlock_emul_atomic ();
149+
return ret;
150+
}
151+
152+
int32_t
153+
_mcommon_emul_atomic_int32_compare_exchange_strong (volatile int32_t *p,
154+
int32_t expect_value,
155+
int32_t new_value,
156+
enum mcommon_memory_order _unused)
157+
{
158+
int32_t ret;
159+
160+
BSON_UNUSED (_unused);
161+
162+
_lock_emul_atomic ();
163+
ret = *p;
164+
if (ret == expect_value) {
165+
*p = new_value;
166+
}
167+
_unlock_emul_atomic ();
168+
return ret;
169+
}
170+
171+
int32_t
172+
_mcommon_emul_atomic_int32_compare_exchange_weak (volatile int32_t *p,
173+
int32_t expect_value,
174+
int32_t new_value,
175+
enum mcommon_memory_order order)
176+
{
177+
/* We're emulating. We can't do a weak version. */
178+
return _mcommon_emul_atomic_int32_compare_exchange_strong (p, expect_value, new_value, order);
179+
}
180+
181+
182+
int
183+
_mcommon_emul_atomic_int_fetch_add (volatile int *p, int n, enum mcommon_memory_order _unused)
184+
{
185+
int ret;
186+
187+
BSON_UNUSED (_unused);
188+
189+
_lock_emul_atomic ();
190+
ret = *p;
191+
*p += n;
192+
_unlock_emul_atomic ();
193+
return ret;
194+
}
195+
196+
int
197+
_mcommon_emul_atomic_int_exchange (volatile int *p, int n, enum mcommon_memory_order _unused)
198+
{
199+
int ret;
200+
201+
BSON_UNUSED (_unused);
202+
203+
_lock_emul_atomic ();
204+
ret = *p;
205+
*p = n;
206+
_unlock_emul_atomic ();
207+
return ret;
208+
}
209+
210+
int
211+
_mcommon_emul_atomic_int_compare_exchange_strong (volatile int *p,
212+
int expect_value,
213+
int new_value,
214+
enum mcommon_memory_order _unused)
215+
{
216+
int ret;
217+
218+
BSON_UNUSED (_unused);
219+
220+
_lock_emul_atomic ();
221+
ret = *p;
222+
if (ret == expect_value) {
223+
*p = new_value;
224+
}
225+
_unlock_emul_atomic ();
226+
return ret;
227+
}
228+
229+
int
230+
_mcommon_emul_atomic_int_compare_exchange_weak (volatile int *p,
231+
int expect_value,
232+
int new_value,
233+
enum mcommon_memory_order order)
234+
{
235+
/* We're emulating. We can't do a weak version. */
236+
return _mcommon_emul_atomic_int_compare_exchange_strong (p, expect_value, new_value, order);
237+
}
238+
239+
void *
240+
_mcommon_emul_atomic_ptr_exchange (void *volatile *p, void *n, enum mcommon_memory_order _unused)
241+
{
242+
void *ret;
243+
244+
BSON_UNUSED (_unused);
245+
246+
_lock_emul_atomic ();
247+
ret = *p;
248+
*p = n;
249+
_unlock_emul_atomic ();
250+
return ret;
251+
}

0 commit comments

Comments
 (0)