66 lines
1.7 KiB
Plaintext
66 lines
1.7 KiB
Plaintext
import t, c
|
|
from . import syscall as syscall
|
|
|
|
def exit(code: int):
|
|
syscall._syscall1(uint64(syscall.EXIT), uint64(code))
|
|
|
|
def yield_cpu():
|
|
syscall._syscall2(uint64(syscall.WAIT_WINDOW), uint64(0), uint64(2))
|
|
|
|
def spawn(path: t.CConst | t.CChar | t.CPtr, blocking: int = 0) -> int:
|
|
return int(syscall._syscall2(uint64(syscall.SPAWN), uint64(path), uint64(blocking)))
|
|
|
|
def thread_create(func: t.CPtr, arg: t.CPtr = None) -> int:
|
|
return int(syscall._syscall2(uint64(syscall.THREAD_CREATE), uint64(func), uint64(arg)))
|
|
|
|
@t.Object
|
|
class Thread:
|
|
_tid: int
|
|
_func: t.CPtr
|
|
_arg: t.CPtr
|
|
_running: int
|
|
|
|
def __init__(self, func: t.CPtr, arg: t.CPtr = 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
|
|
|
|
@t.Object
|
|
class Process:
|
|
_pid: int
|
|
_path: t.CArray[t.CChar, 128]
|
|
|
|
def __init__(self, path: t.CConst | t.CChar | t.CPtr):
|
|
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(c.Addr(self._path[0]), 0)
|
|
return self._pid
|
|
|
|
def start_blocking(self) -> int:
|
|
self._pid = spawn(c.Addr(self._path[0]), 1)
|
|
return self._pid
|
|
|
|
def get_pid(self) -> int:
|
|
return self._pid
|