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

threads.c « lib - github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fbb8d6062899e4d7b0de6ecd2d502e9fea50302f (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <stdlib.h>
#include <luaT.h>
#include <string.h>
#include <dlfcn.h>

#include "THThread.h"
#include "luaTHRD.h"

#include <lua.h>
#include <lualib.h>

static int thread_new(lua_State *L)
{
  THThread *thread = NULL;
  size_t len = 0;
  const char *code = luaL_checklstring(L, 1, &len);
  char *code_dup = malloc(len+1);
  if(!code_dup)
    luaL_error(L, "threads: out of memory");
  memcpy(code_dup, code, len+1);

  void* lib = dlopen("libthreadsmain.so", RTLD_LAZY|RTLD_LOCAL|RTLD_NODELETE);
  if (!lib) {
    free(code_dup);
    luaL_error(L, "threads: dlopen: %s", dlerror());
  }

  void* (*thread_main)(void*) = dlsym(lib, "THThread_main");
  if (!thread_main) {
    free(code_dup);
    luaL_error(L, "threads: dlsym: %s", dlerror());
  }

  thread = THThread_new(thread_main, (void*)code_dup);
  if(!thread) {
    free(code_dup);
    luaL_error(L, "threads: thread new failed");
  }

  luaTHRD_pushudata(L, thread, "threads.Thread");
  return 1;
}

static int thread_tostring(lua_State *L)
{
  char str[128];
  THThread *thread = luaTHRD_checkudata(L, 1, "threads.Thread");
  snprintf(str, 128, "threads.Thread <%lx>", THThread_id(thread));
  lua_pushstring(L, str);
  return 1;
}

static int thread_id(lua_State *L)
{
  THThread *thread = luaTHRD_checkudata(L, 1, "threads.Thread");
  lua_pushinteger(L, THThread_id(thread));
  return 1;
}

static int thread_free(lua_State *L)
{
  THThread *thread = luaTHRD_checkudata(L, 1, "threads.Thread");
  THThread_free(thread);
  return 0;
}

static int mutex_new(lua_State *L)
{
  THMutex *mutex = NULL;
  if(lua_gettop(L) == 0) {
    mutex = THMutex_new();
  }
  else if(lua_gettop(L) == 1) {
    long id = luaL_checklong(L, 1);
    mutex = THMutex_newWithId(id);
  }
  else
    luaL_error(L, "threads: mutex new invalid arguments");
  if(!mutex)
    luaL_error(L, "threads: mutex new failed");
  luaTHRD_pushudata(L, mutex, "threads.Mutex");
  return 1;
}

static int mutex_tostring(lua_State *L)
{
  char str[128];
  THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
  snprintf(str, 128, "threads.Mutex <%lx>", THMutex_id(mutex));
  lua_pushstring(L, str);
  return 1;
}

static int mutex_id(lua_State *L)
{
  THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
  lua_pushinteger(L, THMutex_id(mutex));
  return 1;
}

static int mutex_lock(lua_State *L)
{
  THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
  if(THMutex_lock(mutex))
    luaL_error(L, "threads: mutex lock failed");
  return 0;
}

static int mutex_unlock(lua_State *L)
{
  THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
  if(THMutex_unlock(mutex))
    luaL_error(L, "threads: mutex unlock failed");
  return 0;
}

static int mutex_free(lua_State *L)
{
  THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
  THMutex_free(mutex);
  return 0;
}

static int condition_new(lua_State *L)
{
  THCondition *condition = NULL;
  if(lua_gettop(L) == 0) {
    condition = THCondition_new();
  }
  else if(lua_gettop(L) == 1) {
    long id = luaL_checklong(L, 1);
    condition = THCondition_newWithId(id);
  }
  else
    luaL_error(L, "threads: condition new invalid arguments");
  if(!condition)
    luaL_error(L, "threads: condition new failed");
  luaTHRD_pushudata(L, condition, "threads.Condition");
  return 1;
}

static int condition_tostring(lua_State *L)
{
  char str[128];
  THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
  snprintf(str, 128, "threads.Condition <%lx>", THCondition_id(condition));
  lua_pushstring(L, str);
  return 1;
}

static int condition_id(lua_State *L)
{
  THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
  lua_pushinteger(L, THCondition_id(condition));
  return 1;
}

static int condition_free(lua_State *L)
{
  THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
  if(!condition)
    luaL_error(L, "threads: condition free failed");
  return 0;
}

static int condition_signal(lua_State *L)
{
  THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
  if(THCondition_signal(condition))
    luaL_error(L, "threads: condition signal failed");
  return 0;
}

static int condition_wait(lua_State *L)
{
  THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
  THMutex *mutex = luaTHRD_checkudata(L, 2, "threads.Mutex");
  if(THCondition_wait(condition, mutex))
    luaL_error(L, "threads: condition wait failed");
  return 0;
}

static const struct luaL_Reg thread__ [] = {
  {"new", thread_new},
  {"__tostring", thread_tostring},
  {"id", thread_id},
  {"free", thread_free},
  {NULL, NULL}
};

static const struct luaL_Reg mutex__ [] = {
  {"new", mutex_new},
  {"__tostring", mutex_tostring},
  {"id", mutex_id},
  {"lock", mutex_lock},
  {"unlock", mutex_unlock},
  {"free", mutex_free},
  {NULL, NULL}
};

static const struct luaL_Reg condition__ [] = {
  {"new", condition_new},
  {"__tostring", condition_tostring},
  {"id", condition_id},
  {"signal", condition_signal},
  {"wait", condition_wait},
  {"free", condition_free},
  {NULL, NULL}
};

static void thread_init_pkg(lua_State *L)
{
  if(!luaL_newmetatable(L, "threads.Thread"))
    luaL_error(L, "threads: threads.Thread type already exists");
  luaL_setfuncs(L, thread__, 0);
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);
  lua_rawset(L, -3);
  lua_pop(L, 1);

  if(!luaL_newmetatable(L, "threads.Mutex"))
    luaL_error(L, "threads: threads.Mutex type already exists");
  luaL_setfuncs(L, mutex__, 0);
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);
  lua_rawset(L, -3);
  lua_pop(L, 1);

  if(!luaL_newmetatable(L, "threads.Condition"))
    luaL_error(L, "threads: threads.Condition type already exists");
  luaL_setfuncs(L, condition__, 0);
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);
  lua_rawset(L, -3);
  lua_pop(L, 1);

  lua_pushstring(L, "Thread");
  luaTHRD_pushctortable(L, thread_new, "threads.Thread");
  lua_rawset(L, -3);

  lua_pushstring(L, "Mutex");
  luaTHRD_pushctortable(L, mutex_new, "threads.Mutex");
  lua_rawset(L, -3);

  lua_pushstring(L, "Condition");
  luaTHRD_pushctortable(L, condition_new, "threads.Condition");
  lua_rawset(L, -3);
}