rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions
rlwthread.cpp File Reference
#include <errno.h>
#include "rlwthread.h"
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
Include dependency graph for rlwthread.cpp:

Go to the source code of this file.

Macros

#define USE_OLD_JOIN
 
#define TESTINGx
 

Functions

int rlwthread_attr_init (pthread_attr_t *attr)
 
int rlwthread_create (pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg)
 
void rlwthread_close_handle (pthread_t *tid)
 
void rlwthread_exit (void *status)
 
int rlwthread_join (pthread_t tid, void **status)
 
int rlwthread_mutex_init (pthread_mutex_t *mptr, const pthread_mutexattr_t *attr)
 
int rlwthread_mutex_destroy (pthread_mutex_t *mptr)
 
int rlwthread_mutex_lock (pthread_mutex_t *mptr)
 
WINBASEAPI BOOL WINAPI TryEnterCriticalSection (LPCRITICAL_SECTION lpCriticalSection)
 
int rlwthread_mutex_trylock (pthread_mutex_t *mptr)
 
int rlwthread_mutex_unlock (pthread_mutex_t *mptr)
 
int rlwthread_cancel (pthread_t tid)
 
int rlwrapinit_semaphore (WSEMAPHORE *s, int cmax)
 
int rlwrapdestroy_semaphore (WSEMAPHORE *s)
 
int rlwrapincrement_semaphore (WSEMAPHORE *s)
 
int rlwrapwait_semaphore (WSEMAPHORE *s)
 
int rlwthread_sleep (long msec)
 
void rlsleep (long msec)
 

Macro Definition Documentation

#define TESTINGx

Definition at line 404 of file rlwthread.cpp.

#define USE_OLD_JOIN

Definition at line 112 of file rlwthread.cpp.

Function Documentation

void rlsleep ( long  msec)

Definition at line 396 of file rlwthread.cpp.

{
}
int rlwrapdestroy_semaphore ( WSEMAPHORE s)

Definition at line 294 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
CloseHandle(s->hSemaphore);
#else
#endif
return 0;
}
int rlwrapincrement_semaphore ( WSEMAPHORE s)

Definition at line 310 of file rlwthread.cpp.

{
/* Increment the count of the semaphore. */
#ifdef RLWIN32THREAD
if(!ReleaseSemaphore(
s->hSemaphore, /* handle of semaphore */
1, /* increase count by one */
NULL) ) /* not interested in previous count */
{
return -1; /* Deal with the error. */
}
return 0;
#else
pthread_mutex_lock(&s->mutex);
if(s->nready == 0) pthread_cond_signal(&s->cond);
s->nready++;
pthread_mutex_unlock(&s->mutex);
return 0;
#endif
}
int rlwrapinit_semaphore ( WSEMAPHORE s,
int  cmax 
)

Definition at line 268 of file rlwthread.cpp.

{
/* Create a semaphore with initial count=0 max. counts of cmax. */
#ifdef RLWIN32THREAD
s->cmax = cmax;
s->hSemaphore = CreateSemaphore(
NULL, /* no security attributes */
0, /* initial count */
cmax, /* maximum count */
NULL); /* unnamed semaphore */
if(s->hSemaphore == NULL) return -1; /* Check for error. */
return 0;
#else
s->cmax = cmax;
s->nready = 0;
rlwthread_mutex_init(&s->mutex, NULL);
pthread_cond_init(&s->cond, NULL);
return 0;
#endif
}
int rlwrapwait_semaphore ( WSEMAPHORE s)

Definition at line 341 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
int ret;
ret = WaitForSingleObject(
s->hSemaphore, /* handle of semaphore */
INFINITE); /* infinite time-out interval */
if(ret) return 0;
return 0;
#else
pthread_mutex_lock(&s->mutex);
while(s->nready == 0)
{
pthread_cond_wait(&s->cond,&s->mutex);
}
s->nready--;
pthread_mutex_unlock(&s->mutex);
return 0;
#endif
}
int rlwthread_attr_init ( pthread_attr_t attr)

Definition at line 36 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
memset(attr,0,sizeof(pthread_attr_t));
return 0;
#else
return pthread_attr_init(attr);
#endif
}
int rlwthread_cancel ( pthread_t  tid)

Definition at line 253 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
return (int) CloseHandle((HANDLE) tid);
#else
return pthread_cancel(tid);
#endif
}
void rlwthread_close_handle ( pthread_t tid)

Definition at line 79 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
CloseHandle((HANDLE) *tid);
#else
if(tid == NULL) return;
#endif
}
int rlwthread_create ( pthread_t tid,
const pthread_attr_t attr,
void *(*)(void *)  func,
void *  arg 
)

Definition at line 54 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
HANDLE handle;
int ThreadId;
int dwStackSize = 0;
if(attr != NULL) dwStackSize = attr->__stacksize;
handle = CreateThread( NULL, /* pointer to thread security attributes */
dwStackSize, /* initial thread stack size, in bytes */
(LPTHREAD_START_ROUTINE)func, /* pointer to thread function */
arg, /* argument for new thread */
0, /* creation flags */
(unsigned long *) &ThreadId /* pointer to returned thread identifier */
);
*tid = (pthread_t) handle;
if(handle == NULL) return -1;
else return 0;
#else
int ret = pthread_create(tid,attr,func,arg);
pthread_detach(*tid);
return ret;
#endif
}
void rlwthread_exit ( void *  status)

Definition at line 94 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
DWORD *ptr;
ptr = (DWORD *) status;
if(status == NULL) ExitThread((DWORD) 0);
else ExitThread(*ptr);
#else
pthread_exit(status);
#endif
}
int rlwthread_join ( pthread_t  tid,
void **  status 
)

Definition at line 113 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
#ifdef USE_OLD_JOIN
DWORD exitcode;
while(1)
{
GetExitCodeThread((HANDLE) tid,&exitcode);
if(exitcode != STILL_ACTIVE) return exitcode;
Sleep(10); /* sleep 10 msec */
}
if(status == NULL) return 0;
#else
int result = 1;
DWORD exitcode;
DWORD dwWait = WaitForSingleObject((HANDLE) tid, INFINITE);
if(dwWait == WAIT_OBJECT_0)
{
if(GetExitCodeThread((HANDLE) tid, &exitcode) == TRUE)
{
result = 0;
*status = (int) exitcode; // ???
}
else
{
result = GetLastError();
}
}
else if(dwWait == WAIT_FAILED)
{
result = GetLastError();
}
if(status) return result;
return result;
#endif
#else
return pthread_join(tid,status);
#endif
}
int rlwthread_mutex_destroy ( pthread_mutex_t mptr)

Definition at line 178 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
CloseHandle(*mptr);
//old DeleteCriticalSection(mptr);
return 0;
#else
return pthread_mutex_destroy(mptr);
#endif
}
int rlwthread_mutex_init ( pthread_mutex_t mptr,
const pthread_mutexattr_t attr 
)

Definition at line 160 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
HANDLE handle = CreateMutex(NULL, FALSE, NULL);
if(handle) *mptr = handle;
//old InitializeCriticalSection(mptr);
if(attr) return 0;
return 0;
#else
return pthread_mutex_init(mptr,attr);
#endif
}
int rlwthread_mutex_lock ( pthread_mutex_t mptr)

Definition at line 193 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
if(WaitForSingleObject(*mptr, INFINITE) == WAIT_OBJECT_0) return 0;
//old EnterCriticalSection(mptr); // pointer to critical section object
return 0;
#else
return pthread_mutex_lock(mptr);
#endif
}
int rlwthread_mutex_trylock ( pthread_mutex_t mptr)

Definition at line 215 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
DWORD ret;
ret = WaitForSingleObject(*mptr, 0);
if(ret == WAIT_OBJECT_0) return 1;
return 0;
//old ret = TryEnterCriticalSection(mptr); // pointer to critical section object
return ret;
#else
int ret;
ret = pthread_mutex_trylock(mptr);
if(ret == EBUSY) return 0;
return 1;
#endif
}
int rlwthread_mutex_unlock ( pthread_mutex_t mptr)

Definition at line 238 of file rlwthread.cpp.

{
#ifdef RLWIN32THREAD
ReleaseMutex(*mptr);
//old LeaveCriticalSection(mptr);
return 0;
#else
return pthread_mutex_unlock(mptr);
#endif
}
int rlwthread_sleep ( long  msec)

Definition at line 366 of file rlwthread.cpp.

{
#ifdef RLWIN32
Sleep(msec);
return 0;
#endif
#ifdef RLUNIX
fd_set wset,rset,eset;
struct timeval timeout;
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
timeout.tv_sec = msec / 1000;
timeout.tv_usec = (msec % 1000) * 1000;
select(1,&rset,&wset,&eset,&timeout);
return 0;
#endif
#ifdef __VMS
struct timespec interval;
interval.tv_sec = msec / 1000;
interval.tv_nsec = (msec % 1000) * 1000 * 1000; /* wait msec msec */
pthread_delay_np(&interval);
return 0;
#endif
}
WINBASEAPI BOOL WINAPI TryEnterCriticalSection ( LPCRITICAL_SECTION  lpCriticalSection)