From 7cabb0091919af846de3d54a021973e6080a819c Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Fri, 23 Sep 2016 21:43:34 +0800 Subject: to work on windows with msvc x64 --- CMakeLists.txt | 26 +++++++++++++++++++++----- lib/THThread.c | 16 ++++++++-------- lib/THThread.h | 20 +++++++++++++++----- lib/init.c | 5 +++++ lib/queue.c | 2 +- lib/thread-main.c | 4 ++++ lib/threads.c | 26 ++++++++++++++++++++++---- 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; } -- cgit v1.2.3 From 9ee8b3fea852ea49abe0cba3e49cee050862142e Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Fri, 23 Sep 2016 22:36:38 +0800 Subject: use internal pthread wrapper for windows --- lib/THThread.c | 8 ++++++-- lib/init.c | 2 +- lib/thread-main.c | 2 +- rocks/threads-scm-1.rockspec | 25 +++++++++++++++++++------ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/THThread.c b/lib/THThread.c index 18fc81e..6951caf 100644 --- a/lib/THThread.c +++ b/lib/THThread.c @@ -18,6 +18,10 @@ 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 *), @@ -41,7 +45,7 @@ static int pthread_mutex_init(pthread_mutex_t *restrict mutex, static int pthread_mutex_lock(pthread_mutex_t *mutex) { - return WaitForSingleObject(*mutex, INFINITE) == 0; + return WaitForSingleObject(*mutex, INFINITE) != 0; } static int pthread_mutex_unlock(pthread_mutex_t *mutex) @@ -65,7 +69,7 @@ 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; + return WaitForSingleObject(*mutex, INFINITE) != 0; } static int pthread_cond_destroy(pthread_cond_t *cond) diff --git a/lib/init.c b/lib/init.c index 399aeaa..021f38c 100644 --- a/lib/init.c +++ b/lib/init.c @@ -28,7 +28,7 @@ static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) #include "queue.c" #if defined(_WIN32) -__declspec(dllexport) int luaopen_libthreads(lua_State *L) +__declspec(dllexport) int _cdecl luaopen_libthreads(lua_State *L) #else int luaopen_libthreads(lua_State *L) #endif diff --git a/lib/thread-main.c b/lib/thread-main.c index 5008a07..ee54748 100644 --- a/lib/thread-main.c +++ b/lib/thread-main.c @@ -35,7 +35,7 @@ static int runthread(void *code_) } #if defined(_WIN32) -__declspec(dllexport) void* THThread_main(void *arg) +__declspec(dllexport) void* _cdecl THThread_main(void *arg) #else void* THThread_main(void *arg) #endif diff --git a/rocks/threads-scm-1.rockspec b/rocks/threads-scm-1.rockspec index c9f01f0..9765d4a 100644 --- a/rocks/threads-scm-1.rockspec +++ b/rocks/threads-scm-1.rockspec @@ -21,10 +21,23 @@ dependencies = { } build = { - type = "cmake", - variables = { - CMAKE_BUILD_TYPE="Release", - CMAKE_PREFIX_PATH="$(LUA_BINDIR)/..", - CMAKE_INSTALL_PREFIX="$(PREFIX)" - } + type = "command", + build_command = [[ +cmake -E make_directory build && cd build && cmake .. -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE) + ]], + platforms = { + windows = { + -- example with dlfcn-win + -- luarocks make rocks\threads-scm-1.rockspec WIN_DLFCN_INCDIR="D:\Libraries\include" WIN_DLFCN_LIBDIR="D:\Libraries\lib" + build_command = [[ +cmake -E make_directory build && cd build && cmake .. -G "NMake Makefiles" -DWIN_DLFCN_INCDIR="$(WIN_DLFCN_INCDIR)" -DWIN_DLFCN_LIBDIR="$(WIN_DLFCN_LIBDIR)" -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE) +]] + -- example with dlfcn-win and pthread-win + -- luarocks make rocks\threads-scm-1.rockspec WIN_DLFCN_INCDIR="D:\Libraries\include" WIN_DLFCN_LIBDIR="D:\Libraries\lib" CMAKE_HAVE_PTHREAD_H="D:\Libraries\include" CMAKE_HAVE_LIBC_CREATE="D:\Libraries\lib\pthreadVC2.lib" PTHREAD_LIB_NAME="pthreadVC2" + -- build_command = [[ +-- cmake -E make_directory build && cd build && cmake .. -G "NMake Makefiles" -DWIN_DLFCN_INCDIR="$(WIN_DLFCN_INCDIR)" -DWIN_DLFCN_LIBDIR="$(WIN_DLFCN_LIBDIR)" -DUSE_PTHREAD_THREADS=1 -DCMAKE_HAVE_PTHREAD_H="$(CMAKE_HAVE_PTHREAD_H" -DCMAKE_HAVE_LIBC_CREATE="$(CMAKE_HAVE_LIBC_CREATE)" -DPTHREAD_LIB_NAME="$(PTHREAD_LIB_NAME)" -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE) +-- ]] + } + }, + install_command = "cd build && $(MAKE) install" } -- cgit v1.2.3