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
//! RAII wrappers for raw HCS handles.
//!
//! HCS exposes three handle types — `HCS_OPERATION`, `HCS_SYSTEM`, and
//! `HCS_PROCESS` — each of which must be released via a matching `HcsClose*`
//! call. The wrappers in this module own the raw handle and implement `Drop`
//! to release it.
//!
//! These wrappers are both `Send` and `Sync`. HCS handles are stable
//! identifier values (raw pointers into `computecore.dll` bookkeeping) and
//! the HCS C API is documented as thread-safe for API invocation: functions
//! such as `HcsTerminateComputeSystem`, `HcsGetComputeSystemProperties`,
//! `HcsModifyComputeSystem`, and the operation-completion callbacks are all
//! callable from arbitrary threads, and Microsoft's own hcsshim (the Go
//! reference client used by moby / containerd) invokes them concurrently
//! from multiple goroutines without external serialization.
//!
//! Wrapper-level invariants (for example: "do not terminate twice", or
//! "only one writer may modify a compute system at a time") are enforced at
//! the layer that owns the wrapper — via `&mut self`, `Mutex`, or `RwLock`
//! as appropriate for the caller's logical data model — rather than by
//! stripping `Sync` from the handle type itself. Removing `Sync` would only
//! force every caller of a thread-safe C API to take an unnecessary lock
//! just to share the handle across tasks (for example, when a container
//! entry is stored in an `Arc<RwLock<HashMap<String, ContainerEntry>>>` and
//! accessed from Tokio worker threads).
use ;
/// Thread-safe transparent wrapper around any `Copy` FFI handle.
///
/// The upstream `windows` crate defines `HCS_SYSTEM`, `HCS_OPERATION`, and
/// `HCS_PROCESS` as `#[repr(transparent)]` tuple structs over `*mut c_void`,
/// which makes them `!Send + !Sync`. That's correct for arbitrary
/// `*mut c_void` but wrong for HCS handles specifically: the HCS C API is
/// documented as thread-safe for API invocation (see
/// `HcsTerminateComputeSystem`, `HcsGetComputeSystemProperties`,
/// `HcsCreateOperation`, and the process/operation entry points in
/// `computecore.dll`), and Microsoft's hcsshim Go client — the reference
/// implementation used by moby / containerd — routinely passes these
/// handles between goroutines without external serialization.
///
/// The orphan rule prevents us from `impl`-ing `Send`/`Sync` directly on the
/// upstream raw types. The canonical workaround, used by `hcsshim-rs` and
/// other Windows-Rust FFI crates, is a crate-local `#[repr(transparent)]`
/// newtype carrying the `Send + Sync` assertions. `SendHandle<T>` is that
/// newtype. We use it at every site where a raw HCS_* handle must cross an
/// `.await` boundary inside an `async fn` — the `Owned*` RAII wrappers
/// above already carry `Send + Sync`, but async state machines also keep
/// the un-wrapped raw values live across suspend points (locals, option
/// slots, fn params) and the compiler checks those too.
///
/// # Safety
///
/// The same argument as the `Owned*` wrappers applies: HCS handles are
/// stable pointer-valued identifiers into `computecore.dll` bookkeeping,
/// the taking APIs are documented thread-safe, and caller-level
/// invariants (for example "do not terminate twice") are enforced at the
/// wrapper-owner layer via `&mut self` / `Mutex` / `RwLock`, not by
/// stripping `Sync` from the handle type itself.
;
// Safety: see the type-level doc comment. HCS handles are documented
// thread-safe for API invocation; Microsoft's hcsshim Go client relies on
// this same property to pass handles across goroutines.
unsafe
// Safety: same rationale as Send — HCS taking APIs are thread-safe and
// hcsshim treats these handles as shareable across concurrent workers.
unsafe
/// Owned HCS operation handle. Released via `HcsCloseOperation` on drop.
;
// Safety: HCS handles are stable pointer-valued identifiers into
// `computecore.dll`. The handle value itself can legitimately move between
// threads.
unsafe
// Safety: the HCS C API is documented as thread-safe for API invocation
// (see `HcsCreateOperation`, `HcsWaitForOperationResult`,
// `HcsGetOperationResult`, etc. in the HostComputeSystem docs). Microsoft's
// hcsshim Go client calls these functions from multiple goroutines
// concurrently without serialization. Any caller-level invariants (e.g.
// "only one waiter per operation") are enforced at the wrapper owner's
// level via `&mut self` or a `Mutex`, not by removing `Sync`.
unsafe
/// Owned HCS compute-system handle. Released via `HcsCloseComputeSystem`
/// on drop. Closing does NOT terminate the system — it only releases the
/// caller's reference. Use `HcsTerminateComputeSystem` (wrapped elsewhere)
/// to actually stop a running system.
;
unsafe
// Safety: `HcsGetComputeSystemProperties`, `HcsTerminateComputeSystem`,
// `HcsShutdownComputeSystem`, `HcsModifyComputeSystem`, and the rest of the
// HCS_SYSTEM-taking entry points in `computecore.dll` are documented as
// thread-safe. Microsoft's hcsshim Go client calls them concurrently from
// multiple goroutines that all share the same handle. Caller invariants
// that truly require serialization (e.g. do-not-terminate-twice) are
// enforced at the wrapper-owner layer via `&mut self` or `Mutex`, not by
// making the handle `!Sync`.
unsafe
/// Owned HCS process handle. Released via `HcsCloseProcess` on drop.
/// Closing does NOT signal or terminate the process.
;
unsafe
// Safety: `HcsSignalProcess`, `HcsGetProcessInfo`, `HcsGetProcessProperties`,
// `HcsModifyProcess`, and the process I/O stream entry points are all
// documented as thread-safe. In practice I/O-reader tasks and the
// signal/wait task typically run on different Tokio worker threads and
// share the same `OwnedProcess` through an `Arc` — Sync is required for
// that pattern, and the underlying C API supports it.
unsafe