61 lines
1.2 KiB
Haxe
61 lines
1.2 KiB
Haxe
package kernel.ps;
|
|
|
|
import kernel.log.Log;
|
|
import kernel.ps.ProcessHandle.HandleConfig;
|
|
|
|
using tink.CoreApi;
|
|
|
|
typedef PID = Int;
|
|
|
|
class ProcessManager {
|
|
private static final processList = new Map<PID, ProcessHandle>();
|
|
|
|
public static function run(process:IProcess, config:HandleConfig):PID {
|
|
var pid = createPID();
|
|
var handle = new ProcessHandle(config, pid);
|
|
|
|
processList.set(pid, handle);
|
|
|
|
try {
|
|
process.run(handle);
|
|
} catch (e:Dynamic) {
|
|
Log.error("Error while running process: " + e);
|
|
handle.close(false);
|
|
}
|
|
|
|
return pid;
|
|
}
|
|
|
|
public static function kill(pid:PID) {
|
|
if (!processList.exists(pid)) {
|
|
Log.warn("Trying to kill non-existing process: " + pid);
|
|
return;
|
|
}
|
|
|
|
var handle = processList.get(pid);
|
|
|
|
handle.close();
|
|
}
|
|
|
|
private static function createPID():PID {
|
|
// TODO: better PID generation
|
|
|
|
// generate a random PID
|
|
return Math.ceil(Math.random() * 1000000);
|
|
}
|
|
|
|
@:allow(kernel.ui.WindowManager)
|
|
private static function getProcess(pid:PID):Null<ProcessHandle> {
|
|
return processList.get(pid);
|
|
}
|
|
|
|
@:allow(kernel.ps.ProcessHandle)
|
|
private static function removeProcess(pid:PID):Void {
|
|
processList.remove(pid);
|
|
}
|
|
|
|
public static function listProcesses():Array<PID> {
|
|
return [for (pid in processList.keys()) pid];
|
|
}
|
|
}
|