xref: /openbmc/sdbusplus/include/sdbusplus/server/transaction.hpp (revision 6269157344064457b7e1241d4efe59c6c51c7a59)
1 #pragma once
2 
3 #include <systemd/sd-bus.h>
4 
5 #include <sdbusplus/bus.hpp>
6 
7 #include <chrono>
8 #include <stdexcept>
9 #include <thread>
10 
11 namespace sdbusplus
12 {
13 namespace server
14 {
15 namespace transaction
16 {
17 namespace details
18 {
19 
20 // Transaction Id
21 extern thread_local uint64_t id;
22 
23 struct Transaction
24 {
Transactionsdbusplus::server::transaction::details::Transaction25     Transaction() : time(std::time(nullptr)), thread(std::this_thread::get_id())
26     {}
27 
28     std::time_t time;
29     std::thread::id thread;
30 };
31 
32 } // namespace details
33 
34 struct Transaction
35 {
Transactionsdbusplus::server::transaction::Transaction36     Transaction(sdbusplus::bus_t& bus_in, sdbusplus::message_t& msg_in) :
37         bus(bus_in), msg(msg_in)
38     {}
39 
40     sdbusplus::bus_t& bus;
41     sdbusplus::message_t& msg;
42 };
43 
44 } // namespace transaction
45 } // namespace server
46 } // namespace sdbusplus
47 
48 namespace std
49 {
50 
51 /** @ brief Overload of std::hash for sdbusplus::bus_t */
52 template <>
53 struct hash<sdbusplus::bus_t>
54 {
55     size_t operator()(sdbusplus::bus_t& b) const;
56 };
57 
58 /** @ brief Overload of std::hash for sdbusplus::message_t */
59 template <>
60 struct hash<sdbusplus::message_t>
61 {
62     size_t operator()(sdbusplus::message_t& m) const;
63 };
64 
65 /** @ brief Overload of std::hash for Transaction */
66 template <>
67 struct hash<sdbusplus::server::transaction::Transaction>
68 {
69     size_t operator()(
70         const sdbusplus::server::transaction::Transaction& t) const;
71 };
72 
73 /** @ brief Overload of std::hash for details::Transaction */
74 template <>
75 struct hash<sdbusplus::server::transaction::details::Transaction>
76 {
77     size_t operator()(
78         const sdbusplus::server::transaction::details::Transaction& t) const;
79 };
80 
81 } // namespace std
82 
83 namespace sdbusplus
84 {
85 namespace server
86 {
87 namespace transaction
88 {
89 
90 /** @brief Get transaction id
91  *
92  * @return The value of the transaction id
93  */
94 uint64_t get_id();
95 
96 /** @brief Set transaction id
97  *
98  * @param[in] value - Desired value for the transaction id
99  */
100 void set_id(uint64_t value);
101 
102 /** @brief Set transaction from message.
103  *
104  * @param[in] msg - The message to create the transaction from.
105  */
106 void set_id(message_t& msg);
107 
108 } // namespace transaction
109 } // namespace server
110 } // namespace sdbusplus
111