mirror of
https://github.com/justinian/jsix.git
synced 2025-12-09 16:04:32 -08:00
Instead of making every callsite that may make a thread do a blocking operation also invoke the scheduler, move that logic into thread implementation - if the thread is blocking and is the current thread, call schedule(). Related changes in this commit: - Also make exiting threads and processes call the scheduler when blocking. - Threads start blocked, and get automatically added to the scheduler's blocked list.
68 lines
1.4 KiB
C++
68 lines
1.4 KiB
C++
#include "j6/errors.h"
|
|
#include "j6/types.h"
|
|
|
|
#include "log.h"
|
|
#include "objects/process.h"
|
|
#include "objects/thread.h"
|
|
|
|
namespace syscalls {
|
|
|
|
j6_status_t
|
|
thread_koid(j6_koid_t *koid)
|
|
{
|
|
if (koid == nullptr)
|
|
return j6_err_invalid_arg;
|
|
|
|
*koid = thread::current().koid();
|
|
return j6_status_ok;
|
|
}
|
|
|
|
j6_status_t
|
|
thread_create(void *rip, j6_handle_t *handle)
|
|
{
|
|
thread &parent = thread::current();
|
|
process &p = parent.parent();
|
|
|
|
thread *child = p.create_thread();
|
|
child->add_thunk_user(reinterpret_cast<uintptr_t>(rip));
|
|
*handle = p.add_handle(child);
|
|
child->clear_state(thread::state::loading);
|
|
child->set_state(thread::state::ready);
|
|
|
|
log::debug(logs::syscall, "Thread %llx spawned new thread %llx, handle %d",
|
|
parent.koid(), child->koid(), *handle);
|
|
|
|
return j6_status_ok;
|
|
}
|
|
|
|
j6_status_t
|
|
thread_exit(int64_t status)
|
|
{
|
|
thread &th = thread::current();
|
|
log::debug(logs::syscall, "Thread %llx exiting with code %d", th.koid(), status);
|
|
th.exit(status);
|
|
|
|
log::error(logs::syscall, "returned to exit syscall");
|
|
return j6_err_unexpected;
|
|
}
|
|
|
|
j6_status_t
|
|
thread_pause()
|
|
{
|
|
thread &th = thread::current();
|
|
th.wait_on_signals(&th, -1ull);
|
|
return j6_status_ok;
|
|
}
|
|
|
|
j6_status_t
|
|
thread_sleep(uint64_t til)
|
|
{
|
|
thread &th = thread::current();
|
|
log::debug(logs::syscall, "Thread %llx sleeping until %llu", th.koid(), til);
|
|
|
|
th.wait_on_time(til);
|
|
return j6_status_ok;
|
|
}
|
|
|
|
} // namespace syscalls
|