new actors implementation for nimrod; compiler's generics still not up for the task though
This commit is contained in:
parent
5112677904
commit
4839800c22
8 changed files with 98 additions and 181 deletions
|
|
@ -35,7 +35,7 @@ Core
|
||||||
Nimrod thread support. **Note**: This is part of the system module. Do not
|
Nimrod thread support. **Note**: This is part of the system module. Do not
|
||||||
import it explicitely.
|
import it explicitely.
|
||||||
|
|
||||||
* `inboxes <inboxes.html>`_
|
* `channels <channels.html>`_
|
||||||
Nimrod message passing support for threads. **Note**: This is part of the
|
Nimrod message passing support for threads. **Note**: This is part of the
|
||||||
system module. Do not import it explicitely.
|
system module. Do not import it explicitely.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ include "lib/system/syslocks"
|
||||||
|
|
||||||
type
|
type
|
||||||
TLock* = TSysLock ## Nimrod lock; whether this is re-entrant
|
TLock* = TSysLock ## Nimrod lock; whether this is re-entrant
|
||||||
## or not is unspecified!
|
## or not is unspecified! However, compilation
|
||||||
|
## in preventDeadlocks-mode guarantees re-entrancy.
|
||||||
TCond* = TSysCond ## Nimrod condition variable
|
TCond* = TSysCond ## Nimrod condition variable
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
@ -50,7 +51,7 @@ proc DeinitLock*(lock: var TLock) {.inline.} =
|
||||||
## Frees the resources associated with the lock.
|
## Frees the resources associated with the lock.
|
||||||
DeinitSys(lock)
|
DeinitSys(lock)
|
||||||
|
|
||||||
proc TryAcquire*(lock: var TLock): bool {.inline.} =
|
proc TryAcquire*(lock: var TLock): bool =
|
||||||
## Tries to acquire the given lock. Returns `true` on success.
|
## Tries to acquire the given lock. Returns `true` on success.
|
||||||
result = TryAcquireSys(lock)
|
result = TryAcquireSys(lock)
|
||||||
when noDeadlocks:
|
when noDeadlocks:
|
||||||
|
|
|
||||||
|
|
@ -406,9 +406,11 @@ proc connectAsync*(socket: TSocket, name: string, port = TPort(0),
|
||||||
var err = WSAGetLastError()
|
var err = WSAGetLastError()
|
||||||
# Windows EINTR doesn't behave same as POSIX.
|
# Windows EINTR doesn't behave same as POSIX.
|
||||||
if err == WSAEWOULDBLOCK:
|
if err == WSAEWOULDBLOCK:
|
||||||
|
freeaddrinfo(aiList)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if errno == EINTR or errno == EINPROGRESS:
|
if errno == EINTR or errno == EINPROGRESS:
|
||||||
|
freeaddrinfo(aiList)
|
||||||
return
|
return
|
||||||
|
|
||||||
it = it.ai_next
|
it = it.ai_next
|
||||||
|
|
|
||||||
|
|
@ -1808,7 +1808,7 @@ when not defined(EcmaScript) and not defined(NimrodVM):
|
||||||
|
|
||||||
include "system/sysio"
|
include "system/sysio"
|
||||||
when hasThreadSupport:
|
when hasThreadSupport:
|
||||||
include "system/inboxes"
|
include "system/channels"
|
||||||
|
|
||||||
iterator lines*(filename: string): string =
|
iterator lines*(filename: string): string =
|
||||||
## Iterate over any line in the file named `filename`.
|
## Iterate over any line in the file named `filename`.
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
|
|
||||||
## Message passing for threads. **Note**: This is part of the system module.
|
## Channel support for threads. **Note**: This is part of the system module.
|
||||||
## Do not import it directly. To activate thread support you need to compile
|
## Do not import it directly. To activate thread support you need to compile
|
||||||
## with the ``--threads:on`` command line switch.
|
## with the ``--threads:on`` command line switch.
|
||||||
##
|
##
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
type
|
type
|
||||||
pbytes = ptr array[0.. 0xffff, byte]
|
pbytes = ptr array[0.. 0xffff, byte]
|
||||||
TInbox {.pure, final.} = object ## msg queue for a thread
|
TRawChannel {.pure, final.} = object ## msg queue for a thread
|
||||||
rd, wr, count, mask: int
|
rd, wr, count, mask: int
|
||||||
data: pbytes
|
data: pbytes
|
||||||
lock: TSysLock
|
lock: TSysLock
|
||||||
|
|
@ -24,28 +24,30 @@ type
|
||||||
elemType: PNimType
|
elemType: PNimType
|
||||||
ready: bool
|
ready: bool
|
||||||
region: TMemRegion
|
region: TMemRegion
|
||||||
PInbox = ptr TInbox
|
PRawChannel = ptr TRawChannel
|
||||||
TLoadStoreMode = enum mStore, mLoad
|
TLoadStoreMode = enum mStore, mLoad
|
||||||
|
TChannel*[TMsg] = TRawChannel ## a channel for thread communication
|
||||||
|
|
||||||
const ThreadDeadMask = -2
|
const ChannelDeadMask = -2
|
||||||
|
|
||||||
proc initInbox(p: pointer) =
|
proc initRawChannel(p: pointer) =
|
||||||
var inbox = cast[PInbox](p)
|
var c = cast[PRawChannel](p)
|
||||||
initSysLock(inbox.lock)
|
initSysLock(c.lock)
|
||||||
initSysCond(inbox.cond)
|
initSysCond(c.cond)
|
||||||
inbox.mask = -1
|
c.mask = -1
|
||||||
|
|
||||||
proc freeInbox(p: pointer) =
|
proc deinitRawChannel(p: pointer) =
|
||||||
var inbox = cast[PInbox](p)
|
var c = cast[PRawChannel](p)
|
||||||
# we need to grab the lock to be save against sending threads!
|
# we need to grab the lock to be save against sending threads!
|
||||||
acquireSys(inbox.lock)
|
acquireSys(c.lock)
|
||||||
inbox.mask = ThreadDeadMask
|
c.mask = ChannelDeadMask
|
||||||
deallocOsPages(inbox.region)
|
deallocOsPages(c.region)
|
||||||
deinitSys(inbox.lock)
|
deinitSys(c.lock)
|
||||||
deinitSysCond(inbox.cond)
|
deinitSysCond(c.cond)
|
||||||
|
|
||||||
proc storeAux(dest, src: Pointer, mt: PNimType, t: PInbox, mode: TLoadStoreMode)
|
proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel,
|
||||||
proc storeAux(dest, src: Pointer, n: ptr TNimNode, t: PInbox,
|
mode: TLoadStoreMode)
|
||||||
|
proc storeAux(dest, src: Pointer, n: ptr TNimNode, t: PRawChannel,
|
||||||
mode: TLoadStoreMode) =
|
mode: TLoadStoreMode) =
|
||||||
var
|
var
|
||||||
d = cast[TAddress](dest)
|
d = cast[TAddress](dest)
|
||||||
|
|
@ -62,7 +64,7 @@ proc storeAux(dest, src: Pointer, n: ptr TNimNode, t: PInbox,
|
||||||
if m != nil: storeAux(dest, src, m, t, mode)
|
if m != nil: storeAux(dest, src, m, t, mode)
|
||||||
of nkNone: sysAssert(false)
|
of nkNone: sysAssert(false)
|
||||||
|
|
||||||
proc storeAux(dest, src: Pointer, mt: PNimType, t: PInbox,
|
proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel,
|
||||||
mode: TLoadStoreMode) =
|
mode: TLoadStoreMode) =
|
||||||
var
|
var
|
||||||
d = cast[TAddress](dest)
|
d = cast[TAddress](dest)
|
||||||
|
|
@ -138,9 +140,9 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PInbox,
|
||||||
if mode == mStore:
|
if mode == mStore:
|
||||||
x[] = Alloc(t.region, mt.base.size)
|
x[] = Alloc(t.region, mt.base.size)
|
||||||
else:
|
else:
|
||||||
# XXX we should use the dynamic type here too, but that is not stored in
|
# XXX we should use the dynamic type here too, but that is not stored
|
||||||
# the inbox at all --> use source[]'s object type? but how? we need a
|
# in the inbox at all --> use source[]'s object type? but how? we need
|
||||||
# tyRef to the object!
|
# a tyRef to the object!
|
||||||
var obj = newObj(mt.base, mt.base.size)
|
var obj = newObj(mt.base, mt.base.size)
|
||||||
unsureAsgnRef(x, obj)
|
unsureAsgnRef(x, obj)
|
||||||
storeAux(x[], s, mt.base, t, mode)
|
storeAux(x[], s, mt.base, t, mode)
|
||||||
|
|
@ -148,7 +150,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PInbox,
|
||||||
else:
|
else:
|
||||||
copyMem(dest, src, mt.size) # copy raw bits
|
copyMem(dest, src, mt.size) # copy raw bits
|
||||||
|
|
||||||
proc rawSend(q: PInbox, data: pointer, typ: PNimType) =
|
proc rawSend(q: PRawChannel, data: pointer, typ: PNimType) =
|
||||||
## adds an `item` to the end of the queue `q`.
|
## adds an `item` to the end of the queue `q`.
|
||||||
var cap = q.mask+1
|
var cap = q.mask+1
|
||||||
if q.count >= cap:
|
if q.count >= cap:
|
||||||
|
|
@ -172,19 +174,19 @@ proc rawSend(q: PInbox, data: pointer, typ: PNimType) =
|
||||||
inc q.count
|
inc q.count
|
||||||
q.wr = (q.wr + 1) and q.mask
|
q.wr = (q.wr + 1) and q.mask
|
||||||
|
|
||||||
proc rawRecv(q: PInbox, data: pointer, typ: PNimType) =
|
proc rawRecv(q: PRawChannel, data: pointer, typ: PNimType) =
|
||||||
assert q.count > 0
|
assert q.count > 0
|
||||||
dec q.count
|
dec q.count
|
||||||
storeAux(data, addr(q.data[q.rd * typ.size]), typ, q, mLoad)
|
storeAux(data, addr(q.data[q.rd * typ.size]), typ, q, mLoad)
|
||||||
q.rd = (q.rd + 1) and q.mask
|
q.rd = (q.rd + 1) and q.mask
|
||||||
|
|
||||||
template lockInbox(q: expr, action: stmt) =
|
template lockChannel(q: expr, action: stmt) =
|
||||||
acquireSys(q.lock)
|
acquireSys(q.lock)
|
||||||
action
|
action
|
||||||
releaseSys(q.lock)
|
releaseSys(q.lock)
|
||||||
|
|
||||||
template sendImpl(q: expr) =
|
template sendImpl(q: expr) =
|
||||||
if q.mask == ThreadDeadMask:
|
if q.mask == ChannelDeadMask:
|
||||||
raise newException(EDeadThread, "cannot send message; thread died")
|
raise newException(EDeadThread, "cannot send message; thread died")
|
||||||
acquireSys(q.lock)
|
acquireSys(q.lock)
|
||||||
var m: TMsg
|
var m: TMsg
|
||||||
|
|
@ -195,17 +197,12 @@ template sendImpl(q: expr) =
|
||||||
releaseSys(q.lock)
|
releaseSys(q.lock)
|
||||||
SignalSysCond(q.cond)
|
SignalSysCond(q.cond)
|
||||||
|
|
||||||
proc send*[TMsg](receiver: var TThread[TMsg], msg: TMsg) =
|
proc send*[TMsg](c: var TChannel[TMsg], msg: TMsg) =
|
||||||
## sends a message to a thread. `msg` is deeply copied.
|
## sends a message to a thread. `msg` is deeply copied.
|
||||||
var q = cast[PInbox](getInBoxMem(receiver))
|
var q = cast[PRawChannel](addr(c))
|
||||||
sendImpl(q)
|
sendImpl(q)
|
||||||
|
|
||||||
proc send*[TMsg](receiver: TThreadId[TMsg], msg: TMsg) =
|
proc llRecv(q: PRawChannel, res: pointer, typ: PNimType) =
|
||||||
## sends a message to a thread. `msg` is deeply copied.
|
|
||||||
var q = cast[PInbox](getInBoxMem(receiver[]))
|
|
||||||
sendImpl(q)
|
|
||||||
|
|
||||||
proc llRecv(q: PInbox, res: pointer, typ: PNimType) =
|
|
||||||
# to save space, the generic is as small as possible
|
# to save space, the generic is as small as possible
|
||||||
acquireSys(q.lock)
|
acquireSys(q.lock)
|
||||||
q.ready = true
|
q.ready = true
|
||||||
|
|
@ -218,80 +215,30 @@ proc llRecv(q: PInbox, res: pointer, typ: PNimType) =
|
||||||
rawRecv(q, res, typ)
|
rawRecv(q, res, typ)
|
||||||
releaseSys(q.lock)
|
releaseSys(q.lock)
|
||||||
|
|
||||||
proc recv*[TMsg](): TMsg =
|
|
||||||
## receives a message from its internal message queue. This blocks until
|
|
||||||
## a message has arrived! You may use ``peek`` to avoid the blocking.
|
|
||||||
var q = cast[PInbox](getInBoxMem())
|
|
||||||
llRecv(q, addr(result), cast[PNimType](getTypeInfo(result)))
|
|
||||||
|
|
||||||
proc peek*(): int =
|
|
||||||
## returns the current number of messages in the inbox.
|
|
||||||
var q = cast[PInbox](getInBoxMem())
|
|
||||||
lockInbox(q):
|
|
||||||
result = q.count
|
|
||||||
|
|
||||||
proc peek*[TMsg](t: var TThread[TMsg]): int =
|
|
||||||
## returns the current number of messages in the inbox of thread `t`.
|
|
||||||
var q = cast[PInbox](getInBoxMem(t))
|
|
||||||
if q.mask != ThreadDeadMask:
|
|
||||||
lockInbox(q):
|
|
||||||
result = q.count
|
|
||||||
|
|
||||||
proc ready*[TMsg](t: var TThread[TMsg]): bool =
|
|
||||||
## returns true iff the thread `t` is waiting on ``recv`` for new messages.
|
|
||||||
var q = cast[PInbox](getInBoxMem(t))
|
|
||||||
result = q.ready
|
|
||||||
|
|
||||||
# ---------------------- channel support -------------------------------------
|
|
||||||
|
|
||||||
type
|
|
||||||
TChannel*[TMsg] = TInbox ## a channel for thread communication
|
|
||||||
TChannelId*[TMsg] = ptr TChannel[TMsg] ## the current implementation uses
|
|
||||||
## a pointer as a channel ID.
|
|
||||||
|
|
||||||
proc open*[TMsg](c: var TChannel[TMsg]) =
|
|
||||||
## opens a channel `c` for inter thread communication.
|
|
||||||
initInbox(addr(c))
|
|
||||||
|
|
||||||
proc close*[TMsg](c: var TChannel[TMsg]) =
|
|
||||||
## closes a channel `c` and frees its associated resources.
|
|
||||||
freeInbox(addr(c))
|
|
||||||
|
|
||||||
proc channelId*[TMsg](c: var TChannel[TMsg]): TChannelId[TMsg] {.inline.} =
|
|
||||||
## returns the channel ID of `c`.
|
|
||||||
result = addr(c)
|
|
||||||
|
|
||||||
proc send*[TMsg](c: var TChannel[TMsg], msg: TMsg) =
|
|
||||||
## sends a message to a channel. `msg` is deeply copied.
|
|
||||||
var q = cast[PInbox](addr(c))
|
|
||||||
sendImpl(q)
|
|
||||||
|
|
||||||
proc send*[TMsg](c: TChannelId[TMsg], msg: TMsg) =
|
|
||||||
## sends a message to a thread. `msg` is deeply copied.
|
|
||||||
var q = cast[PInbox](c)
|
|
||||||
sendImpl(q)
|
|
||||||
|
|
||||||
proc peek*[TMsg](c: var TChannel[TMsg]): int =
|
|
||||||
## returns the current number of messages in the channel `c`.
|
|
||||||
var q = cast[PInbox](addr(c))
|
|
||||||
lockInbox(q):
|
|
||||||
result = q.count
|
|
||||||
|
|
||||||
proc peek*[TMsg](c: TChannelId[TMsg]): int =
|
|
||||||
## returns the current number of messages in the channel `c`.
|
|
||||||
var q = cast[PInbox](c)
|
|
||||||
lockInbox(q):
|
|
||||||
result = q.count
|
|
||||||
|
|
||||||
proc recv*[TMsg](c: TChannelId[TMsg]): TMsg =
|
|
||||||
## receives a message from the channel `c`. This blocks until
|
|
||||||
## a message has arrived! You may use ``peek`` to avoid the blocking.
|
|
||||||
var q = cast[PInbox](c)
|
|
||||||
llRecv(q, addr(result), cast[PNimType](getTypeInfo(result)))
|
|
||||||
|
|
||||||
proc recv*[TMsg](c: var TChannel[TMsg]): TMsg =
|
proc recv*[TMsg](c: var TChannel[TMsg]): TMsg =
|
||||||
## receives a message from the channel `c`. This blocks until
|
## receives a message from the channel `c`. This blocks until
|
||||||
## a message has arrived! You may use ``peek`` to avoid the blocking.
|
## a message has arrived! You may use ``peek`` to avoid the blocking.
|
||||||
var q = cast[PInbox](addr(c))
|
var q = cast[PRawChannel](addr(c))
|
||||||
llRecv(q, addr(result), cast[PNimType](getTypeInfo(result)))
|
llRecv(q, addr(result), cast[PNimType](getTypeInfo(result)))
|
||||||
|
|
||||||
|
proc peek*[TMsg](c: var TChannel[TMsg]): int =
|
||||||
|
## returns the current number of messages in the channel `c`.
|
||||||
|
var q = cast[PRawChannel](addr(c))
|
||||||
|
if q.mask != ChannelDeadMask:
|
||||||
|
lockChannel(q):
|
||||||
|
result = q.count
|
||||||
|
|
||||||
|
proc open*[TMsg](c: var TChannel[TMsg]) =
|
||||||
|
## opens a channel `c` for inter thread communication.
|
||||||
|
initRawChannel(addr(c))
|
||||||
|
|
||||||
|
proc close*[TMsg](c: var TChannel[TMsg]) =
|
||||||
|
## closes a channel `c` and frees its associated resources.
|
||||||
|
deinitRawChannel(addr(c))
|
||||||
|
|
||||||
|
proc ready*[TMsg](c: var TChannel[TMsg]): bool =
|
||||||
|
## returns true iff some thread is waiting on the channel `c` for
|
||||||
|
## new messages.
|
||||||
|
var q = cast[PRawChannel](addr(c))
|
||||||
|
result = q.ready
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,6 @@ type
|
||||||
PGcThread = ptr TGcThread
|
PGcThread = ptr TGcThread
|
||||||
TGcThread {.pure.} = object
|
TGcThread {.pure.} = object
|
||||||
sys: TSysThread
|
sys: TSysThread
|
||||||
inbox: TThreadLocalStorage
|
|
||||||
when emulatedThreadVars and not useStackMaskHack:
|
when emulatedThreadVars and not useStackMaskHack:
|
||||||
tls: TThreadLocalStorage
|
tls: TThreadLocalStorage
|
||||||
else:
|
else:
|
||||||
|
|
@ -249,27 +248,22 @@ when not defined(useNimRtl):
|
||||||
# GC'ed closures in Nimrod.
|
# GC'ed closures in Nimrod.
|
||||||
|
|
||||||
type
|
type
|
||||||
TThread* {.pure, final.}[TMsg] =
|
TThread* {.pure, final.}[TArg] =
|
||||||
object of TGcThread ## Nimrod thread. A thread is a heavy object (~14K)
|
object of TGcThread ## Nimrod thread. A thread is a heavy object (~14K)
|
||||||
## that **must not** be part of a message! Use
|
## that **must not** be part of a message! Use
|
||||||
## a ``TThreadId`` for that.
|
## a ``TThreadId`` for that.
|
||||||
emptyFn: proc ()
|
dataFn: proc (m: TArg)
|
||||||
dataFn: proc (m: TMsg)
|
when TArg isnot void:
|
||||||
data: TMsg
|
data: TArg
|
||||||
TThreadId*[TMsg] = ptr TThread[TMsg] ## the current implementation uses
|
TThreadId*[TArg] = ptr TThread[TArg] ## the current implementation uses
|
||||||
## a pointer as a thread ID.
|
## a pointer as a thread ID.
|
||||||
|
|
||||||
proc initInbox(p: pointer)
|
|
||||||
proc freeInbox(p: pointer)
|
|
||||||
when not defined(boehmgc) and not hasSharedHeap:
|
when not defined(boehmgc) and not hasSharedHeap:
|
||||||
proc deallocOsPages()
|
proc deallocOsPages()
|
||||||
|
|
||||||
when defined(mainThread):
|
|
||||||
initInbox(addr(mainThread.inbox))
|
|
||||||
|
|
||||||
template ThreadProcWrapperBody(closure: expr) =
|
template ThreadProcWrapperBody(closure: expr) =
|
||||||
ThreadVarSetValue(globalsSlot, closure)
|
ThreadVarSetValue(globalsSlot, closure)
|
||||||
var t = cast[ptr TThread[TMsg]](closure)
|
var t = cast[ptr TThread[TArg]](closure)
|
||||||
when useStackMaskHack:
|
when useStackMaskHack:
|
||||||
var tls: TThreadLocalStorage
|
var tls: TThreadLocalStorage
|
||||||
when not defined(boehmgc) and not defined(nogc) and not hasSharedHeap:
|
when not defined(boehmgc) and not defined(nogc) and not hasSharedHeap:
|
||||||
|
|
@ -279,9 +273,8 @@ template ThreadProcWrapperBody(closure: expr) =
|
||||||
when defined(registerThread):
|
when defined(registerThread):
|
||||||
t.stackBottom = addr(t)
|
t.stackBottom = addr(t)
|
||||||
registerThread(t)
|
registerThread(t)
|
||||||
if t.emptyFn == nil: t.dataFn(t.data)
|
if TArg is void: t.dataFn()
|
||||||
else: t.emptyFn()
|
else: t.dataFn(t.data)
|
||||||
freeInbox(addr(t.inbox))
|
|
||||||
when defined(registerThread): unregisterThread(t)
|
when defined(registerThread): unregisterThread(t)
|
||||||
when defined(deallocOsPages): deallocOsPages()
|
when defined(deallocOsPages): deallocOsPages()
|
||||||
# Since an unhandled exception terminates the whole process (!), there is
|
# Since an unhandled exception terminates the whole process (!), there is
|
||||||
|
|
@ -291,31 +284,30 @@ template ThreadProcWrapperBody(closure: expr) =
|
||||||
# page!
|
# page!
|
||||||
|
|
||||||
# mark as not running anymore:
|
# mark as not running anymore:
|
||||||
t.emptyFn = nil
|
|
||||||
t.dataFn = nil
|
t.dataFn = nil
|
||||||
|
|
||||||
{.push stack_trace:off.}
|
{.push stack_trace:off.}
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
proc threadProcWrapper[TMsg](closure: pointer): int32 {.stdcall.} =
|
proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} =
|
||||||
ThreadProcWrapperBody(closure)
|
ThreadProcWrapperBody(closure)
|
||||||
# implicitely return 0
|
# implicitely return 0
|
||||||
else:
|
else:
|
||||||
proc threadProcWrapper[TMsg](closure: pointer) {.noconv.} =
|
proc threadProcWrapper[TArg](closure: pointer) {.noconv.} =
|
||||||
ThreadProcWrapperBody(closure)
|
ThreadProcWrapperBody(closure)
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
proc running*[TMsg](t: TThread[TMsg]): bool {.inline.} =
|
proc running*[TArg](t: TThread[TArg]): bool {.inline.} =
|
||||||
## returns true if `t` is running.
|
## returns true if `t` is running.
|
||||||
result = t.emptyFn != nil or t.dataFn != nil
|
result = t.dataFn != nil
|
||||||
|
|
||||||
proc joinThread*[TMsg](t: TThread[TMsg]) {.inline.} =
|
proc joinThread*[TArg](t: TThread[TArg]) {.inline.} =
|
||||||
## waits for the thread `t` to finish.
|
## waits for the thread `t` to finish.
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
discard WaitForSingleObject(t.sys, -1'i32)
|
discard WaitForSingleObject(t.sys, -1'i32)
|
||||||
else:
|
else:
|
||||||
discard pthread_join(t.sys, nil)
|
discard pthread_join(t.sys, nil)
|
||||||
|
|
||||||
proc joinThreads*[TMsg](t: openArray[TThread[TMsg]]) =
|
proc joinThreads*[TArg](t: openArray[TThread[TArg]]) =
|
||||||
## waits for every thread in `t` to finish.
|
## waits for every thread in `t` to finish.
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
var a: array[0..255, TSysThread]
|
var a: array[0..255, TSysThread]
|
||||||
|
|
@ -327,27 +319,28 @@ proc joinThreads*[TMsg](t: openArray[TThread[TMsg]]) =
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
# XXX a thread should really release its heap here somehow:
|
# XXX a thread should really release its heap here somehow:
|
||||||
proc destroyThread*[TMsg](t: var TThread[TMsg]) =
|
proc destroyThread*[TArg](t: var TThread[TArg]) =
|
||||||
## forces the thread `t` to terminate. This is potentially dangerous if
|
## forces the thread `t` to terminate. This is potentially dangerous if
|
||||||
## you don't have full control over `t` and its acquired resources.
|
## you don't have full control over `t` and its acquired resources.
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
discard TerminateThread(t.sys, 1'i32)
|
discard TerminateThread(t.sys, 1'i32)
|
||||||
else:
|
else:
|
||||||
discard pthread_cancel(t.sys)
|
discard pthread_cancel(t.sys)
|
||||||
unregisterThread(addr(t))
|
when defined(registerThread): unregisterThread(addr(t))
|
||||||
|
t.dataFn = nil
|
||||||
|
|
||||||
proc createThread*[TMsg](t: var TThread[TMsg],
|
proc createThread*[TArg](t: var TThread[TArg],
|
||||||
tp: proc (msg: TMsg) {.thread.},
|
tp: proc (arg: TArg) {.thread.},
|
||||||
param: TMsg) =
|
param: TArg) =
|
||||||
## creates a new thread `t` and starts its execution. Entry point is the
|
## creates a new thread `t` and starts its execution. Entry point is the
|
||||||
## proc `tp`. `param` is passed to `tp`.
|
## proc `tp`. `param` is passed to `tp`. `TArg` can be ``void`` if you
|
||||||
t.data = param
|
## don't need to pass any data to the thread.
|
||||||
|
when TArg isnot void: t.data = param
|
||||||
t.dataFn = tp
|
t.dataFn = tp
|
||||||
when hasSharedHeap: t.stackSize = ThreadStackSize
|
when hasSharedHeap: t.stackSize = ThreadStackSize
|
||||||
initInbox(addr(t.inbox))
|
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
var dummyThreadId: int32
|
var dummyThreadId: int32
|
||||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TMsg],
|
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TArg],
|
||||||
addr(t), 0'i32, dummyThreadId)
|
addr(t), 0'i32, dummyThreadId)
|
||||||
if t.sys <= 0:
|
if t.sys <= 0:
|
||||||
raise newException(EResourceExhausted, "cannot create thread")
|
raise newException(EResourceExhausted, "cannot create thread")
|
||||||
|
|
@ -355,39 +348,20 @@ proc createThread*[TMsg](t: var TThread[TMsg],
|
||||||
var a: Tpthread_attr
|
var a: Tpthread_attr
|
||||||
pthread_attr_init(a)
|
pthread_attr_init(a)
|
||||||
pthread_attr_setstacksize(a, ThreadStackSize)
|
pthread_attr_setstacksize(a, ThreadStackSize)
|
||||||
if pthread_create(t.sys, a, threadProcWrapper[TMsg], addr(t)) != 0:
|
if pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0:
|
||||||
raise newException(EResourceExhausted, "cannot create thread")
|
raise newException(EResourceExhausted, "cannot create thread")
|
||||||
|
|
||||||
proc createThread*[TMsg](t: var TThread[TMsg], tp: proc () {.thread.}) =
|
proc threadId*[TArg](t: var TThread[TArg]): TThreadId[TArg] {.inline.} =
|
||||||
## creates a new thread `t` and starts its execution. Entry point is the
|
|
||||||
## proc `tp`.
|
|
||||||
t.emptyFn = tp
|
|
||||||
when hasSharedHeap: t.stackSize = ThreadStackSize
|
|
||||||
initInbox(addr(t.inbox))
|
|
||||||
when hostOS == "windows":
|
|
||||||
var dummyThreadId: int32
|
|
||||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TMsg],
|
|
||||||
addr(t), 0'i32, dummyThreadId)
|
|
||||||
if t.sys <= 0:
|
|
||||||
raise newException(EResourceExhausted, "cannot create thread")
|
|
||||||
else:
|
|
||||||
var a: Tpthread_attr
|
|
||||||
pthread_attr_init(a)
|
|
||||||
pthread_attr_setstacksize(a, ThreadStackSize)
|
|
||||||
if pthread_create(t.sys, a, threadProcWrapper[TMsg], addr(t)) != 0:
|
|
||||||
raise newException(EResourceExhausted, "cannot create thread")
|
|
||||||
|
|
||||||
proc threadId*[TMsg](t: var TThread[TMsg]): TThreadId[TMsg] {.inline.} =
|
|
||||||
## returns the thread ID of `t`.
|
## returns the thread ID of `t`.
|
||||||
result = addr(t)
|
result = addr(t)
|
||||||
|
|
||||||
proc myThreadId*[TMsg](): TThreadId[TMsg] =
|
proc myThreadId*[TArg](): TThreadId[TArg] =
|
||||||
## returns the thread ID of the thread that calls this proc.
|
## returns the thread ID of the thread that calls this proc.
|
||||||
result = cast[TThreadId[TMsg]](ThreadVarGetValue(globalsSlot))
|
result = cast[TThreadId[TArg]](ThreadVarGetValue(globalsSlot))
|
||||||
|
|
||||||
proc mainThreadId*[TMsg](): TThreadId[TMsg] =
|
proc mainThreadId*[TArg](): TThreadId[TArg] =
|
||||||
## returns the thread ID of the main thread.
|
## returns the thread ID of the main thread.
|
||||||
result = cast[TThreadId[TMsg]](addr(mainThread))
|
result = cast[TThreadId[TArg]](addr(mainThread))
|
||||||
|
|
||||||
when useStackMaskHack:
|
when useStackMaskHack:
|
||||||
proc runMain(tp: proc () {.thread.}) {.compilerproc.} =
|
proc runMain(tp: proc () {.thread.}) {.compilerproc.} =
|
||||||
|
|
@ -395,11 +369,3 @@ when useStackMaskHack:
|
||||||
createThread(mainThread, tp)
|
createThread(mainThread, tp)
|
||||||
joinThread(mainThread)
|
joinThread(mainThread)
|
||||||
|
|
||||||
# ------------------------ message passing support ---------------------------
|
|
||||||
|
|
||||||
proc getInBoxMem[TMsg](t: var TThread[TMsg]): pointer {.inline.} =
|
|
||||||
result = addr(t.inbox)
|
|
||||||
|
|
||||||
proc getInBoxMem(): pointer {.inline.} =
|
|
||||||
result = addr(cast[PGcThread](ThreadVarGetValue(globalsSlot)).inbox)
|
|
||||||
|
|
||||||
|
|
|
||||||
9
todo.txt
9
todo.txt
|
|
@ -1,12 +1,11 @@
|
||||||
Version 0.8.14
|
Version 0.8.14
|
||||||
==============
|
==============
|
||||||
|
|
||||||
- optional indentation for 'case' statement
|
|
||||||
- test the sort implementation again
|
|
||||||
- fix the 'const' issues
|
|
||||||
- threads should not have an inbox per default
|
- threads should not have an inbox per default
|
||||||
- make threadvar efficient again on linux after testing
|
- make threadvar efficient again on linux after testing
|
||||||
|
- fix the 'const' issues
|
||||||
|
- test the sort implementation again
|
||||||
|
- optional indentation for 'case' statement
|
||||||
|
|
||||||
version 0.9.0
|
version 0.9.0
|
||||||
=============
|
=============
|
||||||
|
|
@ -47,6 +46,8 @@ version 0.9.XX
|
||||||
- checked exceptions
|
- checked exceptions
|
||||||
- fix implicit generic routines
|
- fix implicit generic routines
|
||||||
- think about ``{:}.toTable[int, string]()``
|
- think about ``{:}.toTable[int, string]()``
|
||||||
|
- mocking support with ``tyProxy`` that does:
|
||||||
|
o.p(x) --> p(o, x) --> myMacro(o, p, x)
|
||||||
- nice idea:
|
- nice idea:
|
||||||
|
|
||||||
p(a, b):
|
p(a, b):
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ doc: "tools;c2nim;niminst"
|
||||||
pdf: "manual;lib;tut1;tut2;nimrodc;c2nim;niminst"
|
pdf: "manual;lib;tut1;tut2;nimrodc;c2nim;niminst"
|
||||||
srcdoc: "core/macros;pure/marshal;core/typeinfo"
|
srcdoc: "core/macros;pure/marshal;core/typeinfo"
|
||||||
srcdoc: "impure/graphics;impure/re;pure/sockets"
|
srcdoc: "impure/graphics;impure/re;pure/sockets"
|
||||||
srcdoc: "system.nim;system/threads.nim;system/inboxes.nim"
|
srcdoc: "system.nim;system/threads.nim;system/channels.nim"
|
||||||
srcdoc: "pure/os;pure/strutils;pure/math;pure/matchers"
|
srcdoc: "pure/os;pure/strutils;pure/math;pure/matchers"
|
||||||
srcdoc: "pure/complex;pure/times;pure/osproc;pure/pegs;pure/dynlib"
|
srcdoc: "pure/complex;pure/times;pure/osproc;pure/pegs;pure/dynlib"
|
||||||
srcdoc: "pure/parseopt;pure/hashes;pure/strtabs;pure/lexbase"
|
srcdoc: "pure/parseopt;pure/hashes;pure/strtabs;pure/lexbase"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue