Skip to content

Commit

Permalink
Thread attributes should be destroyed using the pthread_attr_destroy() (
Browse files Browse the repository at this point in the history
#13293)

On some OSes (such as FreeBSD or Solaris), pthread_attr_init allocate
memory. So it is necessary to deallocate that memory by using
pthread_attr_destroy.
  • Loading branch information
kubo39 authored Jan 29, 2020
1 parent 2ecef8f commit 3ab1274
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/system/threadlocalstorage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ else:
tv_sec: Time
tv_nsec: clong

proc pthread_attr_init(a1: var Pthread_attr) {.
proc pthread_attr_init(a1: var Pthread_attr): cint {.
importc, header: pthreadh.}
proc pthread_attr_setstacksize(a1: var Pthread_attr, a2: int) {.
proc pthread_attr_setstacksize(a1: var Pthread_attr, a2: int): cint {.
importc, header: pthreadh.}
proc pthread_attr_destroy(a1: var Pthread_attr): cint {.
importc, header: pthreadh.}

proc pthread_create(a1: var SysThread, a2: var Pthread_attr,
Expand Down
5 changes: 3 additions & 2 deletions lib/system/threads.nim
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,11 @@ else:
t.dataFn = tp
when hasSharedHeap: t.core.stackSize = ThreadStackSize
var a {.noinit.}: Pthread_attr
pthread_attr_init(a)
pthread_attr_setstacksize(a, ThreadStackSize)
doAssert pthread_attr_init(a) == 0
doAssert pthread_attr_setstacksize(a, ThreadStackSize) == 0
if pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0:
raise newException(ResourceExhaustedError, "cannot create thread")
doAssert pthread_attr_destroy(a) == 0

proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
## Pins a thread to a `CPU`:idx:.
Expand Down

0 comments on commit 3ab1274

Please sign in to comment.