123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108(*
* Concurrent - Generic interface for concurrent operations
* Copyright (C) 2008 David Teller, LIFO, Universite d'Orleans
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)typelock={execute:'a'b.('a->'b)->'a->'b}letnolock={execute=(fun fx->fx)}letsynclock=lock.executeletsynchronizelockerfx=sync(locker())fxletcompose{execute =a}{execute=b}={execute=(funfx->b(af)x)}letcreate~enter~leave={execute =(funfx->enter();tryletresult=fxinleave();resultwith e->leave ();raisee)}moduletypeBaseLock=sigtypet(** The type of a lock. *)valcreate:unit->tvallock:t->unitval unlock:t->unitval try_lock:t->boolendmoduletypeLock=sigtypet(** The type of a lock. *)valcreate:unit->tvallock:t->unitvalunlock:t->unitvaltry_lock:t->boolvalsynchronize:?lock:t->('a->'b)->'a->'bvalmake:unit->lockendletbase_create=createmoduleMakeLock(M:BaseLock):Lockwithtypet=M.t=structtypet=M.tletcreate=M.createletlock=M.lockletunlock=M.unlocklettry_lock=M.try_lockletsynchronize?(lock=M.create())fx=tryM.lock lock;letresult=fxinM.unlocklock;resultwithe-> M.unlocklock;raiseeletmake()=letlock=M.create()inbase_create~enter:(fun()->M.locklock)~leave:(fun()->M.unlocklock)endmoduleBaseNoLock=structtypet=unitexternalcreate:unit->t="%ignore"externallock:t->unit ="%ignore"externalunlock:t->unit ="%ignore"lettry_lock_t=trueendmoduleNoLock=MakeLock(BaseNoLock)