Project
Loading...
Searching...
No Matches
ServiceRegistryRef.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef O2_FRAMEWORK_SERVICEREGISTRYREF_H_
12#define O2_FRAMEWORK_SERVICEREGISTRYREF_H_
13
15#include "Framework/Logger.h"
16
17namespace o2::framework
18{
19
21{
22 public:
24 // Cannot be copied / stored
27
28 // Creating a thread safe zone will unlock
29 // the registry, and re-lock it when it goes out of scope.
30 // Use this to mark a section of code where we are
31 // guaranteed to not be accessing the registry the current thread.
37
39 {
40 mRef.lock();
41 }
42
44 };
45 // The streamId is used to identify the stream in case we have multiple
46 // threads. We cannot merely used the thread id because that does not
47 // work in case the thread is created ad-hoc, like it appears to happen
48 // for both libuv and FairMQ. This behaviour, BTW, makes usage
49 // of thread local storage basically impossible (i.e. you lose state).
50 // We use the following convention:
51 // - streamId == 0 means the main thread
52 // - streamId > 0 means one of the libuv worker threads
53 // - streamId == -1 means the region callback thread
54 // - streamId < -1 means some other worker thread of FairMQ which
55 // we do not know about.
56 //
57 // The getter will also make sure that a service of kind Stream
58 // cannot be accessed if the streamId is <= 0 and complain accordingly.
59 // The dataProcessorId will be used to distinguish between different
60 // data processors when
62 : mRegistry(registry),
63 mSalt(salt)
64 {
65 }
66
67 // Wether or not this is the main thread
69 {
70 return mSalt.streamId == 0;
71 }
72
74 template <typename T>
75 requires(std::is_const_v<T> == false)
76 [[nodiscard]] bool active() const
77 {
78 return mRegistry.active<T>(mSalt);
79 }
80
84 template <typename T>
85 T& get() const
86 {
87 return mRegistry.get<T>(mSalt);
88 }
89
90 void registerService(ServiceTypeHash typeHash, void* service, ServiceKind kind, char const* name = nullptr) const
91 {
92 mRegistry.registerService(typeHash, service, kind, mSalt, name);
93 }
94
101 {
102 mRegistry.registerService({handle.hash}, handle.instance, handle.kind, mSalt, handle.name.c_str());
103 }
104
105 void lock()
106 {
107 mRegistry.lock(mSalt);
108 }
109
110 void unlock()
111 {
112 mRegistry.unlock(mSalt);
113 }
114
115 private:
116 ServiceRegistry& mRegistry;
118};
119
120} // namespace o2::framework
121
122#endif // O2_FRAMEWORK_SERVICEREGISTRY_H_
bool active() const
Check if service of type T is currently active.
ServiceRegistryRef(ServiceRegistry &registry, ServiceRegistry::Salt salt=ServiceRegistry::globalDeviceSalt())
void registerService(ServiceHandle handle)
void registerService(ServiceTypeHash typeHash, void *service, ServiceKind kind, char const *name=nullptr) const
GLuint const GLchar * name
Definition glcorearb.h:781
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
ServiceKind
The kind of service we are asking for.
ServiceKind kind
Kind of service.
void * instance
Type erased pointer to a service.
unsigned int hash
Unique hash associated to the type of service.
std::string name
Mnemonic name to use for the service.
thread_safe_zone(thread_safe_zone const &)=delete
thread_safe_zone & operator=(thread_safe_zone const &)=delete
void unlock(Salt salt) const O2_DPL_RELEASE(mMutex)
void registerService(ServiceTypeHash typeHash, void *service, ServiceKind kind, Salt salt, char const *name=nullptr, ServiceRegistry::SpecIndex specIndex=SpecIndex{-1}) const
bool active(Salt salt) const
Check if service of type T is currently active.
void * get(ServiceTypeHash typeHash, Salt salt, ServiceKind kind, char const *name=nullptr) const
void lock(Salt salt) const O2_DPL_ACQUIRE(mMutex)