diff options
| author | 2021-09-13 18:41:59 +0200 | |
|---|---|---|
| committer | 2021-09-13 18:41:59 +0200 | |
| commit | 677b0c03f8fca937f6b16da3341602637d092179 (patch) | |
| tree | 7a653c83ed13835081b2a556c18a0e325a706bc7 /src | |
| parent | 68f1d4a136fca04665a209f2a215b27bee377003 (diff) | |
| download | libplist-677b0c03f8fca937f6b16da3341602637d092179.tar.gz libplist-677b0c03f8fca937f6b16da3341602637d092179.tar.bz2 | |
Check availability of constructor attribute and use it on Windows in favor of DllMain
Diffstat (limited to 'src')
| -rw-r--r-- | src/plist.c | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/src/plist.c b/src/plist.c index 454d08c..d0e6c77 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -63,7 +63,6 @@ static void internal_plist_deinit(void) | |||
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | #ifdef WIN32 | 65 | #ifdef WIN32 |
| 66 | |||
| 67 | typedef volatile struct { | 66 | typedef volatile struct { |
| 68 | LONG lock; | 67 | LONG lock; |
| 69 | int state; | 68 | int state; |
| @@ -83,7 +82,29 @@ static void thread_once(thread_once_t *once_control, void (*init_routine)(void)) | |||
| 83 | } | 82 | } |
| 84 | InterlockedExchange(&(once_control->lock), 0); | 83 | InterlockedExchange(&(once_control->lock), 0); |
| 85 | } | 84 | } |
| 85 | #else | ||
| 86 | static pthread_once_t init_once = PTHREAD_ONCE_INIT; | ||
| 87 | static pthread_once_t deinit_once = PTHREAD_ONCE_INIT; | ||
| 88 | #define thread_once pthread_once | ||
| 89 | #endif | ||
| 90 | |||
| 91 | #ifndef HAVE_ATTRIBUTE_CONSTRUCTOR | ||
| 92 | #if defined(__llvm__) || defined(__GNUC__) | ||
| 93 | #define HAVE_ATTRIBUTE_CONSTRUCTOR | ||
| 94 | #endif | ||
| 95 | #endif | ||
| 96 | |||
| 97 | #ifdef HAVE_ATTRIBUTE_CONSTRUCTOR | ||
| 98 | static void __attribute__((constructor)) libplist_initialize(void) | ||
| 99 | { | ||
| 100 | thread_once(&init_once, internal_plist_init); | ||
| 101 | } | ||
| 86 | 102 | ||
| 103 | static void __attribute__((destructor)) libplist_deinitialize(void) | ||
| 104 | { | ||
| 105 | thread_once(&deinit_once, internal_plist_deinit); | ||
| 106 | } | ||
| 107 | #elif defined(WIN32) | ||
| 87 | BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) | 108 | BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) |
| 88 | { | 109 | { |
| 89 | switch (dwReason) { | 110 | switch (dwReason) { |
| @@ -98,22 +119,8 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) | |||
| 98 | } | 119 | } |
| 99 | return 1; | 120 | return 1; |
| 100 | } | 121 | } |
| 101 | |||
| 102 | #else | 122 | #else |
| 103 | 123 | #warning No compiler support for constructor/destructor attributes, some features might not be available. | |
| 104 | static pthread_once_t init_once = PTHREAD_ONCE_INIT; | ||
| 105 | static pthread_once_t deinit_once = PTHREAD_ONCE_INIT; | ||
| 106 | |||
| 107 | static void __attribute__((constructor)) libplist_initialize(void) | ||
| 108 | { | ||
| 109 | pthread_once(&init_once, internal_plist_init); | ||
| 110 | } | ||
| 111 | |||
| 112 | static void __attribute__((destructor)) libplist_deinitialize(void) | ||
| 113 | { | ||
| 114 | pthread_once(&deinit_once, internal_plist_deinit); | ||
| 115 | } | ||
| 116 | |||
| 117 | #endif | 124 | #endif |
| 118 | 125 | ||
| 119 | #ifndef HAVE_MEMMEM | 126 | #ifndef HAVE_MEMMEM |
