Line |
Branch |
Exec |
Source |
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 |
|
397 |
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 |
|
397 |
bool run_as_thread(functor&& callback) { |
16 |
|
|
struct thread_wrapper { |
17 |
|
|
functor& callback; |
18 |
|
|
bool result; |
19 |
|
|
|
20 |
|
397 |
void* execute() { |
21 |
|
397 |
result = callback(); |
22 |
2/2
✓ Branch 0 taken 323 times.
✓ Branch 1 taken 74 times.
|
397 |
if (result) |
23 |
|
323 |
pthread_exit(NULL); |
24 |
|
|
else |
25 |
|
74 |
pthread_exit(reinterpret_cast<void*>((uintptr_t)-1)); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
397 |
} wrapper{ callback, false }; |
29 |
|
|
|
30 |
|
397 |
function_pointer_wrapper::thread_callback = [](void* parameter) { |
31 |
|
397 |
return reinterpret_cast<thread_wrapper*>(parameter)->execute(); |
32 |
|
|
}; |
33 |
|
|
|
34 |
|
|
pthread_attr_t settings; |
35 |
|
|
pthread_t thread_id; |
36 |
|
|
int ret; |
37 |
|
|
|
38 |
|
397 |
ret = pthread_attr_init(&settings); |
39 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 397 times.
|
397 |
if (ret == -1){ |
40 |
|
✗ |
return callback(); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
397 |
ret = pthread_attr_setstacksize(&settings, 16 * 1024 * 1024); |
44 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 397 times.
|
397 |
if (ret == -1){ |
45 |
|
✗ |
return callback(); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
397 |
ret = pthread_create(&thread_id, &settings, &function_pointer_wrapper::execute_thread, &wrapper); |
49 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 397 times.
|
397 |
if (ret == -1){ |
50 |
|
✗ |
return callback(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
void* thread_ret; |
54 |
1/2
✓ Branch 0 taken 397 times.
✗ Branch 1 not taken.
|
397 |
pthread_join(thread_id, &thread_ret); |
55 |
|
|
|
56 |
|
397 |
return wrapper.result; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
#ifndef NO_USE_THREADS |
60 |
|
|
void* stack_bottom = nullptr; |
61 |
|
1161 |
void init_stack_use_check() { |
62 |
|
|
pthread_attr_t attrs; |
63 |
|
1161 |
size_t stack_size = 0; |
64 |
|
1161 |
pthread_getattr_np(pthread_self(), &attrs); |
65 |
|
1161 |
pthread_attr_getstack(&attrs, &stack_bottom, &stack_size); |
66 |
|
1161 |
pthread_attr_destroy(&attrs); |
67 |
|
1161 |
} |
68 |
|
1146 |
void deinit_stack_use_check() { |
69 |
|
1146 |
stack_bottom = nullptr; |
70 |
|
1146 |
} |
71 |
|
193081 |
bool have_enough_stack_left() { |
72 |
|
|
#if defined(__clang__) || defined(__GNUC__) |
73 |
|
|
// this is a GCC extension, but it's necessary for this function to work in ASan |
74 |
|
|
// (regular local variables are banished to the shadow realm or some shit, |
75 |
|
|
// so their addresses don't tell us anything about "real" stack usage) |
76 |
|
193081 |
void* stack_top = __builtin_frame_address(0); |
77 |
|
|
#else |
78 |
|
|
// using a random local as a rough estimate for current top-of-stack |
79 |
|
|
char stackvar; |
80 |
|
|
void* stack_top = &stackvar; |
81 |
|
|
#endif |
82 |
|
193081 |
ssize_t stack_left = (char*)stack_top - (char*)stack_bottom; |
83 |
4/4
✓ Branch 0 taken 192887 times.
✓ Branch 1 taken 194 times.
✓ Branch 2 taken 192884 times.
✓ Branch 3 taken 3 times.
|
193081 |
return stack_bottom == nullptr || stack_left >= 32768; |
84 |
|
|
} |
85 |
|
|
#endif |
86 |
|
|
|