Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Io › Threaded › groupConcurrent Private Function groupConcurrent Doc Source Private Function std.Io.Threaded.groupConcurrent
fn groupConcurrent (
userdata : ?*anyopaque ,
type_erased : *Io .Group ,
context : []const u8 ,
context_alignment : Alignment ,
start : *const fn (context : *const anyopaque ) void ,
) Io .ConcurrentError !void Params userdata : ?*anyopaque type_erased : *Io .Group context : []const u8 context_alignment : Alignment start : *const fn (context : *const anyopaque ) void File Io/Threaded.zig: 2229
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280 fn groupConcurrent (
userdata : ?*anyopaque ,
type_erased : *Io .Group ,
context : []const u8 ,
context_alignment : Alignment ,
start : *const fn (context : *const anyopaque ) void ,
) Io .ConcurrentError !void {
if (builtin .single_threaded ) return error .ConcurrencyUnavailable ;
const t : *Threaded = @ptrCast (@alignCast (userdata ));
const g : Group = .{ .ptr = type_erased };
const gpa = t .allocator ;
const task = Group .Task .create (gpa , g , context , context_alignment , start ) catch |err | switch (err ) {
error .OutOfMemory => return error .ConcurrencyUnavailable ,
};
errdefer task .destroy (gpa );
mutexLock (&t .mutex );
defer mutexUnlock (&t .mutex );
const busy_count = t .busy_count ;
if (busy_count >= @backingInt (t .concurrent_limit ))
return error .ConcurrencyUnavailable ;
t .busy_count = busy_count + 1 ;
errdefer t .busy_count = busy_count ;
const pool_size = t .wait_group .value ();
if (pool_size - busy_count == 0 ) {
t .wait_group .start ();
errdefer t .wait_group .finish ();
const thread = std .Thread .spawn (.{ .stack_size = t .stack_size }, worker , .{t }) catch
return error .ConcurrencyUnavailable ;
thread .detach ();
}
// TODO: if this logic is changed to be lock-free, this `fetchAdd` must be released by the queue
// prepend so that the task doesn't finish without observing this and try to decrement the count
// below zero.
_ = g .status ().fetchAdd (.{
.num_running = 1 ,
.have_awaiter = false ,
.canceled = false ,
}, .monotonic );
t .run_queue .prepend (&task .runnable .node );
condSignal (&t .cond );
}