summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-03-28 04:14:54 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-03-28 04:14:54 +0100
commite2e01ed3c503cb8d93446abea162ccf9abd56201 (patch)
tree536df79e6d6200dd261abd78945aa03ee7439e55 /src
parent9937bcb03d4b3c3e71c33786349d4a41203550c0 (diff)
[FEAT] src: Add libThread and ThreadKit framework, rework requirements as well.HEADdevelop
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src')
-rw-r--r--src/kernel/NeKit/Atom.h22
-rw-r--r--src/libThread/ThreadKit/Thread.h11
-rw-r--r--src/libThread/doc/.keep0
-rw-r--r--src/libThread/libThread.json21
-rw-r--r--src/libThread/obj/.keep0
-rw-r--r--src/libThread/src/Thread.cpp81
6 files changed, 124 insertions, 11 deletions
diff --git a/src/kernel/NeKit/Atom.h b/src/kernel/NeKit/Atom.h
index c23508f6..53ab661e 100644
--- a/src/kernel/NeKit/Atom.h
+++ b/src/kernel/NeKit/Atom.h
@@ -10,7 +10,7 @@
namespace Kernel {
-template <class TypeAtomic>
+template <class AtomicType>
class Atom final {
public:
explicit Atom() = default;
@@ -21,29 +21,29 @@ class Atom final {
Atom(const Atom&) = delete;
public:
- using Type = TypeAtomic;
- using Ref = TypeAtomic&;
- using ConstRef = const TypeAtomic&;
+ using Type = AtomicType;
+ using Ref = AtomicType&;
+ using ConstRef = const AtomicType&;
- const TypeAtomic& operator[](const TypeAtomic& bit) {
- return (fArrayOfAtoms & (TypeAtomic{} << bit));
+ const AtomicType& operator[](const AtomicType& bit) {
+ return (fArrayOfAtoms & (AtomicType{} << bit));
}
- void operator|(const TypeAtomic& bit) { fArrayOfAtoms |= (TypeAtomic{1} << bit); }
+ void operator|(const AtomicType& bit) { fArrayOfAtoms |= (AtomicType{1} << bit); }
- Atom& operator|=(const TypeAtomic& bit) {
+ Atom& operator|=(const AtomicType& bit) {
this->operator|(bit);
return *this;
}
- friend bool operator==(Atom<TypeAtomic>& atomic, const TypeAtomic& idx) {
+ friend bool operator==(Atom<AtomicType>& atomic, const AtomicType& idx) {
return atomic[idx] == idx;
}
- friend bool operator!=(Atom<TypeAtomic>& atomic, const TypeAtomic& idx) {
+ friend bool operator!=(Atom<AtomicType>& atomic, const AtomicType& idx) {
return atomic[idx] != idx;
}
private:
- TypeAtomic fArrayOfAtoms;
+ AtomicType fArrayOfAtoms;
};
} // namespace Kernel
diff --git a/src/libThread/ThreadKit/Thread.h b/src/libThread/ThreadKit/Thread.h
new file mode 100644
index 00000000..e8fa7eb0
--- /dev/null
+++ b/src/libThread/ThreadKit/Thread.h
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 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/ne-kernel
+
+#ifndef THREADKIT_THREADKIT_H
+#define THREADKIT_THREADKIT_H
+
+#include <libSystem/SystemKit/System.h>
+
+#endif // THREADKIT_THREADKIT_H
diff --git a/src/libThread/doc/.keep b/src/libThread/doc/.keep
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/libThread/doc/.keep
diff --git a/src/libThread/libThread.json b/src/libThread/libThread.json
new file mode 100644
index 00000000..dc1da098
--- /dev/null
+++ b/src/libThread/libThread.json
@@ -0,0 +1,21 @@
+{
+ "compiler_path": "x86_64-w64-mingw32-g++",
+ "compiler_std": "c++20",
+ "headers_path": ["../", "./"],
+ "sources_path": ["src/*.cpp"],
+ "output_name": "libThread.dll",
+ "compiler_flags": [
+ "-ffreestanding",
+ "-shared",
+ "-fPIC",
+ "-fno-rtti",
+ "-fno-exceptions",
+ "-Wl,--subsystem=17"
+ ],
+ "cpp_macros": [
+ "kLibThreadVersion=0x0100",
+ "kLibThreadVersionHighest=0x0100",
+ "kLibThreadVersionLowest=0x0100"
+ ],
+ "description": "The System Thread Kit for the NeSystem."
+}
diff --git a/src/libThread/obj/.keep b/src/libThread/obj/.keep
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/libThread/obj/.keep
diff --git a/src/libThread/src/Thread.cpp b/src/libThread/src/Thread.cpp
new file mode 100644
index 00000000..89a90e00
--- /dev/null
+++ b/src/libThread/src/Thread.cpp
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 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/ne-kernel
+
+#include <libSystem/SystemKit/Err.h>
+#include <libThread/ThreadKit/Thread.h>
+
+#define kThreadMapMax (1024UL)
+#define kThreadBaseHash (0x5555ffff6ULL)
+
+/// @brief The registered thread for a specific process.
+static ThreadRef kThreadMap[kThreadMapMax];
+
+/// @brief Exit codes as we want to track them.
+static SInt32 kThreadExitCodes[kThreadMapMax];
+
+/// @brief This hash-list stores the thread to be run.
+static ThreadRef kThreadHeads[kThreadMapMax];
+
+/// @brief This hash-list stores the thread to be run.
+static SInt32 kThreadFlags[kThreadMapMax];
+
+/// @brief Current thread reference
+static ThreadRef kCurrentThread;
+
+static __THREAD_UNSAFE Void _ThrRunThread(SInt32 argument_count, VoidPtr args,
+ ThrProcKind procedure, ThreadRef ref) {
+ static SemaphoreRef sem_ref = SemCreate(0, 1000, "ThreadSem");
+
+ if (sem_ref) return;
+
+ auto ret = procedure(argument_count, (Char**) args);
+ kThreadExitCodes[kThreadMapMax % ref->__hash] = ret;
+
+ if (ref == kCurrentThread) kCurrentThread = nullptr;
+
+ SemClose(sem_ref);
+ sem_ref = nullptr;
+}
+
+IMPORT_C ThreadRef ThrCreateThread(const Char* thread_name, ThrProcKind procedure,
+ SInt32 argument_count, VoidPtr args, SInt32 flags) {
+ ThreadRef ref = new REF_TYPE;
+ if (!ref) return nullptr;
+
+ if (!thread_name || !procedure) {
+ delete ref;
+ ref = nullptr;
+ return nullptr;
+ }
+
+ ref->__hash = *(UInt32*) thread_name;
+ ref->__hash += kThreadBaseHash; // pad hash with a seed.
+
+ ref->__self = (VoidPtr) procedure;
+
+ kThreadMap[kThreadMapMax % ref->__hash] = ref;
+ kThreadHeads[kThreadMapMax % ref->__hash] = ref;
+ kThreadFlags[kThreadMapMax % ref->__hash] = flags;
+
+ kCurrentThread = ref;
+
+ _ThrRunThread(argument_count, args, procedure, ref);
+
+ return ref;
+}
+
+IMPORT_C SInt32 ThrExitThread(_Input ThreadRef ref, _Input SInt32 exit_code) {
+ if (!ref) return kErrorInvalidData;
+
+ kThreadMap[kThreadMapMax % ref->__hash] = nullptr;
+ kThreadHeads[kThreadMapMax % ref->__hash] = nullptr;
+
+ kThreadFlags[kThreadMapMax % ref->__hash] = 0;
+
+ if (kCurrentThread == ref) kCurrentThread = nullptr;
+
+ kThreadExitCodes[kThreadMapMax % ref->__hash] = exit_code;
+ return kErrorSuccess;
+} \ No newline at end of file