From 1547eec9428f5ced3c2d5ac1d2420d86254f2048 Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Thu, 13 Nov 2003 03:33:09 +0000 Subject: "" svn path=/trunk/; revision=1182 --- externals/grill/flext/config-max-msvc.txt | 3 - externals/grill/flext/make-inc.max-msvc | 4 +- externals/grill/flext/source/flext.h | 2 + externals/grill/flext/source/flprefix.h | 5 +- externals/grill/flext/source/flsupport.h | 60 ++++++++++++++- externals/grill/flext/source/flthr.cpp | 90 +++++++++++++++++++--- .../grill/flext/tutorial/thread1/thread1.vcproj | 6 +- .../grill/flext/tutorial/thread2/thread2.vcproj | 8 +- 8 files changed, 151 insertions(+), 27 deletions(-) (limited to 'externals/grill/flext') diff --git a/externals/grill/flext/config-max-msvc.txt b/externals/grill/flext/config-max-msvc.txt index 39b171ad..f0ed8edd 100644 --- a/externals/grill/flext/config-max-msvc.txt +++ b/externals/grill/flext/config-max-msvc.txt @@ -5,9 +5,6 @@ # where is the Max SDK? MAXSDKPATH=F:\prog\audio\MaxWinSDK\c74support -# where is the pthreads package? -PTHREADS=F:\prog\packs\pthreads - # where is MS VC++? # (should be commented out when called from the Visual Studio Command prompt) # MSVCPATH="c:\programme\prog\microsoft visual studio\VC98" diff --git a/externals/grill/flext/make-inc.max-msvc b/externals/grill/flext/make-inc.max-msvc index b9b3e448..ab9a397c 100644 --- a/externals/grill/flext/make-inc.max-msvc +++ b/externals/grill/flext/make-inc.max-msvc @@ -10,7 +10,7 @@ SOURCE=source # includes -INCPATH=/I$(MSVCPATH)\include /I$(MAXSDKPATH)\max-includes /I$(MAXSDKPATH)\msp-includes /I$(PTHREADS) /I$(SOURCE) +INCPATH=/I$(MSVCPATH)\include /I$(MAXSDKPATH)\max-includes /I$(MAXSDKPATH)\msp-includes /I$(SOURCE) LDFLAGS=/LIBPATH:$(MSVCPATH)\lib @@ -28,8 +28,6 @@ TARGET=maxwin # appendix to lib name !ifdef FLEXT_SHARED DEFS=$(DEFS) /DFLEXT_SHARED /DFLEXT_EXPORTS -LIBS=$(LIBS) $(PTHREADS)\pthreadVC.lib - !ifdef _DEBUG LIBS=$(LIBS) $(MAXSDKPATH)\max-includes\win-includes\debug\maxapi.lib $(MAXSDKPATH)\msp-includes\win-includes\debug\maxaudio.lib !else diff --git a/externals/grill/flext/source/flext.h b/externals/grill/flext/source/flext.h index 9fd63487..c18fd29c 100644 --- a/externals/grill/flext/source/flext.h +++ b/externals/grill/flext/source/flext.h @@ -44,7 +44,9 @@ WARRANTIES, see the file, "license.txt," in this distribution. #elif FLEXT_THREADS == FLEXT_THR_MP #include #elif FLEXT_THREADS == FLEXT_THR_WIN32 + #define _WIN32_WINNT 0x500 // must be WIN2000 at least! #include + #include #else #error "Thread model not supported" #endif diff --git a/externals/grill/flext/source/flprefix.h b/externals/grill/flext/source/flprefix.h index 93bfbe4d..693d8630 100755 --- a/externals/grill/flext/source/flprefix.h +++ b/externals/grill/flext/source/flprefix.h @@ -335,8 +335,11 @@ WARRANTIES, see the file, "license.txt," in this distribution. #ifdef FLEXT_THREADS #undef FLEXT_THREADS #if FLEXT_OS == FLEXT_OS_MAC && FLEXT_SYS == FLEXT_SYS_MAX - // Max crashes with posix threads (but don't know why...) + // Max crashes with posix threads (because it's in the CFM model) #define FLEXT_THREADS FLEXT_THR_MP + #elif FLEXT_SYS == FLEXT_SYS_MAX && FLEXT_OS == FLEXT_OS_WIN + // for wmax use native Windows threads + #define FLEXT_THREADS FLEXT_THR_WIN32 #else #define FLEXT_THREADS FLEXT_THR_POSIX #endif diff --git a/externals/grill/flext/source/flsupport.h b/externals/grill/flext/source/flsupport.h index 5e63ded0..e8328ea3 100644 --- a/externals/grill/flext/source/flsupport.h +++ b/externals/grill/flext/source/flsupport.h @@ -636,6 +636,8 @@ public: typedef MPTaskID thrid_t; #elif FLEXT_THREADS == FLEXT_THR_POSIX typedef pthread_t thrid_t; +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + typedef DWORD thrid_t; #else #error #endif @@ -647,6 +649,8 @@ public: return pthread_self(); #elif FLEXT_THREADS == FLEXT_THR_MP return MPCurrentTaskID(); +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + return GetCurrentThreadId(); #else #error #endif @@ -660,6 +664,8 @@ public: static bool IsThread(thrid_t t,thrid_t ref = GetThreadId()) { #if FLEXT_THREADS == FLEXT_THR_POSIX return pthread_equal(ref,t) != 0; +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + return ref == t; #else return ref == t; #endif @@ -727,6 +733,7 @@ protected: static bool StartHelper(); static bool StopHelper(); static void ThrHelper(void *); + static void LaunchHelper(thr_entry *e); //! the system's thread id static thrid_t thrid; // the system thread @@ -742,6 +749,8 @@ public: sched_yield(); #elif FLEXT_THREADS == FLEXT_THR_MP MPYield(); +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + SwitchToThread(); #else #error #endif @@ -800,6 +809,28 @@ public: pthread_mutex_t mutex; // int cnt; }; +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + { + public: + //! Construct thread mutex + ThrMutex() { ::InitializeCriticalSection(&mutex); } + //! Destroy thread mutex + ~ThrMutex() { ::DeleteCriticalSection(&mutex); } + + //! Lock thread mutex + bool Lock() { ::EnterCriticalSection(&mutex); return true; } + /*! Wait to lock thread mutex. + \todo Implement! + */ +// bool WaitForLock(double tm) { return pthread_mutex_lock(&mutex) == 0; } + //! Try to lock, but don't wait + bool TryLock() { return ::TryEnterCriticalSection(&mutex) != 0; } + //! Unlock thread mutex + bool Unlock() { ::LeaveCriticalSection(&mutex); return true; } + + protected: + CRITICAL_SECTION mutex; + }; #elif FLEXT_THREADS == FLEXT_THR_MP { public: @@ -842,18 +873,43 @@ public: /*! Wait for condition (for a certain time). \param ftime Wait time in seconds - \ret 0 = signalled, 1 = timed out + \ret true = signalled, false = timed out \remark If ftime = 0 this may suck away your cpu if used in a signalled loop. \remark The time resolution of the implementation is required to be at least ms. */ bool TimedWait(double ftime); //! Signal condition - bool Signal(); + bool Signal() { return pthread_cond_signal(&cond) == 0; } protected: pthread_cond_t cond; }; +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + { + public: + //! Construct thread conditional + ThrCond() { cond = CreateEvent(NULL,FALSE,FALSE,NULL); } + //! Destroy thread conditional + ~ThrCond() { CloseHandle(cond); } + + //! Wait for condition + bool Wait() { return WaitForSingleObject(cond,INFINITE) == WAIT_OBJECT_0; } + + /*! Wait for condition (for a certain time). + \param ftime Wait time in seconds + \ret true = signalled, false = timed out + \remark If ftime = 0 this may suck away your cpu if used in a signalled loop. + \remark The time resolution of the implementation is required to be at least ms. + */ + bool TimedWait(double ftime) { return WaitForSingleObject(cond,(LONG)(ftime*1000)) == WAIT_OBJECT_0; } + + //! Signal condition + bool Signal() { return SetEvent(cond) != 0; } + + protected: + HANDLE cond; + }; #elif FLEXT_THREADS == FLEXT_THR_MP { public: diff --git a/externals/grill/flext/source/flthr.cpp b/externals/grill/flext/source/flthr.cpp index 0ca03f38..65fd4c3b 100644 --- a/externals/grill/flext/source/flthr.cpp +++ b/externals/grill/flext/source/flthr.cpp @@ -48,6 +48,12 @@ static flext::ThrCond *thrhelpcond = NULL; flext::thrid_t flext::GetSysThreadId() { return thrid; } +void flext::LaunchHelper(thr_entry *e) +{ + e->thrid = GetThreadId(); + e->meth(e->params); +} + //! Start helper thread bool flext::StartHelper() { @@ -75,6 +81,8 @@ bool flext::StartHelper() OSStatus ret = MPCreateTask((TaskProc)ThrHelper,NULL,0,0,0,0,0,&thrhelpid); ok = ret == noErr; } +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + ok = _beginthread(ThrHelper,0,NULL) >= 0; #else #error #endif @@ -100,10 +108,11 @@ bool flext::StopHelper() } #endif - //! Static helper thread function void flext::ThrHelper(void *) { + thrhelpid = GetThreadId(); + #if FLEXT_THREADS == FLEXT_THR_POSIX // set prototype thread attributes pthread_attr_t attr; @@ -139,9 +148,13 @@ void flext::ThrHelper(void *) // post("Helper start thread"); #if FLEXT_THREADS == FLEXT_THR_POSIX - ok = pthread_create (&ti->thrid,&attr,(void *(*)(void *))ti->meth,ti->params) == 0; + thrid_t dummy; + ok = pthread_create (&dummy,&attr,(void *(*)(void *))LaunchHelper,ti) == 0; #elif FLEXT_THREADS == FLEXT_THR_MP - ok = MPCreateTask((TaskProc)ti->meth,ti->params,0,0,0,0,0,&ti->thrid) == noErr; + thrid_t dummy; + ok = MPCreateTask((TaskProc)LaunchHelper,ti,0,0,0,0,0,&dummy) == noErr; +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + ok = _beginthread((void (*)(void *))LaunchHelper,0,ti) >= 0; #else #error #endif @@ -342,6 +355,10 @@ bool flext_base::StopThreads() #elif FLEXT_THREADS == FLEXT_THR_MP MPTerminateTask(t->thrid,0); // here, we should use a task queue to check whether the task has really terminated!! + #elif FLEXT_THREADS == FLEXT_THR_WIN32 + // can't use the c library function _endthread.. memory leaks will occur + HANDLE hnd = OpenThread(THREAD_ALL_ACCESS,TRUE,t->thrid); + TerminateThread(hnd,0); #else #error #endif @@ -399,6 +416,41 @@ bool flext::RelPriority(int dp,thrid_t ref,thrid_t id) } } return true; + +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + HANDLE href = OpenThread(THREAD_ALL_ACCESS,TRUE,ref); + HANDLE hid = OpenThread(THREAD_ALL_ACCESS,TRUE,id); + int pr = GetThreadPriority(href); + + if(pr == THREAD_PRIORITY_ERROR_RETURN) { +#ifdef FLEXT_DEBUG + post("flext - failed to get thread priority"); +#endif + return false; + } + + pr += dp; + if(pr < THREAD_PRIORITY_IDLE) { +#ifdef FLEXT_DEBUG + post("flext - minimum thread priority reached"); +#endif + pr = THREAD_PRIORITY_IDLE; + } + else if(pr > THREAD_PRIORITY_TIME_CRITICAL) { +#ifdef FLEXT_DEBUG + post("flext - maximum thread priority reached"); +#endif + pr = THREAD_PRIORITY_TIME_CRITICAL; + } + + if(SetThreadPriority(hid,pr) == 0) { +#ifdef FLEXT_DEBUG + post("flext - failed to change thread priority"); +#endif + return false; + } + return true; + #elif FLEXT_THREADS == FLEXT_THR_MP thr_entry *t; for(t = thrhead; t && t->Id() != id; t = t->nxt) {} @@ -441,6 +493,19 @@ int flext::GetPriority(thrid_t id) return -1; } return parm.sched_priority; + +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + HANDLE hid = OpenThread(THREAD_ALL_ACCESS,TRUE,id); + int pr = GetThreadPriority(hid); + + if(pr == THREAD_PRIORITY_ERROR_RETURN) { +#ifdef FLEXT_DEBUG + post("flext - failed to get thread priority"); +#endif + return -1; + } + return pr; + #elif FLEXT_THREADS == FLEXT_THR_MP thr_entry *t; for(t = thrhead; t && t->Id() != id; t = t->nxt) {} @@ -472,6 +537,17 @@ bool flext::SetPriority(int p,thrid_t id) } } return true; + +#elif FLEXT_THREADS == FLEXT_THR_WIN32 + HANDLE hid = OpenThread(THREAD_ALL_ACCESS,TRUE,id); + if(SetThreadPriority(hid,p) == 0) { +#ifdef FLEXT_DEBUG + post("flext - failed to change thread priority"); +#endif + return false; + } + return true; + #elif FLEXT_THREADS == FLEXT_THR_MP thr_entry *t; for(t = thrhead; t && t->Id() != id; t = t->nxt) {} @@ -540,14 +616,6 @@ bool flext::ThrCond::TimedWait(double ftime) Unlock(); return ret; } - -bool flext::ThrCond::Signal() -{ -// Lock(); - bool ret = pthread_cond_signal(&cond) == 0; -// Unlock(); - return ret; -} #endif diff --git a/externals/grill/flext/tutorial/thread1/thread1.vcproj b/externals/grill/flext/tutorial/thread1/thread1.vcproj index 27931ac1..3cf34ebf 100644 --- a/externals/grill/flext/tutorial/thread1/thread1.vcproj +++ b/externals/grill/flext/tutorial/thread1/thread1.vcproj @@ -45,7 +45,7 @@ OutputFile="../pd-msvc/thread1.dll" LinkIncremental="1" SuppressStartupBanner="TRUE" - AdditionalLibraryDirectories="..\..\pd-msvc" + AdditionalLibraryDirectories="C:\Programme\audio\pd\bin;..\..\pd-msvc" ProgramDatabaseFile=".\msvc/thread1.pdb" ImportLibrary=".\msvc/thread1.lib" TargetMachine="1"/> @@ -90,7 +90,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories="c:\programme\audio\pd\src,..\..\source" - PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;PD;FLEXT_THREADS" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FLEXT_THREADS" BasicRuntimeChecks="3" RuntimeLibrary="1" UsePrecompiledHeader="2" @@ -111,7 +111,7 @@ OutputFile=".\msvc-debug/thread1.dll" LinkIncremental="1" SuppressStartupBanner="TRUE" - AdditionalLibraryDirectories="..\..\pd-msvc" + AdditionalLibraryDirectories="C:\Programme\audio\pd\bin; ..\..\pd-msvc" GenerateDebugInformation="TRUE" ProgramDatabaseFile=".\msvc-debug/thread1.pdb" ImportLibrary=".\msvc-debug/thread1.lib" diff --git a/externals/grill/flext/tutorial/thread2/thread2.vcproj b/externals/grill/flext/tutorial/thread2/thread2.vcproj index 301e6b5c..99a25bad 100644 --- a/externals/grill/flext/tutorial/thread2/thread2.vcproj +++ b/externals/grill/flext/tutorial/thread2/thread2.vcproj @@ -45,7 +45,7 @@ OutputFile="../pd-msvc/thread2.dll" LinkIncremental="1" SuppressStartupBanner="TRUE" - AdditionalLibraryDirectories="..\..\pd-msvc" + AdditionalLibraryDirectories="C:\Programme\audio\pd\bin;..\..\pd-msvc" ProgramDatabaseFile=".\msvc/thread2.pdb" ImportLibrary=".\msvc/thread2.lib" TargetMachine="1"/> @@ -90,7 +90,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories="c:\programme\audio\pd\src,..\..\source" - PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;PD;FLEXT_THREADS" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FLEXT_THREADS" BasicRuntimeChecks="3" RuntimeLibrary="1" UsePrecompiledHeader="2" @@ -107,11 +107,11 @@ Name="VCCustomBuildTool"/>