Skip to content

Commit

Permalink
c11,fwd: Reimplement function aliases in a weird way
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Nov 4, 2024
1 parent e0d762a commit 66d6c1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 106 deletions.
138 changes: 34 additions & 104 deletions mcfgthread/c11.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,21 @@ enum __MCF_thrd_error
__MCF_thrd_error_end
};

/* We are not allowed to define non-reserved names as macros, so do it in a
* weird way. It's very weird, but it works! */
#if defined __GNUC__ || defined __clang__
# define __MCF_C11_ALIAS(f) __typeof__(__MCF_c11_##f) f __asm__(__MCF_USYM "__MCF_c11_" #f)
#else
# define __MCF_C11_ALIAS(f) static __typeof__(__MCF_c11_##f)* const f = __MCF_c11_##f
#endif

/* 7.26.2.1 The call_once function */
__MCF_C11_IMPORT
void
__MCF_c11_call_once(once_flag* __once, __MCF_once_callback* __init_proc) __MCF_MAY_THROW;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
void
call_once(once_flag* __once, __MCF_once_callback* __init_proc) __MCF_MAY_THROW
__MCF_ASM_CALL(__MCF_c11_call_once);
__MCF_C11_ALIAS(call_once);
# define __MCF_c11_call_once call_once
#endif

Expand All @@ -99,10 +104,7 @@ int
__MCF_c11_cnd_broadcast(cnd_t* __cond) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
cnd_broadcast(cnd_t* __cond) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_broadcast);
__MCF_C11_ALIAS(cnd_broadcast);
# define __MCF_c11_cnd_broadcast cnd_broadcast
#endif

Expand All @@ -112,10 +114,7 @@ void
__MCF_c11_cnd_destroy(cnd_t* __cond) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
void
cnd_destroy(cnd_t* __cond) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_destroy);
__MCF_C11_ALIAS(cnd_destroy);
# define __MCF_c11_cnd_destroy cnd_destroy
#endif

Expand All @@ -125,10 +124,7 @@ int
__MCF_c11_cnd_init(cnd_t* __cond) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
cnd_init(cnd_t* __cond) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_init);
__MCF_C11_ALIAS(cnd_init);
# define __MCF_c11_cnd_init cnd_init
#endif

Expand All @@ -138,10 +134,7 @@ int
__MCF_c11_cnd_signal(cnd_t* __cond) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
cnd_signal(cnd_t* __cond) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_signal);
__MCF_C11_ALIAS(cnd_signal);
# define __MCF_c11_cnd_signal cnd_signal
#endif

Expand All @@ -151,10 +144,7 @@ int
__MCF_c11_cnd_timedwait(cnd_t* __cond, mtx_t* __mtx, const __MCF_timespec* __ts) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
cnd_timedwait(cnd_t* __cond, mtx_t* __mtx, const __MCF_timespec* __ts) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_timedwait);
__MCF_C11_ALIAS(cnd_timedwait);
# define __MCF_c11_cnd_timedwait cnd_timedwait
#endif

Expand All @@ -164,10 +154,7 @@ int
__MCF_c11_cnd_wait(cnd_t* __cond, mtx_t* __mtx) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
cnd_wait(cnd_t* __cond, mtx_t* __mtx) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_cnd_wait);
__MCF_C11_ALIAS(cnd_wait);
# define __MCF_c11_cnd_wait cnd_wait
#endif

Expand All @@ -177,10 +164,7 @@ void
__MCF_c11_mtx_destroy(mtx_t* __mtx) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
void
mtx_destroy(mtx_t* __mtx) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_destroy);
__MCF_C11_ALIAS(mtx_destroy);
# define __MCF_c11_mtx_destroy mtx_destroy
#endif

Expand All @@ -190,10 +174,7 @@ int
__MCF_c11_mtx_init(mtx_t* __mtx, int __type) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
mtx_init(mtx_t* __mtx, int __type) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_init);
__MCF_C11_ALIAS(mtx_init);
# define __MCF_c11_mtx_init mtx_init
#endif

Expand All @@ -203,10 +184,7 @@ int
__MCF_c11_mtx_lock(mtx_t* __mtx) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
mtx_lock(mtx_t* __mtx) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_lock);
__MCF_C11_ALIAS(mtx_lock);
# define __MCF_c11_mtx_lock mtx_lock
#endif

Expand All @@ -216,10 +194,7 @@ int
__MCF_c11_mtx_timedlock(mtx_t* __mtx, const __MCF_timespec* __ts) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
mtx_timedlock(mtx_t* __mtx, const __MCF_timespec* __ts) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_timedlock);
__MCF_C11_ALIAS(mtx_timedlock);
# define __MCF_c11_mtx_timedlock mtx_timedlock
#endif

Expand All @@ -229,10 +204,7 @@ int
__MCF_c11_mtx_trylock(mtx_t* __mtx) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
mtx_trylock(mtx_t* __mtx) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_trylock);
__MCF_C11_ALIAS(mtx_trylock);
# define __MCF_c11_mtx_trylock mtx_trylock
#endif

Expand All @@ -242,10 +214,7 @@ int
__MCF_c11_mtx_unlock(mtx_t* __mtx) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
mtx_unlock(mtx_t* __mtx) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_mtx_unlock);
__MCF_C11_ALIAS(mtx_unlock);
# define __MCF_c11_mtx_unlock mtx_unlock
#endif

Expand All @@ -255,10 +224,7 @@ int
__MCF_c11_thrd_create(thrd_t* __thrd, thrd_start_t __proc, void* __arg) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
thrd_create(thrd_t* __thrd, thrd_start_t __proc, void* __arg) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_create);
__MCF_C11_ALIAS(thrd_create);
# define __MCF_c11_thrd_create thrd_create
#endif

Expand All @@ -268,10 +234,7 @@ thrd_t
__MCF_c11_thrd_current(void) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE __MCF_FN_CONST
thrd_t
thrd_current(void) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_current);
__MCF_C11_ALIAS(thrd_current);
# define __MCF_c11_thrd_current thrd_current
#endif

Expand All @@ -281,10 +244,7 @@ int
__MCF_c11_thrd_detach(thrd_t __thrd) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
thrd_detach(thrd_t __thrd) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_detach);
__MCF_C11_ALIAS(thrd_detach);
# define __MCF_c11_thrd_detach thrd_detach
#endif

Expand All @@ -294,10 +254,7 @@ int
__MCF_c11_thrd_equal(thrd_t __t1, thrd_t __t2) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE __MCF_FN_PURE __MCF_CXX11(constexpr)
int
thrd_equal(thrd_t __t1, thrd_t __t2) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_equal);
__MCF_C11_ALIAS(thrd_equal);
# define __MCF_c11_thrd_equal thrd_equal
#endif

Expand All @@ -307,10 +264,7 @@ void
__MCF_c11_thrd_exit(int __res) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE __MCF_NEVER_RETURN
void
thrd_exit(int __res) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_exit);
__MCF_C11_ALIAS(thrd_exit);
# define __MCF_c11_thrd_exit thrd_exit
#endif

Expand All @@ -320,10 +274,7 @@ int
__MCF_c11_thrd_join(thrd_t __thrd, int* __resp_opt) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
thrd_join(thrd_t __thrd, int* __resp_opt) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_join);
__MCF_C11_ALIAS(thrd_join);
# define __MCF_c11_thrd_join thrd_join
#endif

Expand All @@ -333,10 +284,7 @@ int
__MCF_c11_thrd_sleep(const __MCF_timespec* __dur, __MCF_timespec* __rem_opt) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_IMPORT
int
thrd_sleep(const __MCF_timespec* __dur, __MCF_timespec* __rem_opt) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_sleep);
__MCF_C11_ALIAS(thrd_sleep);
# define __MCF_c11_thrd_sleep thrd_sleep
#endif

Expand All @@ -347,10 +295,7 @@ int
__MCF_c11__thrd_sleep_until(const __MCF_timespec* __ts) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_IMPORT
int
_thrd_sleep_until(const __MCF_timespec* __ts) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11__thrd_sleep_until);
__MCF_C11_ALIAS(_thrd_sleep_until);
# define __MCF_c11__thrd_sleep_until _thrd_sleep_until
#endif

Expand All @@ -360,10 +305,7 @@ void
__MCF_c11_thrd_yield(void) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
void
thrd_yield(void) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_thrd_yield);
__MCF_C11_ALIAS(thrd_yield);
# define __MCF_c11_thrd_yield thrd_yield
#endif

Expand All @@ -373,10 +315,7 @@ int
__MCF_c11_tss_create(tss_t* __keyp, tss_dtor_t __dtor_opt) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
tss_create(tss_t* __keyp, tss_dtor_t __dtor_opt) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_tss_create);
__MCF_C11_ALIAS(tss_create);
# define __MCF_c11_tss_create tss_create
#endif

Expand All @@ -386,10 +325,7 @@ void
__MCF_c11_tss_delete(tss_t __key) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
void
tss_delete(tss_t __key) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_tss_delete);
__MCF_C11_ALIAS(tss_delete);
# define __MCF_c11_tss_delete tss_delete
#endif

Expand All @@ -399,10 +335,7 @@ void*
__MCF_c11_tss_get(tss_t __key) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE __MCF_FN_PURE
void*
tss_get(tss_t __key) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_tss_get);
__MCF_C11_ALIAS(tss_get);
# define __MCF_c11_tss_get tss_get
#endif

Expand All @@ -412,10 +345,7 @@ int
__MCF_c11_tss_set(tss_t __key, void* __val_opt) __MCF_noexcept;

#ifndef __MCF_C11_NO_ALIASES
__MCF_C11_INLINE
int
tss_set(tss_t __key, void* __val_opt) __MCF_noexcept
__MCF_ASM_CALL(__MCF_c11_tss_set);
__MCF_C11_ALIAS(tss_set);
# define __MCF_c11_tss_set tss_set
#endif

Expand Down
8 changes: 6 additions & 2 deletions mcfgthread/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ __MCF_CXX(extern "C" {)
# define __MCF_FN_CONST __attribute__((__const__))
# define __MCF_FN_PURE __attribute__((__pure__))
# define __MCF_FN_COLD __attribute__((__cold__))
# define __MCF_ASM_CALL(x) __asm__(__MCF_S(__USER_LABEL_PREFIX__) #x)
# define __MCF_ALIGNED(x) __attribute__((__aligned__(x)))
# define __MCF_UNREACHABLE __builtin_unreachable()
#else
Expand All @@ -136,7 +135,6 @@ __MCF_CXX(extern "C" {)
# define __MCF_FN_CONST __declspec(noalias)
# define __MCF_FN_PURE __declspec(noalias)
# define __MCF_FN_COLD /* unsupported */
# define __MCF_ASM_CALL(x) /* unnecessary */
# define __MCF_ALIGNED(x) __declspec(align(x))
# define __MCF_UNREACHABLE __assume(0)
#endif
Expand Down Expand Up @@ -182,6 +180,7 @@ __MCF_CXX(extern "C" {)
__asm__ volatile ("{ mov %2, %%gs:%c0(,%1,8) | mov gs:[%0+%1*8], %2 }" : : "i"(base), "r"(i), "r"(in))

# define __MCF_64_32(x, y) x
# define __MCF_USYM ""

#elif defined _MSC_VER && (defined _M_X64 && !defined _M_ARM64EC)

Expand Down Expand Up @@ -210,6 +209,7 @@ __MCF_CXX(extern "C" {)
__writegsqword((base) + (i) * 8U, (in))

# define __MCF_64_32(x, y) x
# define __MCF_USYM ""

#elif (defined __GNUC__ || defined __clang__) && defined __i386__

Expand Down Expand Up @@ -238,6 +238,7 @@ __MCF_CXX(extern "C" {)
__asm__ volatile ("{ mov %2, %%fs:%c0(,%1,4) | mov fs:[%0+%1*4], %2 }" : : "i"(base), "r"(i), "r"(in))

# define __MCF_64_32(x, y) y
# define __MCF_USYM "_"

#elif defined _MSC_VER && defined _M_IX86

Expand Down Expand Up @@ -266,6 +267,7 @@ __MCF_CXX(extern "C" {)
__writefsdword((base) + (i) * 4U, (in))

# define __MCF_64_32(x, y) y
# define __MCF_USYM "_"

#elif (defined __GNUC__ || defined __clang__) && (defined __aarch64__ || defined __arm64ec__)

Expand Down Expand Up @@ -294,6 +296,7 @@ __MCF_CXX(extern "C" {)
__asm__ volatile (" str %0, [x18,%w1,uxtw #3] " : : "r"(in), "r"((base) / 8U + (i)))

# define __MCF_64_32(x, y) x
# define __MCF_USYM ""

#elif defined _MSC_VER && (defined _M_ARM64 || defined _M_ARM64EC)

Expand Down Expand Up @@ -322,6 +325,7 @@ __MCF_CXX(extern "C" {)
__writex18qword((base) + (i) * 8U, (in))

# define __MCF_64_32(x, y) x
# define __MCF_USYM ""

#endif /* compiler and target */

Expand Down

0 comments on commit 66d6c1c

Please sign in to comment.