Swapped from using system call wrappers to just system calls and designing the C/C++ RT.

This commit is contained in:
2024-02-08 03:38:56 -08:00
parent 586ed2dfd2
commit 2aecad825b
35 changed files with 1780 additions and 1417 deletions

View File

@@ -33,7 +33,7 @@ namespace ehs
/// Sets the maximum amount of garbage to delete per poll.
/// @param[in] newMax The new maximum.
static void SetMax(const UInt_64 newMax);
static void SetMax(UInt_64 newMax);
/// Gets the maximum amount of garbage to delete per poll.
/// @returns The maximum.
@@ -41,7 +41,7 @@ namespace ehs
/// Sets a new amount for memory pre-allocation to save on memory operations.
/// @param[in] newStride The stride to pre-allocate.
static void SetStride(const UInt_64 newStride);
static void SetStride(UInt_64 newStride);
/// The amount of data pre-allocated to save on memory operations.
/// @returns The stride.

View File

@@ -2,6 +2,7 @@
#include "EHS.h"
#include "BaseObj.h"
#include "ehs/Serializer.h"
#include "ehs/system/Thread.h"
#include "ehs/system/Semaphore.h"

View File

@@ -0,0 +1,91 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/UTF.h"
#include "ehs/Array.h"
namespace ehs
{
class BaseConsole
{
public:
static void Attach();
/// Frees the current console being used.
static void Free();
/// Writes to console using UTF32.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
/// @warning Has to convert from UTF32 to UTF16 for the Windows API.
static void Write_32(const Str_32& str, bool newLine = true);
/// Writes to console using UTF16.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
static void Write_16(const Str_16& str, bool newLine = true);
/// Writes to console using UTF8.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static void Write_8(const Str_8& str, bool newLine = true);
/// Reads from the console using UTF32.
/// @returns The text the user wrote to the console.
/// @warning Has to convert from UTF16 to UTF32 for the Windows API.
static Str_32 Read_32(UInt_64 bufferSize = 1024);
/// Reads from the console using UTF16.
/// @returns The text the user wrote to the console.
static Str_16 Read_16(UInt_64 bufferSize = 1024);
/// Reads from the console using UTF8.
/// @returns The text the user wrote to the console.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static Str_8 Read_8(UInt_64 bufferSize = 1024);
/// Clears the console.
static void Clear();
/// Changes the console's title.
/// @param [in] title The text to change the title to.
/// @warning Has to convert from UTF32 to UTF16 for the Windows API.
static void SetTitle_32(const Str_32& title);
/// Changes the console's title.
/// @param [in] title The text to change the title to.
static void SetTitle_16(const Str_16& title);
/// Changes the console's title.
/// @param [in] title The text to change the title to.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static void SetTitle_8(const Str_8& title);
/// Retrieves the console's title in UTF32.
/// @returns The console's title.
/// @warning Has to convert from UTF16 to UTF32 for the Windows API.
static Str_32 GetTitle_32();
/// Retrieves the console's title in UTF16.
/// @returns The console's title.
static Str_16 GetTitle_16();
/// Retrieves the console's title in UTF8.
/// @returns The console's title.
/// @warning Has to convert from UTF16 to UTF8 for the Windows API.
static Str_8 GetTitle_8();
/// Retrieves the string used when executing the end application through a command line interface in UTF32.
/// @returns The result.
static Vector<Str_32> GetArgs_32(UInt_64 bufferSize = 1024);
/// Retrieves the string used when executing the end application through a command line interface in UTF16.
/// @returns The result.
static Vector<Str_16> GetArgs_16(UInt_64 bufferSize = 1024);
/// Retrieves the string used when executing the end application through a command line interface in UTF8.
/// @returns The result.
static Vector<Str_8> GetArgs_8(UInt_64 bufferSize = 1024);
};
}

View File

@@ -1,115 +1,9 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/UTF.h"
#include "ehs/Array.h"
#include "ehs/system/OS.h"
namespace ehs
{
#if defined(EHS_OS_WINDOWS)
typedef void* ConsoleHdl;
#elif defined(EHS_OS_LINUX)
typedef int ConsoleHdl;
#endif
class Console
{
private:
static ConsoleHdl hdlOut;
static ConsoleHdl hdlIn;
#if defined(EHS_OS_WINDOWS)
static bool isConsole;
#endif
public:
static void Attach();
/// Creates a console using standard input and output.
/// @param [in] inputRequired Whether or not input is required from the console.
static bool Create();
/// Frees the current console being used.
static void Free();
static bool CanRead();
static bool CanWrite();
/// Writes to console using UTF32.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
/// @warning Has to convert from UTF32 to UTF16 for the Windows API.
static void Write_32(const Str_32& str, const bool newLine = true);
/// Writes to console using UTF16.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
static void Write_16(const Str_16& str, const bool newLine = true);
/// Writes to console using UTF8.
/// @param [in] str The text to write to the console.
/// @param [in] newLine To make a new line after the given text.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static void Write_8(const Str_8& str, const bool newLine = true);
/// Reads from the console using UTF32.
/// @returns The text the user wrote to the console.
/// @warning Has to convert from UTF16 to UTF32 for the Windows API.
static Str_32 Read_32(const UInt_64 bufferSize = 1024);
/// Reads from the console using UTF16.
/// @returns The text the user wrote to the console.
static Str_16 Read_16(const UInt_64 bufferSize = 1024);
/// Reads from the console using UTF8.
/// @returns The text the user wrote to the console.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static Str_8 Read_8(const UInt_64 bufferSize = 1024);
/// Clears the console.
static void Clear();
/// Changes the console's title.
/// @param [in] title The text to change the title to.
/// @warning Has to convert from UTF32 to UTF16 for the Windows API.
static void SetTitle_32(const Str_32& title);
/// Changes the console's title.
/// @param [in] title The text to change the title to.
static void SetTitle_16(const Str_16& title);
/// Changes the console's title.
/// @param [in] title The text to change the title to.
/// @warning Has to convert from UTF8 to UTF16 for the Windows API.
static void SetTitle_8(const Str_8& title);
/// Retrieves the console's title in UTF32.
/// @returns The console's title.
/// @warning Has to convert from UTF16 to UTF32 for the Windows API.
static Str_32 GetTitle_32();
/// Retrieves the console's title in UTF16.
/// @returns The console's title.
static Str_16 GetTitle_16();
/// Retrieves the console's title in UTF8.
/// @returns The console's title.
/// @warning Has to convert from UTF16 to UTF8 for the Windows API.
static Str_8 GetTitle_8();
/// Retrieves the string used when executing the end application through a command line interface in UTF32.
/// @returns The result.
static Vector<Str_32> GetArgs_32(const UInt_64 bufferSize = 1024);
/// Retrieves the string used when executing the end application through a command line interface in UTF16.
/// @returns The result.
static Vector<Str_16> GetArgs_16(const UInt_64 bufferSize = 1024);
/// Retrieves the string used when executing the end application through a command line interface in UTF8.
/// @returns The result.
static Vector<Str_8> GetArgs_8(const UInt_64 bufferSize = 1024);
//static void* GetHandle();
};
}
#if defined(EHS_OS_WINDOWS)
#include "Console_W32.h"
#elif defined(EHS_OS_LINUX)
#include "Console_LNX.h"
#endif

View File

@@ -0,0 +1,50 @@
#pragma once
#include "BaseConsole.h"
namespace ehs
{
class Console : public BaseConsole
{
private:
static int hdlOut;
static int hdlIn;
public:
static void Attach();
static void Free();
static void Write_32(const Str_32& str, bool newLine = true);
static void Write_16(const Str_16& str, bool newLine = true);
static void Write_8(const Str_8& str, bool newLine = true);
static Str_32 Read_32(UInt_64 bufferSize = 1024);
static Str_16 Read_16(UInt_64 bufferSize = 1024);
static Str_8 Read_8(UInt_64 bufferSize = 1024);
static void Clear();
static void SetTitle_32(const Str_32& title);
static void SetTitle_16(const Str_16& title);
static void SetTitle_8(const Str_8& title);
static Str_32 GetTitle_32();
static Str_16 GetTitle_16();
static Str_8 GetTitle_8();
static Vector<Str_32> GetArgs_32(UInt_64 bufferSize = 1024);
static Vector<Str_16> GetArgs_16(UInt_64 bufferSize = 1024);
static Vector<Str_8> GetArgs_8(UInt_64 bufferSize = 1024);
};
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include "BaseConsole.h"
namespace ehs
{
class Console : public BaseConsole
{
private:
static void* hdlOut;
static void* hdlIn;
static bool isConsole;
public:
static void Attach();
static void Free();
static void Write_32(const Str_32& str, bool newLine = true);
static void Write_16(const Str_16& str, bool newLine = true);
static void Write_8(const Str_8& str, bool newLine = true);
static Str_32 Read_32(UInt_64 bufferSize = 1024);
static Str_16 Read_16(UInt_64 bufferSize = 1024);
static Str_8 Read_8(UInt_64 bufferSize = 1024);
static void Clear();
static void SetTitle_32(const Str_32& title);
static void SetTitle_16(const Str_16& title);
static void SetTitle_8(const Str_8& title);
static Str_32 GetTitle_32();
static Str_16 GetTitle_16();
static Str_8 GetTitle_8();
static Vector<Str_32> GetArgs_32(UInt_64 bufferSize = 1024);
static Vector<Str_16> GetArgs_16(UInt_64 bufferSize = 1024);
static Vector<Str_8> GetArgs_8(UInt_64 bufferSize = 1024);
};
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
class BaseThread
{
private:
UInt_64 stackSize;
protected:
UInt_32 id;
public:
BaseThread();
BaseThread(UInt_64 stackSize);
BaseThread(BaseThread&& thread) noexcept;
BaseThread(const BaseThread& thread);
BaseThread& operator=(BaseThread&& thread) noexcept;
BaseThread& operator=(const BaseThread& thread);
virtual void Detach();
virtual void Join() = 0;
UInt_64 GetStackSize() const;
UInt_32 GetId() const;
static UInt_32 GetCurrentId();
virtual bool IsValid() const;
/// A software implementation for sleeping the calling thread.
/// @param [in] seconds The amount in seconds to sleep for.
/// @returns The amount in seconds that the sleep exceeded for.
/// @note A more precise sleep method but more taxing on the CPU.
static float SoftSleep(float seconds);
/// A hardware implementation for sleeping the calling thread.
/// @param [in] milliseconds The amount in milliseconds to sleep for.
/// @note A less precise sleep method but less taxing on the CPU.
static void HardSleep(UInt_32 milliseconds);
};
}

58
include/ehs/system/Lock.h Normal file
View File

@@ -0,0 +1,58 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
class Lock
{
/// Does an atomic lock add operation.
/// @param [in] a The address of the value to add to.
/// @param [in] b The value to add from.
static void Add(UInt_64* a, UInt_64 b);
/// Does an atomic lock subtract operation.
/// @param [in] a The address of the value to subtract to.
/// @param [in] b The value to subtract from.
static void Sub(UInt_64* a, UInt_64 b);
/// Does an atomic lock compare exchange operation.
/// @param [in/out] a The address of the value to compare with the value of b.
/// @param [in/out] b The address of the value to compare with the value of a.
/// @param [in] setter The address of the value to set the value of b to when a and b are equal.
/// @returns True if equal.
/// @note If a and b are not equal the value at the address of a will be set to the value at the address of b.
static bool CmpXchg(UInt_64* a, UInt_64* b, UInt_64 setter);
/// Does an atomic lock and operation.
/// @param [in] a The address of the first value to compare.
/// @param [in] b The second value to compare.
static void And(UInt_64* a, UInt_64 b);
/// Does an atomic lock or operation.
/// @param [in] a The address of the first value to compare.
/// @param [in] b The second value to compare.
static void Or(UInt_64* a, UInt_64 b);
/// Does an atomic lock xor operation.
/// @param [in] a The address of the first value to compare.
/// @param [in] b The second value to compare.
static void XOr(UInt_64* a, UInt_64 b);
/// Does an atomic lock increase operation.
/// @param [in] a The address of the value to increase.
static void Inc(UInt_64* a);
/// Does an atomic lock decrease operation.
/// @param [in] a The address of the value to decrease.
static void Dec(UInt_64* a);
/// Does an atomic lock not operation.
/// @param [in] a The address of the value to not.
static void Not(UInt_64* a);
/// Does an atomic lock negate operation.
/// @param [in] a The address of the value to negate.
static void Neg(UInt_64* a);
};
}

View File

@@ -10,7 +10,7 @@ namespace ehs
class Mutex : public BaseMutex
{
private:
pthread_mutex_t hdl;
volatile UInt_64 state;
public:
~Mutex() override;

View File

@@ -1,148 +1,9 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Serializer.h"
#include "ehs/Array.h"
#include "ehs/Log.h"
#include "ehs/Str.h"
#include "ehs/system/OS.h"
#ifdef EHS_OS_WINDOWS
#include <processthreadsapi.h>
#endif
namespace ehs
{
#if defined(EHS_OS_WINDOWS)
#define EHS_INVALID_THREAD nullptr
typedef void* THandle;
#elif defined(EHS_OS_LINUX)
#define EHS_INVALID_THREAD 0
typedef UInt_64 THandle;
#endif
class Thread
{
private:
static UInt_32 mainId;
#ifdef EHS_OS_WINDOWS
static Handle mainTaskHdl;
static UInt_32 mainTaskIndex;
#endif
UInt_64 stackSize;
THandle hdl;
UInt_32 id;
#ifdef EHS_OS_WINDOWS
Handle taskHdl;
UInt_32 taskIndex;
#endif
public:
/// Frees any native handles.
~Thread();
/// Default members initialization.
Thread(const UInt_64 stackSize = 0);
/// Copies some members from the given thread object.
/// @param [in] thread The thread object to copy from.
Thread(const Thread& thread);
/// Copies some members from the given thread object.
/// @param [in] thread The thread object to copy from.
/// @returns The thread that has been assigned to.
Thread& operator=(const Thread& thread);
/// Creates a thread handle and starts it with the given method.
/// @param cb The function to run on the thread.
/// @param args Raw data to send over to the thread.
void Start(UInt_32 (*cb)(void*), void* args);
/// Blocks the calling thread until the referenced thread is finished.
/// @param timeout The time to wait for before moving on.
/// @note Pass "EHS_INFINITE" to wait until the thread is finished.
bool Join(const unsigned int timeout = EHS_INFINITE);
/// Detaches the referenced thread, removing ownership.
void Detach();
/// Retrieves the given stack size available to the referenced thread.
/// @returns The stack size.
UInt_64 GetStackSize() const;
/// Retrieves the native thread handle.
/// @returns The native handle.
THandle GetHandle() const;
/// Retrieves the thread's id.
/// @returns The id.
UInt_32 GetId() const;
/// Checks whether or not the calling thread is the referenced thread.
/// @returns The result.
bool IsCurrent() const;
bool IsValid() const;
#ifdef EHS_OS_WINDOWS
/// Adjusts the thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
void SetTaskType_32(const Str_32& task);
/// Adjusts the thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
void SetTaskType_16(const Str_16& task);
/// Adjusts the thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
void SetTaskType_8(const Str_8& task);
void RevertTaskType();
#endif
/// Gets the main thread id.
static UInt_32 GetMainId();
/// Retrieves the calling thread's id.
/// @returns The id.
static UInt_64 GetCurrentId();
#ifdef EHS_OS_WINDOWS
/// Adjusts the main thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
static void SetMainTaskType_32(const Str_32& task);
/// Adjusts the main thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
static void SetMainTaskType_16(const Str_16& task);
/// Adjusts the main thread's performance based on the type of task provided, that's going to be executed.
/// @param[in] task A task name from the provided list in the registry.
/// @note A list of tasks are in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks.
static void SetMainTaskType_8(const Str_8& task);
static void RevertMainTaskType();
#endif
/// Causes the calling thread to wait until the given time using a while loop and the CPU clock.
/// @param seconds The time in seconds to wait for.
/// @returns The total elapsed time slept for due to this not being a 100% accurate.
/// @warning Use "SleepFor" instead unless accuracy is required because this does not do other tasks while sleeping.
static float HardSleepFor(const float seconds);
/// Causes the calling thread to wait until the given time using native sleep calls.
/// @param miliseconds The time in miliseconds to wait for.
/// @warning This is not at all accurate.
static void SleepFor(const UInt_32 miliseconds);
private:
static void* Redirect(void* args);
};
}
#if defined(EHS_OS_WINDOWS)
#include "Thread_W32.h"
#elif defined(EHS_OS_LINUX)
#include "Thread_LNX.h"
#endif

View File

@@ -0,0 +1,42 @@
#pragma once
#include "BaseThread.h"
#include <sys/wait.h>
namespace ehs
{
class Thread : public BaseThread
{
private:
void* stack;
pid_t hdl;
volatile bool* detached;
public:
~Thread();
Thread();
Thread(UInt_64 stackSize, void* args, UInt_32 (*func)(void*));
Thread(Thread&& thread) noexcept;
Thread(const Thread& thread);
Thread& operator=(Thread&& thread) noexcept;
Thread& operator=(const Thread& thread);
void Detach() override;
void Join() override;
static UInt_32 GetCurrentId();
static void HardSleep(UInt_32 milliseconds);
private:
static UInt_32 Redirect(void* args);
};
}

View File

@@ -0,0 +1,37 @@
#pragma once
#include "BaseThread.h"
namespace ehs
{
class Thread : public BaseThread
{
private:
void* hdl;
public:
~Thread();
Thread();
Thread(UInt_64 stackSize, void* args, UInt_32 (*func)(void*));
Thread(Thread&& thread) noexcept;
Thread(const Thread& thread);
Thread& operator=(Thread&& thread) noexcept;
Thread& operator=(const Thread& thread);
void Detach() override;
void Join() override;
static UInt_32 GetCurrentId();
bool IsValid() const override;
static void HardSleep(UInt_32 milliseconds);
};
}