Skip to content

Commit d4b21dd

Browse files
Add a switch in the CMake build to allow falling back to the system swapcontext.
1 parent a45e6ee commit d4b21dd

File tree

7 files changed

+22
-25
lines changed

7 files changed

+22
-25
lines changed

config/qthread_check_swapcontext.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main()
8383
[qthread_cv_swapcontext_works=yes])],
8484
[qthread_cv_swapcontext_works=yes])])])
8585
AS_IF([test "$qthread_cv_swapcontext_works" = yes],
86-
[AC_DEFINE([HAVE_NATIVE_MAKECONTEXT], [1], [The system provides functional native make/swap/get-context functions])
86+
[AC_DEFINE([USE_SYSTEM_SWAPCONTEXT], [1], [Use the system provided functional native make/swap/get-context functions])
8787
QTHREAD_CHECK_MAKECONTEXT_SPLIT_ARGS(
8888
[AC_DEFINE([QTHREAD_MAKECONTEXT_SPLIT],[1],[makecontext()passes args as int-size, not long-size])])
8989
$1],

include/fastcontext/386-ucontext.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
#include <config.h>
33
#endif
44

5-
#ifdef HAVE_STDARG_H
65
#include <stdarg.h> /* for the qt_makectxt prototype */
7-
#endif
86
#include <stddef.h> /* for size_t, per C89 */
97
#include <stdint.h>
108

include/fastcontext/power-ucontext.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifdef HAVE_STDARG_H
21
#include <stdarg.h> /* for the qt_makectxt prototype */
3-
#endif
42

53
#include "qt_visibility.h"
64

include/fastcontext/taskimpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@
2222
#define NEEDSWAPCONTEXT
2323
#include "power-ucontext.h"
2424
#elif (QTHREAD_ASSEMBLY_ARCH == QTHREAD_ARM)
25-
#ifdef HAVE_STDARG_H
2625
#include <stdarg.h>
27-
#endif
2826
#define NEEDARMMAKECONTEXT
2927
#define NEEDSWAPCONTEXT
3028
#include "arm-ucontext.h"
3129
#elif (QTHREAD_ASSEMBLY_ARCH == QTHREAD_ARMV8_A64)
32-
#ifdef HAVE_STDARG_H
3330
#include <stdarg.h>
34-
#endif
3531
#define NEEDARMA64MAKECONTEXT
3632
#define NEEDSWAPCONTEXT
3733
#include "arm-ucontext.h"

include/qt_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef QT_CONTEXT_H
22
#define QT_CONTEXT_H
33

4-
#if defined(HAVE_UCONTEXT_H) && defined(HAVE_NATIVE_MAKECONTEXT)
4+
#if defined(USE_SYSTEM_SWAPCONTEXT)
55
#include <ucontext.h> /* for ucontext_t */
66
typedef ucontext_t qt_context_t;
77
#else

src/CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(QTHREADS_DEFAULT_STACK_SIZE 32768 CACHE STRING "Default qthread stack size."
88
set(QTHREADS_HASHMAP hashmap CACHE STRING "Which hashmap implementation to use. Valid values are \"hashmap\" and \"lf_hashmap\".")
99
set(QTHREADS_DICT_TYPE shavit CACHE STRING "Which dictionary implementation to use. Valid values are \"shavit\", \"trie\", and \"simple\".")
1010
set(QTHREADS_TIMER_TYPE gettimeofday CACHE STRING "Which timer implementation to use. Valid values are \"clock_gettime\", \"mach\", \"gettimeofday\", and \"gethrtime\".")
11+
set(QTHREADS_CONTEXT_SWAP_IMPL fastcontext CACHE STRING "Which context swap implementation to use. Valid values are \"system\" and \"fastcontext\".")
1112

1213
set(QTHREADS_SOURCES
1314
cacheline.c
@@ -34,8 +35,6 @@ set(QTHREADS_SOURCES
3435
touch.c
3536
tls.c
3637
teams.c
37-
fastcontext/asm.S
38-
fastcontext/context.c
3938
${QTHREADS_HASHMAP}.c
4039
ds/qarray.c
4140
ds/qdqueue.c
@@ -60,10 +59,16 @@ set(QTHREADS_SOURCES
6059
patterns/wavefront.c
6160
)
6261

63-
# TODO: switch/checks necessary to include the correct
64-
# fastcontext version when we need the fallback.
65-
6662
add_library(qthread SHARED ${QTHREADS_SOURCES})
63+
64+
if ("${QTHREADS_CONTEXT_SWAP_IMPL}" STREQUAL "fastcontext")
65+
target_sources(qthread PRIVATE fastcontext/asm.S fastcontext/context.c)
66+
elseif ("${QTHREADS_CONTEXT_SWAP_IMPL}" STREQUAL "system")
67+
target_compile_definitions(qthread PRIVATE USE_SYSTEM_SWAPCONTEXT)
68+
else()
69+
message(FATAL_ERROR "The specified context swap implementation does not match any known implementations.")
70+
endif()
71+
6772
target_include_directories(qthread
6873
PRIVATE "../include"
6974
)

src/qthread.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ static void *qthread_master(void *arg) {
439439

440440
*current = t;
441441

442-
#ifdef HAVE_NATIVE_MAKECONTEXT
442+
#ifdef USE_SYSTEM_SWAPCONTEXT
443443
getcontext(&my_context);
444444
#endif
445445
qthread_exec(t, &my_context);
@@ -846,7 +846,7 @@ int API_FUNC qthread_initialize(void) { /*{{{ */
846846

847847
qthread_before_swap_to_main();
848848

849-
#ifdef HAVE_NATIVE_MAKECONTEXT
849+
#ifdef USE_SYSTEM_SWAPCONTEXT
850850
qassert(
851851
swapcontext(&qlib->mccoy_thread->rdata->context, &(qlib->master_context)),
852852
0);
@@ -938,21 +938,21 @@ static inline void qthread_makecontext(qt_context_t *const c,
938938
#ifdef UCSTACK_HAS_SSFLAGS
939939
c->uc_stack.ss_flags = 0;
940940
#endif
941-
#ifdef HAVE_NATIVE_MAKECONTEXT
941+
#ifdef USE_SYSTEM_SWAPCONTEXT
942942
/* the makecontext man page (Linux) says: set the uc_link FIRST.
943943
* why? no idea */
944944
c->uc_link = returnc; /* NULL pthread_exit() */
945945
#endif
946-
#ifdef HAVE_NATIVE_MAKECONTEXT
946+
#ifdef USE_SYSTEM_SWAPCONTEXT
947947
#ifdef QTHREAD_MAKECONTEXT_SPLIT
948948
makecontext(c, func, 2, high, low);
949949
#else /* QTHREAD_MAKECONTEXT_SPLIT */
950950
makecontext(c, func, 1, arg);
951951
#endif /* QTHREAD_MAKECONTEXT_SPLIT */
952952
assert((void *)c->uc_link == (void *)returnc);
953-
#else /* ifdef HAVE_NATIVE_MAKECONTEXT */
953+
#else /* ifdef USE_SYSTEM_SWAPCONTEXT */
954954
qt_makectxt(c, func, 1, arg);
955-
#endif /* ifdef HAVE_NATIVE_MAKECONTEXT */
955+
#endif /* ifdef USE_SYSTEM_SWAPCONTEXT */
956956
} /*}}} */
957957

958958
/* this adds a function to the list of cleanup functions to call at finalize;
@@ -1743,7 +1743,7 @@ void INTERNAL qthread_exec(qthread_t *t, qt_context_t *c) { /*{{{ */
17431743
(void (*)(void))qthread_wrapper,
17441744
t,
17451745
c);
1746-
#ifdef HAVE_NATIVE_MAKECONTEXT
1746+
#ifdef USE_SYSTEM_SWAPCONTEXT
17471747
} else {
17481748
t->rdata->context.uc_link = c; /* NULL pthread_exit() */
17491749
#endif
@@ -1766,7 +1766,7 @@ void INTERNAL qthread_exec(qthread_t *t, qt_context_t *c) { /*{{{ */
17661766

17671767
qthread_before_swap_to_qthread(t);
17681768

1769-
#ifdef HAVE_NATIVE_MAKECONTEXT
1769+
#ifdef USE_SYSTEM_SWAPCONTEXT
17701770
qassert(swapcontext(atomic_load_explicit(&t->rdata->return_context,
17711771
memory_order_relaxed),
17721772
&t->rdata->context),
@@ -2233,7 +2233,7 @@ void INTERNAL qthread_back_to_master(qthread_t *t) { /*{{{ */
22332233
sizeof(qt_context_t));
22342234
#endif
22352235
qthread_before_swap_from_qthread(t);
2236-
#ifdef HAVE_NATIVE_MAKECONTEXT
2236+
#ifdef USE_SYSTEM_SWAPCONTEXT
22372237
qassert(swapcontext(&t->rdata->context,
22382238
atomic_load_explicit(&t->rdata->return_context,
22392239
memory_order_relaxed)),
@@ -2262,7 +2262,7 @@ void INTERNAL qthread_back_to_master2(qthread_t *t) { /*{{{ */
22622262
atomic_load_explicit(&t->rdata->return_context, memory_order_relaxed),
22632263
sizeof(qt_context_t));
22642264
#endif
2265-
#ifdef HAVE_NATIVE_MAKECONTEXT
2265+
#ifdef USE_SYSTEM_SWAPCONTEXT
22662266
setcontext(
22672267
atomic_load_explicit(&t->rdata->return_context, memory_order_relaxed));
22682268
#else

0 commit comments

Comments
 (0)