DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
SingletonCreator.hpp
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 ers/internal/SingletonCreator.h to include/ers/internal/SingletonCreator.hpp).
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#ifndef ERS_SINGLETON_CREATOR_H
13#define ERS_SINGLETON_CREATOR_H
14
19
20#include <mutex>
21
22/*
23 * SingletonCreator.h
24 * ers
25 *
26 * Created by Serguei Kolos on 25.08.11.
27 * Copyright 2011 CERN. All rights reserved.
28 * This class creates singleton instance at the first use attempt
29 * in a threading-safe way.
30 */
31namespace ers {
32template<class T>
34{
35 static std::mutex s_mutex;
36 static T* s_instance;
37
38public:
39 static T* create()
40 {
41// on MacOS with gcc 4.2 the s_mutex is not yet created
42// at this moment which causes crash. This might be fixed
43// when movining to newer gcc version
44#if !defined(__APPLE__) && !defined(__rtems__)
45 std::scoped_lock lock(s_mutex);
46#endif
47 if (!s_instance) {
48 s_instance = new T();
49 }
50 return s_instance;
51 }
52};
53
54template<class T>
56
57template<class T>
59}
60
61#endif
static std::mutex s_mutex