Commit bf96cc34 authored by sof's avatar sof

[project @ 2002-02-04 20:18:26 by sof]

- renamed MutexVar to Mutex, CondVar to Condition.
- added yieldThread().
- simplified/specialised type of entry point to new (native) threads;
  now takes no args & returns no result.
parent 237d1510
......@@ -20,52 +20,75 @@
*
*/
void initCondVar( CondVar* pCond )
void
initCondition( Condition* pCond )
{
pthread_cond_init(pCond, NULL);
return;
}
void closeCondVar( CondVar* pCond )
void
closeCondition( Condition* pCond )
{
pthread_cond_destroy(pCond);
return;
}
rtsBool
broadcastCondVar ( CondVar* pCond )
broadcastCondition ( Condition* pCond )
{
return (pthread_cond_broadcast(pCond) == 0);
}
rtsBool
signalCondVar ( CondVar* pCond )
signalCondition ( Condition* pCond )
{
return (pthread_cond_signal(pCond) == 0);
}
rtsBool
waitCondVar ( CondVar* pCond, MutexVar* pMut )
waitCondition ( Condition* pCond, Mutex* pMut )
{
return (pthread_cond_wait(pCond,pMut) == 0);
}
void shutdownThread()
void
yieldThread()
{
sched_yield();
return;
}
void
shutdownThread()
{
pthread_exit(NULL);
}
int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
/* Don't need the argument nor the result, at least not yet. */
static void* startProcWrapper(void* pProc);
static void*
startProcWrapper(void* pProc)
{
((void (*)(void))pProc)();
return NULL;
}
int
createOSThread ( OSThreadId* pId, void (*startProc)(void))
{
return pthread_create(pId, NULL, startProc, NULL);
return pthread_create(pId, NULL, startProcWrapper, (void*)startProc);
}
OSThreadId osThreadId()
OSThreadId
osThreadId()
{
return pthread_self();
}
void initMutexVar (MutexVar* pMut)
void
initMutex(Mutex* pMut)
{
pthread_mutex_init(pMut,NULL);
return;
......@@ -76,8 +99,8 @@ void initMutexVar (MutexVar* pMut)
/* Win32 threads and synchronisation objects */
/* A CondVar is represented by a Win32 Event object,
* a MutexVar by a Mutex kernel object.
/* A Condition is represented by a Win32 Event object;
* a Mutex by a Mutex kernel object.
*
* ToDo: go through the defn and usage of these to
* make sure the semantics match up with that of
......@@ -85,7 +108,8 @@ void initMutexVar (MutexVar* pMut)
* just a first pass at getting something compilable.
*/
void initCondVar( CondVar* pCond )
void
initCondition( Condition* pCond )
{
HANDLE h = CreateEvent(NULL,
TRUE, /* manual reset */
......@@ -93,36 +117,37 @@ void initCondVar( CondVar* pCond )
NULL); /* unnamed => process-local. */
if ( h == NULL ) {
belch("initCondVar: unable to create");
belch("initCondition: unable to create");
}
*pCond = h;
return;
}
void closeCondVar( CondVar* pCond )
void
closeCondition( Condition* pCond )
{
if ( CloseHandle(*pCond) == 0 ) {
belch("closeCondVar: failed to close");
belch("closeCondition: failed to close");
}
return;
}
rtsBool
broadcastCondVar ( CondVar* pCond )
broadcastCondition ( Condition* pCond )
{
PulseEvent(*pCond);
return rtsTrue;
}
rtsBool
signalCondVar ( CondVar* pCond )
signalCondition ( Condition* pCond )
{
SetEvent(*pCond);
return rtsTrue;
}
rtsBool
waitCondVar ( CondVar* pCond, MutexVar* pMut )
waitCondition ( Condition* pCond, Mutex* pMut )
{
ReleaseMutex(*pMut);
WaitForSingleObject(*pCond, INFINITE);
......@@ -131,19 +156,22 @@ waitCondVar ( CondVar* pCond, MutexVar* pMut )
return rtsTrue;
}
void shutdownThread()
void
shutdownThread()
{
_endthreadex(0);
}
static unsigned __stdcall startProcWrapper(void* pReal);
static unsigned __stdcall startProcWrapper(void* pReal)
static unsigned __stdcall
startProcWrapper(void* pReal)
{
((void (*)(void*))pReal)(NULL);
((void (*)(void))pReal)();
return 0;
}
int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
int
createOSThread ( OSThreadId* pId, void (*startProc)(void*))
{
return _beginthreadex ( NULL, /* default security attributes */
......@@ -154,12 +182,14 @@ int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
(unsigned*)pId);
}
OSThreadId osThreadId()
OSThreadId
osThreadId()
{
return GetCurrentThreadId();
}
void initMutexVar (MutexVar* pMut)
void
initMutex (Mutex* pMut)
{
HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
FALSE, /* not owned => initially signalled */
......
......@@ -11,37 +11,53 @@
#define __OSTHREADS_H__
#if defined(RTS_SUPPORTS_THREADS) /*to the end */
#if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
#include <pthread.h>
typedef pthread_cond_t CondVar;
typedef pthread_mutex_t MutexVar;
# if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
# include <pthread.h>
typedef pthread_cond_t Condition;
typedef pthread_mutex_t Mutex;
typedef pthread_t OSThreadId;
#define INIT_MUTEX_VAR PTHREAD_MUTEX_INITIALIZER
#define INIT_COND_VAR PTHREAD_COND_INITIALIZER
#elif defined(HAVE_WINDOWS_H)
#define INIT_MUTEX_VAR PTHREAD_MUTEX_INITIALIZER
#define INIT_COND_VAR PTHREAD_COND_INITIALIZER
#define ACQUIRE_LOCK(mutex) pthread_mutex_lock(mutex)
#define RELEASE_LOCK(mutex) pthread_mutex_unlock(mutex)
# elif defined(HAVE_WINDOWS_H)
#include <windows.h>
typedef HANDLE CondVar;
typedef HANDLE MutexVar;
typedef HANDLE Condition;
typedef HANDLE Mutex;
typedef DWORD OSThreadId;
#define INIT_MUTEX_VAR 0
#define INIT_COND_VAR 0
#else
#error "Threads not supported"
#endif
extern void initCondVar ( CondVar* pCond );
extern void closeCondVar ( CondVar* pCond );
extern rtsBool broadcastCondVar (CondVar* pCond );
extern rtsBool signalCondVar ( CondVar* pCond );
extern rtsBool waitCondVar ( CondVar* pCond, MutexVar* pMut);
#define ACQURE_LOCK(mutex) WaitForSingleObject(mutex,INFINITE)
#define RELEASE_LOCK(mutex) ReleaseMutex(handle)
#define
# else
# error "Threads not supported"
# endif
extern OSThreadId osThreadId(void);
extern void shutdownThread(void);
extern int createOSThread ( OSThreadId* tid, void *(*startProc)(void*));
extern void initCondition ( Condition* pCond );
extern void closeCondition ( Condition* pCond );
extern rtsBool broadcastCondition ( Condition* pCond );
extern rtsBool signalCondition ( Condition* pCond );
extern rtsBool waitCondition ( Condition* pCond,
Mutex* pMut );
extern void initMutex ( Mutex* pMut );
extern OSThreadId osThreadId ( void );
extern void shutdownThread ( void );
extern void yieldThread ( void );
extern int createOSThread ( OSThreadId* tid,
void (*startProc)(void) );
#else
extern void initMutexVar ( MutexVar* pMut );
#define ACQUIRE_LOCK(l)
#define RELEASE_LOCK(l)
#endif /* defined(RTS_SUPPORTS_THREADS) */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment