70 lines
1.8 KiB
Plaintext
70 lines
1.8 KiB
Plaintext
from . import syscall as syscall
|
|
|
|
|
|
def exit(code: int):
|
|
syscall._syscall1(uint64(syscall.EXIT), uint64(code))
|
|
|
|
def thread_exit():
|
|
"""当前用户线程显式退出:内核回收其用户栈并标记线程结束(不返回)。"""
|
|
syscall._syscall0(uint64(syscall.THREAD_EXIT))
|
|
|
|
def yield_cpu():
|
|
syscall._syscall2(uint64(syscall.WAIT_WINDOW), uint64(0), uint64(2))
|
|
|
|
def spawn(path: const str, blocking: int = 0) -> int:
|
|
return int(syscall._syscall2(uint64(syscall.SPAWN), uint64(path), uint64(blocking)))
|
|
|
|
def thread_create(func: rawptr, arg: rawptr = None) -> int:
|
|
return int(syscall._syscall2(uint64(syscall.THREAD_CREATE), 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
|