[m-rev.] for review: concurrency additions
Peter Wang
wangp at students.csse.unimelb.edu.au
Thu Apr 12 14:04:58 AEST 2007
Branches: main
Add predicates from MC's versions of the concurrency modules.
library/thread.channel.m:
Add channel.try_take/4, a non-blocking version of channel.take/4.
library/thread.mvar.m:
Add mvar.try_take/4, a non-blocking version of mvar.take/4.
NEWS:
Announce the additions.
Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.452
diff -u -r1.452 NEWS
--- NEWS 18 Mar 2007 23:34:44 -0000 1.452
+++ NEWS 12 Apr 2007 03:00:26 -0000
@@ -37,7 +37,8 @@
* We have moved some of the concurrency primitives out of the extras
distribution and into a new standard library module called `thread',
and its submodules `thread.channel', `thread.mvar', and `thread.semaphore'.
- The predicate `thread.can_spawn' has also been added.
+ The predicates `thread.can_spawn', `thread.channel.try_take'
+ and `thread.mvar.try_take' have also been added.
* The following predicates have been added to the list module:
list.map_corresponding/4
Index: library/thread.channel.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/thread.channel.m,v
retrieving revision 1.1
diff -u -r1.1 thread.channel.m
--- library/thread.channel.m 1 Feb 2007 08:07:59 -0000 1.1
+++ library/thread.channel.m 12 Apr 2007 03:00:26 -0000
@@ -24,6 +24,7 @@
:- interface.
:- import_module io.
+:- import_module maybe.
%-----------------------------------------------------------------------------%
@@ -42,6 +43,11 @@
%
:- pred channel.take(channel(T)::in, T::out, io::di, io::uo) is det.
+ % Take an item from the start of the channel.
+ % Returns immediately with no if the channel was empty.
+ %
+:- pred channel.try_take(channel(T)::in, maybe(T)::out, io::di, io::uo) is det.
+
% Duplicate a channel. The new channel sees all (and only) the
% data written to the channel after the channel.duplicate call.
%
@@ -93,6 +99,19 @@
mvar.take(Head, item(Val, NewHead), !IO),
mvar.put(Read, NewHead, !IO).
+channel.try_take(channel(Read, _Write), MaybeVal, !IO) :-
+ mvar.take(Read, Head, !IO),
+ mvar.try_take(Head, MaybeItem, !IO),
+ (
+ MaybeItem = yes(item(Val, NewHead)),
+ MaybeVal = yes(Val)
+ ;
+ MaybeItem = no,
+ MaybeVal = no,
+ NewHead = Head
+ ),
+ mvar.put(Read, NewHead, !IO).
+
channel.duplicate(channel(_Read, Write), channel(NewRead, Write), !IO) :-
mvar.init(NewRead, !IO),
mvar.take(Write, Hole, !IO),
Index: library/thread.mvar.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/thread.mvar.m,v
retrieving revision 1.1
diff -u -r1.1 thread.mvar.m
--- library/thread.mvar.m 1 Feb 2007 08:07:59 -0000 1.1
+++ library/thread.mvar.m 12 Apr 2007 03:00:26 -0000
@@ -24,6 +24,7 @@
:- interface.
:- import_module io.
+:- import_module maybe.
%-----------------------------------------------------------------------------%
@@ -38,6 +39,12 @@
%
:- pred mvar.take(mvar(T)::in, T::out, io::di, io::uo) is det.
+ % Take the contents of the mvar out leaving the mvar empty.
+ % Returns immediately with no if the mvar was empty, or yes(X) if
+ % the mvar contained X.
+ %
+:- pred mvar.try_take(mvar(T)::in, maybe(T)::out, io::di, io::uo) is det.
+
% Place the value of type T into an empty mvar.
% If the mvar is full block until it becomes empty.
%
@@ -48,6 +55,7 @@
:- implementation.
+:- import_module bool.
:- import_module mutvar.
:- import_module thread.semaphore.
@@ -73,6 +81,19 @@
impure get_mutvar(Ref, Data),
semaphore.signal(Empty, !IO).
+:- pragma promise_pure(mvar.try_take/4).
+mvar.try_take(mvar(Full, Empty, Ref), MaybeData, !IO) :-
+ semaphore.try_wait(Full, Success, !IO),
+ (
+ Success = yes,
+ impure get_mutvar(Ref, Data),
+ semaphore.signal(Empty, !IO),
+ MaybeData = yes(Data)
+ ;
+ Success = no,
+ MaybeData = no
+ ).
+
:- pragma promise_pure(mvar.put/4).
mvar.put(mvar(Full, Empty, Ref), Data, !IO) :-
semaphore.wait(Empty, !IO),
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list