github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ipc/shm_bookkeeper.h (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #pragma once 21 #include "common/common.h" 22 #include "common/ipc/shm_base.h" 23 #include <boost/interprocess/allocators/allocator.hpp> 24 #include <boost/interprocess/containers/map.hpp> 25 #include <boost/interprocess/containers/string.hpp> 26 #include <boost/interprocess/managed_shared_memory.hpp> 27 28 namespace neb { 29 namespace ipc { 30 31 void clean_bookkeeper_env(const std::string &name); 32 33 namespace internal { 34 class shm_bookkeeper { 35 public: 36 shm_bookkeeper(const std::string &name); 37 38 std::unique_ptr<boost::interprocess::named_mutex> 39 acquire_named_mutex(const std::string &name); 40 void release_named_mutex(const std::string &name); 41 42 std::unique_ptr<boost::interprocess::named_semaphore> 43 acquire_named_semaphore(const std::string &name); 44 void release_named_semaphore(const std::string &name); 45 46 std::unique_ptr<boost::interprocess::named_condition> 47 acquire_named_condition(const std::string &name); 48 void release_named_condition(const std::string &name); 49 50 void acquire(const std::string &name, const std::function<void()> &action); 51 void release(const std::string &name, const std::function<void()> &action); 52 53 void reset(); 54 55 private: 56 void acquire(const std::string &name, const std::function<void()> &action, 57 uint8_t type); 58 59 inline std::string mutex_name() { return m_name + ".mutex"; } 60 inline std::string mem_name() { return m_name + ".mem"; } 61 62 protected: 63 struct tag_counter_t { 64 public: 65 enum type_tag { 66 boost_mutex = 0, 67 boost_semaphore = 1, 68 boost_condition = 2, 69 other_unknown, 70 }; 71 72 inline uint64_t data() { return m_data.m_data; } 73 inline void set_data(uint64_t d) { m_data.m_data = d; } 74 inline uint64_t counter() { return m_data.m_detail.m_counter; } 75 inline type_tag type() { 76 return static_cast<type_tag>(m_data.m_detail.m_type); 77 } 78 inline void set_type(type_tag tt) { m_data.m_detail.m_type = tt; } 79 inline void set_counter(uint64_t c) { m_data.m_detail.m_counter = c; }; 80 81 protected: 82 union tag_counter_data { 83 uint64_t m_data; 84 struct { 85 uint8_t m_type : 2; 86 uint64_t m_counter : 62; 87 } m_detail; 88 }; 89 90 tag_counter_data m_data; 91 }; 92 93 typedef boost::interprocess::managed_shared_memory::segment_manager 94 segment_manager_t; 95 typedef boost::interprocess::allocator<char, segment_manager_t> 96 char_allocator_t; 97 typedef boost::interprocess::basic_string<char, std::char_traits<char>, 98 char_allocator_t> 99 char_string_t; 100 101 typedef std::pair<const char_string_t, uint64_t> map_value_t; 102 typedef std::pair<char_string_t, uint64_t> movable_map_value_t; 103 typedef boost::interprocess::allocator<map_value_t, segment_manager_t> 104 map_allocator_t; 105 106 typedef boost::interprocess::map<char_string_t, uint64_t, 107 std::less<char_string_t>, map_allocator_t> 108 map_t; 109 110 std::string m_name; 111 112 std::unique_ptr<boost::interprocess::managed_shared_memory> m_segment; 113 std::unique_ptr<map_allocator_t> m_allocator; 114 std::unique_ptr<boost::interprocess::named_mutex> m_mutex; 115 map_t *m_map; 116 117 }; // end class shm_bookkeeper 118 } 119 } 120 }