@@ -347,7 +347,7 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
347
347
self . workers = workers
348
348
}
349
349
350
- func start( timeout: Duration , checkInterval: Duration ) async throws {
350
+ func start( stackSize : Int ? , timeout: Duration , checkInterval: Duration ) async throws {
351
351
#if canImport(wasi_pthread) && compiler(>=6.1) && _runtime(_multithreaded)
352
352
class Context : @unchecked Sendable {
353
353
let executor : WebWorkerTaskExecutor . Executor
@@ -375,9 +375,21 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
375
375
let unmanagedContext = Unmanaged . passRetained ( context)
376
376
contexts. append ( unmanagedContext)
377
377
let ptr = unmanagedContext. toOpaque ( )
378
+ var attr = pthread_attr_t ( )
379
+ pthread_attr_init ( & attr)
380
+ // Set the stack size if specified.
381
+ if let stackSize {
382
+ let ret = pthread_attr_setstacksize ( & attr, stackSize)
383
+ guard ret == 0 else {
384
+ let strerror = String ( cString: strerror ( ret) )
385
+ throw SpawnError (
386
+ reason: " Failed to set stack size ( \( stackSize) ) for thread ( \( ret) : \( strerror) ) "
387
+ )
388
+ }
389
+ }
378
390
let ret = pthread_create (
379
391
nil ,
380
- nil ,
392
+ & attr ,
381
393
{ ptr in
382
394
// Cast to a optional pointer to absorb nullability variations between platforms.
383
395
let ptr : UnsafeMutableRawPointer ? = ptr
@@ -390,6 +402,7 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
390
402
} ,
391
403
ptr
392
404
)
405
+ pthread_attr_destroy ( & attr)
393
406
guard ret == 0 else {
394
407
let strerror = String ( cString: strerror ( ret) )
395
408
throw SpawnError ( reason: " Failed to create a thread ( \( ret) : \( strerror) ) " )
@@ -467,16 +480,19 @@ public final class WebWorkerTaskExecutor: TaskExecutor {
467
480
///
468
481
/// - Parameters:
469
482
/// - numberOfThreads: The number of Web Worker threads to spawn.
483
+ /// - stackSize: The stack size for each worker thread. Default is `nil` (use the platform default stack size).
470
484
/// - timeout: The maximum time to wait for all worker threads to be started. Default is 3 seconds.
471
485
/// - checkInterval: The interval to check if all worker threads are started. Default is 5 microseconds.
472
486
/// - Throws: An error if any worker thread fails to initialize within the timeout period.
487
+ /// - Note: The default stack size of wasi-libc is typically 128KB.
473
488
public init (
474
489
numberOfThreads: Int ,
490
+ stackSize: Int ? = nil ,
475
491
timeout: Duration = . seconds( 3 ) ,
476
492
checkInterval: Duration = . microseconds( 5 )
477
493
) async throws {
478
494
self . executor = Executor ( numberOfThreads: numberOfThreads)
479
- try await self . executor. start ( timeout: timeout, checkInterval: checkInterval)
495
+ try await self . executor. start ( stackSize : stackSize , timeout: timeout, checkInterval: checkInterval)
480
496
}
481
497
482
498
/// Terminates all worker threads managed by this executor.
0 commit comments