Patrick Williams | 7f66425 | 2020-06-04 10:20:56 -0500 | [diff] [blame] | 1 | #include "sdbusplus/server/transaction.hpp" |
Adriana Kobylak | 1afb4e6 | 2017-01-31 11:10:01 -0600 | [diff] [blame] | 2 | |
| 3 | namespace sdbusplus |
| 4 | { |
| 5 | namespace server |
| 6 | { |
| 7 | namespace transaction |
| 8 | { |
| 9 | namespace details |
| 10 | { |
| 11 | |
| 12 | // Transaction Id |
| 13 | thread_local uint64_t id = 0; |
| 14 | |
| 15 | } // namespace details |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 16 | |
| 17 | uint64_t get_id() |
| 18 | { |
| 19 | // If the transaction id has not been initialized, generate one. |
| 20 | if (!details::id) |
| 21 | { |
| 22 | details::Transaction t; |
| 23 | details::id = std::hash<details::Transaction>{}(t); |
| 24 | } |
| 25 | return details::id; |
| 26 | } |
| 27 | |
| 28 | void set_id(uint64_t value) |
| 29 | { |
| 30 | details::id = value; |
| 31 | } |
| 32 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 33 | void set_id(message_t& msg) |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 34 | { |
| 35 | auto tbus = msg.get_bus(); |
| 36 | auto t = Transaction(tbus, msg); |
| 37 | set_id(std::hash<Transaction>{}(t)); |
| 38 | } |
| 39 | |
Adriana Kobylak | 1afb4e6 | 2017-01-31 11:10:01 -0600 | [diff] [blame] | 40 | } // namespace transaction |
| 41 | } // namespace server |
| 42 | } // namespace sdbusplus |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 43 | |
| 44 | namespace std |
| 45 | { |
| 46 | |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 47 | size_t hash<sdbusplus::bus_t>::operator()(sdbusplus::bus_t& b) const |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 48 | { |
| 49 | auto name = b.get_unique_name(); |
| 50 | return std::hash<std::string>{}(name); |
| 51 | } |
| 52 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 53 | size_t hash<sdbusplus::message_t>::operator()(sdbusplus::message_t& m) const |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 54 | { |
| 55 | switch (m.get_type()) |
| 56 | { |
| 57 | // Reply messages will always embed the cookie of the original |
| 58 | // message in a separate location. We want to use this cookie |
| 59 | // to correlate messages as one transaction. |
| 60 | case SD_BUS_MESSAGE_METHOD_RETURN: |
| 61 | case SD_BUS_MESSAGE_METHOD_ERROR: |
| 62 | return std::hash<uint64_t>{}(m.get_reply_cookie()); |
| 63 | // Method calls will have the cookie in the header when sealed. |
| 64 | // Since we are on the server side that should always be the case. |
| 65 | case SD_BUS_MESSAGE_METHOD_CALL: |
| 66 | return std::hash<uint64_t>{}(m.get_cookie()); |
| 67 | // Outgoing signals don't have a cookie so we need to use |
| 68 | // something else as an id. Just use a monotonic unique one. |
| 69 | case SD_BUS_MESSAGE_SIGNAL: |
| 70 | return std::hash<uint64_t>{}( |
| 71 | std::chrono::steady_clock::now().time_since_epoch().count()); |
| 72 | default: |
| 73 | throw std::runtime_error("hash message: Unknown message type"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | size_t hash<sdbusplus::server::transaction::Transaction>::operator()( |
Patrick Williams | 41f42f6 | 2022-09-30 06:49:50 -0500 | [diff] [blame] | 78 | const sdbusplus::server::transaction::Transaction& t) const |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 79 | { |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 80 | auto hash1 = std::hash<sdbusplus::bus_t>{}(t.bus); |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 81 | auto hash2 = std::hash<sdbusplus::message_t>{}(t.msg); |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 82 | |
| 83 | // boost::hash_combine() algorithm. |
| 84 | return static_cast<size_t>( |
| 85 | hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2))); |
| 86 | } |
| 87 | |
| 88 | size_t hash<sdbusplus::server::transaction::details::Transaction>::operator()( |
Patrick Williams | 41f42f6 | 2022-09-30 06:49:50 -0500 | [diff] [blame] | 89 | const sdbusplus::server::transaction::details::Transaction& t) const |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 90 | { |
| 91 | auto hash1 = std::hash<int>{}(t.time); |
| 92 | auto hash2 = std::hash<std::thread::id>{}(t.thread); |
| 93 | |
| 94 | // boost::hash_combine() algorithm. |
| 95 | return static_cast<size_t>( |
| 96 | hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2))); |
| 97 | } |
| 98 | } // namespace std |