@@ -33,8 +33,12 @@ const log = std.log.scoped(.loop);
33
33
pub const SingleThreaded = struct {
34
34
alloc : std.mem.Allocator , // TODO: unmanaged version ?
35
35
io : IO ,
36
+
37
+ // both events_nb are used to track how many callbacks are to be called.
38
+ // We use these counters to wait until all the events are finished.
36
39
js_events_nb : usize ,
37
40
zig_events_nb : usize ,
41
+
38
42
cbk_error : bool = false ,
39
43
40
44
// js_ctx_id is incremented each time the loop is reset for JS.
@@ -284,21 +288,36 @@ pub const SingleThreaded = struct {
284
288
self .io .cancel_one (* ContextCancel , ctx , cancelCallback , completion , comp_cancel );
285
289
}
286
290
291
+ // cancelAll will cancel all future events.
292
+ // The loop will not be usable anymore after cancelAll.
293
+ // It is used only during the deinit.
287
294
fn cancelAll (self : * Self ) void {
288
295
self .resetEvents (.js );
289
296
self .resetEvents (.zig );
290
297
self .io .cancel_all ();
291
298
}
292
299
293
300
// Reset all existing JS callbacks.
301
+ // The existing events will happen and their memory will be cleanup but the
302
+ // corresponding callbacks will not be called.
294
303
pub fn resetJS (self : * Self ) void {
295
304
self .js_ctx_id += 1 ;
296
305
self .cancelled .clearRetainingCapacity ();
306
+ // We don't call self.resetEvents(.js) intentionnaly.
307
+ // Indeed we don't want to prevent the possibility to wait for events.
308
+ // The caller may want to wait to have memory correcly cleaned after
309
+ // the events happen, even if the callback are ignoreed.
297
310
}
298
311
299
312
// Reset all existing Zig callbacks.
313
+ // The existing events will happen and their memory will be cleanup but the
314
+ // corresponding callbacks will not be called.
300
315
pub fn resetZig (self : * Self ) void {
301
316
self .zig_ctx_id += 1 ;
317
+ // We don't call self.resetEvents(.zig) intentionnaly.
318
+ // Indeed we don't want to prevent the possibility to wait for events.
319
+ // The caller may want to wait to have memory correcly cleaned after
320
+ // the events happen, even if the callback are ignoreed.
302
321
}
303
322
304
323
// IO callbacks APIs
0 commit comments