this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add SimPersistentTerm

garrison b61f9452 a8a5de68

+61 -2
+3 -1
lib/trinity/scheduler.ex
··· 12 12 proc_aliases: :ets.table, 13 13 14 14 nodes: :ets.table, 15 + node_persistent_terms: :ets.table, 15 16 node_procs: :ets.table, 16 17 proc_nodes: :ets.table, 17 18 ··· 31 32 :proc_aliases, 32 33 33 34 :nodes, 35 + :node_persistent_terms, 36 + :node_procs, 34 37 :proc_nodes, 35 - :node_procs, 36 38 37 39 :file_paths, 38 40 :file_data,
+1
lib/trinity/scheduler/simulation_supervisor.ex
··· 58 58 proc_aliases: :ets.new(__MODULE__, [:set, :public]), 59 59 60 60 nodes: :ets.new(__MODULE__, [:set, :public]), 61 + node_persistent_terms: :ets.new(__MODULE__, [:set, :public]), 61 62 node_procs: :ets.new(__MODULE__, [:set, :public]), 62 63 proc_nodes: :ets.new(__MODULE__, [:set, :public]), 63 64
+52
lib/trinity/sim_persistent_term.ex
··· 1 + defmodule Trinity.SimPersistentTerm do 2 + alias Trinity.Scheduler.Simulation 3 + 4 + import Trinity.Scheduler, only: [simulation_key: 0, sim_node_key: 0] 5 + 6 + @spec get_sim :: Simulation.t | nil 7 + defp get_sim, do: Process.get(simulation_key()) 8 + 9 + @spec get_proc_node :: atom 10 + defp get_proc_node, do: Process.get(sim_node_key()) 11 + 12 + @spec put(:persistent_term.key, :persistent_term.value) :: :ok 13 + def put(key, value) do 14 + case get_sim() do 15 + nil -> :persistent_term.put(key, value) 16 + _sim -> sim_put(key, value) 17 + end 18 + end 19 + 20 + @spec get(:persistent_term.key, :persistent_term.value) :: :persistent_term.value 21 + def get(key, default) do 22 + case get_sim() do 23 + nil -> :persistent_term.get(key, default) 24 + _sim -> sim_get(key, default) 25 + end 26 + end 27 + 28 + defp sim_put(key, value) do 29 + %Simulation{node_persistent_terms: node_persistent_terms} = get_sim() 30 + node = get_proc_node() 31 + 32 + terms = 33 + case :ets.lookup(node_persistent_terms, node) do 34 + [{^node, terms}] -> terms 35 + [] -> %{} 36 + end 37 + 38 + terms = Map.put(terms, key, value) 39 + :ets.insert(node_persistent_terms, {node, terms}) 40 + :ok 41 + end 42 + 43 + defp sim_get(key, default) do 44 + %Simulation{node_persistent_terms: node_persistent_terms} = get_sim() 45 + node = get_proc_node() 46 + 47 + case :ets.lookup(node_persistent_terms, node) do 48 + [{^node, %{^key => value}}] -> value 49 + _ -> default 50 + end 51 + end 52 + end
+5 -1
test/trinity_test.exs
··· 1 1 defmodule TrinityTest do 2 2 use ExUnit.Case 3 3 4 - alias Trinity.{Sim, SimProcess, SimLogger, Scheduler} 4 + alias Trinity.{Sim, SimProcess, SimPersistentTerm, SimLogger, Scheduler} 5 5 import Trinity.Scheduler, only: [receive_yield: 1] 6 6 require SimLogger 7 7 ··· 34 34 } 35 35 state = write_value(state, initial_count) 36 36 SimLogger.debug "Initial state (id=#{id}): #{inspect(state)}" 37 + 38 + :ok = SimPersistentTerm.put("counter-#{id}", initial_count) 39 + term = SimPersistentTerm.get("counter-#{id}", nil) 40 + SimLogger.debug "Persistent term: #{term}" 37 41 38 42 # Note: this intentionally tests a log message with no variables 39 43 SimLogger.debug "Init complete"