# include <pthread.h>
typedef pthread_mutex_t Lock;
+typedef pthread_cond_t WaitCondition;
# define lock_init(x) pthread_mutex_init(x, NULL)
# define lock_grab(x) pthread_mutex_lock(x)
# define lock_release(x) pthread_mutex_unlock(x)
# define lock_destroy(x) pthread_mutex_destroy(x)
-
+# define cond_destroy(x) pthread_cond_destroy(x);
#else
#undef WIN32_LEAN_AND_MEAN
typedef CRITICAL_SECTION Lock;
+typedef HANDLE WaitCondition;
+
# define lock_init(x) InitializeCriticalSection(x)
# define lock_grab(x) EnterCriticalSection(x)
# define lock_release(x) LeaveCriticalSection(x)
# define lock_destroy(x) DeleteCriticalSection(x)
-
+# define cond_destroy(x) CloseHandle(*x);
#endif
int ActiveThreads;
volatile bool AllThreadsShouldExit, AllThreadsShouldSleep;
Thread threads[MAX_THREADS];
-
Lock MPLock, WaitLock;
-
-#if !defined(_MSC_VER)
- pthread_cond_t WaitCond[MAX_THREADS];
-#else
- HANDLE SitIdleEvent[MAX_THREADS];
-#endif
-
+ WaitCondition WaitCond[MAX_THREADS];
};
pthread_cond_wait(&WaitCond[threadID], &WaitLock);
lock_release(&WaitLock);
#else
- WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
+ WaitForSingleObject(WaitCond[threadID], INFINITE);
#endif
}
volatile int i;
bool ok;
-#if !defined(_MSC_VER)
- pthread_t pthread[1];
-#endif
-
// Initialize global locks
lock_init(&MPLock);
lock_init(&WaitLock);
#if !defined(_MSC_VER)
pthread_cond_init(&WaitCond[i], NULL);
#else
- SitIdleEvent[i] = CreateEvent(0, FALSE, FALSE, 0);
+ WaitCond[i] = CreateEvent(0, FALSE, FALSE, 0);
#endif
// Initialize splitPoints[] locks
{
#if !defined(_MSC_VER)
+ pthread_t pthread[1];
ok = (pthread_create(pthread, NULL, init_thread, (void*)(&i)) == 0);
#else
ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&i), 0, NULL) != NULL);
lock_destroy(&WaitLock);
lock_destroy(&MPLock);
+
+ // Now we can safely destroy the wait conditions
+ for (int i = 0; i < MAX_THREADS; i++)
+ cond_destroy(&WaitCond[i]);
}
pthread_cond_signal(&WaitCond[threadID]);
pthread_mutex_unlock(&WaitLock);
#else
- SetEvent(SitIdleEvent[threadID]);
+ SetEvent(WaitCond[threadID]);
#endif
}