Mercurial > flash_v2
comparison host/tools/Utils/common/eCosThreadUtils.h @ 82:6736c52df507 ecos-sw-2000-04-14
Merge from eCos master repository on 2000-04-14-13:35:46-BST
| author | jlarmour |
|---|---|
| date | Tue, 18 Apr 2000 21:51:55 +0000 |
| parents | 435cced73e2f |
| children |
comparison
equal
deleted
inserted
replaced
| 81:89fef2181d7d | 82:6736c52df507 |
|---|---|
| 41 | 41 |
| 42 #ifndef _ECOSTHREADUTILS_H | 42 #ifndef _ECOSTHREADUTILS_H |
| 43 #define _ECOSTHREADUTILS_H | 43 #define _ECOSTHREADUTILS_H |
| 44 #include "eCosStd.h" | 44 #include "eCosStd.h" |
| 45 #include "Collections.h" | 45 #include "Collections.h" |
| 46 #define ENTERCRITICAL {CeCosThreadUtils::CS c | |
| 47 #define LEAVECRITICAL } | |
| 48 | 46 |
| 49 #ifndef _WIN32 // UNIX | 47 #ifndef _WIN32 // UNIX |
| 50 #ifndef NO_THREADS | 48 #ifndef NO_THREADS |
| 51 #include <pthread.h> | 49 #include <pthread.h> |
| 52 #endif | 50 #endif |
| 53 #endif | 51 #endif |
| 54 | 52 |
| 53 //================================================================= | |
| 54 // This class handles threads in a host-independent manner. | |
| 55 // It also contains a few thread-related functions such as Sleep | |
| 56 //================================================================= | |
| 57 | |
| 55 class CeCosThreadUtils { | 58 class CeCosThreadUtils { |
| 56 public: | 59 public: |
| 60 | |
| 57 #ifdef _WIN32 | 61 #ifdef _WIN32 |
| 58 static int CALLBACK FilterFunction(LPEXCEPTION_POINTERS p); | |
| 59 typedef DWORD THREAD_ID; | 62 typedef DWORD THREAD_ID; |
| 60 #else // UNIX | 63 #else // UNIX |
| 61 #ifndef NO_THREADS | 64 #ifndef NO_THREADS |
| 62 typedef int THREAD_ID; | 65 typedef int THREAD_ID; |
| 63 #else | 66 #else |
| 64 typedef pthread_t THREAD_ID; | 67 typedef pthread_t THREAD_ID; |
| 65 #endif | 68 #endif |
| 66 #endif | 69 #endif |
| 67 static THREAD_ID GetThreadId(); | 70 |
| 71 static THREAD_ID GetThreadId(); // Get my current thread ID, mostly for debugging | |
| 68 | 72 |
| 73 // CS supports a single system-wide critical sections (recursive mutexes). | |
| 74 // You are expected to use macros ENTERCRITICAL and LEAVECRITICAL to use this class - these macros define | |
| 75 // a block containing a CS object, which has the effect of creating a critical section. | |
| 76 // Exit from the block (by whatever means, including an exception) causes the CS dtor | |
| 77 // to be called so as to release the section. | |
| 78 | |
| 69 class CS{ | 79 class CS{ |
| 70 // p and q routines (process-local recursive mutexes) | 80 public: |
| 81 static bool InCriticalSection(); // This thread owns the critical section | |
| 82 CS(); | |
| 83 virtual ~CS(); | |
| 84 protected: | |
| 85 static int m_nCriticalSectionLock; // The number of times the recursive mutex has been locked. Management of this allows us to avoid use of true recursive mutexes on UNIX. | |
| 86 static THREAD_ID nCSOwner; // The thread owning the resource. | |
| 71 #ifdef _WIN32 | 87 #ifdef _WIN32 |
| 72 static CRITICAL_SECTION cs; | 88 static CRITICAL_SECTION cs; // The one and only critical section |
| 73 static bool bCSInitialized; | 89 static bool bCSInitialized; |
| 74 #else // UNIX | 90 #else // UNIX |
| 75 #ifndef NO_THREADS | 91 #ifndef NO_THREADS |
| 76 // Static recursive mutex for unix critical section | 92 static pthread_mutex_t cs; // The one and only critical section |
| 77 static pthread_mutex_t cs; | |
| 78 #endif | 93 #endif |
| 79 #endif | 94 #endif |
| 80 | |
| 81 public: | |
| 82 | |
| 83 static int m_nCriticalSectionLock; | |
| 84 static THREAD_ID nCSOwner; | |
| 85 | |
| 86 static int AtomicIncrement (int &n); // return old value | |
| 87 static int AtomicDecrement (int &n); // return old value | |
| 88 CS(); | |
| 89 virtual ~CS(); | |
| 90 }; | 95 }; |
| 96 | |
| 97 #define ENTERCRITICAL {CeCosThreadUtils::CS c | |
| 98 #define LEAVECRITICAL } | |
| 99 | |
| 100 static int AtomicIncrement (int &n); // return old value | |
| 101 static int AtomicDecrement (int &n); // return old value | |
| 102 | |
| 91 | 103 |
| 92 // Wait for this boolean to become true, subject to the given timeout | 104 // Wait for this boolean to become true, subject to the given timeout |
| 105 // If the timeout happens first, the return code will be false - otherwise true | |
| 93 static bool WaitFor (bool &b, int dTimeout=0x7fffffff); | 106 static bool WaitFor (bool &b, int dTimeout=0x7fffffff); |
| 94 | 107 |
| 95 /////////////////////////////////////////////////////////////////////////// | 108 /////////////////////////////////////////////////////////////////////////// |
| 96 // Define the characteristics of a callback procedure: | 109 // Define the characteristics of a callback procedure: |
| 97 | 110 |
| 98 // A callback procedure: | 111 // A callback procedure, used both for thread entry points and thread completion callbacks |
| 99 typedef void (CALLBACK CallbackProc)(void *); | 112 typedef void (CALLBACK CallbackProc)(void *); |
| 100 | 113 |
| 101 // Run a thread: pThreadFunc is the entry point (passed pParam). No notification of completion. | 114 // Run a thread: pThreadFunc is the entry point (passed pParam). No notification of completion. |
| 102 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,0,0,pszName); } | 115 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,0,0,pszName); } |
| 103 // Run a thread, setting the bool on completion | 116 // Run a thread, setting the bool on completion |
| 104 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, bool *pb, LPCTSTR pszName=_T("")) { *pb=false; return RunThread(pThreadFunc,pParam,0,pb,pszName); } | 117 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, bool *pb, LPCTSTR pszName=_T("")) { *pb=false; return RunThread(pThreadFunc,pParam,0,pb,pszName); } |
| 105 // Run a thread, calling the callback on completion | 118 // Run a thread, calling the callback on completion |
| 106 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,pCompletionFunc,pParam,pszName); } | 119 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,pCompletionFunc,pParam,pszName); } |
| 107 | 120 |
| 121 static void Sleep (int nMsec); | |
| 122 | |
| 108 protected: | 123 protected: |
| 109 | 124 |
| 110 // If m_pProc is non-zero then it is called | |
| 111 // If m_pProc is 0 and m_pParam is non-0 then the callback sets the boolean whose address is held in m_pParam | |
| 112 // If m_pProc is 0 and m_pParam is 0 then the callback is "blocking" | |
| 113 //bool IsBlocking() const { return 0==m_pProc && 0==m_pParam; } | |
| 114 | |
| 115 // Run a thread: arbitrary callbcak | 125 // Run a thread: arbitrary callbcak |
| 116 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, void *pCompletionParam, LPCTSTR pszName); | 126 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, void *pCompletionParam, LPCTSTR pszName); |
| 117 | 127 |
| 128 // This is the information that is passed to the host-specific thread proc. It is simply enough to call the thread entry point and | |
| 129 // call the callback (or set the boolean) at the end. | |
| 118 struct ThreadInfo { | 130 struct ThreadInfo { |
| 119 CallbackProc *pThreadFunc; | 131 CallbackProc *pThreadFunc; // The thread proc is this function |
| 120 void *pThreadParam; | 132 void *pThreadParam; // - called with this parameter |
| 121 CallbackProc *pCompletionFunc; // Call this function | 133 CallbackProc *pCompletionFunc; // At the end - call this function |
| 122 void *pCompletionParam; // With this parameter | 134 void *pCompletionParam; // with this parameter |
| 123 String strName; | 135 String strName; // For debugging |
| 124 ThreadInfo (CallbackProc *_pThreadFunc,void *_pThreadParam,CallbackProc *_pCompletionFunc,void *_pCompletionParam,LPCTSTR pszName) : | 136 ThreadInfo (CallbackProc *_pThreadFunc,void *_pThreadParam,CallbackProc *_pCompletionFunc,void *_pCompletionParam,LPCTSTR pszName) : |
| 125 pThreadFunc(_pThreadFunc), | 137 pThreadFunc(_pThreadFunc), |
| 126 pThreadParam(_pThreadParam), | 138 pThreadParam(_pThreadParam), |
| 127 pCompletionFunc(_pCompletionFunc), | 139 pCompletionFunc(_pCompletionFunc), |
| 128 pCompletionParam(_pCompletionParam), | 140 pCompletionParam(_pCompletionParam), |
| 129 strName(pszName){} | 141 strName(pszName){} |
| 130 }; | 142 }; |
| 131 | 143 |
| 132 // Result type of the thread function | 144 // THREADFUNC is the result type of the thread function |
| 133 #ifdef _WIN32 | 145 #ifdef _WIN32 |
| 134 typedef unsigned long THREADFUNC; | 146 typedef unsigned long THREADFUNC; |
| 147 static int CALLBACK FilterFunction(LPEXCEPTION_POINTERS p); | |
| 135 #else // UNIX | 148 #else // UNIX |
| 136 typedef void * THREADFUNC; | 149 typedef void * THREADFUNC; |
| 137 #endif | 150 #endif |
| 138 static THREADFUNC CALLBACK SThreadFunc (void *pParam); | 151 static THREADFUNC CALLBACK SThreadFunc (void *pParam); |
| 139 | 152 |
