dymokomi/luce-server
Source of release 0.5.1
0.5.1 / src / internal / lifecycle.lucb
from internal.model import State
import internal.storage
import memory
import sync
import thread
import time
pub func retain(state: State*):
_ = state.references.add(1, .relaxed)
pub func release(state: State*):
if state.references.sub(1, .acq_rel) != 1:
return
state.table.destroy()
state.listener.destroy()
storage.release(state.temporary_directory)
free(state) in memory.heap
pub func stop(state: State*):
state.cancellation.request()
state.dispatch.lock.lock()
state.dispatch.ready.broadcast()
state.dispatch.changed.broadcast()
state.dispatch.lock.unlock()
pub func begin_drain(state: State*):
state.dispatch.lock.lock()
if not state.lifecycle.draining.load(.relaxed):
state.lifecycle.drain_until = time.now() + state.config.shutdown_timeout_ms * 1000000
state.lifecycle.draining.store(true, .release)
state.dispatch.lock.unlock()
## Timed response waits use a single shared clock thread because sync.Condition
## has no timed-wait operation. Idle application workers are not woken by ticks.
pub func clock_worker(context: void*?):
let state = (State*)(context else return)
while not state.cancellation.is_requested():
thread.sleep(10)
if let termination = state.lifecycle.termination:
if termination.requested():
begin_drain(state)
if state.lifecycle.draining.load(.acquire):
if state.counters.active.load(.acquire) == 0 or time.now() >= state.lifecycle.drain_until:
stop(state)
return
state.dispatch.lock.lock()
if state.dispatch.waiting > 0:
state.dispatch.changed.broadcast()
state.dispatch.lock.unlock()