diff --git a/lib/trinity/sim.ex b/lib/trinity/sim.ex new file mode 100644 index 0000000..2862f67 --- /dev/null +++ b/lib/trinity/sim.ex @@ -0,0 +1,15 @@ +defmodule Trinity.Sim do + alias Trinity.Scheduler.Simulation + import Trinity.Scheduler, only: [simulation_key: 0] + + @spec get_sim :: Simulation.t | nil + defp get_sim, do: Process.get(simulation_key()) + + @spec simulated? :: boolean + def simulated? do + case get_sim() do + nil -> false + _sim -> true + end + end +end diff --git a/lib/trinity/sim_system.ex b/lib/trinity/sim_system.ex new file mode 100644 index 0000000..dfa6603 --- /dev/null +++ b/lib/trinity/sim_system.ex @@ -0,0 +1,20 @@ +defmodule Trinity.SimSystem do + alias Trinity.Scheduler.Simulation + import Trinity.Scheduler, only: [simulation_key: 0] + + @spec get_sim :: Simulation.t | nil + defp get_sim, do: Process.get(simulation_key()) + + @spec monotonic_time :: integer + def monotonic_time do + case get_sim() do + nil -> System.monotonic_time() + _sim -> sim_monotonic_time() + end + end + + defp sim_monotonic_time do + %{now: now} = get_sim() + :atomics.get(now, 1) + end +end