tried to implement network logging. app completely broken. probably threading issue.

This commit is contained in:
Dennis Eichhorn 2024-11-30 06:07:51 +01:00
parent 4b70891c88
commit 87f29a8310
8 changed files with 86 additions and 61 deletions

View File

@ -14,6 +14,8 @@ global_persist DebugContainer* debug_container = NULL;
#if _WIN32
#include <windows.h>
#include "../platform/win32/threading/Atomic.h"
#include "../platform/win32/threading/Spinlock.cpp"
void setup_performance_count() {
if (!debug_container) {
return;
@ -24,6 +26,8 @@ global_persist DebugContainer* debug_container = NULL;
debug_container->performance_count_frequency = perf_counter.QuadPart;
}
#elif __linux__
#include "../platform/linux/threading/Atomic.h"
#include "../platform/linux/threading/Spinlock.cpp"
void setup_performance_count() {
if (!debug_container) {
return;
@ -98,9 +102,7 @@ void update_timing_stat(uint32 stat, const char* function)
inline
void update_timing_stat_start(uint32 stat, const char*)
{
spinlock_start(&debug_container->perf_stats_spinlock);
debug_container->perf_stats[stat].old_tick_count = __rdtsc();
spinlock_end(&debug_container->perf_stats_spinlock);
atomic_set((int64 *) &debug_container->perf_stats[stat].old_tick_count, __rdtsc());
}
inline
@ -134,33 +136,25 @@ void update_timing_stat_end_continued(uint32 stat, const char* function)
inline
void update_timing_stat_reset(uint32 stat)
{
spinlock_start(&debug_container->perf_stats_spinlock);
debug_container->perf_stats[stat].function = NULL;
spinlock_end(&debug_container->perf_stats_spinlock);
atomic_set((int32 *) debug_container->perf_stats[stat].function, 0);
}
inline
void reset_counter(int32 id)
{
spinlock_start(&debug_container->perf_stats_spinlock);
debug_container->counter[id] = 0;
spinlock_end(&debug_container->perf_stats_spinlock);
atomic_set(&debug_container->counter[id], 0);
}
inline
void log_increment(int32 id, int32 by = 1)
{
spinlock_start(&debug_container->perf_stats_spinlock);
debug_container->counter[id] += by;
spinlock_end(&debug_container->perf_stats_spinlock);
atomic_add(&debug_container->counter[id], by);
}
inline
void log_counter(int32 id, int32 value)
{
spinlock_start(&debug_container->perf_stats_spinlock);
debug_container->counter[id] = value;
spinlock_end(&debug_container->perf_stats_spinlock);
atomic_set(&debug_container->counter[id], value);
}
// @todo don't use a pointer to this should be in a global together with other logging data (see Log.h)

View File

@ -18,6 +18,12 @@ void atomic_set(volatile int32* value, int32 new_value)
__atomic_store_n(value, new_value, __ATOMIC_SEQ_CST);
}
inline
void atomic_set(volatile int64* value, int64 new_value)
{
__atomic_store_n(value, new_value, __ATOMIC_SEQ_CST);
}
inline
void atomic_get(volatile byte* value, byte data[16])
{
@ -53,13 +59,13 @@ void atomic_decrement(volatile int32* value) {
}
inline
int32 atomic_add(volatile int32* value, int32 increment) {
return __atomic_fetch_add(value, increment, __ATOMIC_SEQ_CST);
void atomic_add(volatile int32* value, int32 increment) {
__atomic_fetch_add(value, increment, __ATOMIC_SEQ_CST);
}
inline
int32 atomic_subtract(volatile int32* value, int32 decrement) {
return __atomic_fetch_sub(value, decrement, __ATOMIC_SEQ_CST);
void atomic_sub(volatile int32* value, int32 decrement) {
__atomic_fetch_sub(value, decrement, __ATOMIC_SEQ_CST);
}
inline

View File

@ -0,0 +1,28 @@
/**
* Jingga
*
* @copyright Jingga
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
#ifndef TOS_PLATFORM_LINUX_THREADING_SPINLOCK_C
#define TOS_PLATFORM_LINUX_THREADING_SPINLOCK_C
#include "../../../stdlib/Types.h"
#include "Spinlock.h"
#include <time.h>
inline
void spinlock_start(spinlock32* lock, int32 delay = 10) {
while (__atomic_exchange_n(lock, 1, __ATOMIC_ACQUIRE) != 0) {
usleep(delay);
}
}
inline
void spinlock_end(spinlock32* lock) {
__atomic_store_n(lock, 0, __ATOMIC_RELEASE);
}
#endif

View File

@ -9,19 +9,9 @@
#ifndef TOS_PLATFORM_LINUX_THREADING_SPINLOCK_H
#define TOS_PLATFORM_LINUX_THREADING_SPINLOCK_H
#include <stdatomic.h>
#include "../../../stdlib/Types.h"
#include "Spinlock.h"
typedef volatile int32 spinlock32;
inline
void spinlock_start(spinlock32* lock) {
while (__atomic_exchange_n(lock, 1, __ATOMIC_ACQUIRE)) {}
}
inline
void spinlock_end(spinlock32* lock) {
__atomic_store_n(lock, 0, __ATOMIC_RELEASE);
}
#endif

View File

@ -18,6 +18,12 @@ void atomic_set(volatile int32* value, int32 new_value)
InterlockedExchange((long *) value, new_value);
}
inline
void atomic_set(volatile int64* value, int64 new_value)
{
InterlockedExchange((long *) value, (long) new_value);
}
inline
void atomic_set(volatile byte* value, const byte new_value[16])
{
@ -31,7 +37,7 @@ void atomic_set(volatile byte* value, const byte new_value[16])
expected_high = value64[1];
} while (
!InterlockedCompareExchange128(
(volatile long long*) value,
(volatile long long *) value,
new_value64[1],
new_value64[0],
&expected_low
@ -68,13 +74,13 @@ void atomic_decrement(volatile int32* value) {
}
inline
int32 atomic_add(volatile int32* value, int32 increment) {
return InterlockedExchangeAdd((long *) value, increment);
void atomic_add(volatile int32* value, int32 increment) {
InterlockedAdd((long *) value, increment);
}
inline
int32 atomic_subtract(volatile int32* value, int32 decrement) {
return InterlockedExchangeAdd((long *) value, -decrement);
void atomic_sub(volatile int32* value, int32 decrement) {
InterlockedAdd((long *) value, -decrement);
}
inline

View File

@ -0,0 +1,27 @@
/**
* Jingga
*
* @copyright Jingga
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
#ifndef TOS_PLATFORM_WIN32_THREADING_SPINLOCK_C
#define TOS_PLATFORM_WIN32_THREADING_SPINLOCK_C
#include <windows.h>
#include "../TimeUtils.h"
inline
void spinlock_start(spinlock32* lock, int32 delay = 10) {
while (InterlockedExchange(lock, 1) != 0) {
usleep(delay);
}
}
inline
void spinlock_end(spinlock32* lock) {
InterlockedExchange(lock, 0);
}
#endif

View File

@ -13,14 +13,4 @@
typedef volatile long spinlock32;
inline
void spinlock_start(spinlock32* lock) {
while (InterlockedExchange(lock, 1) == 1) {}
}
inline
void spinlock_end(spinlock32* lock) {
InterlockedExchange(lock, 0);
}
#endif

View File

@ -65,20 +65,4 @@ inline uint32 hash(uint64 a, uint64 b = 0)
return _mm_extract_epi32(hash, 0);
}
inline void atomic_increment(int32* a, int32 b) {
_aadd_i32(a, b);
}
inline void atomic_increment(int64* a, int64 b) {
_aadd_i64((long long int *) a, (long long int) b);
}
inline void atomic_decrement(int32* a, int32 b) {
_aadd_i32(a, -b);
}
inline void atomic_decrement(int64* a, int64 b) {
_aadd_i64((long long int *) a, (long long int) -b);
}
#endif