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
32{
33 template <class T>
35 {
36 static std::mutex s_mutex;
37 static T * s_instance;
38
39 public:
40 static T * create( )
41 {
42 // on MacOS with gcc 4.2 the s_mutex is not yet created
43 // at this moment which causes crash. This might be fixed
44 // when movining to newer gcc version
45 #if !defined( __APPLE__) && !defined(__rtems__)
46 std::scoped_lock lock(s_mutex);
47 #endif
48 if ( !s_instance )
49 {
50 s_instance = new T();
51 }
52 return s_instance;
53 }
54 };
55
56 template <class T>
58
59 template <class T>
61}
62
63#endif
static std::mutex s_mutex