DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
cptr.hpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3// Fork baseline commit: dbe-02-12-17 (2022-05-12).
4// Renamed since fork: yes (from dbe/cptr.hpp to include/dbe/cptr.hpp).
5
6/************************************************************
7 * cptr.hpp *
8 * *
9 * Created on: Sep 14, 2014 *
10 * Author: Leonidas Georgopoulos *
11 *
12 * Copyright (C) 2014 Leonidas Georgopoulos
13 * Distributed under the Boost Software License, Version 1.0.
14 * (See accompanying file LICENSE_1_0.txt or
15 * copy at http://www.boost.org/LICENSE_1_0.txt)
16 *
17 ************************************************************/
18#ifndef LUTILS_CPTR_HPP
19#define LUTILS_CPTR_HPP
20
21#include <utility>
22#include <iostream>
23#include <mutex>
24#include <memory>
25#include <thread>
26#include <chrono>
27
28template<typename T, typename M>
29class cptr_proxy;
30
31//-----------------------------------------------------------------------------------------------------
54template<typename T, typename M = std::recursive_mutex, int N = 100> class cptr
55{
56protected:
57 std::shared_ptr<M> llock;
58
59 T * base;
60
61public:
62 explicit cptr ( T * p )
63 : llock ( new M() ),
64 base ( p )
65 {
66 }
67
69 {
70 while ( !llock->try_lock() )
71 {
72 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
73 }
74
75 return *this;
76 }
77
79 {
80 while ( !llock->try_lock() )
81 {
82 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
83 }
84
85 return *this;
86 }
87
88 T * get()
89 {
90 return base;
91 }
92
93 T * get() const
94 {
95 return base;
96 }
97
98 friend class cptr_proxy<T, M> ;
99};
100//-----------------------------------------------------------------------------------------------------
101//template<typename T, typename M, int N> M cptr<T,M,N>::llock;
102//-----------------------------------------------------------------------------------------------------
110template<typename T, typename M>
112{
113private:
114 T * that;
115
116 std::shared_ptr<M> llock_ptr;
117
118public:
119 cptr_proxy ( cptr<T, M> const & cp )
120 : that ( cp.base ),
121 llock_ptr ( cp.llock )
122 {
123 }
124
126 {
127 return that;
128 }
129
131 {
132 llock_ptr->unlock();
133 }
134};
135//-----------------------------------------------------------------------------------------------------
136
137#endif /*LUTILS_CPTR_HPP*/
cptr_proxy(cptr< T, M > const &cp)
Definition cptr.hpp:119
T * that
Definition cptr.hpp:114
std::shared_ptr< M > llock_ptr
Definition cptr.hpp:116
~cptr_proxy()
Definition cptr.hpp:130
T * operator->()
Definition cptr.hpp:125
Definition cptr.hpp:55
cptr_proxy< T, M > operator->() const
Definition cptr.hpp:78
cptr(T *p)
Definition cptr.hpp:62
cptr_proxy< T, M > operator->()
Definition cptr.hpp:68
T * get()
Definition cptr.hpp:88
std::shared_ptr< std::recursive_mutex > llock
Definition cptr.hpp:57
t_internal_changes_stack * base
Definition cptr.hpp:59