From 69abc8b64954206da6ffe5fc40a1142b39777762 Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 7 Jun 2021 23:20:46 +0800 Subject: [PATCH] add std/mutexes --- changelog.md | 2 + lib/std/mutexes.nim | 90 +++++++++++++++++++++++++++++++++++++++ tests/stdlib/tmutexes.nim | 49 +++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 lib/std/mutexes.nim create mode 100644 tests/stdlib/tmutexes.nim diff --git a/changelog.md b/changelog.md index 1050e4916e..8487f43f4b 100644 --- a/changelog.md +++ b/changelog.md @@ -335,6 +335,8 @@ - Added setCurrentException for JS backend. +- Added `std/mutexes`. + ## Language changes - `nimscript` now handles `except Exception as e`. diff --git a/lib/std/mutexes.nim b/lib/std/mutexes.nim new file mode 100644 index 0000000000..e4e25ea440 --- /dev/null +++ b/lib/std/mutexes.nim @@ -0,0 +1,90 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2021 Nim Contributors +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module contains Nim's support for mutexes. It provides basic +## destructors supports. + +runnableExamples("--threads:on --gc:orc"): + type + PassObj = object + id: int + + Pass = ptr PassObj + + proc worker(p: Pass) {.thread.} = + var m: Mutex + init(m) + acquire(m) + inc p.id + release(m) + # After leaving the function scope, + # the resource owned by `m` is freed. + + var p = cast[Pass](allocShared0(sizeof(PassObj))) + var ts = newSeq[Thread[Pass]](10) + for i in 0..