File: threads.h

package info (click to toggle)
udpcast 20120424-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 876 kB
  • ctags: 1,011
  • sloc: ansic: 7,713; sh: 2,838; perl: 227; makefile: 114
file content (125 lines) | stat: -rw-r--r-- 2,796 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef UDPCTHREADS_H
#define UDPCTHREADS_H

#ifdef __MINGW32__

#include "socklib.h"
#include <winsock2.h>
#include <winbase.h>
#include <sys/time.h>
typedef HANDLE pthread_t;
typedef CRITICAL_SECTION pthread_mutex_t;
typedef HANDLE pthread_cond_t;

struct timespec {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};

static inline int pthread_create(pthread_t *thread, void *dummy1,
				 LPTHREAD_START_ROUTINE start_routine, 
				 void *arg) {
  /* Start thread ... 
     * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp
     */	       
    *thread = CreateThread(NULL,	/* lpThreadAttributes */
			   0,	/* dwStackSize */
			   start_routine,
			   arg,	/* lpParameter */
			   0,	/* dwCreationFlags */
			   NULL    /* lpThreadId */);
    return *thread != NULL ? 0 : -1;
}

static inline int pthread_join(pthread_t th, void **thread_return) {
  return WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0 ? 0 : -1;
}

static inline int pthread_mutex_init(pthread_mutex_t  *mutex, void *dummy) {
  InitializeCriticalSection(mutex);
  return 0;
}

static inline int pthread_mutex_lock(pthread_mutex_t  *mutex) {
  EnterCriticalSection(mutex);
  return 0;
}

static inline int pthread_mutex_unlock(pthread_mutex_t  *mutex) {
  LeaveCriticalSection(mutex);
  return 0;
}


static inline int pthread_cond_init(pthread_cond_t  *cond, void *dummy) {
  *cond = CreateEvent(NULL, TRUE, TRUE, NULL);
    if(*cond == NULL)
      return -1;
    else
      return 0;
}

static inline int pthread_cond_signal(pthread_cond_t  *cond) {
  return SetEvent(*cond) ? 0 : -1;
}

static inline int pthread_cond_wait(pthread_cond_t  *cond, 
				    pthread_mutex_t *mutex) {
  int r;
  ResetEvent(*cond);
  LeaveCriticalSection(mutex);
  r= WaitForSingleObject(*cond, INFINITE) == WAIT_OBJECT_0 ? 0 : -1;
  EnterCriticalSection(mutex);
  return r;
}

static inline void pthread_cancel(pthread_t *thread)
{
  TerminateThread(thread, 0);
}

#define ETIMEDOUT -2
#define MILLION    1000000
#define BILLION 1000000000

static inline int pthread_cond_timedwait(pthread_cond_t  *cond, 
					 pthread_mutex_t *mutex,
					 struct timespec *ts) {
  int r;
  struct timeval tv;
  long delta;

  gettimeofday(&tv, NULL);

  delta = (ts->tv_sec - tv.tv_sec) * 1000 + 
    (ts->tv_nsec / BILLION - tv.tv_usec / MILLION);
  if(delta < 0)
    delta = 0;

  ResetEvent(*cond);
  LeaveCriticalSection(mutex);
  
  switch(WaitForSingleObject(*cond, delta )) {
  case WAIT_OBJECT_0:
    r=0;
    break;
  case WAIT_TIMEOUT:
    r=ETIMEDOUT;
    break;
  default:
    r=-1;
    break;
  }
  EnterCriticalSection(mutex);
  return  r;
}

#define THREAD_RETURN DWORD WINAPI


#else /* __MINGW32__ */
#include <pthread.h>
#define THREAD_RETURN void *
#endif /* __MINGW32__ */

#endif