82 thread.mvar
%--------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%--------------------------------------------------%
% Copyright (C) 2000-2003, 2006-2007, 2011 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.mvar.m.
% Main author: petdr, fjh.
% Stability: low.
%
% This module provides a Mercury version of Haskell mutable variables. A
% mutable variable (mvar) is a reference to a mutable location which can
% either contain a value of type T or be empty.
%
% Access to a mvar is thread-safe and can be used to synchronize between
% different threads.
%
%--------------------------------------------------%
%--------------------------------------------------%
:- module thread.mvar.
:- interface.
:- import_module bool.
:- import_module io.
:- import_module maybe.
%--------------------------------------------------%
:- type mvar(T).
% Create an empty mvar.
%
:- impure func mvar.init = (mvar(T)::uo) is det.
% Create an empty mvar.
%
:- pred mvar.init(mvar(T)::out, io::di, io::uo) is det.
% Take the contents of the mvar out leaving the mvar empty.
% If the mvar is empty, block until some thread fills the mvar.
%
:- 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.
%
:- pred mvar.put(mvar(T)::in, T::in, io::di, io::uo) is det.
% Place the value of type T into an empty mvar, returning yes on success.
% If the mvar is full, return no immediately without blocking.
%
:- pred mvar.try_put(mvar(T)::in, T::in, bool::out, io::di, io::uo) is det.
% Read the contents of mvar, without taking it out.
% If the mvar is empty, block until it is full.
% This is equivalent to mvar.take followed by mvar.put.
%
:- pred mvar.read(mvar(T)::in, T::out, io::di, io::uo) is det.
%--------------------------------------------------%
%--------------------------------------------------%