[m-rev.] for review: move more concurrency modules to stdlib
Julien Fischer
juliensf at csse.unimelb.edu.au
Tue Jan 30 17:58:17 AEDT 2007
On Tue, 30 Jan 2007, Peter Wang wrote:
> Branches: main
>
Add a brief log message, e.g.
Move more of the concurrency related modules from extras
into the standard library.
> library/Mercury.options:
> library/library.m:
> library/thread.channel.m:
> library/thread.mvar.m:
> library/thread.semaphore.m:
> Move the concurrency-related modules `channel', `mvar' and
> `semaphore' from extras/concurrency into the standard library.
>
> Make thread.mvar use the standard library module mutvar instead of
> providing its own implementation of the same thing.
The NEWS file should also be updated.
> library/mutvar.m:
> Add predicate `new_mutvar0' which is like `new_mutvar' but does not
> require an initial value for the mutvar. This is needed for
> thread.mvar.
>
> Define `new_mutvar' in terms of `new_mutvar0' and `set_mutvar'.
>
> extras/concurrency/channel.m:
> extras/concurrency/mvar.m:
> extras/concurrency/semaphore.m:
> Replace predicates to call their counterparts now in the standard
> library.
>
> Mark them as obsolete and add pointers to the new versions.
>
> library/thread.m:
> Fix a bug in which a field in ML_ThreadWrapperArgs had the wrong
> pointer type.
>
> Allocate ML_ThreadWrapperArgs with MR_GC_* macros instead of non-GC
> versions, since it contains pointers which should be traced.
>
>
> mmake has trouble building things in extras/concurrency after this
> change. When generating dependencies it thinks semaphore.m is the
> source file for thread.semaphore, etc. Would it be a problem just to
> remove the forwarding modules?
I wouldn't think so, other than the example code in extras/concurrency
would need to be updated.
...
> Index: library/mutvar.m
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/library/mutvar.m,v
> retrieving revision 1.2
> diff -u -r1.2 mutvar.m
> --- library/mutvar.m 19 Apr 2006 05:17:53 -0000 1.2
> +++ library/mutvar.m 30 Jan 2007 03:41:07 -0000
> @@ -33,6 +33,11 @@
> :- mode new_mutvar(in, out) is det.
> :- mode new_mutvar(di, uo) is det.
>
> + % Create a new mutvar with undefined initial value.
> + %
> +:- impure pred new_mutvar0(mutvar(T)).
> +:- mode new_mutvar0(uo) is det.
> +
> % Get the value currently referred to by a reference.
> %
> :- impure pred get_mutvar(mutvar(T), T) is det.
> @@ -58,10 +63,18 @@
> :- implementation.
>
> :- pragma inline(new_mutvar/2).
> +:- pragma inline(new_mutvar0/1).
> :- pragma inline(get_mutvar/2).
> :- pragma inline(set_mutvar/2).
>
> %-----------------------------------------------------------------------------%
> +
> +new_mutvar(X, Ref) :-
> + impure new_mutvar0(Ref0),
> + impure set_mutvar(Ref0, X),
> + Ref = unsafe_promise_unique(Ref0).
> +
> +%-----------------------------------------------------------------------------%
> %
> % C implementation
> %
> @@ -72,22 +85,12 @@
> ---> mutvar(private_builtin.ref(T)).
>
> :- pragma foreign_proc("C",
> - new_mutvar(X::in, Ref::out),
> - [will_not_call_mercury, thread_safe],
> -"
> - MR_offset_incr_hp_msg(Ref, MR_SIZE_SLOT_SIZE, MR_SIZE_SLOT_SIZE + 1,
> - MR_PROC_LABEL, ""mutvar.mutvar/1"");
> - MR_define_size_slot(0, Ref, 1);
> - * (MR_Word *) Ref = X;
> -").
> -:- pragma foreign_proc("C",
> - new_mutvar(X::di, Ref::uo),
> + new_mutvar0(Ref::uo),
> [will_not_call_mercury, thread_safe],
> "
> MR_offset_incr_hp_msg(Ref, MR_SIZE_SLOT_SIZE, MR_SIZE_SLOT_SIZE + 1,
> MR_PROC_LABEL, ""mutvar.mutvar/1"");
> MR_define_size_slot(0, Ref, 1);
> - * (MR_Word *) Ref = X;
> ").
>
> :- pragma foreign_proc("C",
> @@ -110,18 +113,10 @@
> %
>
> :- pragma foreign_proc("C#",
> - new_mutvar(X::in, Ref::out),
> + new_mutvar0(Ref::uo),
> [will_not_call_mercury, thread_safe],
> "
> Ref = new object[1];
> - Ref[0] = X;
> -").
> -:- pragma foreign_proc("C#",
> - new_mutvar(X::di, Ref::uo),
> - [will_not_call_mercury, thread_safe],
> -"
> - Ref = new object[1];
> - Ref[0] = X;
> ").
>
> :- pragma foreign_proc("C#",
> @@ -147,26 +142,16 @@
> "
> public static class Mutvar {
> public Object object;
> -
> - public Mutvar(Object init) {
> - object = init;
> - }
> }
> ").
>
> :- pragma foreign_type("Java", mutvar(T), "mercury.mutvar.Mutvar").
>
> :- pragma foreign_proc("Java",
> - new_mutvar(X::in, Ref::out),
> - [will_not_call_mercury, thread_safe],
> -"
> - Ref = new mercury.mutvar.Mutvar(X);
> -").
> -:- pragma foreign_proc("Java",
> - new_mutvar(X::di, Ref::uo),
> + new_mutvar0(Ref::uo),
> [will_not_call_mercury, thread_safe],
> "
> - Ref = new mercury.mutvar.Mutvar(X);
> + Ref = new mercury.mutvar.Mutvar();
> ").
>
> :- pragma foreign_proc("Java",
...
> Index: library/thread.semaphore.m
> ===================================================================
> RCS file: library/thread.semaphore.m
> diff -N library/thread.semaphore.m
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ library/thread.semaphore.m 30 Jan 2007 03:41:07 -0000
> @@ -0,0 +1,341 @@
> +%-----------------------------------------------------------------------------%
> +% vim: ft=mercury ts=4 sw=4 et
> +%-----------------------------------------------------------------------------%
> +% Copyright (C) 2000-2001,2003-2004, 2006-2007 The University of Melbourne.
> +% This file may only be copied under the terms of the GNU Library General
> +% Public License - see the file COPYING.LIB in the Mercury distribution.
> +%-----------------------------------------------------------------------------%
> +%
> +% File: thread.semaphore.m.
> +% Main author: conway
> +% Stability: medium.
> +%
> +% This module implements a simple semaphore data type for allowing
> +% coroutines to synchronise with one another.
> +%
> +% The operations in this module are no-ops in the hlc grades which don't
> +% contain a .par component.
> +%
> +%-----------------------------------------------------------------------------%
> +
> +:- module thread.semaphore.
> +:- interface.
> +
> +:- import_module bool.
> +:- import_module io.
> +
> +%-----------------------------------------------------------------------------%
> +
> +:- type semaphore.
> +
> + % new(Sem, !IO) creates a new semaphore `Sem' with it's counter
> + % initialized to 0.
> + %
> +:- pred semaphore.new(semaphore::out, io::di, io::uo) is det.
> +
> + % wait(Sem, !IO) blocks until the counter associated with `Sem'
> + % becomes greater than 0, whereupon it wakes, decrements the
> + % counter and returns.
> + %
> +:- pred semaphore.wait(semaphore::in, io::di, io::uo) is det.
> +
> + % try_wait(Sem, Succ, !IO) is the same as wait/3, except that
> + % instead of blocking, it binds `Succ' to a boolean indicating
> + % whether the call succeeded in obtaining the semaphore or not.
> + %
> +:- pred semaphore.try_wait(semaphore::in, bool::out, io::di, io::uo) is det.
> +
> + % signal(Sem, !IO) increments the counter associated with `Sem'
> + % and if the resulting counter has a value greater than 0, it wakes
> + % one or more coroutines that are waiting on this semaphore (if
> + % any).
> + %
> +:- pred semaphore.signal(semaphore::in, io::di, io::uo) is det.
> +
> +%-----------------------------------------------------------------------------%
> +%-----------------------------------------------------------------------------%
> +
> +:- implementation.
> +
> +%-----------------------------------------------------------------------------%
> +
> +:- pragma foreign_decl("C", "
> + #include <stdio.h>
> + #include ""mercury_context.h""
> + #include ""mercury_thread.h""
> +
> + typedef struct ME_SEMAPHORE_STRUCT {
> + int count;
> +#ifndef MR_HIGHLEVEL_CODE
> + MR_Context *suspended;
> +#else
> + #ifdef MR_THREAD_SAFE
> + MercuryCond cond;
> + #endif
> +#endif
> +#ifdef MR_THREAD_SAFE
> + MercuryLock lock;
> +#endif
> + } ME_Semaphore;
s/ME_/ML_/ throughout otherwise the namespace cleanliness check on the
library directory will fail.
Otherwise that looks okay. Please make sure that the library still
compiles in a non .par grade, e.g hlc.gc, before comitting.
Julien.
--------------------------------------------------------------------------
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