-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathid_manager.h
55 lines (44 loc) · 1.55 KB
/
id_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef LEVIN_ID_MANAGER_H
#define LEVIN_ID_MANAGER_H
#include <mutex>
#include <boost/noncopyable.hpp>
#include "shared_utils.h"
namespace levin {
// @brief ID manager
// manage System V share memory segment identifier
class IdManager : public boost::noncopyable {
public:
// @brief {shm id, shared container binfile path}
typedef std::unordered_map<int, std::string> IdMap;
// @brief {shared container binfile path, shm id}
typedef std::unordered_map<std::string, int> ShmMap;
bool Register(const int id, const std::string &name);
bool DeRegister(const int id);
bool GetId(const std::string &name, int &id) const;
static IdManager& GetInstance() {
static IdManager instance;
return instance;
}
// @brief get_all_shmid maybe called before main(). logger by stdout
static bool get_all_shmid(std::vector<SharedMidInfo> &shmid_info, bool no_attach);
private:
// @brief This constructor does nothing more than ensure that instance is init before main()
// thus creating the static object before multithreading race issues come up
struct ObjectCreator {
ObjectCreator() {
IdManager::GetInstance().init();
}
};
static ObjectCreator creator;
// @brief init is called before main(). logger by stdout
bool init();
bool insertWithLock(const int id, const std::string &name);
bool deleteWithLock(const int id);
IdManager() {}
private:
IdMap _id_map;
ShmMap _shm_map;
mutable std::mutex _mutex;
};
} // namespace levin
#endif // LEVIN_ID_MANAGER_H