threads.c revision 8e461ea4a717b8ada3bcb586c0496c91b3222a5c
/* Copyright (c) 2001, Stanford University
* All rights reserved.
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#include <stdio.h>
#include "cr_threads.h"
#include "cr_error.h"
/* perror() messages */
#define INIT_TSD_ERROR "InitTSD: failed to allocate key"
#define FREE_TSD_ERROR "FreeTSD: failed to destroy key"
#define SET_TSD_ERROR "InitTSD: thread failed to set thread specific data"
#define GET_TSD_ERROR "InitTSD: failed to get thread specific data"
/* Magic number to determine if a CRtsd has been initialized */
#define INIT_MAGIC 0xff8adc98
/* Initialize a CRtsd */
{
#ifdef WINDOWS
crError("crInitTSD failed!");
}
(void) destructor;
#else
crDebug("crServer: failed to allocate TLS key.");
else
#endif
}
{
}
{
#ifdef WINDOWS
/* Windows returns true on success, 0 on failure */
crError("crFreeTSD failed!");
}
#else
else
#endif
}
/* Set thread-specific data */
{
/* initialize this CRtsd */
}
#ifdef WINDOWS
crError("crSetTSD failed!");
}
#else
crError("crSetTSD failed!");
}
#endif
}
/* Get thread-specific data */
{
#ifdef WINDOWS
void * value;
#endif
}
#ifdef WINDOWS
if (!value) {
err = GetLastError();
if ( err != ERROR_SUCCESS ) {
NULL,
err,
0, NULL );
}
}
return value;
#else
#endif
}
/* Return ID of calling thread */
unsigned long crThreadID(void)
{
#ifdef WINDOWS
return (unsigned long) GetCurrentThreadId();
#else
return (unsigned long) pthread_self();
#endif
}
{
#ifdef WINDOWS
#else
int rc;
#endif
}
{
#ifdef WINDOWS
#else
#endif
}
{
#ifdef WINDOWS
#else
#endif
}
{
#ifdef WINDOWS
#else
#endif
}
{
#ifdef WINDOWS
/* XXX fix me */
(void) cond;
#else
if (err) {
crError("crInitCondition failed");
}
#endif
}
{
#ifdef WINDOWS
/* XXX fix me */
(void) cond;
#else
if (err) {
crError("crFreeCondition error (threads waiting on the condition?)");
}
#endif
}
/**
* We're basically just wrapping the pthread condition var interface.
* See the man page for pthread_cond_wait to learn about the mutex parameter.
*/
{
#ifdef WINDOWS
/* XXX fix me */
(void) cond;
(void) mutex;
#else
#endif
}
{
#ifdef WINDOWS
/* XXX fix me */
(void) cond;
#else
#endif
}
{
#ifdef WINDOWS
unsigned int i;
for (i = 0; i < count; i++)
#else
b->waiting = 0;
#endif
}
void crFreeBarrier(CRbarrier *b)
{
/* XXX anything to do? */
}
void crWaitBarrier(CRbarrier *b)
{
#ifdef WINDOWS
#else
pthread_mutex_lock( &(b->mutex) );
b->waiting++;
}
else {
pthread_cond_broadcast( &(b->cond) );
b->waiting = 0;
}
pthread_mutex_unlock( &(b->mutex) );
#endif
}
{
#ifdef WINDOWS
crWarning("CRsemaphore functions not implemented on Windows");
#else
#endif
}
void crWaitSemaphore(CRsemaphore *s)
{
#ifdef WINDOWS
/* to do */
#else
sem_wait(s);
#endif
}
void crSignalSemaphore(CRsemaphore *s)
{
#ifdef WINDOWS
/* to do */
#else
sem_post(s);
#endif
}