Next: , Previous: thread.mvar, Up: Top


83 thread.semaphore

     %--------------------------------------------------%
     % vim: ft=mercury ts=4 sw=4 et
     %--------------------------------------------------%
     % Copyright (C) 2000-2001,2003-2004, 2006-2007, 2009-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.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 that do not
     % contain a .par component.
     %
     %--------------------------------------------------%
     
     :- module thread.semaphore.
     :- interface.
     
     :- import_module bool.
     :- import_module io.
     
     %--------------------------------------------------%
     
     :- type semaphore.
     
         % init(Sem, !IO) creates a new semaphore `Sem' with its counter
         % initialized to 0.
         %
     :- pred semaphore.init(semaphore::out, io::di, io::uo) is det.
     
         % A synonym for the above.
         %
     :- pragma obsolete(semaphore.new/3).
     :- pred semaphore.new(semaphore::out, io::di, io::uo) is det.
     
         % Returns a new semaphore `Sem' with its counter initialized to Count.
         %
     :- impure func semaphore.init(int::in) = (semaphore::uo) is det.
     
         % A synonym for the above.
         %
     :- pragma obsolete(semaphore.new/1).
     :- impure func semaphore.new(int::in) = (semaphore::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.
     
     %--------------------------------------------------%
     %--------------------------------------------------%