Arcane  v3.16.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ThreadPrivate.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ThreadPrivate.h (C) 2000-2025 */
9/* */
10/* Classe permettant de conserver une valeur spécifique par thread. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_CONCURRENCY_THREADPRIVATE_H
13#define ARCCORE_CONCURRENCY_THREADPRIVATE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/ArccoreGlobal.h"
18
19#include "arccore/concurrency/GlibAdapter.h"
20
21#include <vector>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
39class ARCCORE_CONCURRENCY_EXPORT ThreadPrivateStorage
40{
41 public:
42
43 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
44 ThreadPrivateStorage();
45 ~ThreadPrivateStorage();
46
47 public:
48
57 void initialize();
58 void* getValue();
59 void setValue(void* v);
60
61 private:
62
63 GlibPrivate* m_storage;
64};
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
73class ARCCORE_CONCURRENCY_EXPORT ThreadPrivateBase
74{
75 public:
76
78 {
79 public:
80
81 virtual ~ICreateFunctor() {}
82 virtual void* createInstance() = 0;
83 };
84
85 public:
86
87 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
88 ThreadPrivateBase(ThreadPrivateStorage* key, ICreateFunctor* create_functor)
89 : m_key(key)
90 , m_create_functor(create_functor)
91 {
92 }
93
95 {
96 }
97
98 public:
99
110 void* item();
111
112 private:
113
114 ThreadPrivateStorage* m_key;
115 GlibMutex m_mutex;
116 ICreateFunctor* m_create_functor;
117};
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
138template <typename T>
139class ThreadPrivate
141{
142 public:
143
144 ARCCORE_DEPRECATED_REASON("Y2022; This class is deprecated. Use 'thread_local' specifier.")
145 ThreadPrivate(ThreadPrivateStorage* key)
146 : m_storage(key, this)
147 {
148 }
149
150 ~ThreadPrivate()
151 {
152 for (T* item : m_allocated_items)
153 delete item;
154 }
155
156 public:
157
159 T* item()
160 {
161 return (T*)(m_storage.item());
162 }
163
164 private:
165
166 void* createInstance() override
167 {
168 T* new_ptr = new T();
169 new_ptr->build();
170 m_allocated_items.push_back(new_ptr);
171 return new_ptr;
172 }
173
174 private:
175
176 std::vector<T*> m_allocated_items;
177 ThreadPrivateBase m_storage;
178};
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183} // namespace Arcane
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188#endif
Encapsule un GPrivate de la glib.
Definition GlibAdapter.h:80
Classe de base permettant de conserveur une instance d'un objet par thread.
Conteneur pour les valeurs privées par thread.
void initialize()
Initialise la clé contenant les valeurs par thread. Cette méthode peut être appelée plusieurs fois et...
T * item()
Instance spécifique au thread courant.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-