[m-rev.] for review: dependent parallel conjunctions (2/2)

Julien Fischer juliensf at cs.mu.OZ.AU
Fri Jun 23 14:51:40 AEST 2006


On Thu, 22 Jun 2006, Peter Wang wrote:

> Index: library/par_builtin.m
> ===================================================================
> RCS file: library/par_builtin.m
> diff -N library/par_builtin.m
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ library/par_builtin.m	21 Jun 2006 09:17:49 -0000
> @@ -0,0 +1,131 @@
> +%---------------------------------------------------------------------------%
> +% vim: ft=mercury ts=8 sw=4 sts=4 et wm=0 tw=0
> +%---------------------------------------------------------------------------%
> +% Copyright (C) 2006 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: par_builtin.m.
> +% Main authors: wangp.
> +% Stability: low.
> +
> +% This file is automatically imported, as if via `use_module', into every
> +% module in parallel grades.  It is intended for the builtin procedures that
> +% the compiler generates implicit calls to when implementing parallel
> +% conjunctions.
> +%
> +% This module is a private part of the Mercury implementation; user modules
> +% should never explicitly import this module. The interface for this module
> +% does not get included in the Mercury library reference manual.
> +
> +%-----------------------------------------------------------------------------%
> +%-----------------------------------------------------------------------------%
> +
> +:- module par_builtin.
> +
> +%-----------------------------------------------------------------------------%
> +
> +:- interface.
> +
> +:- type prom(T).

Since the corresponding C type is MR_Promise I would be more inclined to call
that promise(T) but see below.

> +:- pred new_promise(prom(T)::uo) is det.
> +:- pred wait(prom(T)::in, T::out) is det.
> +:- impure pred signal(prom(T)::in, T::in) is det.

This needs to be better documented (or at the least there needs to be a
pointer to the documentation if it is elsewhere.)  Also, promise is already an
overloaded term in the compiler, e.g. we have promise declarations etc.  Is
there an alternative name?

>+%-----------------------------------------------------------------------------%
> +
> +:- implementation.
> +
> +:- pragma foreign_decl("C",
> +"
> +    typedef struct MR_Promise MR_Promise;
> +
> +#ifdef MR_THREAD_SAFE
> +# ifdef MR_HAVE_SEMAPHORE_H
> +    /* POSIX 1003.1b semaphores available. */
> +    #include <semaphore.h>
> +
> +    struct MR_Promise {
> +        sem_t   semaphore;
> +        MR_Word value;
> +    };
> +# else /* !MR_HAVE_SEMAPHORE_H */
> +    /* Use POSIX thread mutexes and condition variables. */
> +    #include <pthread.h>
> +
> +    struct MR_Promise {
> +        pthread_mutex_t mutex;
> +        pthread_cond_t cond;
> +        MR_Word value;
> +    };
> +# endif /* !MR_HAVE_SEMAPHORE_H */
> +#else /* !MR_THREAD_SAFE */
> +    struct MR_Promise {
> +    };
> +#endif /* !MR_THREAD_SAFE */
> +").
> +
> +:- pragma foreign_type("C", prom(T), "MR_Promise *").

That can have the `can_pass_as_mercury_type' attribute enabled.

> +
> +    % Placeholder only.
> +:- pragma foreign_type(il, prom(T), "class [mscorlib]System.Object").
> +
> +:- pragma foreign_proc("C",
> +    new_promise(Promise::uo),
> +    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
> +"
> +#ifdef MR_THREAD_SAFE
> +# ifdef MR_HAVE_SEMAPHORE_H
> +    Promise = MR_GC_NEW(MR_Promise);
> +    sem_init(&Promise->semaphore, MR_NO, 0);
> +    Promise->value = 0;
> +# else
> +    Promise = MR_GC_NEW(MR_Promise);
> +    pthread_mutex_init(&Promise->mutex, NULL);
> +    pthread_cond_init(&Promise->cond, NULL);
> +    Promise->value = 0;
> +# endif
> +#endif
> +").
> +
> +:- pragma foreign_proc("C",
> +    wait(Promise::in, Value::out),
> +    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
> +"
> +#ifdef MR_THREAD_SAFE
> +# ifdef MR_HAVE_SEMAPHORE_H
> +    sem_wait(&Promise->semaphore);
> +    sem_post(&Promise->semaphore);
> +    Value = Promise->value;
> +# else
> +    pthread_mutex_lock(&Promise->mutex);
> +    while (!Promise->pass) {
> +        pthread_cond_wait(&Promise->cond, &Promise->mutex);
> +    }
> +    Value = Promise->value;
> +    pthread_mutex_unlock(&Promise->mutex);
> +# endif
> +#endif
> +").
> +
> +:- pragma foreign_proc("C",
> +    signal(Promise::in, Value::in),
> +    [will_not_call_mercury, thread_safe, will_not_modify_trail],
> +"
> +#ifdef MR_THREAD_SAFE
> +# ifdef MR_HAVE_SEMAPHORE_H
> +    Promise->value = Value;
> +    sem_post(&Promise->semaphore);
> +# else
> +    pthread_mutex_lock(&Promise->mutex);
> +    Value = Promise->value;
> +    pthread_cond_broadcast(&Promise->cond);
> +    pthread_mutex_unlock(&Promise->mutex);
> +# endif
> +#endif

...

> Index: library/private_builtin.m
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/library/private_builtin.m,v
> retrieving revision 1.156
> diff -u -r1.156 private_builtin.m
> --- library/private_builtin.m	1 May 2006 14:45:18 -0000	1.156
> +++ library/private_builtin.m	13 Jun 2006 15:46:40 -0000
> @@ -14,9 +14,10 @@
>  % module. It is intended for builtins that are just implementation details,
>  % such as procedures that the compiler generates implicit calls to when
>  % implementing polymorphism, unification, compare/3, etc.
> -% Note that the builtins used for tabling and deep profiling are in separate
> -% modules (table_builtin.m and profiling_builtin.m).
> -%
> +% Note that the builtins used for tabling, deep profiling and parallelism are
> +% in separate modules (table_builtin.m, profiling_builtin.m and
> +% par_builting.m).
> +

s/builting/builtin/

To be continued ...
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list