DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
LocalStream.cpp
Go to the documentation of this file.
1/*
2 * DUNE DAQ modification notice:
3 * This file has been modified from the original ATLAS ers source for the DUNE DAQ project.
4 * Fork baseline commit: 8267df82a4f6fe6bf02c4014923eba19eddc4614 (2020-04-14).
5 * Renamed since fork: yes (from src/LocalStream.cxx to src/LocalStream.cpp).
6 *
7 * Original copyright:
8 * Copyright (C) 2001-2020 CERN for the benefit of the ATLAS collaboration.
9 * Licensed under the Apache License, Version 2.0.
10 */
11
12/*
13 * LocalStream.cxx
14 * ERS
15 *
16 * Created by Serguei Kolos on 21.01.05.
17 * Copyright 2005 CERN. All rights reserved.
18 *
19 */
20#include <ers/LocalStream.hpp>
21#include <ers/StreamManager.hpp>
23
28ers::LocalStream &
29ers::LocalStream::instance()
30{
33 static ers::LocalStream * instance = ers::SingletonCreator<ers::LocalStream>::create();
34
35 return *instance;
36}
37
41ers::LocalStream::LocalStream( )
42 : m_terminated( false )
43{ }
44
45ers::LocalStream::~LocalStream( )
46{
47 remove_issue_catcher();
48}
49
50void
51ers::LocalStream::remove_issue_catcher( )
52{
53 std::unique_ptr<std::thread> catcher;
54 {
55 std::unique_lock lock( m_mutex );
56 if ( !m_issue_catcher_thread.get() )
57 {
58 return ;
59 }
60 m_terminated = true;
61 m_condition.notify_one();
62 catcher.swap(m_issue_catcher_thread);
63 }
64
65 catcher -> join();
66}
67
68void
69ers::LocalStream::thread_wrapper()
70{
71 std::unique_lock lock( m_mutex );
72 m_catcher_thread_id = std::this_thread::get_id();
73 while( !m_terminated )
74 {
75 m_condition.wait( lock, [this](){return !m_issues.empty() || m_terminated;} );
76
77 while( !m_terminated && !m_issues.empty() )
78 {
79 ers::Issue * issue = m_issues.front();
80 m_issues.pop();
81
82 lock.unlock();
83 m_issue_catcher( *issue );
84 delete issue;
85 lock.lock();
86 }
87 }
88 m_catcher_thread_id = {};
89 m_terminated = false;
90}
91
93ers::LocalStream::set_issue_catcher( const std::function<void ( const ers::Issue & )> & catcher )
94{
95 std::unique_lock lock( m_mutex );
96 if ( m_issue_catcher_thread.get() )
97 {
98 throw ers::IssueCatcherAlreadySet( ERS_HERE );
99 }
100 m_issue_catcher = catcher;
101 m_issue_catcher_thread.reset( new std::thread( std::bind( &ers::LocalStream::thread_wrapper, this ) ) );
102
103 return new ers::IssueCatcherHandler;
104}
105
106void
107ers::LocalStream::report_issue( ers::severity type, const ers::Issue & issue )
108{
109 if ( m_issue_catcher_thread.get() && m_catcher_thread_id != std::this_thread::get_id() )
110 {
111 ers::Issue * clone = issue.clone();
112 clone->set_severity( type );
113 std::unique_lock lock( m_mutex );
114 m_issues.push( clone );
115 m_condition.notify_one();
116 }
117 else
118 {
119 StreamManager::instance().report_issue( type, issue );
120 }
121}
122
123void
124ers::LocalStream::error( const ers::Issue & issue )
125{
126 report_issue( ers::Error, issue );
127}
128
129void
130ers::LocalStream::fatal( const ers::Issue & issue )
131{
132 report_issue( ers::Fatal, issue );
133}
134
135void
136ers::LocalStream::warning( const ers::Issue & issue )
137{
138 report_issue( ers::Warning, issue );
139}
#define ERS_HERE
Implements issue catcher lifetime management.
Base class for any user define issue.
Definition Issue.hpp:80
virtual Issue * clone() const =0
ers::Severity set_severity(ers::Severity severity) const
Definition Issue.cpp:191
std::string join(const C &strings, const std::string &delimiter)
Converts a vector of strings in a delimiter-separated string.
Definition toolbox.hxx:95
severity
Definition Severity.hpp:37
@ Error
Definition Severity.hpp:37
@ Fatal
Definition Severity.hpp:37
@ Warning
Definition Severity.hpp:37