class wxMutexInternal
{
public:
- HANDLE p_mutex;
+ wxMutexInternal()
+ {
+ m_mutex = ::CreateMutex(NULL, FALSE, NULL);
+ if ( !m_mutex )
+ {
+ wxLogSysError(_("Can not create mutex"));
+ }
+ }
+
+ ~wxMutexInternal() { if ( m_mutex ) CloseHandle(m_mutex); }
+
+public:
+ HANDLE m_mutex;
};
wxMutex::wxMutex()
{
m_internal = new wxMutexInternal;
- m_internal->p_mutex = CreateMutex(NULL, FALSE, NULL);
- if ( !m_internal->p_mutex )
- {
- wxLogSysError(_("Can not create mutex."));
- }
m_locked = 0;
}
wxMutex::~wxMutex()
{
- if (m_locked > 0)
- wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked);
- CloseHandle(m_internal->p_mutex);
+ if ( m_locked > 0 )
+ {
+ wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked);
+ }
+
+ delete m_internal;
}
wxMutexError wxMutex::Lock()
{
DWORD ret;
- ret = WaitForSingleObject(m_internal->p_mutex, INFINITE);
+ ret = WaitForSingleObject(m_internal->m_mutex, INFINITE);
switch ( ret )
{
case WAIT_ABANDONED:
{
DWORD ret;
- ret = WaitForSingleObject(m_internal->p_mutex, 0);
+ ret = WaitForSingleObject(m_internal->m_mutex, 0);
if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED)
return wxMUTEX_BUSY;
if (m_locked > 0)
m_locked--;
- BOOL ret = ReleaseMutex(m_internal->p_mutex);
+ BOOL ret = ReleaseMutex(m_internal->m_mutex);
if ( ret == 0 )
{
wxLogSysError(_("Couldn't release a mutex"));
DWORD wxThreadInternal::WinThreadStart(wxThread *thread)
{
+ // first of all, check whether we hadn't been cancelled already
+ if ( thread->m_internal->GetState() == STATE_EXITED )
+ {
+ return (DWORD)-1;
+ }
+
// store the thread object in the TLS
if ( !::TlsSetValue(gs_tlsThisThread, thread) )
{
typedef unsigned (__stdcall *RtlThreadStart)(void *);
m_hThread = (HANDLE)_beginthreadex(NULL, 0,
- (RtlThreadStart)
+ (RtlThreadStart)
wxThreadInternal::WinThreadStart,
thread, CREATE_SUSPENDED,
(unsigned int *)&m_tid);
return ::GetCurrentThreadId() == gs_idMainThread;
}
+#ifdef Yield
+#undef Yield
+#endif
+
void wxThread::Yield()
{
// 0 argument to Sleep() is special and means to just give away the rest of
::Sleep(milliseconds);
}
+int wxThread::GetCPUCount()
+{
+ SYSTEM_INFO si;
+ GetSystemInfo(&si);
+
+ return si.dwNumberOfProcessors;
+}
+
+bool wxThread::SetConcurrency(size_t level)
+{
+ wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
+
+ // ok only for the default one
+ if ( level == 0 )
+ return 0;
+
+ // get system affinity mask first
+ HANDLE hProcess = ::GetCurrentProcess();
+ DWORD dwProcMask, dwSysMask;
+ if ( ::GetProcessAffinityMask(hProcess, &dwProcMask, &dwSysMask) == 0 )
+ {
+ wxLogLastError(_T("GetProcessAffinityMask"));
+
+ return FALSE;
+ }
+
+ // how many CPUs have we got?
+ if ( dwSysMask == 1 )
+ {
+ // don't bother with all this complicated stuff - on a single
+ // processor system it doesn't make much sense anyhow
+ return level == 1;
+ }
+
+ // calculate the process mask: it's a bit vector with one bit per
+ // processor; we want to schedule the process to run on first level
+ // CPUs
+ DWORD bit = 1;
+ while ( bit )
+ {
+ if ( dwSysMask & bit )
+ {
+ // ok, we can set this bit
+ dwProcMask |= bit;
+
+ // another process added
+ if ( !--level )
+ {
+ // and that's enough
+ break;
+ }
+ }
+
+ // next bit
+ bit <<= 1;
+ }
+
+ // could we set all bits?
+ if ( level != 0 )
+ {
+ wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level);
+
+ return FALSE;
+ }
+
+ // set it: we can't link to SetProcessAffinityMask() because it doesn't
+ // exist in Win9x, use RT binding instead
+
+ typedef BOOL (*SETPROCESSAFFINITYMASK)(HANDLE, DWORD);
+
+ // can use static var because we're always in the main thread here
+ static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask = NULL;
+
+ if ( !pfnSetProcessAffinityMask )
+ {
+ HMODULE hModKernel = ::LoadLibrary(_T("kernel32"));
+ if ( hModKernel )
+ {
+ pfnSetProcessAffinityMask = (SETPROCESSAFFINITYMASK)
+ ::GetProcAddress(hModKernel, _T("SetProcessAffinityMask"));
+ }
+
+ // we've discovered a MT version of Win9x!
+ wxASSERT_MSG( pfnSetProcessAffinityMask,
+ _T("this system has several CPUs but no "
+ "SetProcessAffinityMask function?") );
+ }
+
+ if ( !pfnSetProcessAffinityMask )
+ {
+ // msg given above - do it only once
+ return FALSE;
+ }
+
+ if ( pfnSetProcessAffinityMask(hProcess, dwProcMask) == 0 )
+ {
+ wxLogLastError(_T("SetProcessAffinityMask"));
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
// ctor and dtor
// -------------
ExitCode rc = 0;
// Delete() is always safe to call, so consider all possible states
- if ( IsPaused() )
+
+ // has the thread started to run?
+ bool shouldResume = FALSE;
+
+ {
+ wxCriticalSectionLocker lock(m_critsect);
+
+ if ( m_internal->GetState() == STATE_NEW )
+ {
+ // WinThreadStart() will see it and terminate immediately
+ m_internal->SetState(STATE_EXITED);
+
+ shouldResume = TRUE;
+ }
+ }
+
+ // is the thread paused?
+ if ( shouldResume || IsPaused() )
Resume();
HANDLE hThread = m_internal->GetHandle();
+ // does is still run?
if ( IsRunning() )
{
if ( IsMain() )