|
1
|
1 #pragma once
|
|
|
2 #include "synchro.h"
|
|
|
3
|
|
|
4 namespace pfc {
|
|
|
5 // Read/write lock guarded object store for safe concurrent access
|
|
|
6 template<typename t_object>
|
|
|
7 class syncd_storage {
|
|
|
8 private:
|
|
|
9 typedef syncd_storage<t_object> t_self;
|
|
|
10 public:
|
|
|
11 syncd_storage() {}
|
|
|
12 template<typename t_source>
|
|
|
13 syncd_storage(const t_source & p_source) : m_object(p_source) {}
|
|
|
14 template<typename t_source>
|
|
|
15 void set(t_source && p_in) {
|
|
|
16 inWriteSync(m_sync);
|
|
|
17 m_object = std::forward<t_source>( p_in );
|
|
|
18 }
|
|
|
19 template<typename t_destination>
|
|
|
20 void get(t_destination & p_out) const {
|
|
|
21 inReadSync(m_sync);
|
|
|
22 p_out = m_object;
|
|
|
23 }
|
|
|
24 t_object get() const {
|
|
|
25 inReadSync(m_sync);
|
|
|
26 return m_object;
|
|
|
27 }
|
|
|
28 template<typename t_source>
|
|
|
29 const t_self & operator=(t_source && p_source) {set(std::forward<t_source>(p_source)); return *this;}
|
|
|
30 private:
|
|
|
31 mutable ::pfc::readWriteLock m_sync;
|
|
|
32 t_object m_object;
|
|
|
33 };
|
|
|
34
|
|
|
35 // Read/write lock guarded object store for safe concurrent access
|
|
|
36 // With 'has changed since last read' flag
|
|
|
37 template<typename t_object>
|
|
|
38 class syncd_storage_flagged {
|
|
|
39 private:
|
|
|
40 typedef syncd_storage_flagged<t_object> t_self;
|
|
|
41 public:
|
|
|
42 syncd_storage_flagged() : m_changed_flag(false) {}
|
|
|
43 template<typename t_source>
|
|
|
44 syncd_storage_flagged(const t_source & p_source, bool initChanged = false) : m_changed_flag(initChanged), m_object(p_source) {}
|
|
|
45 void set_changed(bool p_flag = true) {
|
|
|
46 inWriteSync(m_sync);
|
|
|
47 m_changed_flag = p_flag;
|
|
|
48 }
|
|
|
49 template<typename t_source>
|
|
|
50 void set(t_source && p_in) {
|
|
|
51 inWriteSync(m_sync);
|
|
|
52 m_object = std::forward<t_source>(p_in);
|
|
|
53 m_changed_flag = true;
|
|
|
54 }
|
|
|
55 bool has_changed() const {
|
|
|
56 // No point in locking here
|
|
|
57 // inReadSync(m_sync);
|
|
|
58 return m_changed_flag;
|
|
|
59 }
|
|
|
60 t_object peek() const {inReadSync(m_sync); return m_object;}
|
|
|
61 template<typename t_destination>
|
|
|
62 bool get_if_changed(t_destination & p_out) {
|
|
|
63 inReadSync(m_sync);
|
|
|
64 if (m_changed_flag) {
|
|
|
65 p_out = m_object;
|
|
|
66 m_changed_flag = false;
|
|
|
67 return true;
|
|
|
68 } else {
|
|
|
69 return false;
|
|
|
70 }
|
|
|
71 }
|
|
|
72 t_object get() {
|
|
|
73 inReadSync(m_sync);
|
|
|
74 m_changed_flag = false;
|
|
|
75 return m_object;
|
|
|
76 }
|
|
|
77 t_object get( bool & bHasChanged ) {
|
|
|
78 inReadSync(m_sync);
|
|
|
79 bHasChanged = m_changed_flag;
|
|
|
80 m_changed_flag = false;
|
|
|
81 return m_object;
|
|
|
82 }
|
|
|
83 template<typename t_destination>
|
|
|
84 void get(t_destination & p_out) {
|
|
|
85 inReadSync(m_sync);
|
|
|
86 p_out = m_object;
|
|
|
87 m_changed_flag = false;
|
|
|
88 }
|
|
|
89 template<typename t_source>
|
|
|
90 const t_self & operator=(t_source && p_source) {set(std::forward<t_source>(p_source)); return *this;}
|
|
|
91
|
|
|
92 template<typename arg_t>
|
|
|
93 bool compare_and_set(arg_t&& arg) {
|
|
|
94 inWriteSync(m_sync);
|
|
|
95 bool ret = false;
|
|
|
96 if (arg != m_object) {
|
|
|
97 m_object = std::forward<arg_t>(arg);
|
|
|
98 m_changed_flag = true;
|
|
|
99 ret = true;
|
|
|
100 }
|
|
|
101 return ret;
|
|
|
102 }
|
|
|
103 private:
|
|
|
104 mutable volatile bool m_changed_flag;
|
|
|
105 mutable ::pfc::readWriteLock m_sync;
|
|
|
106 t_object m_object;
|
|
|
107 };
|
|
|
108
|
|
|
109 } |