lichao
2021-05-21 1ff714838c03cba1a18884d5b48a20ee6c4275ac
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * =====================================================================================
 *
 *       Filename:  shm_msg_queue.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月26日 16时25分05秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include "shm_msg_queue.h"
 
using namespace bhome_msg;
using namespace boost::interprocess;
 
namespace
{
std::string MsgQIdToName(const ShmMsgQueue::MQId id)
{
    char buf[40] = "mqOx";
    int n = sprintf(buf + 4, "%lx", id);
    return std::string(buf, n + 4);
}
 
} // namespace
 
ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, const int len) :
    id_(id),
    queue_(segment, MsgQIdToName(id_), len, segment.get_segment_manager())
{
}
 
ShmMsgQueue::ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len) :
    id_(id),
    queue_(segment, create_or_else_find, MsgQIdToName(id_), len, segment.get_segment_manager())
{
    if (!queue_.IsOk()) {
        throw("error create/find msgq " + std::to_string(id_));
    }
}
ShmMsgQueue::ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id) :
    id_(id), queue_(abs_addr, segment, MsgQIdToName(id_))
{
    //TODO check some tag.
}
 
ShmMsgQueue::~ShmMsgQueue() {}
 
#ifndef BH_USE_ATOMIC_Q
ShmMsgQueue::Mutex &ShmMsgQueue::GetMutex(const MQId id)
{
    static std::unordered_map<MQId, std::shared_ptr<Mutex>> imm;
 
    static std::mutex mtx;
    std::lock_guard<std::mutex> lock(mtx);
    auto pos = imm.find(id);
    if (pos == imm.end()) {
        pos = imm.emplace(id, new Mutex(id)).first;
        // pos = imm.emplace(id, new Mutex()).first;
    }
    return *pos->second;
}
#endif
 
bool ShmMsgQueue::Remove(SharedMemory &shm, const MQId id)
{
    Queue *q = Find(shm, id);
    if (q) {
        RawData val = 0;
        while (q->TryRead(val)) {
            if (IsCmd(val)) {
                LOG_DEBUG() << "clsing queue " << id << ", has a cmd" << DecodeCmd(val);
            } else {
                MsgI(val, shm).Release();
            }
        }
    }
    return Shmq::Remove(shm, MsgQIdToName(id));
}
 
ShmMsgQueue::Queue *ShmMsgQueue::Find(SharedMemory &shm, const MQId remote_id)
{
    return Shmq::Find(shm, MsgQIdToName(remote_id));
}
 
bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQInfo &remote, const RawData val)
{
    try {
        //TODO find from center, or use offset.
        ShmMsgQueue dest(remote.offset_, shm, remote.id_);
#ifndef BH_USE_ATOMIC_Q
        Guard lock(GetMutex(remote_id));
#endif
        return dest.queue().TryWrite(val);
    } catch (...) {
        // SetLastError(eNotFound, "remote not found");
        return false;
    }
}
 
// Test shows that in the 2 cases:
// 1) build msg first, then find remote queue;
// 2) find remote queue first, then build msg;
// 1 is about 50% faster than 2, maybe cache related.