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
//! 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.
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
// ═══════════════════════════════════════════════════════════════════════════════