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

github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRui Guo <guorui.xt@gmail.com>2016-09-23 16:43:34 +0300
committerRui Guo <guorui.xt@gmail.com>2016-09-23 16:43:34 +0300
commit7cabb0091919af846de3d54a021973e6080a819c (patch)
tree984c00a9689d45db7457606f6552774c11a8deac
parent6bb6be81e43cbc2b682d332f82a33d64543901d7 (diff)
to work on windows with msvc x64
-rw-r--r--CMakeLists.txt26
-rw-r--r--lib/THThread.c16
-rw-r--r--lib/THThread.h20
-rw-r--r--lib/init.c5
-rw-r--r--lib/queue.c2
-rw-r--r--lib/thread-main.c4
-rw-r--r--lib/threads.c26
7 files changed, 76 insertions, 23 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8ae8615..355dc12 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,9 +19,6 @@ set(luasrc
queue.lua
)
-add_torch_package(threads "${src}" "${luasrc}" "Threads")
-target_link_libraries(threads luaT TH)
-
ADD_LIBRARY(threadsmain MODULE lib/thread-main.c)
IF(APPLE)
SET_TARGET_PROPERTIES(threadsmain PROPERTIES
@@ -30,18 +27,37 @@ ENDIF()
INSTALL(TARGETS threadsmain LIBRARY DESTINATION "${Torch_INSTALL_LIB_SUBDIR}")
if(WIN32)
- add_definitions(-DUSE_WIN_THREADS=1)
+ link_directories(${LINK_DIRECTORIES} ${WIN_DLFCN_LIBDIR})
+ include_directories(${INCLUDE_DIRECTORIES} ${WIN_DLFCN_INCDIR})
+endif()
+
+add_torch_package(threads "${src}" "${luasrc}" "Threads")
+target_link_libraries(threads luaT TH)
+if(WIN32)
+ target_link_libraries(threads dl)
+endif()
+
+if(WIN32 AND (NOT DEFINED USE_PTHREAD_THREADS))
+ add_definitions(-DUSE_WIN32_THREADS=1)
else()
+ if(WIN32)
+ add_definitions(-D_TIMESPEC_DEFINED=1)
+ endif()
set(CMAKE_THREAD_PREFER_PTHREAD)
find_package(Threads REQUIRED)
if(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
add_definitions(-DUSE_PTHREAD_THREADS=1)
- target_link_libraries(threads ${CMAKE_THREAD_LIBS_INIT})
+ if(PTHREAD_LIB_NAME)
+ target_link_libraries(threads ${PTHREAD_LIB_NAME})
+ else()
+ target_link_libraries(threads ${CMAKE_THREAD_LIBS_INIT})
+ endif()
else()
message(FATAL_ERROR "no threading system (pthread or Win32) available")
endif()
endif()
IF(LUALIB)
+ TARGET_LINK_LIBRARIES(threads ${LUALIB})
TARGET_LINK_LIBRARIES(threadsmain ${LUALIB})
ENDIF()
diff --git a/lib/THThread.c b/lib/THThread.c
index a273600..18fc81e 100644
--- a/lib/THThread.c
+++ b/lib/THThread.c
@@ -114,9 +114,9 @@ THThread* THThread_new(void* (*func)(void*), void *data)
return self;
}
-long THThread_id(THThread *self)
+AddressType THThread_id(THThread *self)
{
- return (long)(self);
+ return (AddressType)self;
}
int THThread_free(THThread *self)
@@ -144,16 +144,16 @@ THMutex* THMutex_new(void)
return self;
}
-THMutex* THMutex_newWithId(long id)
+THMutex* THMutex_newWithId(AddressType id)
{
THMutex *self = (THMutex*)id;
THAtomicIncrementRef(&self->refcount);
return self;
}
-long THMutex_id(THMutex *self)
+AddressType THMutex_id(THMutex *self)
{
- return (long)(self);
+ return (AddressType)self;
}
int THMutex_lock(THMutex *self)
@@ -193,16 +193,16 @@ THCondition* THCondition_new(void)
return self;
}
-THCondition* THCondition_newWithId(long id)
+THCondition* THCondition_newWithId(AddressType id)
{
THCondition *self = (THCondition*)id;
THAtomicIncrementRef(&self->refcount);
return self;
}
-long THCondition_id(THCondition *self)
+AddressType THCondition_id(THCondition *self)
{
- return (long)(self);
+ return (AddressType)self;
}
int THCondition_signal(THCondition *self)
diff --git a/lib/THThread.h b/lib/THThread.h
index 008223d..7377089 100644
--- a/lib/THThread.h
+++ b/lib/THThread.h
@@ -1,6 +1,16 @@
#ifndef TH_THREAD_INC
#define TH_THREAD_INC
+#ifndef _MSC_VER
+typedef long AddressType;
+#else
+#ifdef _WIN64
+typedef __int64 AddressType;
+#else
+typedef __int32 AddressType;
+#endif
+#endif
+
typedef struct THThread_ THThread;
typedef struct THMutex_ THMutex;
typedef struct THCondition_ THCondition;
@@ -10,19 +20,19 @@ typedef struct THThreadState_ {
} THThreadState;
THThread* THThread_new(void* (*closure)(void*), void *data);
-long THThread_id(THThread *self);
+AddressType THThread_id(THThread *self);
int THThread_free(THThread *self);
THMutex* THMutex_new(void);
-THMutex* THMutex_newWithId(long id);
-long THMutex_id(THMutex *self);
+THMutex* THMutex_newWithId(AddressType id);
+AddressType THMutex_id(THMutex *self);
int THMutex_lock(THMutex *self);
int THMutex_unlock(THMutex *self);
void THMutex_free(THMutex *self);
THCondition* THCondition_new(void);
-THCondition* THCondition_newWithId(long id);
-long THCondition_id(THCondition *self);
+THCondition* THCondition_newWithId(AddressType id);
+AddressType THCondition_id(THCondition *self);
int THCondition_signal(THCondition *self);
int THCondition_wait(THCondition *self, THMutex *mutex);
void THCondition_free(THCondition *self);
diff --git a/lib/init.c b/lib/init.c
index 318004d..399aeaa 100644
--- a/lib/init.c
+++ b/lib/init.c
@@ -22,11 +22,16 @@ static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#endif
+#define luaL_checkaddr(L,n) ((AddressType)luaL_checkinteger(L, (n)))
#include "threads.c"
#include "queue.c"
+#if defined(_WIN32)
+__declspec(dllexport) int luaopen_libthreads(lua_State *L)
+#else
int luaopen_libthreads(lua_State *L)
+#endif
{
lua_newtable(L);
thread_init_pkg(L);
diff --git a/lib/queue.c b/lib/queue.c
index a25586c..cd805aa 100644
--- a/lib/queue.c
+++ b/lib/queue.c
@@ -320,7 +320,7 @@ static int queue__newindex(lua_State *L)
static int queue_id(lua_State *L)
{
THQueue *queue = luaTHRD_checkudata(L, 1, "threads.Queue");
- lua_pushinteger(L, (long)queue);
+ lua_pushinteger(L, (AddressType)queue);
return 1;
}
diff --git a/lib/thread-main.c b/lib/thread-main.c
index 344e679..5008a07 100644
--- a/lib/thread-main.c
+++ b/lib/thread-main.c
@@ -34,7 +34,11 @@ static int runthread(void *code_)
return 0;
}
+#if defined(_WIN32)
+__declspec(dllexport) void* THThread_main(void *arg)
+#else
void* THThread_main(void *arg)
+#endif
{
THThreadState* state = arg;
state->status = runthread(state->data);
diff --git a/lib/threads.c b/lib/threads.c
index 6d845c4..46a771d 100644
--- a/lib/threads.c
+++ b/lib/threads.c
@@ -20,10 +20,16 @@ static int thread_new(lua_State *L)
luaL_error(L, "threads: out of memory");
memcpy(code_dup, code, len+1);
+#ifdef _WIN32
+#define LIBTHREADSMAIN "threadsmain.dll"
+#else
+#define LIBTHREADSMAIN "libthreadsmain.so"
+#endif
+
#ifdef RTLD_NODELETE /* platforms like android dont seem to support this */
- void* lib = dlopen("libthreadsmain.so", RTLD_LAZY|RTLD_LOCAL|RTLD_NODELETE);
+ void* lib = dlopen(LIBTHREADSMAIN, RTLD_LAZY|RTLD_LOCAL|RTLD_NODELETE);
#else
- void* lib = dlopen("libthreadsmain.so", RTLD_LAZY|RTLD_LOCAL);
+ void* lib = dlopen(LIBTHREADSMAIN, RTLD_LAZY|RTLD_LOCAL);
#endif
if (!lib) {
free(code_dup);
@@ -50,7 +56,11 @@ static int thread_tostring(lua_State *L)
{
char str[128];
THThread *thread = luaTHRD_checkudata(L, 1, "threads.Thread");
+#ifndef _WIN64
snprintf(str, 128, "threads.Thread <%lx>", THThread_id(thread));
+#else
+ snprintf(str, 128, "threads.Thread <%llx>", THThread_id(thread));
+#endif
lua_pushstring(L, str);
return 1;
}
@@ -76,7 +86,7 @@ static int mutex_new(lua_State *L)
mutex = THMutex_new();
}
else if(lua_gettop(L) == 1) {
- long id = luaL_checklong(L, 1);
+ AddressType id = luaL_checkaddr(L, 1);
mutex = THMutex_newWithId(id);
}
else
@@ -91,7 +101,11 @@ static int mutex_tostring(lua_State *L)
{
char str[128];
THMutex *mutex = luaTHRD_checkudata(L, 1, "threads.Mutex");
+#ifndef _WIN64
snprintf(str, 128, "threads.Mutex <%lx>", THMutex_id(mutex));
+#else
+ snprintf(str, 128, "threads.Mutex <%llx>", THMutex_id(mutex));
+#endif
lua_pushstring(L, str);
return 1;
}
@@ -133,7 +147,7 @@ static int condition_new(lua_State *L)
condition = THCondition_new();
}
else if(lua_gettop(L) == 1) {
- long id = luaL_checklong(L, 1);
+ AddressType id = luaL_checkaddr(L, 1);
condition = THCondition_newWithId(id);
}
else
@@ -148,7 +162,11 @@ static int condition_tostring(lua_State *L)
{
char str[128];
THCondition *condition = luaTHRD_checkudata(L, 1, "threads.Condition");
+#ifndef _WIN64
snprintf(str, 128, "threads.Condition <%lx>", THCondition_id(condition));
+#else
+ snprintf(str, 128, "threads.Condition <%llx>", THCondition_id(condition));
+#endif
lua_pushstring(L, str);
return 1;
}