Welcome to mirror list, hosted at ThFree Co, Russian Federation.

THThread.c « lib - github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6951cafc32597f7bf932c37dde5b04fc7d6167f8 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdlib.h>
#include <string.h>

#include "TH.h"
#include "THThread.h"

#if defined(USE_PTHREAD_THREADS)
#include <pthread.h>

#elif defined(USE_WIN32_THREADS)

/* very basic emulation to suit our needs */

#include <process.h>
#include <windows.h>

typedef HANDLE pthread_t;
typedef DWORD pthread_attr_t;
typedef HANDLE pthread_mutex_t;
typedef HANDLE pthread_cond_t;
typedef HANDLE pthread_mutexattr_t;
typedef HANDLE pthread_condattr_t;
typedef unsigned ( __stdcall *THREAD_FUNCTION )( void * );
#define restrict __restrict

static int pthread_create(pthread_t *restrict thread,
                          const pthread_attr_t *restrict attr, void *(*start_routine)(void *),
                          void *restrict arg)
{
  *thread = (HANDLE)_beginthreadex(NULL, 0, (THREAD_FUNCTION)start_routine, arg, 0, NULL);
  return (int)(*thread == NULL);
}

static int pthread_join(pthread_t thread, void **value_ptr)
{
  return ((WaitForSingleObject((thread), INFINITE) != WAIT_OBJECT_0) || !CloseHandle(thread));
}

static int pthread_mutex_init(pthread_mutex_t *restrict mutex,
                              const pthread_mutexattr_t *restrict attr)
{
  *mutex = CreateMutex(NULL, FALSE, NULL);
  return (int)(*mutex == NULL);
}

static int pthread_mutex_lock(pthread_mutex_t *mutex)
{
  return WaitForSingleObject(*mutex, INFINITE) != 0;
}

static int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
  return ReleaseMutex(*mutex) == 0;
}

static int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
  return CloseHandle(*mutex) == 0;
}

static int pthread_cond_init(pthread_cond_t *restrict cond,
                             const pthread_condattr_t *restrict attr)
{
  *cond = CreateEvent(NULL, FALSE, FALSE, NULL);
  return (int)(*cond == NULL);
}

static int pthread_cond_wait(pthread_cond_t *restrict cond,
                             pthread_mutex_t *restrict mutex)
{
  SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE);
  return WaitForSingleObject(*mutex, INFINITE) != 0;
}

static int pthread_cond_destroy(pthread_cond_t *cond)
{
  return CloseHandle(*cond) == 0;
}

int pthread_cond_signal(pthread_cond_t *cond)
{
  return SetEvent(*cond) == 0;
}

#else
#error no thread system available
#endif

struct THThread_ {
  pthread_t id;
  int (*func)(void*);
  THThreadState state;
};

struct THMutex_{
  pthread_mutex_t id;
  int refcount;
};

struct THCondition_ {
  pthread_cond_t id;
  int refcount;
};

THThread* THThread_new(void* (*func)(void*), void *data)
{
  THThread *self = malloc(sizeof(THThread));
  if(!self)
    return NULL;

  self->state.data = data;
  self->state.status = 0;

  if(pthread_create(&self->id, NULL, func, &self->state)) {
    free(self);
    return NULL;
  }
  return self;
}

AddressType THThread_id(THThread *self)
{
  return (AddressType)self;
}

int THThread_free(THThread *self)
{
  int status = 1;
  if(self) {
    if(pthread_join(self->id, NULL))
      return 1;
    status = self->state.status;
    free(self);
  }
  return status;
}

THMutex* THMutex_new(void)
{
  THMutex *self = malloc(sizeof(THMutex));
  if(!self)
    return NULL;
  if(pthread_mutex_init(&self->id, NULL) != 0) {
    free(self);
    return NULL;
  }
  self->refcount = 1;
  return self;
}

THMutex* THMutex_newWithId(AddressType id)
{
  THMutex *self = (THMutex*)id;
  THAtomicIncrementRef(&self->refcount);
  return self;
}

AddressType THMutex_id(THMutex *self)
{
  return (AddressType)self;
}

int THMutex_lock(THMutex *self)
{
  if(pthread_mutex_lock(&self->id) != 0)
    return 1;
  return 0;
}

int THMutex_unlock(THMutex *self)
{
  if(pthread_mutex_unlock(&self->id) != 0)
    return 1;
  return 0;
}

void THMutex_free(THMutex *self)
{
  if(self) {
    if(THAtomicDecrementRef(&self->refcount)) {
      pthread_mutex_destroy(&self->id);
      free(self);
    }
  }
}

THCondition* THCondition_new(void)
{
  THCondition *self = malloc(sizeof(THCondition));
  if(!self)
    return NULL;
  if(pthread_cond_init(&self->id, NULL)) {
    free(self);
    return NULL;
  }
  self->refcount = 1;
  return self;
}

THCondition* THCondition_newWithId(AddressType id)
{
  THCondition *self = (THCondition*)id;
  THAtomicIncrementRef(&self->refcount);
  return self;
}

AddressType THCondition_id(THCondition *self)
{
  return (AddressType)self;
}

int THCondition_signal(THCondition *self)
{
  if(pthread_cond_signal(&self->id))
    return 1;
  return 0;
}

int THCondition_wait(THCondition *self, THMutex *mutex)
{
  if(pthread_cond_wait(&self->id, &mutex->id))
    return 1;
  return 0;
}

void THCondition_free(THCondition *self)
{
  if(self) {
    if(THAtomicDecrementRef(&self->refcount)) {
      pthread_cond_destroy(&self->id);
      free(self);
    }
  }
}