X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b568d04ffa191f9e3b643ca33526094eca0ba304..9085d634d9e29a877761ef51a01c740749648e33:/include/wx/thread.h?ds=inline diff --git a/include/wx/thread.h b/include/wx/thread.h index 3f1415adc7..60f5c7c5c4 100644 --- a/include/wx/thread.h +++ b/include/wx/thread.h @@ -40,21 +40,21 @@ enum wxMutexError { - wxMUTEX_NO_ERROR = 0, - wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread - wxMUTEX_BUSY, // Mutex has been already locked by ONE thread - wxMUTEX_UNLOCKED, - wxMUTEX_MISC_ERROR + wxMUTEX_NO_ERROR = 0, + wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread + wxMUTEX_BUSY, // Mutex has been already locked by ONE thread + wxMUTEX_UNLOCKED, + wxMUTEX_MISC_ERROR }; enum wxThreadError { - wxTHREAD_NO_ERROR = 0, // No error - wxTHREAD_NO_RESOURCE, // No resource left to create a new thread - wxTHREAD_RUNNING, // The thread is already running - wxTHREAD_NOT_RUNNING, // The thread isn't running - wxTHREAD_KILLED, // Thread we waited for had to be killed - wxTHREAD_MISC_ERROR // Some other error + wxTHREAD_NO_ERROR = 0, // No error + wxTHREAD_NO_RESOURCE, // No resource left to create a new thread + wxTHREAD_RUNNING, // The thread is already running + wxTHREAD_NOT_RUNNING, // The thread isn't running + wxTHREAD_KILLED, // Thread we waited for had to be killed + wxTHREAD_MISC_ERROR // Some other error }; enum wxThreadKind @@ -80,6 +80,7 @@ enum // you should consider wxMutexLocker whenever possible instead of directly // working with wxMutex class - it is safer +class WXDLLEXPORT wxConditionInternal; class WXDLLEXPORT wxMutexInternal; class WXDLLEXPORT wxMutex { @@ -99,14 +100,14 @@ public: bool IsLocked() const { return (m_locked > 0); } protected: - friend class wxCondition; - // no assignment operator nor copy ctor wxMutex(const wxMutex&); wxMutex& operator=(const wxMutex&); int m_locked; - wxMutexInternal *p_internal; + wxMutexInternal *m_internal; + + friend class wxConditionInternal; }; // a helper class which locks the mutex in the ctor and unlocks it in the dtor: @@ -150,11 +151,11 @@ class WXDLLEXPORT wxCriticalSectionInternal; // in order to avoid any overhead under platforms where critical sections are // just mutexes make all wxCriticalSection class functions inline -#if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__) +#if !defined(__WXMSW__) && !defined(__WXPM__) #define WXCRITICAL_INLINE inline #define wxCRITSECT_IS_MUTEX 1 -#else // MSW || Mac || OS2 +#else // MSW || OS2 #define WXCRITICAL_INLINE #define wxCRITSECT_IS_MUTEX 0 @@ -213,32 +214,97 @@ private: }; // ---------------------------------------------------------------------------- -// Condition handler. +// wxCondition models a POSIX condition variable which allows one (or more) +// thread(s) to wait until some condition is fulfilled // ---------------------------------------------------------------------------- -class wxConditionInternal; class WXDLLEXPORT wxCondition { public: - // constructor & destructor - wxCondition(); - ~wxCondition(); - - // Waits indefinitely. - void Wait(wxMutex& mutex); - // Waits until a signal is raised or the timeout is elapsed. - bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec); - // Raises a signal: only one "Waiter" is released. - void Signal(); - // Broadcasts to all "Waiters". - void Broadcast(); + // constructor and destructor + + // Each wxCondition object is associated with with a wxMutex object The + // mutex object MUST be locked before calling Wait() + wxCondition(wxMutex& mutex); + + // dtor is not virtual, don't use this class polymorphically + ~wxCondition(); + + // NB: the associated mutex MUST be locked beforehand by the calling thread + // + // it atomically releases the lock on the associated mutex + // and starts waiting to be woken up by a Signal()/Broadcast() + // once its signaled, then it will wait until it can reacquire + // the lock on the associated mutex object, before returning. + void Wait(); + + // exactly as Wait() except that it may also return if the specified + // timeout ellapses even if the condition hasn't been signalled: in this + // case, the return value is FALSE, otherwise (i.e. in case of a normal + // return) it is TRUE + // + // the timeeout parameter specifies a interval that needs to be waited in + // milliseconds + bool Wait( unsigned long timeout_millis ); + + // NB: the associated mutex may or may not be locked by the calling thread + // + // this method unblocks one thread if any are blocking on the condition. + // if no thread is blocking in Wait(), then the signal is NOT remembered + // The thread which was blocking on Wait(), will then reacquire the lock + // on the associated mutex object before returning + void Signal(); + + // NB: the associated mutex may or may not be locked by the calling thread + // + // this method unblocks all threads if any are blocking on the condition. + // if no thread is blocking in Wait(), then the signal is NOT remembered + // The threads which were blocking on Wait(), will then reacquire the lock + // on the associated mutex object before returning. + void Broadcast(); private: - wxConditionInternal *p_internal; + wxConditionInternal *m_internal; }; // ---------------------------------------------------------------------------- -// Thread class +// wxSemaphore: a counter limiting the number of threads concurrently accessing +// a shared resource +// ---------------------------------------------------------------------------- + +class WXDLLEXPORT wxSemaphoreInternal; +class WXDLLEXPORT wxSemaphore +{ +public: + // specifying a maxcount of 0 actually makes wxSemaphore behave as if there + // is no upper limit, if maxcount is 1 the semaphore behaves as a mutex + wxSemaphore( int initialcount = 0, int maxcount = 0 ); + + // dtor is not virtual, don't use this class polymorphically + ~wxSemaphore(); + + // wait indefinitely, until the semaphore count goes beyond 0 + // and then decrement it and return (this method might have been called + // Acquire()) + void Wait(); + + // same as Wait(), but does not block, returns TRUE if successful and + // FALSE if the count is zero + bool TryWait(); + + // same as Wait(), but as a timeout limit, returns TRUE if the semaphore + // was acquired and FALSE if the timeout has ellapsed + bool Wait( unsigned long timeout_millis ); + + // increments the semaphore count and signals one of the waiting threads + void Post(); + +private: + wxSemaphoreInternal *m_internal; +}; + +// ---------------------------------------------------------------------------- +// wxThread: class encpasulating a thread of execution // ---------------------------------------------------------------------------- // there are two different kinds of threads: joinable and detached (default) @@ -250,6 +316,13 @@ private: // created by the wxThread object while "main thread" is the thread created // during the process initialization (a.k.a. the GUI thread) +// On VMS thread pointers are 64 bits (also needed for other systems??? +#ifdef __VMS + typedef unsigned long long wxThreadIdType; +#else + typedef unsigned long wxThreadIdType; +#endif + class wxThreadInternal; class WXDLLEXPORT wxThread { @@ -276,6 +349,26 @@ public: // NB: at least under MSW worker threads can not call ::wxSleep()! static void Sleep(unsigned long milliseconds); + // get the number of system CPUs - useful with SetConcurrency() + // (the "best" value for it is usually number of CPUs + 1) + // + // Returns -1 if unknown, number of CPUs otherwise + static int GetCPUCount(); + + // Get the platform specific thread ID and return as a long. This + // can be used to uniquely identify threads, even if they are not + // wxThreads. This is used by wxPython. + static wxThreadIdType GetCurrentId(); + + // sets the concurrency level: this is, roughly, the number of threads + // the system tries to schedule to run in parallel. 0 means the + // default value (usually acceptable, but may not yield the best + // performance for this process) + // + // Returns TRUE on success, FALSE otherwise (if not implemented, for + // example) + static bool SetConcurrency(size_t level); + // constructor only creates the C++ thread object and doesn't create (or // start) the real thread wxThread(wxThreadKind kind = wxTHREAD_DETACHED); @@ -284,8 +377,11 @@ public: // from _another_ thread (typically the thread that created this one, e.g. // the main thread), not from the thread itself - // create a new thread - call Run() to start it - wxThreadError Create(); + // create a new thread and optionally set the stack size on + // platforms that support that - call Run() to start it + // (special cased for watcom which won't accept 0 default) + + wxThreadError Create(unsigned int stackSize = 0); // starts execution of the thread - from the moment Run() is called // the execution of wxThread::Entry() may start at any moment, caller @@ -349,7 +445,7 @@ public: // Get the thread ID - a platform dependent number which uniquely // identifies a thread inside a process - unsigned long GetId() const; + wxThreadIdType GetId() const; // called when the thread exits - in the context of this thread // @@ -381,7 +477,7 @@ private: friend class wxThreadInternal; // the (platform-dependent) thread class implementation - wxThreadInternal *p_internal; + wxThreadInternal *m_internal; // protects access to any methods of wxThreadInternal object wxCriticalSection m_critsect; @@ -402,6 +498,7 @@ void WXDLLEXPORT wxMutexGuiLeave(); // having to take them inside "#if wxUSE_THREADS" #define wxENTER_CRIT_SECT(cs) (cs).Enter() #define wxLEAVE_CRIT_SECT(cs) (cs).Leave() +#define wxCRIT_SECT_DECLARE(cs) static wxCriticalSection cs #define wxCRIT_SECT_LOCKER(name, cs) wxCriticalSectionLocker name(cs) #else // !wxUSE_THREADS @@ -416,6 +513,7 @@ inline void WXDLLEXPORT wxMutexGuiLeave() { } // having to take them inside "#if wxUSE_THREADS" #define wxENTER_CRIT_SECT(cs) #define wxLEAVE_CRIT_SECT(cs) +#define wxCRIT_SECT_DECLARE(cs) #define wxCRIT_SECT_LOCKER(name, cs) #endif // wxUSE_THREADS @@ -450,7 +548,7 @@ public: // wxApp then should block all "dangerous" messages extern bool WXDLLEXPORT wxIsWaitingForThread(); #elif defined(__WXMAC__) - extern void WXDLLEXPORT wxMutexGuiLeaveOrEnter(); + extern void WXDLLEXPORT wxMutexGuiLeaveOrEnter(); // returns TRUE if the main thread has GUI lock extern bool WXDLLEXPORT wxGuiOwnedByMainThread(); @@ -461,6 +559,13 @@ public: // return TRUE if the main thread is waiting for some other to terminate: // wxApp then should block all "dangerous" messages extern bool WXDLLEXPORT wxIsWaitingForThread(); + + // implement wxCriticalSection using mutexes + inline wxCriticalSection::wxCriticalSection() { } + inline wxCriticalSection::~wxCriticalSection() { } + + inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); } + inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); } #elif defined(__WXPM__) // unlock GUI if there are threads waiting for and lock it back when // there are no more of them - should be called periodically by the main @@ -493,3 +598,4 @@ public: #endif // wxUSE_THREADS #endif // __THREADH__ +