1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Threading support (§57, §93, §85 Phase 1).
//!
//! Thread-local state, concurrent parsing, concurrent transformation,
//! shared immutable dictionaries, callback isolation.
//!
//! # UPSTREAM-PARITY
//!
//! libxml2's threading support provides:
//!
//! - `xmlInitThreads()` / `xmlCleanupThreads()` — lifecycle
//! - `xmlLockLibrary()` / `xmlUnlockLibrary()` — global lock
//! - Thread-local storage for error state and parser contexts
//!
//! In modern libxml2 (2.12+), threading is initialized automatically
//! by `xmlInitParser`. The explicit thread functions exist for backward
//! compatibility.
//!
//! In Rust, we use standard thread-safe primitives and `thread_local!`
//! for thread-local storage. The global lock is a no-op since Rust's
//! type system prevents data races at compile time.
//!
//! # Phase 1 status
//!
//! Complete — all threading support is implemented.
//!
//! # Upstream contract
//!
//! Mirrors upstream `threads.c` (`SRC-LIBXML2-2.15.0-THREADS-C`, parity
//! target libxml2 2.15.3 oracle): `xmlInitThreads`, `xmlCleanupThreads`,
//! `xmlLockLibrary`, `xmlUnlockLibrary`, `xmlNewMutex`/`xmlFreeMutex`/
//! `xmlMutexLock`/`xmlMutexUnlock`, `xmlNewRMutex`/`xmlRMutexLock`/
//! `xmlRMutexUnlock`, `xmlNewCond`/`xmlCondWaitSignal`, and the
//! thread-local variants.
//!
//! # Conceptual behavior
//!
//! Implements the legacy explicit threading API. In modern libxml2 (2.12+)
//! initialization is lazy — `xmlInitParser` calls the init path
//! automatically, and the deprecated entry points exist for backward
//! compatibility. Rust primitives (`thread_local!`, parking_lot, atomics)
//! provide the same guarantees without upstream platform dispatch
//! (HAVE_POSIX_THREADS / HAVE_WIN32_THREADS).
//!
//! # Ownership & safety invariants
//!
//! Mutex/rmutex/cond handles are heap objects owned by the caller and
//! freed with the matching free function; thread-local error/parser state
//! is owned per thread and never shared. Rust memory-safety guarantees
//! replace the upstream data-race discipline — the SAFETY argument is the
//! type system, not lock discipline.
//!
//! # Historical quirks & epochs
//!
//! Thread support predates the thread-local globals era: globals.c
//! threading was integrated 2001-10-12/13 (commits b847864f, d0463560,
//! LORE-0005). R-000138: the deprecated init/cleanup entry points are
//! genuine no-ops in modern upstream (lazy init) and the candidate matches
//! that; `xmlCheckThreadLocalStorage` always passes with Rust thread-locals.
//!
//! # Deliberate oddities
//!
//! The global library lock is a deliberate no-op: Rust prevents data races
//! at compile time, and upstream xmlLockLibrary itself became vestigial
//! after the thread-local rewrite. Deprecated entry points keep their
//! no-op bodies to match the oracle byte-for-byte (R-000138).
//!
//! # Proving courts
//!
//! The globals-threading differential probe (tools/abi/globals_threading_
//! probe.py + courts/suites/data-abi/globals-threading-probe.c) verifies
//! handler-slot and error-global behavior byte-identical vs the oracle;
//! the parallel lib suite (100/100 runs clean, R-000170/R-000171) and
//! cargo test exercise the thread-local error model.
//!
//! # Tempting simplifications that would break parity
//!
//! Do not replace thread-locals with globals: per-thread parser error
//! state and the exported xmlLastError mirror (R-000170) depend on the
//! thread-local model. Do not make the deprecated entry points do real
//! work: upstream bodies are empty and observable behavior must match.
use c_void;
use ;
use c_int;
/// Whether threading has been initialized.
static THREADS_INITIALIZED: AtomicBool = new;
/// Initialize threading support.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// int xmlInitThreads(void);
/// ```
///
/// Returns 0 on success. In modern libxml2, this is called automatically
/// by `xmlInitParser`.
/// Clean up threading support.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlCleanupThreads(void);
/// ```
/// Check whether threading has been initialized.
/// Lock the library (global mutex).
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlLockLibrary(void);
/// ```
///
/// In upstream libxml2, this locks a global mutex. In Rust, this is
/// a no-op because Rust's type system prevents data races. However,
/// for FFI safety with C callers that may manipulate shared state,
/// a real mutex would be needed. This will be enhanced in Phase 2+.
pub const
/// Unlock the library.
///
/// # UPSTREAM-PARITY
///
/// ```c
/// void xmlUnlockLibrary(void);
/// ```
pub const
/// Get the number of active threads (for compatibility).
///
/// Returns the number of active threads, or 0 if unknown.
/// This is a compatibility stub — upstream doesn't expose this directly.
pub const
// ═══════════════════════════════════════════════════════════════════════════════
// Mutex / recursive-mutex API (upstream threads.h)
// ═══════════════════════════════════════════════════════════════════════════════
//
// `xmlMutexPtr`/`xmlRMutexPtr` are opaque handles. The candidate boxes a
// parking_lot mutex (a `Mutex<()>` for the simple mutex, a reentrant
// `ReentrantMutex` for the recursive mutex) so lock/unlock round-trips
// through the FFI boundary with real exclusion semantics.
//
// The `RawMutex` trait import brings the manual `lock`/`unlock` methods
// into scope for the raw-mutex lock/unlock used below.
use RawMutex as _;
/// Create a simple mutex (upstream threads.h `xmlNewMutex`).
///
/// Returns an opaque handle (free with `xmlFreeMutex`), or NULL on
/// allocation failure.
/// Free a simple mutex (upstream threads.h `xmlFreeMutex`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewMutex` (or NULL), and must not be
/// locked by any thread when freed.
pub unsafe
/// Lock a simple mutex (upstream threads.h `xmlMutexLock`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewMutex` or NULL.
pub unsafe
/// Unlock a simple mutex (upstream threads.h `xmlMutexUnlock`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewMutex` or NULL, and must be
/// locked by the calling thread.
pub unsafe
/// Create a recursive mutex (upstream threads.h `xmlNewRMutex`).
///
/// Returns an opaque handle (free with `xmlFreeRMutex`), or NULL on
/// allocation failure.
/// Free a recursive mutex (upstream threads.h `xmlFreeRMutex`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewRMutex` (or NULL).
pub unsafe
/// Lock a recursive mutex (upstream threads.h `xmlRMutexLock`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewRMutex` or NULL.
pub unsafe
/// Unlock a recursive mutex (upstream threads.h `xmlRMutexUnlock`).
///
/// # SAFETY
///
/// - `tok` must be a handle from `xmlNewRMutex` or NULL.
pub unsafe
// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════