]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/mtasker_ucontext.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "mtasker_context.hh"
23 #include <system_error>
30 #ifdef PDNS_USE_VALGRIND
31 #include <valgrind/valgrind.h>
32 #endif /* PDNS_USE_VALGRIND */
34 #ifdef HAVE_FIBER_SANITIZER
35 __thread
void* t_mainStack
{nullptr};
36 __thread
size_t t_mainStackSize
{0};
37 #endif /* HAVE_FIBER_SANITIZER */
39 template <typename Message
> static __attribute__((noinline
, cold
, noreturn
))
41 throw_errno (Message
&& msg
) {
42 throw std::system_error
43 (errno
, std::system_category(), std::forward
<Message
>(msg
));
48 splitPointer (void* const ptr
) noexcept
{
49 static_assert (sizeof(int) == 4, "splitPointer() requires an 4 byte 'int'");
50 // In theory, we need this assertion. In practice, it prevents compilation
51 // on EL6 i386. Without the assertion, everything works.
52 // If you ever run into trouble with this code, please heed the warnings at
53 // http://man7.org/linux/man-pages/man3/makecontext.3.html#NOTES
54 // static_assert (sizeof(uintptr_t) == 8,
55 // "splitPointer() requires an 8 byte 'uintptr_t'");
56 std::pair
<int, int> words
;
57 auto rep
= reinterpret_cast<uintptr_t>(ptr
);
58 uint32_t const hw
= rep
>> 32;
59 auto const lw
= static_cast<uint32_t>(rep
);
60 std::memcpy (&words
.first
, &hw
, 4);
61 std::memcpy (&words
.second
, &lw
, 4);
65 template <typename T
> static inline
67 joinPtr (int const first
, int const second
) noexcept
{
68 static_assert (sizeof(int) == 4, "joinPtr() requires an 4 byte 'int'");
70 // static_assert (sizeof(uintptr_t) == 8,
71 // "joinPtr() requires an 8 byte 'uintptr_t'");
74 std::memcpy (&hw
, &first
, 4);
75 std::memcpy (&lw
, &second
, 4);
76 return reinterpret_cast<T
*>((static_cast<uintptr_t>(hw
) << 32) | lw
);
82 threadWrapper (int const ctx0
, int const ctx1
, int const fun0
, int const fun1
) {
83 notifyStackSwitchDone();
84 auto ctx
= joinPtr
<pdns_ucontext_t
>(ctx0
, ctx1
);
86 auto start
= std::move(*joinPtr
<boost::function
<void()>>(fun0
, fun1
));
89 ctx
->exception
= std::current_exception();
91 notifyStackSwitchToKernel();
95 pdns_ucontext_t::pdns_ucontext_t() {
96 uc_mcontext
= new ::ucontext_t();
98 #ifdef PDNS_USE_VALGRIND
100 #endif /* PDNS_USE_VALGRIND */
103 pdns_ucontext_t::~pdns_ucontext_t() {
104 delete static_cast<ucontext_t
*>(uc_mcontext
);
105 #ifdef PDNS_USE_VALGRIND
106 if (valgrind_id
!= 0) {
107 VALGRIND_STACK_DEREGISTER(valgrind_id
);
109 #endif /* PDNS_USE_VALGRIND */
114 (pdns_ucontext_t
& __restrict octx
, pdns_ucontext_t
const& __restrict ctx
) {
115 if (::swapcontext (static_cast<ucontext_t
*>(octx
.uc_mcontext
),
116 static_cast<ucontext_t
*>(ctx
.uc_mcontext
))) {
117 throw_errno ("swapcontext() failed");
120 std::rethrow_exception (ctx
.exception
);
126 (pdns_ucontext_t
& ctx
, boost::function
<void(void)>& start
) {
127 assert (ctx
.uc_link
);
128 assert (ctx
.uc_stack
.size());
130 auto const mcp
= static_cast<ucontext_t
*>(ctx
.uc_mcontext
);
131 auto const next
= static_cast<ucontext_t
*>(ctx
.uc_link
->uc_mcontext
);
132 if (::getcontext (mcp
)) {
133 throw_errno ("getcontext() failed");
136 mcp
->uc_stack
.ss_sp
= ctx
.uc_stack
.data();
137 mcp
->uc_stack
.ss_size
= ctx
.uc_stack
.size()-1;
138 mcp
->uc_stack
.ss_flags
= 0;
140 auto ctxarg
= splitPointer (&ctx
);
141 auto funarg
= splitPointer (&start
);
142 return ::makecontext (mcp
, reinterpret_cast<void(*)(void)>(&threadWrapper
),
143 4, ctxarg
.first
, ctxarg
.second
,
144 funarg
.first
, funarg
.second
);