Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions M2/Macaulay2/d/binding.d
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ bumpPrecedence();
export profileS := special("profile", unaryop, precSpace, wide);
export shieldS := special("shield", unaryop, precSpace, wide);
export trapS := special("trap", unaryop, precSpace, wide);
export withLockS := special("withLock", unaryop, precSpace, wide);
export throwS := special("throw", nunaryop, precSpace, wide);
export returnS := special("return", nunaryop, precSpace, wide);
export breakS := special("break", nunaryop, precSpace, wide);
Expand Down
14 changes: 14 additions & 0 deletions M2/Macaulay2/d/pthread.d
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ unlock(e:Expr):Expr := (
else WrongArgMutex());
setupfun("unlock0", unlock);

withLock(c:Code):Expr := (
when c
is a:sequenceCode do (
if length(a.x) == 2 then (
e := eval(a.x.0);
l := lock(e);
when l is Error do return l else nothing;
r := eval(a.x.1);
u := unlock(e);
when u is Error do u else r)
else WrongNumArgs(2))
else WrongNumArgs(2));
setupop(withLockS, withLock);

-- Local Variables:
-- compile-command: "echo \"make: Entering directory \\`$M2BUILDDIR/Macaulay2/d'\" && make -C $M2BUILDDIR/Macaulay2/d pthread.o "
-- End:
1 change: 1 addition & 0 deletions M2/Macaulay2/m2/exports.m2
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ export {
"when",
"while",
"width",
"withLock",
"wrap",
"xor",
"youngest",
Expand Down
27 changes: 27 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/doc_mutex.m2
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ doc ///
(lock, Mutex)
(tryLock, Mutex)
(unlock, Mutex)
symbol withLock
///

doc ///
Expand Down Expand Up @@ -136,3 +137,29 @@ doc ///
(lock, Mutex)
(tryLock, Mutex)
///

doc ///
Key
symbol withLock
Headline
evaluate an expression while holding a mutex lock
Usage
withLock(m, c)
Inputs
m:Mutex
c: -- code to evaluate
Description
Text
@M2CODE "withLock"@ is a keyword that locks the mutex @VAR "m"@, evaluates
the code @VAR "c"@, unlocks the mutex, and returns the result of
evaluating @VAR "c"@.

Using @M2CODE "withLock"@ is equivalent to surrounding the code with
calls to @TO lock@ and @TO unlock@, but with the added guarantee that the
mutex is unlocked even if the code raises an error.
Example
m = new Mutex
last trap withLock(m, (print last trap tryLock m; 1/0))
tryLock m
unlock m
///
2 changes: 2 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/ov_threads.m2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Node
SeeAlso
"parallelism in engine computations"
"elapsedTime"
AtomicInt
Mutex
Description
Text
The simplest way to run computations in parallel is to use @ TO parallelApply @. This works
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/tests/normal/threads.m2
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ assert try tryLock m then true else false
assert try tryLock m then false else true
unlock m

withLock(m, assert try tryLock m then false else true)
assert try tryLock m then true else false
unlock m

-- Local Variables:
-- compile-command: "make -C $M2BUILDDIR/Macaulay2/packages/Macaulay2Doc/test threads.out"
-- End: