Skip to content

Commit

Permalink
Check availability of constructor attribute and use it on Windows in …
Browse files Browse the repository at this point in the history
…favor of DllMain
  • Loading branch information
nikias committed Sep 19, 2021
1 parent 156fa95 commit 6cc8d69
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
18 changes: 18 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ case ${host_os} in
esac
AM_CONDITIONAL(WIN32, test x$win32 = xtrue)

# Check if the C compiler supports __attribute__((constructor))
AC_CACHE_CHECK([wether the C compiler supports constructor/destructor attributes],
ac_cv_attribute_constructor, [
ac_cv_attribute_constructor=no
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
[[
static void __attribute__((constructor)) test_constructor(void) {
}
static void __attribute__((destructor)) test_destructor(void) {
}
]], [])],
[ac_cv_attribute_constructor=yes]
)]
)
if test "$ac_cv_attribute_constructor" = "yes"; then
AC_DEFINE(HAVE_ATTRIBUTE_CONSTRUCTOR, 1, [Define if the C compiler supports constructor/destructor attributes])
fi

AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter -fsigned-char -fvisibility=hidden")
AC_SUBST(GLOBAL_CFLAGS)

Expand Down
40 changes: 23 additions & 17 deletions src/activation.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ static void internal_libideviceactivation_deinit(void)
}

#ifdef WIN32

typedef volatile struct {
LONG lock;
int state;
Expand All @@ -121,7 +120,29 @@ static void thread_once(thread_once_t *once_control, void (*init_routine)(void))
}
InterlockedExchange(&(once_control->lock), 0);
}
#else
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static pthread_once_t deinit_once = PTHREAD_ONCE_INIT;
#define thread_once pthread_once
#endif

#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR
#if defined(__llvm__) || defined(__GNUC__)
#define HAVE_ATTRIBUTE_CONSTRUCTOR
#endif
#endif

#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR
static void __attribute__((constructor)) libideviceactivation_initialize(void)
{
thread_once(&init_once, internal_libideviceactivation_init);
}

static void __attribute__((destructor)) libideviceactivation_deinitialize(void)
{
thread_once(&deinit_once, internal_libideviceactivation_deinit);
}
#elif defined(WIN32)
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason) {
Expand All @@ -136,25 +157,10 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
}
return 1;
}

#else

static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static pthread_once_t deinit_once = PTHREAD_ONCE_INIT;

static void __attribute__((constructor)) libideviceactivation_initialize(void)
{
pthread_once(&init_once, internal_libideviceactivation_init);
}

static void __attribute__((destructor)) libideviceactivation_deinitialize(void)
{
pthread_once(&deinit_once, internal_libideviceactivation_deinit);
}

#warning No compiler support for constructor/destructor attributes, some features might not be available.
#endif


static int debug_level = 0;

IDEVICE_ACTIVATION_API void idevice_activation_set_debug_level(int level) {
Expand Down

0 comments on commit 6cc8d69

Please sign in to comment.