2023-07-03 13:10:16 +02:00
|
|
|
#pragma once
|
2023-07-06 21:10:15 +02:00
|
|
|
#include "evf.hpp"
|
2023-07-03 13:10:16 +02:00
|
|
|
#include "utils/LinkedNode.hpp"
|
|
|
|
|
#include "utils/SharedMutex.hpp"
|
|
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
#include "KernelAllocator.hpp"
|
2023-07-06 21:10:15 +02:00
|
|
|
#include "orbis/thread/types.hpp"
|
2023-07-03 13:10:16 +02:00
|
|
|
#include <algorithm>
|
2023-07-06 21:10:15 +02:00
|
|
|
#include <pthread.h>
|
2023-07-03 13:10:16 +02:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
namespace orbis {
|
2023-07-04 18:19:17 +02:00
|
|
|
struct Process;
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) KernelContext final {
|
|
|
|
|
public:
|
|
|
|
|
KernelContext();
|
|
|
|
|
~KernelContext();
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-04 18:19:17 +02:00
|
|
|
Process *createProcess(pid_t pid);
|
|
|
|
|
void deleteProcess(Process *proc);
|
|
|
|
|
Process *findProcessById(pid_t pid) const;
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-06 21:10:15 +02:00
|
|
|
void *kalloc(std::size_t size,
|
|
|
|
|
std::size_t align = __STDCPP_DEFAULT_NEW_ALIGNMENT__);
|
|
|
|
|
void kfree(void *ptr, std::size_t size);
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-06 21:10:15 +02:00
|
|
|
std::pair<EventFlag *, bool> createEventFlag(utils::kstring name,
|
|
|
|
|
std::int32_t flags) {
|
|
|
|
|
auto [it, inserted] =
|
|
|
|
|
m_event_flags.try_emplace(std::move(name), knew<EventFlag>(flags));
|
|
|
|
|
return {it->second.get(), inserted};
|
2023-07-05 00:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ref<EventFlag> findEventFlag(std::string_view name) {
|
|
|
|
|
if (auto it = m_event_flags.find(name); it != m_event_flags.end()) {
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 21:10:15 +02:00
|
|
|
return {};
|
2023-07-05 00:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 13:10:16 +02:00
|
|
|
private:
|
2023-07-06 21:10:15 +02:00
|
|
|
mutable pthread_mutex_t m_heap_mtx;
|
|
|
|
|
void *m_heap_next = this + 1;
|
|
|
|
|
bool m_heap_is_freeing = false;
|
|
|
|
|
utils::kmultimap<std::size_t, void *> m_free_heap;
|
|
|
|
|
utils::kmultimap<std::size_t, void *> m_used_node;
|
|
|
|
|
|
2023-07-03 13:10:16 +02:00
|
|
|
mutable shared_mutex m_proc_mtx;
|
|
|
|
|
utils::LinkedNode<Process> *m_processes = nullptr;
|
2023-07-05 12:08:13 +02:00
|
|
|
utils::kmap<utils::kstring, Ref<EventFlag>> m_event_flags;
|
2023-07-03 13:10:16 +02:00
|
|
|
};
|
2023-07-04 18:19:17 +02:00
|
|
|
|
|
|
|
|
extern KernelContext &g_context;
|
|
|
|
|
} // namespace orbis
|