Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LinkedObject.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_LINKEDOBJECT_HXX_
37#define _UTILS_LINKEDOBJECT_HXX_
38
39#include <atomic>
40
41#include "utils/Atomic.hxx"
43
48{
49public:
51 {
52 // Ensures the object gets created at static initialization time while
53 // the program is still single threaded.
54 get();
55 }
56
57 Atomic *get()
58 {
59 if (isInitialized_.exchange(1) == 0)
60 {
61 atomic_.emplace();
62 }
63 return &atomic_.value();
64 }
65
66private:
67 std::atomic_uint_least8_t isInitialized_;
69};
70
74template <class T> class LinkedObjectHeadMutex
75{
76public:
77 static CreateOnlyAtomic headMu_;
78};
79
83template <class T> class LinkedObject
84{
85public:
89 {
90 return static_cast<T *>(link_);
91 }
92
94 static T *link_head()
95 {
96 return static_cast<T *>(head_);
97 }
98
100 static Atomic* head_mu() {
102 }
103
104protected:
107 {
108 return static_cast<T *>(this);
109 }
110
113 {
115 link_ = head_;
116 head_ = link_this();
117 }
118
121 {
123 T **p = &head_;
124 while (*p && *p != this)
125 {
126 p = &((*p)->LinkedObject<T>::link_);
127 }
128 if (*p == this)
129 {
130 *p = this->link_;
131 }
132 else
133 {
134 HASSERT(0);
135 }
136 }
137
141 static T *head_;
142};
143
144// static
145template <class T> T *LinkedObject<T>::head_{nullptr};
147
148#endif // _UTILS_LINKEDOBJECT_HXX_
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
This object encapsulates an Atomic, which never gets destroyed.
This static object is factored into a separate namespace because current GDB crashes when trying to p...
Using this class as a base class will cause the given class to have all its instances linked up in a ...
T * link_
Linked list pointer.
LinkedObject()
Constructor. Puts *this on the linked list.
static Atomic * head_mu()
Locks the list for modification (at any entry!).
~LinkedObject()
Constructor. Removes *this from the linked list.
static T * link_head()
static T * head_
Beginning of the list.
Template class that allows allocating storage for an object but not calling its constructor.
T & emplace(Args &&... args)
Constructs the embedded object.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138