Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move get_stack_ptr to source #6594

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/core/coroutines/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(coroutines_sources
detail/context_posix.cpp
detail/coroutine_impl.cpp
detail/coroutine_self.cpp
detail/get_stack_pointer.cpp
detail/posix_utility.cpp
detail/tss.cpp
swapcontext.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2013-2016 Thomas Heller
// Copyright (c) 2022 Christopher Taylor
// Copyright (c) 2024 Isidoros Tsaousis-Seiras
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0.
Expand All @@ -8,8 +9,7 @@

#pragma once

#include <hpx/config/compiler_specific.hpp>
#include <hpx/config/defines.hpp>
#include <hpx/config.hpp>

#if defined(HPX_WINDOWS)
#define HPX_HAVE_THREADS_GET_STACK_POINTER
Expand All @@ -28,28 +28,6 @@
#include <limits>

namespace hpx::threads::coroutines::detail {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function is defined only for #if !defined(HPX_WINDOWS), shouldn't it be declared only in that case as well?

Copy link
Contributor Author

@isidorostsa isidorostsa Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you are describing already happens since

extends to the bottom of the document


inline std::size_t get_stack_ptr() noexcept
{
#if defined(HPX_HAVE_BUILTIN_FRAME_ADDRESS)
return std::size_t(__builtin_frame_address(0));
#else
std::size_t stack_ptr = (std::numeric_limits<std::size_t>::max)();
#if defined(__x86_64__) || defined(__amd64)
asm("movq %%rsp, %0" : "=r"(stack_ptr));
#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__)
asm("movl %%esp, %0" : "=r"(stack_ptr));
#elif defined(__powerpc__)
void* stack_ptr_p = &stack_ptr;
asm("stw %%r1, 0(%0)" : "=&r"(stack_ptr_p));
#elif defined(__arm__)
asm("mov %0, sp" : "=r"(stack_ptr));
#elif defined(__riscv)
__asm__ __volatile__("add %0, x0, sp" : "=r"(stack_ptr));
#endif
return stack_ptr;
#endif
}
HPX_CORE_EXPORT std::size_t get_stack_ptr() noexcept;
} // namespace hpx::threads::coroutines::detail
#endif
42 changes: 42 additions & 0 deletions libs/core/coroutines/src/detail/get_stack_pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2013-2016 Thomas Heller
// Copyright (c) 2022 Christopher Taylor
// Copyright (c) 2024 Isidoros Tsaousis-Seiras
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <hpx/coroutines/detail/get_stack_pointer.hpp>

#if !defined(HPX_WINDOWS)

#include <cstddef>
#include <limits>

namespace hpx::threads::coroutines::detail {

std::size_t get_stack_ptr() noexcept
{
#if defined(HPX_HAVE_BUILTIN_FRAME_ADDRESS)
return std::size_t(__builtin_frame_address(0));
#else
std::size_t stack_ptr = (std::numeric_limits<std::size_t>::max)();
#if defined(__x86_64__) || defined(__amd64)
asm("movq %%rsp, %0" : "=r"(stack_ptr));
#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__)
asm("movl %%esp, %0" : "=r"(stack_ptr));
#elif defined(__powerpc__)
void* stack_ptr_p = &stack_ptr;
asm("stw %%r1, 0(%0)" : "=&r"(stack_ptr_p));
#elif defined(__arm__)
asm("mov %0, sp" : "=r"(stack_ptr));
#elif defined(__riscv)
__asm__ __volatile__("add %0, x0, sp" : "=r"(stack_ptr));
#endif
return stack_ptr;
#endif
}
} // namespace hpx::threads::coroutines::detail
#endif
Loading