rllib  1
Classes | Macros | Typedefs | Functions
rlwthread.h File Reference
#include "rldefine.h"
#include <windows.h>
#include <winbase.h>
#include <stddef.h>
#include <string.h>
Include dependency graph for rlwthread.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  __sched_param
 
struct  pthread_attr_t
 
struct  WSEMAPHORE
 

Macros

#define WTREAD_GNUC10   ( __GNUC__ * 1000 ) + __GNUC_MINOR__
 
#define RLWIN32THREAD
 

Typedefs

typedef unsigned long int pthread_t
 
typedef struct __sched_param SCHED_PARAM
 
typedef HANDLE pthread_mutex_t
 
typedef long pthread_mutexattr_t
 

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)
 
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

◆ RLWIN32THREAD

#define RLWIN32THREAD

Definition at line 34 of file rlwthread.h.

◆ WTREAD_GNUC10

#define WTREAD_GNUC10   ( __GNUC__ * 1000 ) + __GNUC_MINOR__

Definition at line 32 of file rlwthread.h.

Typedef Documentation

◆ pthread_mutex_t

typedef HANDLE pthread_mutex_t

Definition at line 70 of file rlwthread.h.

◆ pthread_mutexattr_t

typedef long pthread_mutexattr_t

Definition at line 72 of file rlwthread.h.

◆ pthread_t

typedef unsigned long int pthread_t

Definition at line 49 of file rlwthread.h.

◆ SCHED_PARAM

typedef struct __sched_param SCHED_PARAM

Function Documentation

◆ rlsleep()

void rlsleep ( long  msec)

Definition at line 396 of file rlwthread.cpp.

397 {
398  rlwthread_sleep(msec);
399 }
int rlwthread_sleep(long msec)
Definition: rlwthread.cpp:366

◆ rlwrapdestroy_semaphore()

int rlwrapdestroy_semaphore ( WSEMAPHORE s)

Definition at line 294 of file rlwthread.cpp.

295 {
296 #ifdef RLWIN32THREAD
297  CloseHandle(s->hSemaphore);
298 #else
299  rlwthread_mutex_destroy(&s->mutex);
300 #endif
301  return 0;
302 }
int rlwthread_mutex_destroy(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:178
HANDLE hSemaphore
Definition: rlwthread.h:88

◆ rlwrapincrement_semaphore()

int rlwrapincrement_semaphore ( WSEMAPHORE s)

Definition at line 310 of file rlwthread.cpp.

311 {
312 /* Increment the count of the semaphore. */
313 #ifdef RLWIN32THREAD
314 
315  if(!ReleaseSemaphore(
316  s->hSemaphore, /* handle of semaphore */
317  1, /* increase count by one */
318  NULL) ) /* not interested in previous count */
319  {
320  return -1; /* Deal with the error. */
321  }
322  return 0;
323 
324 #else
325 
326  pthread_mutex_lock(&s->mutex);
327  if(s->nready == 0) pthread_cond_signal(&s->cond);
328  s->nready++;
329  pthread_mutex_unlock(&s->mutex);
330  return 0;
331 
332 #endif
333 }
HANDLE hSemaphore
Definition: rlwthread.h:88

◆ rlwrapinit_semaphore()

int rlwrapinit_semaphore ( WSEMAPHORE s,
int  cmax 
)

Definition at line 268 of file rlwthread.cpp.

269 {
270 /* Create a semaphore with initial count=0 max. counts of cmax. */
271 #ifdef RLWIN32THREAD
272 
273  s->cmax = cmax;
274  s->hSemaphore = CreateSemaphore(
275  NULL, /* no security attributes */
276  0, /* initial count */
277  cmax, /* maximum count */
278  NULL); /* unnamed semaphore */
279 
280  if(s->hSemaphore == NULL) return -1; /* Check for error. */
281  return 0;
282 
283 #else
284 
285  s->cmax = cmax;
286  s->nready = 0;
287  rlwthread_mutex_init(&s->mutex, NULL);
288  pthread_cond_init(&s->cond, NULL);
289  return 0;
290 
291 #endif
292 }
HANDLE hSemaphore
Definition: rlwthread.h:88
int rlwthread_mutex_init(pthread_mutex_t *mptr, const pthread_mutexattr_t *attr)
Definition: rlwthread.cpp:160
int cmax
Definition: rlwthread.h:87

◆ rlwrapwait_semaphore()

int rlwrapwait_semaphore ( WSEMAPHORE s)

Definition at line 341 of file rlwthread.cpp.

342 {
343 #ifdef RLWIN32THREAD
344 
345  int ret;
346  ret = WaitForSingleObject(
347  s->hSemaphore, /* handle of semaphore */
348  INFINITE); /* infinite time-out interval */
349  if(ret) return 0;
350  return 0;
351 
352 #else
353 
354  pthread_mutex_lock(&s->mutex);
355  while(s->nready == 0)
356  {
357  pthread_cond_wait(&s->cond,&s->mutex);
358  }
359  s->nready--;
360  pthread_mutex_unlock(&s->mutex);
361  return 0;
362 
363 #endif
364 }
HANDLE hSemaphore
Definition: rlwthread.h:88

◆ rlwthread_attr_init()

int rlwthread_attr_init ( pthread_attr_t attr)

Definition at line 36 of file rlwthread.cpp.

37 {
38 #ifdef RLWIN32THREAD
39  memset(attr,0,sizeof(pthread_attr_t));
40  return 0;
41 #else
42  return pthread_attr_init(attr);
43 #endif
44 }

◆ rlwthread_cancel()

int rlwthread_cancel ( pthread_t  tid)

Definition at line 253 of file rlwthread.cpp.

254 {
255 #ifdef RLWIN32THREAD
256  return (int) CloseHandle((HANDLE) tid);
257 #else
258  return pthread_cancel(tid);
259 #endif
260 }

◆ rlwthread_close_handle()

void rlwthread_close_handle ( pthread_t tid)

Definition at line 79 of file rlwthread.cpp.

80 {
81 #ifdef RLWIN32THREAD
82  CloseHandle((HANDLE) *tid);
83 #else
84  if(tid == NULL) return;
85 #endif
86 }

◆ rlwthread_create()

int rlwthread_create ( pthread_t tid,
const pthread_attr_t attr,
void *(*)(void *)  func,
void *  arg 
)

Definition at line 54 of file rlwthread.cpp.

56 {
57 #ifdef RLWIN32THREAD
58  HANDLE handle;
59  int ThreadId;
60  int dwStackSize = 0;
61  if(attr != NULL) dwStackSize = attr->__stacksize;
62  handle = CreateThread( NULL, /* pointer to thread security attributes */
63  dwStackSize, /* initial thread stack size, in bytes */
64  (LPTHREAD_START_ROUTINE)func, /* pointer to thread function */
65  arg, /* argument for new thread */
66  0, /* creation flags */
67  (unsigned long *) &ThreadId /* pointer to returned thread identifier */
68  );
69  *tid = (pthread_t) handle;
70  if(handle == NULL) return -1;
71  else return 0;
72 #else
73  int ret = pthread_create(tid,attr,func,arg);
74  pthread_detach(*tid);
75  return ret;
76 #endif
77 }
size_t __stacksize
Definition: rlwthread.h:67
unsigned long int pthread_t
Definition: rlwthread.h:49

◆ rlwthread_exit()

void rlwthread_exit ( void *  status)

Definition at line 94 of file rlwthread.cpp.

95 {
96 #ifdef RLWIN32THREAD
97  DWORD *ptr;
98  ptr = (DWORD *) status;
99  if(status == NULL) ExitThread((DWORD) 0);
100  else ExitThread(*ptr);
101 #else
102  pthread_exit(status);
103 #endif
104 }

◆ rlwthread_join()

int rlwthread_join ( pthread_t  tid,
void **  status 
)

Definition at line 113 of file rlwthread.cpp.

114 {
115 #ifdef RLWIN32THREAD
116 
117 #ifdef USE_OLD_JOIN
118  DWORD exitcode;
119  while(1)
120  {
121  GetExitCodeThread((HANDLE) tid,&exitcode);
122  if(exitcode != STILL_ACTIVE) return exitcode;
123  Sleep(10); /* sleep 10 msec */
124  }
125  if(status == NULL) return 0;
126 #else
127  int result = 1;
128  DWORD exitcode;
129  DWORD dwWait = WaitForSingleObject((HANDLE) tid, INFINITE);
130  if(dwWait == WAIT_OBJECT_0)
131  {
132  if(GetExitCodeThread((HANDLE) tid, &exitcode) == TRUE)
133  {
134  result = 0;
135  *status = (int) exitcode; // ???
136  }
137  else
138  {
139  result = GetLastError();
140  }
141  }
142  else if(dwWait == WAIT_FAILED)
143  {
144  result = GetLastError();
145  }
146  if(status) return result;
147  return result;
148 #endif
149 
150 #else
151  return pthread_join(tid,status);
152 #endif
153 }

◆ rlwthread_mutex_destroy()

int rlwthread_mutex_destroy ( pthread_mutex_t mptr)

Definition at line 178 of file rlwthread.cpp.

179 {
180 #ifdef RLWIN32THREAD
181  CloseHandle(*mptr);
182  //old DeleteCriticalSection(mptr);
183  return 0;
184 #else
185  return pthread_mutex_destroy(mptr);
186 #endif
187 }

◆ rlwthread_mutex_init()

int rlwthread_mutex_init ( pthread_mutex_t mptr,
const pthread_mutexattr_t attr 
)

Definition at line 160 of file rlwthread.cpp.

162 {
163 #ifdef RLWIN32THREAD
164  HANDLE handle = CreateMutex(NULL, FALSE, NULL);
165  if(handle) *mptr = handle;
166  //old InitializeCriticalSection(mptr);
167  if(attr) return 0;
168  return 0;
169 #else
170  return pthread_mutex_init(mptr,attr);
171 #endif
172 }

◆ rlwthread_mutex_lock()

int rlwthread_mutex_lock ( pthread_mutex_t mptr)

Definition at line 193 of file rlwthread.cpp.

194 {
195 #ifdef RLWIN32THREAD
196  if(WaitForSingleObject(*mptr, INFINITE) == WAIT_OBJECT_0) return 0;
197  //old EnterCriticalSection(mptr); // pointer to critical section object
198  return 0;
199 #else
200  return pthread_mutex_lock(mptr);
201 #endif
202 }

◆ rlwthread_mutex_trylock()

int rlwthread_mutex_trylock ( pthread_mutex_t mptr)

Definition at line 215 of file rlwthread.cpp.

216 {
217 #ifdef RLWIN32THREAD
218  DWORD ret;
219 
220  ret = WaitForSingleObject(*mptr, 0);
221  if(ret == WAIT_OBJECT_0) return 1;
222  return 0;
223  //old ret = TryEnterCriticalSection(mptr); // pointer to critical section object
224  return ret;
225 #else
226  int ret;
227 
228  ret = pthread_mutex_trylock(mptr);
229  if(ret == EBUSY) return 0;
230  return 1;
231 #endif
232 }

◆ rlwthread_mutex_unlock()

int rlwthread_mutex_unlock ( pthread_mutex_t mptr)

Definition at line 238 of file rlwthread.cpp.

239 {
240 #ifdef RLWIN32THREAD
241  ReleaseMutex(*mptr);
242  //old LeaveCriticalSection(mptr);
243  return 0;
244 #else
245  return pthread_mutex_unlock(mptr);
246 #endif
247 }

◆ rlwthread_sleep()

int rlwthread_sleep ( long  msec)

Definition at line 366 of file rlwthread.cpp.

367 {
368 #ifdef RLWIN32
369  Sleep(msec);
370  return 0;
371 #endif
372 
373 #ifdef RLUNIX
374  fd_set wset,rset,eset;
375  struct timeval timeout;
376 
377  FD_ZERO(&rset);
378  FD_ZERO(&wset);
379  FD_ZERO(&eset);
380  timeout.tv_sec = msec / 1000;
381  timeout.tv_usec = (msec % 1000) * 1000;
382  select(1,&rset,&wset,&eset,&timeout);
383  return 0;
384 #endif
385 
386 #ifdef __VMS
387  struct timespec interval;
388 
389  interval.tv_sec = msec / 1000;
390  interval.tv_nsec = (msec % 1000) * 1000 * 1000; /* wait msec msec */
391  pthread_delay_np(&interval);
392  return 0;
393 #endif
394 }