123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135(* Copyright (C) 2014--2020 Petter A. Urkedal <paurkedal@gmail.com>
*
* 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 3 of the License, or (at your
* option) any later version, with the OCaml static compilation exception.
*
* 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, see <http://www.gnu.org/licenses/>.
*)openCaqti_compat[@@warning"-33"]letdefault_max_size=tryint_of_string(Sys.getenv"CAQTI_POOL_MAX_SIZE")withNot_found->8moduleMake(System:Caqti_driver_sig.System_common)=structopenSystemlet(>>=?)mmf=m>>=(functionOkx->mfx|Errore->return(Errore))moduleTask=structtypet={priority:float;mvar:unitMvar.t}letwake{mvar;_}=Mvar.store()mvarletcompare{priority=pA;_}{priority=pB;_}=Float.comparepBpAendmoduleTaskq=Caqti_heap.Make(Task)type('a,+'e)t={create:unit->('a,'e)resultfuture;free:'a->unitfuture;check:'a->(bool->unit)->unit;validate:'a->boolfuture;max_idle_size:int;max_size:int;mutablecur_size:int;pool:'aQueue.t;mutablewaiting:Taskq.t;}letcreate?(max_size=default_max_size)?(max_idle_size=max_size)?(check=fun_f->ftrue)?(validate=fun_->returntrue)createfree=assert(max_size>0);assert(max_size>=max_idle_size);{create;free;check;validate;max_idle_size;max_size;cur_size=0;pool=Queue.create();waiting=Taskq.empty}letsize{cur_size;_}=cur_sizeletwait~priorityp=letmvar=Mvar.create()inp.waiting<-Taskq.pushTask.({priority;mvar})p.waiting;Mvar.fetchmvarletschedulep=ifnot(Taskq.is_emptyp.waiting)thenbeginlettask,taskq=Taskq.pop_ep.waitinginp.waiting<-taskq;Task.waketaskendletreallocp=p.create()>|=(function|Oke->Oke|Errorerr->p.cur_size<-p.cur_size-1;schedulep;Errorerr)letrecacquire~priorityp=ifQueue.is_emptyp.poolthenbeginifp.cur_size<p.max_sizethenbeginp.cur_size<-p.cur_size+1;reallocpendelsewait~priorityp>>=fun()->acquire~prioritypendelsebeginlete=Queue.takep.poolinp.validatee>>=funok->ifokthenreturn(Oke)elsereallocpendletreleasepe=ifp.cur_size>p.max_idle_sizethenbeginp.cur_size<-p.cur_size-1;p.freee>|=fun()->schedulependelsebeginp.checkebeginfunok->ifokthenQueue.addep.poolelsep.cur_size<-p.cur_size-1;schedulepend;return()endletuse?(priority=0.0)fp=acquire~priorityp>>=?fune->tryfe>>=(function|Oky->releasepe>|=fun()->Oky|Errorerr->releasepe>|=fun()->Errorerr)withexn->releasepe>|=fun()->raiseexnletdisposepe=p.freee>|=fun()->p.cur_size<-p.cur_size-1letrecdrainp=ifp.cur_size=0thenreturn()else(ifQueue.is_emptyp.poolthenwait~priority:0.0pelsedisposep(Queue.takep.pool))>>=fun()->drainpend