So many changes. Broken.
[invirt/third/libt4.git] / t4.cc
1 #include "include/t4.h"
2 #include <unistd.h>
3 #include "include/rpc/rpc.h"
4
5 using namespace std::chrono;
6
7 t4_state *global;
8
9 t4_state::t4_state(char log_prefix) : log_thread_prefix(log_prefix) {
10     uint32_t seed = std::random_device()();
11     auto time = system_clock::now().time_since_epoch();
12     auto ticks = time / 1ns;
13     seed ^= (uint32_t)ticks;
14     auto pid = getpid();
15     seed ^= (uint32_t)pid;
16     auto tid = std::hash<std::thread::id>()(std::this_thread::get_id());
17     seed ^= (uint32_t)tid;
18     random_generator.seed(seed);
19     // make sure the clock will read differently next time!
20     std::this_thread::sleep_for(1us);
21 }
22
23 t4_state::~t4_state() {
24     lock ml(handle_cache_mutex);
25     handle_cache.clear();
26     shared_mgr.shutdown();
27 }
28
29 shared_ptr<rpcc> t4_state::get_handle(const string & destination) {
30     lock ml(handle_cache_mutex);
31     if (!handle_cache[destination])
32         handle_cache[destination] = std::make_shared<rpcc>(destination);
33     return handle_cache[destination];
34 }
35
36 void t4_state::erase_handle(const string & destination) {
37     lock ml(handle_cache_mutex);
38     handle_cache.erase(destination);
39 }