fork download
  1. function create_queue()
  2. local queue = { buffer = {} }
  3.  
  4. function queue.put(...)
  5. for _, v in ipairs(arg) do
  6. table.insert(queue.buffer, 1, v)
  7. end
  8. end
  9.  
  10. function queue.next()
  11. return table.remove(queue.buffer)
  12. end
  13.  
  14. return queue
  15. end
  16.  
  17. function spawn_service_manager()
  18. local service_manager = {}
  19. local services = {}
  20.  
  21. local function create_service(chunk)
  22. local service = {}
  23.  
  24. -- TODO
  25.  
  26. return service
  27. end
  28.  
  29. function service_manager.kldload(chunk)
  30. local service = create_service(chunk)
  31. table.insert(services, service)
  32. service() -- Initialize the service
  33. end
  34.  
  35. return service_manager
  36. end
  37.  
  38. function spawn_vfs()
  39. local vfs = {}
  40. local handles = {}
  41. local mountpoints = {}
  42. local filesystems = {}
  43.  
  44. function vfs.open() end
  45. function vfs.close() end
  46. function vfs.read() end
  47. function vfs.write() end
  48.  
  49. return vfs
  50. end
  51.  
  52. function spawn_scheduler(init)
  53. local scheduler = {}
  54. local processes = {}
  55.  
  56. -- Load a process into memory:
  57. local function create_process(chunk)
  58. local process = create_queue()
  59. local watchers = {}
  60. local co = nil
  61.  
  62. function process.next()
  63. for _, msg in ipairs(process.buffer) do
  64. for _, watcher in ipairs(watchers) do
  65. -- Handle watcher
  66. end
  67. table.remove(queue.buffer) -- deueue-like operation
  68. end
  69. coroutine.resume(co)
  70. return coroutine.status(co)
  71. end
  72.  
  73. -- Load process:
  74. co = coroutine.create(chunk)
  75. return process
  76. end
  77.  
  78. function scheduler.spawn(chunk)
  79. local proc = create_process(chunk)
  80. table.insert(processes, proc)
  81. end
  82.  
  83. function scheduler.run()
  84. for index, process in ipairs(processes) do
  85. local status = process.next()
  86. if status == 'dead' then
  87. process = nil
  88. table.remove(processes, index)
  89. print("Process quit")
  90. end
  91. end
  92. -- return scheduler.run()
  93. end
  94.  
  95. -- Bootstrap the init system:
  96. scheduler.spawn(init)
  97. return scheduler
  98. end
  99.  
  100. function _main()
  101. local scheduler = spawn_scheduler(loadstring(""))
  102. local service = spawn_service_manager()
  103. local vfs = spawn_vfs()
  104.  
  105. scheduler.run()
  106. end
  107.  
  108. _main()
  109.  
Success #stdin #stdout 0s 2844KB
stdin
Standard input is empty
stdout
Process quit