diff options
author | Thomas Grill <xovo@users.sourceforge.net> | 2002-12-17 04:36:31 +0000 |
---|---|---|
committer | Thomas Grill <xovo@users.sourceforge.net> | 2002-12-17 04:36:31 +0000 |
commit | a5b45e8e2e99686ea73fa2793d7fec84f0d1a3ed (patch) | |
tree | 4b31406382bacd13aa784ea22d584ac634f8d8ad /externals/grill/vasp/source/buflib.cpp | |
parent | a0cb0bd2e02740e50ec16dde2fb4e07f1702d1d0 (diff) |
""
svn path=/trunk/; revision=303
Diffstat (limited to 'externals/grill/vasp/source/buflib.cpp')
-rw-r--r-- | externals/grill/vasp/source/buflib.cpp | 112 |
1 files changed, 77 insertions, 35 deletions
diff --git a/externals/grill/vasp/source/buflib.cpp b/externals/grill/vasp/source/buflib.cpp index 03c2944e..78866a8e 100644 --- a/externals/grill/vasp/source/buflib.cpp +++ b/externals/grill/vasp/source/buflib.cpp @@ -52,7 +52,6 @@ public: static BufEntry *libhead = NULL,*libtail = NULL; static FreeEntry *freehead = NULL,*freetail = NULL; static I libcnt = 0,libtick = 0; -static t_clock *libclk = NULL; #ifdef FLEXT_THREADS static flext::ThrMutex libmtx; @@ -114,6 +113,73 @@ V BufLib::DecRef(t_symbol *s) } } +static V Collect() +{ +#ifdef FLEXT_THREADS + libmtx.Lock(); +#endif + + // collect garbage + BufEntry *e,*p; + for(p = NULL,e = libhead; e; ) { + if(e->refcnt <= 0 && e->tick+LIBTOL < libtick) { + ASSERT(e->refcnt == 0); + + BufEntry *n = e->nxt; + + if(p) p->nxt = n; + else libhead = n; + + if(!n) libtail = p; + else e->nxt = NULL; + + delete e; + + e = n; + } + else + p = e,e = e->nxt; + } + +#ifdef FLEXT_THREADS + libmtx.Unlock(); +#endif +} + + +#ifdef FLEXT_THREADS +static bool libthractive = false; +static pthread_t libthrid; +static bool libthrexit = false; // currently not used +static flext::ThrCond *libthrcond = NULL; + +static V *LibThr(V *) +{ + flext::RelPriority(-2); + + while(libthrexit) { + libthrcond->TimedWait(0.5f); + // TODO - should process return value of TimedWait + Collect(); + } + return NULL; +} +#endif + +static t_clock *libclk = NULL; +static V LibTick(V *) +{ +#ifdef FLEXT_THREADS + libthrcond->Signal(); +#else + Collect(); +#endif + + ++libtick; + clock_delay(libclk,LIBTICK); + +} + static t_symbol *GetLibSym() { if(freehead) { @@ -135,7 +201,7 @@ static t_symbol *GetLibSym() libcnt++; return gensym(tmp); } - + clock_delay(libclk,LIBTICK); } @@ -147,44 +213,20 @@ static V FreeLibSym(t_symbol *sym) freetail = f; } -static V LibTick(V *) + +BufEntry *BufLib::NewImm(I fr,BL zero) { #ifdef FLEXT_THREADS - libmtx.Lock(); -#endif - - // collect garbage - BufEntry *e,*p; - for(p = NULL,e = libhead; e; ) { - if(e->refcnt <= 0 && e->tick+LIBTOL < libtick) { - ASSERT(e->refcnt == 0); - - BufEntry *n = e->nxt; - - if(p) p->nxt = n; - else libhead = n; - - if(!n) libtail = p; - else e->nxt = NULL; - - delete e; - - e = n; + if(!libthractive) { + int ret = pthread_create(&libthrid,NULL,LibThr,NULL); + if(ret) + error("vasp - Could not launch helper thread"); + else { + libthrcond = new flext::ThrCond; + libthractive = true; } - else - p = e,e = e->nxt; } - - ++libtick; - clock_delay(libclk,LIBTICK); - -#ifdef FLEXT_THREADS - libmtx.Unlock(); #endif -} - -BufEntry *BufLib::NewImm(I fr,BL zero) -{ if(!libclk) { libclk = (t_clock *)clock_new(NULL,(t_method)LibTick); clock_delay(libclk,LIBTICK); |