Skip to content

Update gconstructor.h handling #4620

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Modelica/Resources/BuildProjects/CMake/src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ set(EXTC_SOURCES
"${MODELICA_SOURCE_DIR}/ModelicaRandom.h"
"${MODELICA_SOURCE_DIR}/ModelicaStrings.c"
"${MODELICA_SOURCE_DIR}/ModelicaStrings.h"
"${MODELICA_SOURCE_DIR}/gconstructor.h"
"${MODELICA_SOURCE_DIR}/g2constructor.h"
"${MODELICA_SOURCE_DIR}/stdint_msvc.h"
"${MODELICA_SOURCE_DIR}/stdint_wrap.h"
"${MODELICA_SOURCE_DIR}/uthash.h"
Expand All @@ -84,7 +84,7 @@ set(TABLES_SOURCES
"${MODELICA_SOURCE_DIR}/ModelicaStandardTables.h"
"${MODELICA_SOURCE_DIR}/ModelicaStandardTablesUsertab.c"
"${MODELICA_SOURCE_DIR}/ModelicaMatIO.h"
"${MODELICA_SOURCE_DIR}/gconstructor.h"
"${MODELICA_SOURCE_DIR}/g2constructor.h"
"${MODELICA_SOURCE_DIR}/stdint_msvc.h"
"${MODELICA_SOURCE_DIR}/stdint_wrap.h"
"${MODELICA_SOURCE_DIR}/uthash.h"
Expand Down
30 changes: 15 additions & 15 deletions Modelica/Resources/C-Sources/ModelicaInternal.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void ModelicaInternal_setenv(_In_z_ const char* name,
#define HASH_NO_STDINT 1
#define HASH_NONFATAL_OOM 1
#include "uthash.h"
#include "gconstructor.h"
#include "g2constructor.h"

#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -774,16 +774,16 @@ typedef struct FileCache {
static FileCache* fileCache = NULL;
#if defined(_POSIX_) && !defined(NO_MUTEX)
#include <pthread.h>
#if defined(G_HAS_CONSTRUCTORS)
#if defined(G2_HAS_CONSTRUCTORS)
static pthread_mutex_t m;
G_DEFINE_CONSTRUCTOR(initializeMutex)
static void initializeMutex(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex))
static void G2_FUNCNAME(initializeMutex)(void) {
if (pthread_mutex_init(&m, NULL) != 0) {
ModelicaError("Initialization of mutex failed\n");
}
}
G_DEFINE_DESTRUCTOR(destroyMutex)
static void destroyMutex(void) {
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex))
static void G2_FUNCNAME(destroyMutex)(void) {
if (pthread_mutex_destroy(&m) != 0) {
ModelicaError("Destruction of mutex failed\n");
}
Expand All @@ -793,24 +793,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
#endif
#define MUTEX_LOCK() pthread_mutex_lock(&m)
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
static CRITICAL_SECTION cs;
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaInternal_initializeCS)
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_initializeCS))
#endif
G_DEFINE_CONSTRUCTOR(ModelicaInternal_initializeCS)
static void ModelicaInternal_initializeCS(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaInternal_initializeCS))
static void G2_FUNCNAME(ModelicaInternal_initializeCS)(void) {
InitializeCriticalSection(&cs);
}
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaInternal_deleteCS)
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_deleteCS))
#endif
G_DEFINE_DESTRUCTOR(ModelicaInternal_deleteCS)
static void ModelicaInternal_deleteCS(void) {
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaInternal_deleteCS))
static void G2_FUNCNAME(ModelicaInternal_deleteCS)(void) {
DeleteCriticalSection(&cs);
}
#define MUTEX_LOCK() EnterCriticalSection(&cs)
Expand Down
20 changes: 10 additions & 10 deletions Modelica/Resources/C-Sources/ModelicaRandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include <string.h>
#include "ModelicaInternal.h"
#include "ModelicaUtilities.h"
#include "gconstructor.h"
#include "g2constructor.h"

/* The standard way to detect POSIX is to check _POSIX_VERSION,
* which is defined in <unistd.h>
Expand All @@ -68,24 +68,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)

/* On Windows systems define a critical section using the single static variable "cs" */
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
static CRITICAL_SECTION cs;
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaRandom_initializeCS)
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_initializeCS))
#endif
G_DEFINE_CONSTRUCTOR(ModelicaRandom_initializeCS)
static void ModelicaRandom_initializeCS(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaRandom_initializeCS))
static void G2_FUNCNAME(ModelicaRandom_initializeCS)(void) {
InitializeCriticalSection(&cs);
}
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaRandom_deleteCS)
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_deleteCS))
#endif
G_DEFINE_DESTRUCTOR(ModelicaRandom_deleteCS)
static void ModelicaRandom_deleteCS(void) {
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaRandom_deleteCS))
static void G2_FUNCNAME(ModelicaRandom_deleteCS)(void) {
DeleteCriticalSection(&cs);
}
#define MUTEX_LOCK() EnterCriticalSection(&cs)
Expand Down
30 changes: 15 additions & 15 deletions Modelica/Resources/C-Sources/ModelicaStandardTables.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
#define uthash_strlen(s) key_strlen(s)
#define HASH_NONFATAL_OOM 1
#include "uthash.h"
#include "gconstructor.h"
#include "g2constructor.h"
#endif
#include <assert.h>
#include <float.h>
Expand Down Expand Up @@ -433,16 +433,16 @@ typedef struct TableShare {
static TableShare* tableShare = NULL;
#if defined(_POSIX_) && !defined(NO_MUTEX)
#include <pthread.h>
#if defined(G_HAS_CONSTRUCTORS)
#if defined(G2_HAS_CONSTRUCTORS)
static pthread_mutex_t m;
G_DEFINE_CONSTRUCTOR(initializeMutex)
static void initializeMutex(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex))
static void G2_FUNCNAME(initializeMutex)(void) {
if (pthread_mutex_init(&m, NULL) != 0) {
ModelicaError("Initialization of mutex failed\n");
}
}
G_DEFINE_DESTRUCTOR(destroyMutex)
static void destroyMutex(void) {
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex))
static void G2_FUNCNAME(destroyMutex)(void) {
if (pthread_mutex_destroy(&m) != 0) {
ModelicaError("Destruction of mutex failed\n");
}
Expand All @@ -452,24 +452,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
#endif
#define MUTEX_LOCK() pthread_mutex_lock(&m)
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
static CRITICAL_SECTION cs;
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_initializeCS)
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_initializeCS))
#endif
G_DEFINE_CONSTRUCTOR(ModelicaStandardTables_initializeCS)
static void ModelicaStandardTables_initializeCS(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_initializeCS))
static void G2_FUNCNAME(ModelicaStandardTables_initializeCS)(void) {
InitializeCriticalSection(&cs);
}
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_deleteCS)
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_deleteCS))
#endif
G_DEFINE_DESTRUCTOR(ModelicaStandardTables_deleteCS)
static void ModelicaStandardTables_deleteCS(void) {
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_deleteCS))
static void G2_FUNCNAME(ModelicaStandardTables_deleteCS)(void) {
DeleteCriticalSection(&cs);
}
#define MUTEX_LOCK() EnterCriticalSection(&cs)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,85 @@
/* gconstructor.h - Module constructor and destructor helper header
/* g2constructor.h - Module constructor and destructor helper header

If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
If G2_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
destructors, in a sane way, including e.g. on library unload. If not you're on
your own.

Some compilers need #pragma to handle this, which does not work with macros,
so the way you need to use this is (for constructors):

#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(my_constructor))
#endif
G_DEFINE_CONSTRUCTOR(my_constructor)
static void my_constructor(void) {
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(my_constructor))
static void G2_FUNCNAME(my_constructor)(void) {
...
}

*/

#ifndef G_CONSTRUCTOR_H_
#define G_CONSTRUCTOR_H_
#ifndef G2_CONSTRUCTOR_H_
#define G2_CONSTRUCTOR_H_

#ifndef G2_MODEL_PREFIX
#ifdef MODEL_IDENTIFIER
#define G2_MODEL_PREFIX MODEL_IDENTIFIER
#else
#define G2_MODEL_PREFIX
#endif
#endif

#define G2_CONCAT(a, b) a ## b
#define G2_CONCAT_(a, b) G2_CONCAT(a, b)
#define G2_FUNCNAME(name) G2_CONCAT_(G2_MODEL_PREFIX, name)

#if defined(__cplusplus)

#define G_HAS_CONSTRUCTORS 1
#define G2_HAS_CONSTRUCTORS 1

#define G2_DEFINE_CONSTRUCTOR(_func) G2_CXX_CTOR(_func)
#define G2_DEFINE_DESTRUCTOR(_func) G2_CXX_DTOR(_func)

#define G_DEFINE_CONSTRUCTOR(_func) \
#define G2_CXX_CTOR(_func) \
static void _func(void); \
struct _func ## _wrapper_struct { _func ## _wrapper_struct() { _func(); } }; \
static _func ## _wrapper_struct _func ## _wrapper;

#define G_DEFINE_DESTRUCTOR(_func) \
#define G2_CXX_DTOR(_func) \
static void _func(void); \
struct _func ## _wrapper_struct2 { ~_func ## _wrapper_struct2() { _func(); } }; \
static _func ## _wrapper_struct2 _func ## _wrapper2;

#elif (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))) || \
defined(__clang__)

#define G_HAS_CONSTRUCTORS 1
#define G2_HAS_CONSTRUCTORS 1

#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
#define G2_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
#define G2_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);

#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
/* Visual Studio 2008 and later has _pragma */

#define G_HAS_CONSTRUCTORS 1
#define G2_HAS_CONSTRUCTORS 1

#ifdef _WIN64
#define G_MSVC_SYMBOL_PREFIX ""
#define G2_MSVC_SYMBOL_PREFIX ""
#else
#define G_MSVC_SYMBOL_PREFIX "_"
#define G2_MSVC_SYMBOL_PREFIX "_"
#endif

#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
#define G2_DEFINE_CONSTRUCTOR(_func) G2_MSVC_CTOR(_func, G2_MSVC_SYMBOL_PREFIX)
#define G2_DEFINE_DESTRUCTOR(_func) G2_MSVC_DTOR(_func, G2_MSVC_SYMBOL_PREFIX)

#define G_MSVC_CTOR(_func,_sym_prefix) \
#define G2_MSVC_CTOR(_func, _sym_prefix) \
static void _func(void); \
extern int (* _array ## _func)(void); \
int _func ## _wrapper(void) { _func(); return _array ## _func == NULL; } \
__pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
__pragma(section(".CRT$XCU",read)) \
__declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;

#define G_MSVC_DTOR(_func,_sym_prefix) \
#define G2_MSVC_DTOR(_func, _sym_prefix) \
static void _func(void); \
extern int (* _array ## _func)(void); \
int _func ## _constructor(void) { atexit (_func); return _array ## _func == NULL; } \
Expand All @@ -74,22 +89,22 @@

#elif defined(_MSC_VER) && (_MSC_VER >= 1400)

#define G_HAS_CONSTRUCTORS 1
#define G2_HAS_CONSTRUCTORS 1

/* Pre Visual Studio 2008 must use #pragma section */
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1

#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define G_DEFINE_CONSTRUCTOR(_func) \
#define G2_DEFINE_CONSTRUCTOR(_func) \
static void _func(void); \
static int _func ## _wrapper(void) { _func(); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;

#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define G_DEFINE_DESTRUCTOR(_func) \
#define G2_DEFINE_DESTRUCTOR(_func) \
static void _func(void); \
static int _func ## _constructor(void) { atexit (_func); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
Expand All @@ -100,19 +115,19 @@
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
*/

#define G_HAS_CONSTRUCTORS 1
#define G2_HAS_CONSTRUCTORS 1

#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1

#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
init(_func)
#define G_DEFINE_CONSTRUCTOR(_func) \
#define G2_DEFINE_CONSTRUCTOR(_func) \
static void _func(void);

#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
fini(_func)
#define G_DEFINE_DESTRUCTOR(_func) \
#define G2_DEFINE_DESTRUCTOR(_func) \
static void _func(void);

#else
Expand Down