Files
ViperOS/vpsdk/process.vp
T

102 lines
3.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from . import syscall as syscall
def exit(code: int):
syscall._syscall1(uint64(syscall.Syscall.EXIT), uint64(code))
def reboot():
"""Reset the machine via Linux reboot(169, LINUX_REBOOT_CMD_RESTART)."""
syscall.reboot_linux(uint64(0x01234567))
def shutdown():
"""Power off the machine via Linux reboot(169, LINUX_REBOOT_CMD_POWER_OFF)."""
syscall.reboot_linux(uint64(0x4321FEDC))
def thread_exit():
"""当前用户线程显式退出:内核回收其用户栈并标记线程结束(不返回)。"""
syscall._syscall0(uint64(syscall.Syscall.THREAD_EXIT))
def yield_cpu():
# S3e:原走 WAIT_WINDOW(0,2)(UI 私有 syscall,已随内核 UI 一并删除)。
# 语义等价的是 Linux 对齐段里的 sched_yield ⇒ 内核同样是 try_reschedule()。
syscall._syscall0(uint64(syscall.Syscall.YIELD))
def get_pid() -> int:
"""当前进程 pid(getpid)。/dev/desk 的事件按 pid 过滤,建窗前就要拿到它。"""
return int(syscall._syscall0(uint64(syscall.Syscall.GETPID)))
def get_ppid() -> int:
return int(syscall._syscall0(uint64(syscall.Syscall.GETPPID)))
def spawn(path: const str, blocking: int = 0) -> int:
"""ViperOS launcher: start a new child and keep the caller alive."""
return int(syscall._syscall2(uint64(syscall.Syscall.SPAWN_PROC), uint64(path), uint64(blocking)))
def execve(path: const str, argv: rawptr, envp: rawptr) -> int:
"""Linux execve: replace the current process (returns only on failure)."""
return int(syscall._syscall3(uint64(syscall.Syscall.SPAWN), uint64(path), uint64(argv), uint64(envp)))
def fork() -> int:
return int(syscall._syscall0(uint64(syscall.Syscall.FORK)))
def clone(flags: uint64, stack: uint64, ptid: uint64 = 0, ctid: uint64 = 0, tls: uint64 = 0) -> int:
return int(syscall._syscall5(uint64(syscall.Syscall.CLONE), flags, stack, ptid, ctid, tls))
def wait4(pid: int, status: rawptr, options: uint32 = 0) -> int:
return int(syscall._syscall4(uint64(syscall.Syscall.WAIT4), uint64(pid),
uint64(status), uint64(options), 0))
def thread_create(func: rawptr, arg: rawptr = None) -> int:
return int(syscall._syscall2(uint64(syscall.Syscall.THREAD_CREATE_OLD), uint64(func), uint64(arg)))
class Thread:
_tid: int
_func: rawptr
_arg: rawptr
_running: int
def __init__(self, func: rawptr, arg: rawptr = None):
self._func = func
self._arg = arg
self._tid = -1
self._running = 0
def start(self) -> int:
if self._running: return -1
self._tid = thread_create(self._func, self._arg)
if self._tid < 0: return -1
self._running = 1
return 0
def is_alive(self) -> int:
return self._running
def get_tid(self) -> int:
return self._tid
class Process:
_pid: int
_path: [128]char
def __init__(self, path: const str):
self._pid = -1
i: int = 0
for ch in path:
if i >= 127: break
self._path[i] = ch
i += 1
self._path[i] = 0
def start(self) -> int:
self._pid = spawn(&self._path[0], 0)
return self._pid
def start_blocking(self) -> int:
self._pid = spawn(&self._path[0], 1)
return self._pid
def get_pid(self) -> int:
return self._pid