Skip to content

Commit e844d45

Browse files
committed
Added pthread support + increased fiber stack size
1 parent c0b73ff commit e844d45

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

src/asar/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ macro(set_asar_shared_properties target msvc_lib_type_param enable_sanitizer)
9191
if (UNIX)
9292
target_compile_definitions(${target} PRIVATE "linux")
9393
target_compile_definitions(${target} PRIVATE "stricmp=strcasecmp")
94+
95+
set(THREADS_PREFER_PTHREAD_FLAG ON)
96+
find_package(Threads REQUIRED)
97+
target_link_libraries(${target} PRIVATE Threads::Threads)
9498
endif()
9599

96100

@@ -183,11 +187,11 @@ list(
183187
"${CMAKE_CURRENT_SOURCE_DIR}/interface-shared.h"
184188
"${CMAKE_CURRENT_SOURCE_DIR}/arch-shared.h"
185189
"${CMAKE_CURRENT_SOURCE_DIR}/virtualfile.h"
186-
"${CMAKE_CURRENT_SOURCE_DIR}/dll_helper.h"
187190
"${CMAKE_CURRENT_SOURCE_DIR}/table.h"
188191
"${CMAKE_CURRENT_SOURCE_DIR}/interface-lib.h"
189192
"${CMAKE_CURRENT_SOURCE_DIR}/std-includes.h"
190193
"${CMAKE_CURRENT_SOURCE_DIR}/platform/file-helpers.h"
194+
"${CMAKE_CURRENT_SOURCE_DIR}/platform/thread-helpers.h"
191195
)
192196

193197

@@ -201,6 +205,7 @@ if(WIN32)
201205
APPEND ASAR_SHARED_SOURCE_FILES
202206

203207
"${CMAKE_CURRENT_SOURCE_DIR}/platform/windows/file-helpers-win32.cpp"
208+
"${CMAKE_CURRENT_SOURCE_DIR}/platform/windows/thread-helpers-win32.h"
204209
)
205210

206211
list(
@@ -220,13 +225,15 @@ elseif(UNIX)
220225
APPEND ASAR_SHARED_SOURCE_FILES
221226

222227
"${CMAKE_CURRENT_SOURCE_DIR}/platform/linux/file-helpers-linux.cpp"
228+
"${CMAKE_CURRENT_SOURCE_DIR}/platform/generic/thread-helpers-pthread.h"
223229
)
224230
else()
225231
# Files for any other platform
226232
list(
227233
APPEND ASAR_SHARED_SOURCE_FILES
228234

229235
"${CMAKE_CURRENT_SOURCE_DIR}/platform/generic/file-helpers-generic.cpp"
236+
"${CMAKE_CURRENT_SOURCE_DIR}/platform/generic/thread-helpers-pthread.h"
230237
)
231238
endif()
232239

src/asar/interface-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void set_text_color(FILE* output_loc, string* in_out_str, ansi_text_color::e col
7575
}
7676
}
7777
#elif defined(_WIN32)
78-
// Currently using SetConsoleTextAttribute() approach over an ASCI escape character
78+
// Currently using SetConsoleTextAttribute() approach over an ASCII escape character
7979
// approach because it makes the output text easier to parse. Unfortunately, this
8080
// also currently makes this a Windows-only solution.
8181
CONSOLE_SCREEN_BUFFER_INFO screenInfo;

src/asar/interface-lib.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#include "interface-shared.h"
66
#include "assembleblock.h"
77
#include "asar_math.h"
8-
#if defined(_WIN32)
9-
#include "dll_helper.h"
10-
#endif
8+
#include "platform/thread-helpers.h"
119

1210
#if defined(CPPCLI)
1311
#define EXPORT extern "C"
@@ -25,7 +23,7 @@
2523
// when used in combination with -fsanitize=address.
2624
#if defined(_WIN32) && defined(NDEBUG)
2725
# define RUN_VIA_FIBER
28-
#elif defined(_WIN32)
26+
#else
2927
# define RUN_VIA_THREAD
3028
#endif
3129

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <pthread.h>
5+
6+
struct function_pointer_wrapper /*have this struct at global level*/
7+
{
8+
static void* (*thread_callback)(void*);
9+
static void* execute_thread(void* parameter) { return thread_callback(parameter); }
10+
};
11+
12+
void* (*function_pointer_wrapper::thread_callback)(void*) = nullptr;
13+
14+
template <typename functor>
15+
bool run_as_thread(functor&& callback) {
16+
struct thread_wrapper {
17+
functor& callback;
18+
bool result;
19+
20+
void* execute() {
21+
result = callback();
22+
if (result)
23+
pthread_exit(NULL);
24+
else
25+
pthread_exit(reinterpret_cast<void*>((uintptr_t)-1));
26+
}
27+
28+
} wrapper{ callback, false };
29+
30+
function_pointer_wrapper::thread_callback = [](void* parameter) {
31+
return reinterpret_cast<thread_wrapper*>(parameter)->execute();
32+
};
33+
34+
pthread_attr_t settings;
35+
pthread_t thread_id;
36+
int ret;
37+
38+
ret = pthread_attr_init(&settings);
39+
if (ret == -1){
40+
return callback();
41+
}
42+
43+
ret = pthread_attr_setstacksize(&settings, 16 * 1024 * 1024);
44+
if (ret == -1){
45+
return callback();
46+
}
47+
48+
ret = pthread_create(&thread_id, &settings, &function_pointer_wrapper::execute_thread, &wrapper);
49+
if (ret == -1){
50+
return callback();
51+
}
52+
53+
void* thread_ret;
54+
pthread_join(thread_id, &thread_ret);
55+
56+
return wrapper.result;
57+
}

src/asar/platform/thread-helpers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#if defined(windows)
4+
# include "windows/thread-helpers-win32.h"
5+
#else
6+
# include "generic/thread-helpers-pthread.h"
7+
#endif

src/asar/dll_helper.h renamed to src/asar/platform/windows/thread-helpers-win32.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#pragma once
2+
13
#if defined(_WIN32)
24

35
#include <windows.h>
46

5-
struct function_pointer_wrapper/*have this struct at global level*/
7+
struct function_pointer_wrapper /*have this struct at global level*/
68
{
79
static void (*fiber_callback)(void *);
810
static void __stdcall execute_fiber(void* parameter) { return fiber_callback(parameter); }
@@ -31,7 +33,7 @@ bool run_as_fiber(functor &&callback) {
3133
function_pointer_wrapper::fiber_callback = [](void *parameter){
3234
reinterpret_cast<fiber_wrapper*>(parameter)->execute();
3335
};
34-
auto fiber = CreateFiberEx(4*1024*1024, 8*1024*1024, 0, &function_pointer_wrapper::execute_fiber, &wrapper);
36+
auto fiber = CreateFiberEx(16*1024*1024, 16*1024*1024, 0, &function_pointer_wrapper::execute_fiber, &wrapper);
3537

3638
if (!fiber) {
3739
return callback();

0 commit comments

Comments
 (0)