diff --git a/taskpools/primitives/barriers.nim b/taskpools/primitives/barriers.nim index 4c48a55..2ce6ffe 100644 --- a/taskpools/primitives/barriers.nim +++ b/taskpools/primitives/barriers.nim @@ -43,7 +43,7 @@ else: proc init*(syncBarrier: var SyncBarrier, threadCount: range[0'i32..high(int32)]) {.inline.} = ## Initialize a synchronization barrier that will block ``threadCount`` threads ## before release. - let err {.used.} = pthread_barrier_init(syncBarrier, nil, threadCount) + let err {.used.} = pthread_barrier_init(syncBarrier, nil, cuint threadCount) when compileOption("assertions"): if err != 0: raiseOSError(OSErrorCode(err)) diff --git a/taskpools/primitives/barriers_macos.nim b/taskpools/primitives/barriers_macos.nim index dfaa38b..af8d589 100644 --- a/taskpools/primitives/barriers_macos.nim +++ b/taskpools/primitives/barriers_macos.nim @@ -13,11 +13,9 @@ import locks type - Natural32 = range[0'i32..high(int32)] - Errno* = cint - PthreadAttr* = object + PthreadBarrierAttr* = object ## Dummy PthreadBarrier* = object ## Implementation of a sense reversing barrier @@ -26,8 +24,8 @@ type lock: Lock # Alternatively spinlock on Atomic cond {.guard: lock.}: Cond sense {.guard: lock.}: bool # Choose int32 to avoid zero-expansion cost in registers? - left {.guard: lock.}: Natural32 # Number of threads missing at the barrier before opening - count: Natural32 # Total number of threads that need to arrive before opening the barrier + left {.guard: lock.}: cuint # Number of threads missing at the barrier before opening + count: cuint # Total number of threads that need to arrive before opening the barrier const PTHREAD_BARRIER_SERIAL_THREAD* = Errno(1) @@ -41,8 +39,8 @@ proc broadcast(cond: var Cond) {.inline.}= func pthread_barrier_init*( barrier: var PthreadBarrier, - attr: ptr PthreadAttr, - count: range[0'i32..high(int32)] + attr: ptr PthreadBarrierAttr, + count: cuint ): Errno = barrier.lock.initLock() {.locks: [barrier.lock].}: diff --git a/taskpools/primitives/barriers_posix.nim b/taskpools/primitives/barriers_posix.nim index 8b45e8f..d85f223 100644 --- a/taskpools/primitives/barriers_posix.nim +++ b/taskpools/primitives/barriers_posix.nim @@ -15,11 +15,15 @@ when not compileOption("threads"): when defined(osx): import ./barriers_macos - export PthreadAttr, PthreadBarrier, Errno, PTHREAD_BARRIER_SERIAL_THREAD + export PthreadBarrierAttr, PthreadBarrier, Errno, PTHREAD_BARRIER_SERIAL_THREAD else: type - PthreadAttr* {.byref, importc: "pthread_attr_t", header: "".} = object - PthreadBarrier* {.byref, importc: "pthread_barrier_t", header: "".} = object + PthreadBarrierAttr* {.importc: "pthread_barrierattr_t", header: "", byref.} = object + when (defined(linux) and not defined(android)) and defined(amd64): + abi: array[4 div sizeof(cint), cint] # https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86/nptl/bits/pthreadtypes-arch.h;h=dd06d6753ebc80d94ede6c3c18227a3ad3104570;hb=HEAD#l45 + PthreadBarrier* {.importc: "pthread_barrier_t", header: "", byref.} = object + when (defined(linux) and not defined(android)) and defined(amd64): + abi: array[32 div sizeof(clong), clong] # https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86/nptl/bits/pthreadtypes-arch.h;h=dd06d6753ebc80d94ede6c3c18227a3ad3104570;hb=HEAD#l28 Errno* = cint @@ -30,14 +34,19 @@ else: when defined(osx): export pthread_barrier_init, pthread_barrier_wait, pthread_barrier_destroy else: + # TODO careful, this function mutates `barrier` without it being `var` which + # is allowed as a consequence of `byref` - it is also different from the + # one in barriers_macos + # see https://github.com/status-im/nim-taskpools/pull/20#discussion_r923843093 proc pthread_barrier_init*( barrier: PthreadBarrier, - attr: PthreadAttr or ptr PthreadAttr, - count: range[0'i32..high(int32)] + attr: ptr PthreadBarrierAttr, + count: cuint ): Errno {.header: "".} ## Initialize `barrier` with the attributes `attr`. ## The barrier is opened when `count` waiters arrived. + # TODO the macos signature is var instead of sink proc pthread_barrier_destroy*( barrier: sink PthreadBarrier): Errno {.header: "".} ## Destroy a previously dynamically initialized `barrier`.