From 35fb9574c5efc426491f7ce55689e0f911890e98 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 16 Mar 2026 05:07:39 +0100 Subject: [CHORE] Improve and fix libDDK and implement libMsg. Signed-off-by: Amlal El Mahrouss --- src/libDDK/src/Allocator.cpp | 30 ------------------- src/libDDK/src/DDKVersion.cpp | 23 +++++++++++++++ src/libDDK/src/Device.cpp | 25 ---------------- src/libDDK/src/DriverBase.cpp | 8 ----- src/libDDK/src/IO.cpp | 32 -------------------- src/libDDK/src/KernelAllocator.cpp | 30 +++++++++++++++++++ src/libDDK/src/KernelCall.cpp | 8 ++--- src/libDDK/src/KernelDevice.cpp | 25 ++++++++++++++++ src/libDDK/src/KernelDriverBase.cpp | 8 +++++ src/libDDK/src/KernelIO.cpp | 32 ++++++++++++++++++++ src/libDDK/src/KernelString.cpp | 34 ++++++++++++++++++++++ src/libDDK/src/String.cpp | 34 ---------------------- src/libDDK/src/Version.cpp | 23 --------------- src/libMsg/MsgKit/Server.h | 8 +++-- src/libMsg/libMsg.json | 2 +- src/libMsg/scripts/hello-world.json | 7 +++++ src/libMsg/scripts/window_client.json | 7 ----- src/libMsg/src/Server.cpp | 37 +++++++++++++++++++++-- src/libPThread/PThreadKit/Thread.h | 7 +++++ src/libPThread/src/Thread.cpp | 55 ++++++++++++++++++++--------------- src/libSystem/SystemKit/System.h | 19 +++++++++++- src/libSystem/libSystem.json | 2 +- 22 files changed, 262 insertions(+), 194 deletions(-) delete mode 100644 src/libDDK/src/Allocator.cpp create mode 100644 src/libDDK/src/DDKVersion.cpp delete mode 100644 src/libDDK/src/Device.cpp delete mode 100644 src/libDDK/src/DriverBase.cpp delete mode 100644 src/libDDK/src/IO.cpp create mode 100644 src/libDDK/src/KernelAllocator.cpp create mode 100644 src/libDDK/src/KernelDevice.cpp create mode 100644 src/libDDK/src/KernelDriverBase.cpp create mode 100644 src/libDDK/src/KernelIO.cpp create mode 100644 src/libDDK/src/KernelString.cpp delete mode 100644 src/libDDK/src/String.cpp delete mode 100644 src/libDDK/src/Version.cpp create mode 100644 src/libMsg/scripts/hello-world.json delete mode 100644 src/libMsg/scripts/window_client.json (limited to 'src') diff --git a/src/libDDK/src/Allocator.cpp b/src/libDDK/src/Allocator.cpp deleted file mode 100644 index f350ab22..00000000 --- a/src/libDDK/src/Allocator.cpp +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#include - -/** - \brief Allocates a new heap on the Kernel's side. - \param sz the size of the heap block. - \return the newly allocated pointer. -*/ -DDK_EXTERN void* kalloc(size_t sz) { - if (!sz) ++sz; - - void* ptr = ke_call_dispatch("mm_alloc_ptr", 1, &sz, sizeof(size_t)); - - return ptr; -} - -/** - \brief Frees a pointer from the heap. - \param ptr the pointer to free. -*/ -DDK_EXTERN void kfree(void* ptr) { - if (!ptr) return; - - ke_call_dispatch("mm_free_ptr", 1, ptr, 0); -} diff --git a/src/libDDK/src/DDKVersion.cpp b/src/libDDK/src/DDKVersion.cpp new file mode 100644 index 00000000..eb65da89 --- /dev/null +++ b/src/libDDK/src/DDKVersion.cpp @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include + +#ifndef kDDKVersionHighest +#define kDDKVersionHighest 0x010000 +#endif // !kDDKVersionHighest + +#ifndef kDDKVersionLowest +#define kDDKVersionLowest 0x010000 +#endif // !kDDKVersionLowest + +#ifndef kDDKVersion +#define kDDKVersion 0x010000 +#endif // !kDDKVersion + +uint32_t kApiVersionHighest = kDDKVersionHighest; +uint32_t kApiVersionLowest = kDDKVersionLowest; +uint32_t kApiVersion = kDDKVersion; diff --git a/src/libDDK/src/Device.cpp b/src/libDDK/src/Device.cpp deleted file mode 100644 index 6390f05c..00000000 --- a/src/libDDK/src/Device.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#include -#include - -/// @brief Open a new binary device from path. -DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* devicePath) { - if (nil == devicePath) return nil; - - return (DDK_DEVICE_PTR) ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, - kstrlen(devicePath)); -} - -/// @brief Close any device. -/// @param device valid device. -DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device) { - if (nil == device) return NO; - - ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); - return YES; -} diff --git a/src/libDDK/src/DriverBase.cpp b/src/libDDK/src/DriverBase.cpp deleted file mode 100644 index 37b138a0..00000000 --- a/src/libDDK/src/DriverBase.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/ne-foss-org/nekernel - -#include - - diff --git a/src/libDDK/src/IO.cpp b/src/libDDK/src/IO.cpp deleted file mode 100644 index fc247e15..00000000 --- a/src/libDDK/src/IO.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#include - -DDK_EXTERN void kputc(const char ch) { - if (!ch) return; - - char assembled[2] = {0}; - assembled[0] = ch; - assembled[1] = 0; - - ke_call_dispatch("ke_put_string", 2, assembled, 2); -} - -/// @brief print string to UART. -/// @param message UART to transmit. -DDK_EXTERN void kprint(const char* message) { - if (nil == message) return; - if (*message == '\0') return; - - size_t index = 0; - size_t len = kstrlen(message); - - while (index < len) { - kputc(message[index]); - ++index; - } -} diff --git a/src/libDDK/src/KernelAllocator.cpp b/src/libDDK/src/KernelAllocator.cpp new file mode 100644 index 00000000..f2dd7b45 --- /dev/null +++ b/src/libDDK/src/KernelAllocator.cpp @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include + +/** + \brief Allocates a new heap on the Kernel's side. + \param sz the size of the heap block. + \return the newly allocated pointer. +*/ +DDK_EXTERN void* kalloc(size_t sz) { + if (!sz) ++sz; + + void* ptr = ::ke_call_dispatch("mm_alloc_ptr", 1, &sz, sizeof(size_t)); + + return ptr; +} + +/** + \brief Frees a pointer from the heap. + \param ptr the pointer to free. +*/ +DDK_EXTERN void kfree(void* ptr) { + if (!ptr) return; + + ::ke_call_dispatch("mm_free_ptr", 1, ptr, 0); +} diff --git a/src/libDDK/src/KernelCall.cpp b/src/libDDK/src/KernelCall.cpp index bbaded64..43e4b79f 100644 --- a/src/libDDK/src/KernelCall.cpp +++ b/src/libDDK/src/KernelCall.cpp @@ -40,14 +40,14 @@ static uint64_t ddk_fnv_64(const char* path) { /// @retval nil Kernel call failed, call KernelLastError(void) DDK_EXTERN void* ke_call_dispatch(const char* name, int32_t cnt, void* data, size_t sz) { if (name == nil || *name == 0 || data == nil || cnt == 0) return nil; - return __ke_call_dispatch(ddk_fnv_64(name), cnt, data, sz); + return ::__ke_call_dispatch(ddk_fnv_64(name), cnt, data, sz); } /// @brief Add system call. /// @param slot system call slot /// @param slotFn, syscall slot. DDK_EXTERN void ke_set_syscall(const int slot, void (*slotFn)(void* a0)) { - ke_call_dispatch("ke_set_syscall", slot, (ptr_t) slotFn, 1); + ::ke_call_dispatch("ke_set_syscall", slot, (ptr_t) slotFn, 1); } /// @brief Get a Kernel object. @@ -56,7 +56,7 @@ DDK_EXTERN void ke_set_syscall(const int slot, void (*slotFn)(void* a0)) { /// @return Object manifest. DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name) { struct DDK_OBJECT_MANIFEST* manifest = - (struct DDK_OBJECT_MANIFEST*) ke_call_dispatch("cfkit_get_kobj", slot, (void*) name, 1); + (struct DDK_OBJECT_MANIFEST*) ::ke_call_dispatch("cfkit_get_kobj", slot, (void*) name, 1); if (!manifest) return nil; @@ -70,5 +70,5 @@ DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* na /// @return property's object. DDK_EXTERN void* ke_set_obj(const int slot, const struct DDK_OBJECT_MANIFEST* ddk_ptr) { if (ddk_ptr == nil) return nil; - return ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_ptr, 1); + return ::ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_ptr, 1); } diff --git a/src/libDDK/src/KernelDevice.cpp b/src/libDDK/src/KernelDevice.cpp new file mode 100644 index 00000000..c5fb785f --- /dev/null +++ b/src/libDDK/src/KernelDevice.cpp @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include +#include + +/// @brief Open a new binary device from path. +DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* devicePath) { + if (nil == devicePath) return nil; + + return (DDK_DEVICE_PTR) ::ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, + kstrlen(devicePath)); +} + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device) { + if (nil == device) return NO; + + ::ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); + return YES; +} diff --git a/src/libDDK/src/KernelDriverBase.cpp b/src/libDDK/src/KernelDriverBase.cpp new file mode 100644 index 00000000..37b138a0 --- /dev/null +++ b/src/libDDK/src/KernelDriverBase.cpp @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/ne-foss-org/nekernel + +#include + + diff --git a/src/libDDK/src/KernelIO.cpp b/src/libDDK/src/KernelIO.cpp new file mode 100644 index 00000000..e978525d --- /dev/null +++ b/src/libDDK/src/KernelIO.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include + +DDK_EXTERN void kputc(const char ch) { + if (!ch) return; + + char assembled[2] = {0}; + assembled[0] = ch; + assembled[1] = 0; + + ::ke_call_dispatch("ke_put_string", 2, assembled, 2); +} + +/// @brief print string to UART. +/// @param message UART to transmit. +DDK_EXTERN void kprint(const char* message) { + if (nil == message) return; + if (*message == '\0') return; + + size_t index = 0; + size_t len = ::kstrlen(message); + + while (index < len) { + ::kputc(message[index]); + ++index; + } +} diff --git a/src/libDDK/src/KernelString.cpp b/src/libDDK/src/KernelString.cpp new file mode 100644 index 00000000..7aee7495 --- /dev/null +++ b/src/libDDK/src/KernelString.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include + +DDK_EXTERN size_t kstrlen(const char* in) { + if (in == nil) return 0; + + if (*in == 0) return 0; + + size_t index = 0; + + while (in[index] != 0) { + ++index; + } + + return index; +} + +DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len) { + if (nil == dst || nil == src) return 0; + + size_t index = 0; + + while (index != len) { + dst[index] = src[index]; + ++index; + } + + return index; +} diff --git a/src/libDDK/src/String.cpp b/src/libDDK/src/String.cpp deleted file mode 100644 index 7aee7495..00000000 --- a/src/libDDK/src/String.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#include - -DDK_EXTERN size_t kstrlen(const char* in) { - if (in == nil) return 0; - - if (*in == 0) return 0; - - size_t index = 0; - - while (in[index] != 0) { - ++index; - } - - return index; -} - -DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len) { - if (nil == dst || nil == src) return 0; - - size_t index = 0; - - while (index != len) { - dst[index] = src[index]; - ++index; - } - - return index; -} diff --git a/src/libDDK/src/Version.cpp b/src/libDDK/src/Version.cpp deleted file mode 100644 index 5e60fbc4..00000000 --- a/src/libDDK/src/Version.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#include - -#ifndef kDDKVersionHighest -#define kDDKVersionHighest 1 -#endif // !kDDKVersionHighest - -#ifndef kDDKVersionLowest -#define kDDKVersionLowest 1 -#endif // !kDDKVersionLowest - -#ifndef kDDKVersion -#define kDDKVersion 1 -#endif // !kDDKVersion - -uint32_t kApiVersionHighest = kDDKVersionHighest; -uint32_t kApiVersionLowest = kDDKVersionLowest; -uint32_t kApiVersion = kDDKVersion; diff --git a/src/libMsg/MsgKit/Server.h b/src/libMsg/MsgKit/Server.h index 3ab8eb73..faac0b0b 100644 --- a/src/libMsg/MsgKit/Server.h +++ b/src/libMsg/MsgKit/Server.h @@ -6,6 +6,7 @@ #ifndef MSGKIT_SERVER_H #define MSGKIT_SERVER_H +#include "libSystem/SystemKit/Macros.h" #ifdef __cplusplus #include #else @@ -25,9 +26,12 @@ struct LIBMSG_EXPR final { CF::CFString* l_value{nullptr}; #else // if we use C, we won't know about CF, so let's make those private. - VoidPtr l_private_data[2]{nullptr}; + VoidPtr l_key{nullptr}; + VoidPtr l_value{nullptr}; #endif + SizeT l_index{}; + LIBMSG_EXPR* l_head{nullptr}; LIBMSG_EXPR* l_tail{nullptr}; LIBMSG_EXPR* l_child{nullptr}; @@ -38,6 +42,6 @@ typedef Void (*libmsg_func_type)(struct LIBMSG_EXPR* self, VoidPtr arg, SizeT ar IMPORT_C Void libmsg_init_library(libmsg_func_type* funcs, SizeT cnt); IMPORT_C UInt32 libmsg_close_library(Void); -IMPORT_C UInt32 libmsg_eval_expr(struct LIBMSG_EXPR* head); +IMPORT_C UInt32 libmsg_eval_expr(struct LIBMSG_EXPR* head, VoidPtr arg, SizeT arg_size); #endif diff --git a/src/libMsg/libMsg.json b/src/libMsg/libMsg.json index 4b22534f..54f284b8 100644 --- a/src/libMsg/libMsg.json +++ b/src/libMsg/libMsg.json @@ -17,5 +17,5 @@ "kLibSystemVersionHighest=0x0100", "kLibSystemVersionLowest=0x0100" ], - "description": "OpenMessage Kit for NeKernel." + "description": "OpenMessage Kit for the NeSystem." } diff --git a/src/libMsg/scripts/hello-world.json b/src/libMsg/scripts/hello-world.json new file mode 100644 index 00000000..f23a3714 --- /dev/null +++ b/src/libMsg/scripts/hello-world.json @@ -0,0 +1,7 @@ +{ + "id": 1, + "pos": { "x": 100, "y": 100 }, + "size": { "w": 300, "h": 200 }, + "title": "Message", + "on-init": "_PrintHelloWorld" +} \ No newline at end of file diff --git a/src/libMsg/scripts/window_client.json b/src/libMsg/scripts/window_client.json deleted file mode 100644 index 4c8a21ba..00000000 --- a/src/libMsg/scripts/window_client.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id": 1, - "pos": { "x": 100, "y": 100 }, - "size": { "w": 300, "h": 200 }, - "title": "Window Client", - "on-click": "_OnClickCSymbol" -} \ No newline at end of file diff --git a/src/libMsg/src/Server.cpp b/src/libMsg/src/Server.cpp index 3f2e8f5a..e4602047 100644 --- a/src/libMsg/src/Server.cpp +++ b/src/libMsg/src/Server.cpp @@ -5,9 +5,40 @@ #include -IMPORT_C UInt32 libmsg_close_library(Void) { return 0; } +static libmsg_func_type* kFuncs{nullptr}; +static SizeT kFuncCnt{0}; +static SemaphoreRef kSemaphore{nullptr}; -IMPORT_C UInt32 libmsg_eval_expr(struct LIBMSG_EXPR* head) { return 0; } +IMPORT_C UInt32 libmsg_close_library(Void) { + if (kSemaphore) return 0; -IMPORT_C Void libmsg_init_library(libmsg_func_type* funcs, SizeT cnt) {} + kFuncs = nullptr; + kFuncCnt = 0; + return 0; +} + +IMPORT_C UInt32 libmsg_eval_expr(struct LIBMSG_EXPR* head, VoidPtr arg, SizeT arg_size) { + if (kSemaphore) return 0; + + if (!head) return 0; + + kSemaphore = SemCreate(1000, 1000, "libmsg_semaphore"); + + if (!kSemaphore) return 0; + + kFuncs[head->l_index](head, arg, arg_size); + + SemClose(kSemaphore); + kSemaphore = nullptr; + + return 0; +} + +IMPORT_C Void libmsg_init_library(libmsg_func_type* funcs, SizeT cnt) { + kFuncs = funcs; + kFuncCnt = cnt; + + MUST_PASS(kFuncs != nullptr); + MUST_PASS(kFuncCnt > 0); +} diff --git a/src/libPThread/PThreadKit/Thread.h b/src/libPThread/PThreadKit/Thread.h index 053885e7..85f7a320 100644 --- a/src/libPThread/PThreadKit/Thread.h +++ b/src/libPThread/PThreadKit/Thread.h @@ -11,4 +11,11 @@ #define PTHREAD_UNSAFE /* hint */ #define PTHREAD_SAFE /* hint */ +PTHREAD_SAFE SInt32 pthread_create(_Output ThreadRef* thread, VoidPtr attr, VoidPtr (*start_routine)(VoidPtr), VoidPtr arg); +PTHREAD_SAFE SInt32 pthread_join(ThreadRef thread, VoidPtr* retval); +PTHREAD_SAFE SInt32 pthread_exit(SInt32 retval); +PTHREAD_SAFE SInt32 pthread_detach(ThreadRef thread); +PTHREAD_SAFE ThreadRef pthread_self(Void); +PTHREAD_SAFE SInt32 pthread_yield(void); + #endif // LIBPOSIX_POSIXKIT_THREAD_H diff --git a/src/libPThread/src/Thread.cpp b/src/libPThread/src/Thread.cpp index 34a610cd..5a351be8 100644 --- a/src/libPThread/src/Thread.cpp +++ b/src/libPThread/src/Thread.cpp @@ -5,29 +5,38 @@ #include -namespace POSIXKit::Detail { - - /// @brief Max path structure. - constexpr auto kMaxPathLen = 255; - static constexpr auto kCanaryValue = 0xf0f0488f; - - /// @brief Thread Information Structure. - struct ThreadFrameParams final { - SInt64 fCanary; - VoidPtr fStackPtr; - VoidPtr fCodePtr; - SizeT fCodeSz; - SizeT fStackSz; - SInt64 fThrdID; - SInt64 fUsrID, fGrpID; - SInt64* fFD{}; - SizeT fFDCnt; - Char fCWD[kMaxPathLen]; - Char fRoot[kMaxPathLen]; - ThreadRef fRef; - }; - -} // namespace POSIX::Detail +PTHREAD_SAFE SInt32 pthread_detach(ThreadRef thread) { + return ThrDetachThread(thread); +} +PTHREAD_SAFE SInt32 pthread_yield(void) { + return ThrYieldThread(pthread_self()); +} +PTHREAD_SAFE ThreadRef pthread_self(Void) { + return ThrCurrentThread(); +} + +PTHREAD_SAFE SInt32 pthread_exit(SInt32 retval) { + return ThrExitCurrentThread(retval); +} + +PTHREAD_SAFE SInt32 pthread_join(ThreadRef thread, VoidPtr* retval) { + SInt32* ret = (SInt32*)retval; + *ret = ThrJoinThread(thread); + + return 0; +} + +PTHREAD_SAFE SInt32 pthread_create(_Output ThreadRef* thread, VoidPtr attr, VoidPtr (*start_routine)(VoidPtr), VoidPtr arg) { + LIBSYS_UNUSED(attr); + + /// @note passing zero means you'd have to read the argv until you hit a nullptr. + ThreadRef thrd = ThrCreateThread("pthread_thread", (ThrProcKind)start_routine, 0, arg, 0); + + if (!thrd) return -1; + + *thread = thrd; + return 0; +} diff --git a/src/libSystem/SystemKit/System.h b/src/libSystem/SystemKit/System.h index 4e766d9d..69cce9a7 100644 --- a/src/libSystem/SystemKit/System.h +++ b/src/libSystem/SystemKit/System.h @@ -21,6 +21,7 @@ /// @brief Types API. // ------------------------------------------------------------------------------------------ // +/// @brief Reference type, used for all references in the system, such as file descriptors, dylib handles, thread handles, etc. struct REF_TYPE { UInt64 __hash; /// @brief Hash of the syscall VoidPtr __self; /// @brief Syscall self value. @@ -210,12 +211,15 @@ typedef SInt32 (*ThrProcKind)(SInt32 argc, Char** argv); /// @param flags Thread flags. /// @return the thread object. IMPORT_C ThreadRef ThrCreateThread(const Char* thread_name, ThrProcKind procedure, - SInt32 argument_count, SInt32 flags); + SInt32 argument_count, VoidPtr args, SInt32 flags); /// @brief Yields the current thread. /// @param thread the thread to yield. IMPORT_C SInt32 ThrYieldThread(ThreadRef thrd); +/// @brief Get the current thread's ID. +IMPORT_C ThreadRef ThrCurrentThread(Void); + /// @brief Joins a thread. /// @param thread the thread to join. IMPORT_C SInt32 ThrJoinThread(ThreadRef thrd); @@ -389,4 +393,17 @@ IMPORT_C Char* StrFmt(const Char* fmt, ...); IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base); +// ------------------------------------------------------------------------------------------ // +// @brief Semaphore API. +// ------------------------------------------------------------------------------------------ // + +/// @brief Create a semaphore. +IMPORT_C _Output SemaphoreRef SemCreate(_Input UInt32 initial_count, _Input UInt32 max_count, _Input const Char* name); + +/// @brief Wait on a semaphore. +IMPORT_C SInt32 SemWait(_Input SemaphoreRef sem); + +/// @brief Close a semaphore. +IMPORT_C SInt32 SemClose(_Input SemaphoreRef sem); + #endif // ifndef SYSTEMKIT_SYSTEM_H diff --git a/src/libSystem/libSystem.json b/src/libSystem/libSystem.json index 16557645..14f4bfc5 100644 --- a/src/libSystem/libSystem.json +++ b/src/libSystem/libSystem.json @@ -17,5 +17,5 @@ "kLibSystemVersionHighest=0x0100", "kLibSystemVersionLowest=0x0100" ], - "description": "The System Call Kit for the NeKernel Stack." + "description": "The System Call Kit for the NeSystem." } -- cgit v1.2.3