From 86bdc7d828f8df51a072da396217ce6c38b7034a Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Mon, 30 Jun 2003 02:33:10 +0000 Subject: "" svn path=/trunk/; revision=739 --- externals/grill/flext/source/flsupport.cpp | 35 ++++++++++++++++++++++ externals/grill/flext/source/flsupport.h | 47 +++++++++++++++++++++--------- externals/grill/flext/source/flthr.cpp | 47 +++++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 17 deletions(-) (limited to 'externals/grill/flext/source') diff --git a/externals/grill/flext/source/flsupport.cpp b/externals/grill/flext/source/flsupport.cpp index ac501f78..9e6a2491 100644 --- a/externals/grill/flext/source/flsupport.cpp +++ b/externals/grill/flext/source/flsupport.cpp @@ -14,6 +14,9 @@ WARRANTIES, see the file, "license.txt," in this distribution. #include "flext.h" +#include +#include + const t_symbol *flext::sym_float = NULL; const t_symbol *flext::sym_symbol = NULL; const t_symbol *flext::sym_bang = NULL; @@ -171,3 +174,35 @@ int flext::Int2Bits(unsigned long n) return b; } +void flext::post(const char *fmt, ...) +{ +#ifdef FLEXT_THREADS + static ThrMutex mutex; + mutex.Lock(); +#endif + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + putc('\n', stderr); +#ifdef FLEXT_THREADS + mutex.Unlock(); +#endif +} + +void flext::error(const char *fmt,...) +{ +#ifdef FLEXT_THREADS + static ThrMutex mutex; + mutex.Lock(); +#endif + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, ap); + va_end(ap); + putc('\n', stderr); +#ifdef FLEXT_THREADS + mutex.Unlock(); +#endif +} diff --git a/externals/grill/flext/source/flsupport.h b/externals/grill/flext/source/flsupport.h index 6762c9c2..72bfcf3e 100644 --- a/externals/grill/flext/source/flsupport.h +++ b/externals/grill/flext/source/flsupport.h @@ -18,6 +18,10 @@ WARRANTIES, see the file, "license.txt," in this distribution. #include "flstdc.h" #include +#if FLEXT_OS == FLEXT_OS_WIN +#include +#endif + class FLEXT_SHARE flext_base; /*! \brief Flext support class @@ -53,12 +57,10 @@ public: // --- console output ----------------------------------------------- -#if FLEXT_SYS == FLEXT_SYS_JMAX //! post message to console static void post(const char *s,...); //! post error message to console static void error(const char *s,...); -#endif // --- memory ------------------------------------------------------- @@ -608,7 +610,7 @@ public: /*! \brief Thread parameters \internal */ - class thr_params + class FLEXT_SHARE thr_params { public: thr_params(int n = 1); @@ -632,7 +634,7 @@ public: /*! \brief This represents an entry to the list of active method threads \internal */ - class thr_entry + class FLEXT_SHARE thr_entry { public: thr_entry(void (*m)(thr_params *),thr_params *p,thrid_t id = GetThreadId()); @@ -780,22 +782,31 @@ public: /*! \brief Wait for condition (for a certain time). \param ftime Wait time in seconds \ret 0 = signalled, 1 = timed out - \remark Depending on the implementation ftime may not be fractional. - \remark So if ftime = 0 this may suck away your cpu if used in a signalled loop. + \remark If ftime = 0 this may suck away your cpu if used in a signalled loop. */ bool TimedWait(double ftime) { timespec tm; -#if 0 // find out when the following is defined +#if FLEXT_OS == FLEXT_OS_WIN + _timeb tmb; + _ftime(&tmb); + tm.tv_nsec = tmb.millitm*1000000; + tm.tv_sec = tmb.time; +#else + #if 0 // find out when the following is defined clock_gettime(CLOCK_REALTIME,tm); + #else + struct timeval tp; + rs = gettimeofday(&tp, NULL); + tm.tv_nsec = tp.tv_usec*1000; + tm.tv_sec = tp.tv_sec; + #endif +#endif tm.tv_nsec += (long)((ftime-(long)ftime)*1.e9); long nns = tm.tv_nsec%1000000000; tm.tv_sec += (long)ftime+(tm.tv_nsec-nns)/1000000000; tm.tv_nsec = nns; -#else - tm.tv_sec = time(NULL)+(long)ftime; - tm.tv_nsec = 0; -#endif + Lock(); bool ret = pthread_cond_timedwait(&cond,&mutex,&tm) == 0; Unlock(); @@ -854,9 +865,19 @@ public: static void PopThread(); /*! \brief Launch a thread. - \remark thr_params *p may be NULL if not needed. + \param meth Thread function + \param params Parameters to pass to the thread, may be NULL if not needed. + \return Thread id on success, NULL on failure + */ + static bool LaunchThread(void (*meth)(thr_params *p),thr_params *params = NULL); + + /*! \brief Terminate a thread. + \param meth Thread function + \param params Parameters to pass to the thread, may be NULL if not needed. + \return True if at least one matching thread has been found. + \remark Terminates all running threads with matching meth and params. */ - static bool LaunchThread(void (*meth)(thr_params *p),thr_params *p = NULL); + static bool StopThread(void (*meth)(thr_params *p),thr_params *params = NULL,bool wait = false); //! @} FLEXT_S_THREAD diff --git a/externals/grill/flext/source/flthr.cpp b/externals/grill/flext/source/flthr.cpp index 3bd5bbf4..84623a3d 100644 --- a/externals/grill/flext/source/flthr.cpp +++ b/externals/grill/flext/source/flthr.cpp @@ -25,10 +25,6 @@ flext::thrid_t flext::thrid = 0; //! Thread id of helper thread flext::thrid_t flext::thrhelpid = 0; -/* -flext::thr_entry *flext::thrhead = NULL,*flext::thrtail = NULL; -flext::ThrMutex flext::tlmutex; -*/ static flext::thr_entry *thrhead = NULL,*thrtail = NULL; static flext::ThrMutex tlmutex; @@ -198,6 +194,49 @@ bool flext::LaunchThread(void (*meth)(thr_params *p),thr_params *p) return true; } +bool flext::StopThread(void (*meth)(thr_params *p),thr_params *p,bool wait) +{ +#ifdef FLEXT_DEBUG + if(!thrhelpcond) { + ERRINTERNAL(); + return false; + } +#endif + + int found = 0; + + tlmutex.Lock(); + for(thr_entry *ti = thrhead; ti; ti = ti->nxt) + // set shouldexit if meth and params match + if(ti->meth == meth && ti->params == p) { ti->shouldexit = true; found++; } + tlmutex.Unlock(); + + if(found) { + // signal thread helper + thrhelpcond->Signal(); + + int cnt = 0; + for(int wi = 0; wi < 100; ++wi) { + // lock and count this object's threads + cnt = 0; + tlmutex.Lock(); + for(thr_entry *t = thrhead; t; t = t->nxt) + if(t->meth == meth && t->params == p) ++cnt; + tlmutex.Unlock(); + + // if no threads are remaining, we are done + if(!cnt) break; + + // Wait + Sleep(0.01f); + } + + return cnt == 0; + } + else + return false; +} + bool flext_base::ShouldExit() const { bool ret = true; -- cgit v1.2.1