Use common pattern for function-like macro (#4625)

This commit is contained in:
Alexander Shabalin
2021-01-12 12:47:00 +03:00
committed by Nikolay Krasko
parent 32e3deace1
commit adf80418af
4 changed files with 36 additions and 22 deletions
@@ -129,10 +129,10 @@ class CyclicCollector {
public: public:
CyclicCollector() { CyclicCollector() {
CHECK_CALL(pthread_mutex_init(&lock_, nullptr), "Cannot init collector mutex") CHECK_CALL(pthread_mutex_init(&lock_, nullptr), "Cannot init collector mutex");
CHECK_CALL(pthread_mutex_init(&timestampLock_, nullptr), "Cannot init collector timestamp mutex") CHECK_CALL(pthread_mutex_init(&timestampLock_, nullptr), "Cannot init collector timestamp mutex");
CHECK_CALL(pthread_cond_init(&cond_, nullptr), "Cannot init collector condition") CHECK_CALL(pthread_cond_init(&cond_, nullptr), "Cannot init collector condition");
CHECK_CALL(pthread_create(&gcThread_, nullptr, gcWorkerRoutine, this), "Cannot start collector thread") CHECK_CALL(pthread_create(&gcThread_, nullptr, gcWorkerRoutine, this), "Cannot start collector thread");
} }
void clear() { void clear() {
@@ -146,7 +146,7 @@ class CyclicCollector {
Locker locker(&lock_); Locker locker(&lock_);
terminateCollector_ = true; terminateCollector_ = true;
if (enabled) shallRunCollector_ = true; if (enabled) shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector");
} }
// TODO: improve waiting for collector termination. // TODO: improve waiting for collector termination.
while (atomicGet(&terminateCollector_)) {} while (atomicGet(&terminateCollector_)) {}
@@ -173,7 +173,7 @@ class CyclicCollector {
KStdUnorderedMap<ObjHeader*, int> sideRefCounts; KStdUnorderedMap<ObjHeader*, int> sideRefCounts;
int restartCount = 0; int restartCount = 0;
while (!terminateCollector_) { while (!terminateCollector_) {
CHECK_CALL(pthread_cond_wait(&cond_, &lock_), "Cannot wait collector condition") CHECK_CALL(pthread_cond_wait(&cond_, &lock_), "Cannot wait collector condition");
if (!shallRunCollector_) continue; if (!shallRunCollector_) continue;
atomicSet(&gcRunning_, 1); atomicSet(&gcRunning_, 1);
restartCount = 0; restartCount = 0;
@@ -325,7 +325,7 @@ class CyclicCollector {
// When exiting the worker - we shall collect the cyclic garbage here. // When exiting the worker - we shall collect the cyclic garbage here.
if (enabled) { if (enabled) {
shallRunCollector_ = true; shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector");
} }
currentAliveWorkers_--; currentAliveWorkers_--;
} }
@@ -416,7 +416,7 @@ class CyclicCollector {
if (checkIfShallCollect()) { if (checkIfShallCollect()) {
Locker locker(&lock_); Locker locker(&lock_);
shallRunCollector_ = true; shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector");
} }
} }
@@ -424,7 +424,7 @@ class CyclicCollector {
if (atomicGet(&gcRunning_) != 0) return; if (atomicGet(&gcRunning_) != 0) return;
Locker lock(&lock_); Locker lock(&lock_);
shallRunCollector_ = true; shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector");
} }
void localGC() { void localGC() {
@@ -1164,7 +1164,7 @@ void freeAggregatingFrozenContainer(ContainerHeader* container) {
auto* state = memoryState; auto* state = memoryState;
RuntimeAssert(isAggregatingFrozenContainer(container), "expected fictitious frozen container"); RuntimeAssert(isAggregatingFrozenContainer(container), "expected fictitious frozen container");
MEMORY_LOG("%p is fictitious frozen container\n", container); MEMORY_LOG("%p is fictitious frozen container\n", container);
RuntimeAssert(!container->buffered(), "frozen objects must not participate in GC") RuntimeAssert(!container->buffered(), "frozen objects must not participate in GC");
#if USE_GC #if USE_GC
// Forbid finalizerQueue handling. // Forbid finalizerQueue handling.
++state->finalizerQueueSuspendCount; ++state->finalizerQueueSuspendCount;
@@ -2056,7 +2056,7 @@ MemoryState* initMemory(bool firstRuntime) {
== ==
offsetof(MetaObjHeader, typeInfo_), offsetof(MetaObjHeader, typeInfo_),
"Layout mismatch"); "Layout mismatch");
RuntimeAssert(sizeof(FrameOverlay) % sizeof(ObjHeader**) == 0, "Frame overlay should contain only pointers") RuntimeAssert(sizeof(FrameOverlay) % sizeof(ObjHeader**) == 0, "Frame overlay should contain only pointers");
RuntimeAssert(memoryState == nullptr, "memory state must be clear"); RuntimeAssert(memoryState == nullptr, "memory state must be clear");
memoryState = konanConstructInstance<MemoryState>(); memoryState = konanConstructInstance<MemoryState>();
INIT_EVENT(memoryState) INIT_EVENT(memoryState)
+24 -10
View File
@@ -25,6 +25,13 @@
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
#if KONAN_ENABLE_ASSERT
#define CURRENT_SOURCE_LOCATION __FILE__ ":" TOSTRING(__LINE__)
#else
// Do not generate location strings, when asserts are disabled to reduce code size.
#define CURRENT_SOURCE_LOCATION nullptr
#endif
RUNTIME_NORETURN void RuntimeAssertFailed(const char* location, const char* message, ...); RUNTIME_NORETURN void RuntimeAssertFailed(const char* location, const char* message, ...);
namespace internal { namespace internal {
@@ -46,24 +53,31 @@ extern "C" const int KonanNeedDebugInfo;
#if KONAN_ENABLE_ASSERT #if KONAN_ENABLE_ASSERT
// Use RuntimeAssert() in internal state checks, which could be ignored in production. // Use RuntimeAssert() in internal state checks, which could be ignored in production.
#define RuntimeAssert(condition, format, ...) \ #define RuntimeAssert(condition, format, ...) \
if (KonanNeedDebugInfo && (!(condition))) { \ do { \
RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), format, ##__VA_ARGS__); \ if (KonanNeedDebugInfo && (!(condition))) { \
} RuntimeAssertFailed(CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \
} \
} while (false)
#else #else
#define RuntimeAssert(condition, message) #define RuntimeAssert(condition, format, ...) \
do { \
} while (false)
#endif #endif
// Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead // Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead
// to program termination. Never compiled out. // to program termination. Never compiled out.
#define RuntimeCheck(condition, format, ...) \ // TODO: Consider using `CURRENT_SOURCE_LOCATION` when `KonanNeedDebugInfo` is `true`.
if (!(condition)) { \ #define RuntimeCheck(condition, format, ...) \
RuntimeAssertFailed(nullptr, format, ##__VA_ARGS__); \ do { \
} if (!(condition)) { \
RuntimeAssertFailed(nullptr, format, ##__VA_ARGS__); \
} \
} while (false)
#define TODO(...) \ #define TODO(...) \
do { \ do { \
::internal::TODOImpl(__FILE__ ":" TOSTRING(__LINE__), ##__VA_ARGS__); \ ::internal::TODOImpl(CURRENT_SOURCE_LOCATION, ##__VA_ARGS__); \
} while (false) } while (false)
#endif // RUNTIME_ASSERT_H #endif // RUNTIME_ASSERT_H
@@ -177,7 +177,7 @@ void BackRefFromAssociatedObject::releaseRef() {
} }
void BackRefFromAssociatedObject::detach() { void BackRefFromAssociatedObject::detach() {
RuntimeAssert(atomicGet(&refCount) == 0, "unexpected refCount") RuntimeAssert(atomicGet(&refCount) == 0, "unexpected refCount");
obj_ = nullptr; // Handled in addRef/tryAddRef/releaseRef/ref. obj_ = nullptr; // Handled in addRef/tryAddRef/releaseRef/ref.
} }