Initial import of ViperOS
This commit is contained in:
65
vpsdk/process.py
Normal file
65
vpsdk/process.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import t, c
|
||||
from . import syscall as syscall
|
||||
|
||||
def exit(code: t.CInt) -> t.CVoid:
|
||||
syscall._syscall1(t.CUInt64T(syscall.EXIT), t.CUInt64T(code))
|
||||
|
||||
def yield_cpu() -> t.CVoid:
|
||||
syscall._syscall2(t.CUInt64T(syscall.WAIT_WINDOW), t.CUInt64T(0), t.CUInt64T(2))
|
||||
|
||||
def spawn(path: t.CConst | t.CChar | t.CPtr, blocking: t.CInt = 0) -> t.CInt:
|
||||
return t.CInt(syscall._syscall2(t.CUInt64T(syscall.SPAWN), t.CUInt64T(path), t.CUInt64T(blocking)))
|
||||
|
||||
def thread_create(func: t.CVoid | t.CPtr, arg: t.CVoid | t.CPtr = None) -> t.CInt:
|
||||
return t.CInt(syscall._syscall2(t.CUInt64T(syscall.THREAD_CREATE), t.CUInt64T(func), t.CUInt64T(arg)))
|
||||
|
||||
@t.Object
|
||||
class Thread:
|
||||
_tid: t.CInt
|
||||
_func: t.CVoid | t.CPtr
|
||||
_arg: t.CVoid | t.CPtr
|
||||
_running: t.CInt
|
||||
|
||||
def __init__(self, func: t.CVoid | t.CPtr, arg: t.CVoid | t.CPtr = None):
|
||||
self._func = func
|
||||
self._arg = arg
|
||||
self._tid = -1
|
||||
self._running = 0
|
||||
|
||||
def start(self) -> t.CInt:
|
||||
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) -> t.CInt:
|
||||
return self._running
|
||||
|
||||
def get_tid(self) -> t.CInt:
|
||||
return self._tid
|
||||
|
||||
@t.Object
|
||||
class Process:
|
||||
_pid: t.CInt
|
||||
_path: t.CArray[t.CChar, 128]
|
||||
|
||||
def __init__(self, path: t.CConst | t.CChar | t.CPtr):
|
||||
self._pid = -1
|
||||
i: t.CInt = 0
|
||||
for ch in path:
|
||||
if i >= 127: break
|
||||
self._path[i] = ch
|
||||
i += 1
|
||||
self._path[i] = 0
|
||||
|
||||
def start(self) -> t.CInt:
|
||||
self._pid = spawn(c.Addr(self._path[0]), 0)
|
||||
return self._pid
|
||||
|
||||
def start_blocking(self) -> t.CInt:
|
||||
self._pid = spawn(c.Addr(self._path[0]), 1)
|
||||
return self._pid
|
||||
|
||||
def get_pid(self) -> t.CInt:
|
||||
return self._pid
|
||||
Reference in New Issue
Block a user