Skip to content
Merged
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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ CMakeUserPresets.json
.DS_Store

scripts/__pycache__/**

# Generated compile database (symlink to build dir)
compile_commands.json
.cache/
6 changes: 4 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
"cacheVariables": {
"KIT_BUILD_SCRIPT": "ON",
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_OSX_SYSROOT": "macosx"
"CMAKE_OSX_SYSROOT": "macosx",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
}
},
{
"name": "linux",
"binaryDir": "${sourceDir}/_Build_lnx",
"cacheVariables": {
"KIT_BUILD_SCRIPT": "ON",
"CMAKE_BUILD_TYPE": "Release"
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
}
}
]
Expand Down
39 changes: 39 additions & 0 deletions include/kit/endian.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <cstdint>

#if defined(_MSC_VER)
#include <stdlib.h>
#endif

namespace kit
{

inline uint16_t bswap16(uint16_t iu_val)
{
#if defined(_MSC_VER)
return _byteswap_ushort(iu_val);
#else
return __builtin_bswap16(iu_val);
#endif
}

inline uint32_t bswap32(uint32_t iu_val)
{
#if defined(_MSC_VER)
return _byteswap_ulong(iu_val);
#else
return __builtin_bswap32(iu_val);
#endif
}

inline uint64_t bswap64(uint64_t iu_val)
{
#if defined(_MSC_VER)
return _byteswap_uint64(iu_val);
#else
return __builtin_bswap64(iu_val);
#endif
}

} // namespace kit
45 changes: 45 additions & 0 deletions include/kit/event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <cstdint>

////////////////////////////////////////////////////////////////////////////////
// c_event - multi-event wait primitive (wait-any). Manages a small set of
// events, each of type e_single_auto, e_single_manual or e_multi,
// and lets a thread wait until ANY of them is signaled, returning
// the index of the signaled event.
class c_event
{
public:
// Opaque, platform-specific implementation (defined in each event_*.cpp).
struct s_impl;

enum e_type
{
e_single_auto = 0, // auto-reset, releases one waiter per set()
e_single_manual, // manual-reset, stays signaled until clr()
e_multi // counting / semaphore
};

// Returned by wait() on timeout; valid signal indices are 0..(count - 1).
static constexpr uint32_t timeout = 0xFFFFFFFFu;

c_event();
~c_event();

// Array-based replacement for the original va_list Init(count, ...).
bool init(uint8_t iu_count, const e_type *ip_types);

bool set(uint32_t iu_id);

bool clr(uint32_t iu_id); // only meaningful for e_single_manual

uint32_t wait();

uint32_t wait(uint32_t iu_timeout_ms);

private:
c_event(const c_event &) = delete;
c_event &operator=(const c_event &) = delete;

s_impl *mp_impl;
};
1 change: 1 addition & 0 deletions include/kit/kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "kit/list.hpp"
#include "kit/spin_lock.hpp"
#include "kit/shared_mem.hpp"
#include "kit/event.hpp"

#ifdef __cplusplus
extern "C"
Expand Down
20 changes: 20 additions & 0 deletions include/kit/system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <cstddef>
#include <cstdint>

namespace kit
{

/// Returns the current process ID.
uint32_t get_process_id();

/// Fills op_buf with the executable name (without path). Null-terminates.
/// @return true on success, false on failure (op_buf set to "unknown").
bool get_process_name(char *op_buf, size_t iz_buf);

/// Fills op_buf with the host name. Null-terminates.
/// @return true on success, false on failure (op_buf set to "unknown").
bool get_host_name(char *op_buf, size_t iz_buf);

} // namespace kit
20 changes: 20 additions & 0 deletions include/kit/time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <cstdint>

namespace kit
{

/// Returns the current value of a high-resolution monotonic tick counter.
uint64_t get_hires_ticks();

/// Returns the tick-to-nanosecond conversion ratio: nanoseconds = ticks * o_numer / o_denom.
void get_hires_ticks_freq(uint64_t &o_numer, uint64_t &o_denom);

/// Returns system time as 100-nanosecond intervals since January 1, 1601 (UTC).
uint64_t get_system_time();

/// Returns the local UTC offset in seconds (e.g. +3600 for UTC+1, -18000 for UTC-5).
int32_t get_utc_offset_seconds();

} // namespace kit
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ set(KIT_SOURCES
)

if(KIT_PLATFORM_LINUX)
list(APPEND KIT_SOURCES shared/shared_linux.cpp)
list(APPEND KIT_SOURCES shared/shared_linux.cpp time/time_linux.cpp system/system_linux.cpp event/event_linux.cpp)
elseif(KIT_PLATFORM_DARWIN)
list(APPEND KIT_SOURCES shared/shared_macos.cpp)
list(APPEND KIT_SOURCES shared/shared_macos.cpp time/time_macos.cpp system/system_macos.cpp event/event_macos.cpp)
elseif(KIT_PLATFORM_WINDOWS)
list(APPEND KIT_SOURCES shared/shared_win32.cpp)
list(APPEND KIT_SOURCES shared/shared_win32.cpp time/time_win32.cpp system/system_win32.cpp event/event_win32.cpp)
endif()

add_library(kit_obj OBJECT ${KIT_SOURCES})
Expand Down
Loading
Loading