diff --git a/lib/core/locks.nim b/lib/core/locks.nim index fbe9c8acfc..9f52078fa8 100644 --- a/lib/core/locks.nim +++ b/lib/core/locks.nim @@ -59,6 +59,7 @@ proc signal*(cond: var Cond) {.inline.} = template withLock*(a: Lock, body: untyped) = ## Acquires the given lock, executes the statements in body and ## releases the lock after the statements finish executing. + mixin acquire, release a.acquire() {.locks: [a].}: try: diff --git a/tests/stdlib/tlocks.nim b/tests/stdlib/tlocks.nim new file mode 100644 index 0000000000..e567cfde8f --- /dev/null +++ b/tests/stdlib/tlocks.nim @@ -0,0 +1,10 @@ +discard """ + output: '''3''' + cmd: "nim $target --threads:on $options $file" +""" + +#bug #6049 +import uselocks + +var m = createMyType[int]() +echo $m.use() diff --git a/tests/stdlib/uselocks.nim b/tests/stdlib/uselocks.nim new file mode 100644 index 0000000000..cde9641b22 --- /dev/null +++ b/tests/stdlib/uselocks.nim @@ -0,0 +1,11 @@ +import locks + +type MyType* [T] = object + lock: Lock + +proc createMyType*[T]: MyType[T] = + initLock(result.lock) + +proc use* (m: var MyType): int = + withLock m.lock: + result = 3