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 <chrono>
22#include <iostream>
23#include <memory>
24#include <mutex>
25#include <thread>
26#include <utility>
27
28template<typename T, typename M>
29class cptr_proxy;
30
31//-----------------------------------------------------------------------------------------------------
54template<typename T, typename M = std::recursive_mutex, int N = 100>
55class cptr
56{
57protected:
58 std::shared_ptr<M> llock;
59
60 T* base;
61
62public:
63 explicit cptr(T* p)
64 : llock(new M())
65 , base(p)
66 {
67 }
68
70 {
71 while (!llock->try_lock()) {
72 std::this_thread::sleep_for(std::chrono::nanoseconds(N));
73 }
74
75 return *this;
76 }
77
79 {
80 while (!llock->try_lock()) {
81 std::this_thread::sleep_for(std::chrono::nanoseconds(N));
82 }
83
84 return *this;
85 }
86
87 T* get() { return base; }
88
89 T* get() const { return base; }
90
91 friend class cptr_proxy<T, M>;
92};
93//-----------------------------------------------------------------------------------------------------
94// template<typename T, typename M, int N> M cptr<T,M,N>::llock;
95//-----------------------------------------------------------------------------------------------------
103template<typename T, typename M>
105{
106private:
108
109 std::shared_ptr<M> llock_ptr;
110
111public:
113 : that(cp.base)
114 , llock_ptr(cp.llock)
115 {
116 }
117
118 T* operator->() { return that; }
119
120 ~cptr_proxy() { llock_ptr->unlock(); }
121};
122//-----------------------------------------------------------------------------------------------------
123
124#endif /*LUTILS_CPTR_HPP*/
cptr_proxy(cptr< T, M > const &cp)
Definition cptr.hpp:112
T * that
Definition cptr.hpp:107
std::shared_ptr< M > llock_ptr
Definition cptr.hpp:109
~cptr_proxy()
Definition cptr.hpp:120
T * operator->()
Definition cptr.hpp:118
Definition cptr.hpp:56
cptr_proxy< T, M > operator->() const
Definition cptr.hpp:78
cptr(T *p)
Definition cptr.hpp:63
cptr_proxy< T, M > operator->()
Definition cptr.hpp:69
T * get()
Definition cptr.hpp:87
std::shared_ptr< std::recursive_mutex > llock
Definition cptr.hpp:58
t_internal_changes_stack * base
Definition cptr.hpp:60