Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
GlibAdapter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* GlibAdapter.cc (C) 2000-2021 */
9/* */
10/* Classes utilitaires pour s'adapter aux différentes versions de la 'glib'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/concurrency/GlibAdapter.h"
15
16#include <glib.h>
17
18// A partir de 2.32, 'glib' utilise un nouveau mécanisme pour gérer
19// tout ce qui concerne le multi-threading. En particulier, toutes les
20// fonctions de création/destruction changent.
21
22// A terme, ces fonctionnalités seront disponibles dans la norme C++
23// et lorsqu'on pourra utiliser des compilateurs récents elles ne devraient
24// plus être utilisées.
25
26#define ARCCORE_GLIB_HAS_NEW_THREAD
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arccore
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
41{
42 public:
43 Impl() ARCCORE_NOEXCEPT
44 : m_mutex(nullptr)
45 {
46 m_mutex = &m_mutex_instance;
47 g_mutex_init(m_mutex);
48 }
49 ~Impl()
50 {
51 g_mutex_clear(m_mutex);
52 }
53 public:
54 GMutex* value() const { return m_mutex; }
55 void lock() { g_mutex_lock(m_mutex); }
56 void unlock() { g_mutex_unlock(m_mutex); }
57 private:
58 GMutex m_mutex_instance;
59 GMutex* m_mutex;
60};
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
65GlibMutex::
66GlibMutex() ARCCORE_NOEXCEPT
67: m_p(new Impl())
68{
69}
70
71GlibMutex::
72~GlibMutex()
73{
74 delete m_p;
75}
76
77void GlibMutex::lock() { m_p->lock(); }
78void GlibMutex::unlock() { m_p->unlock(); }
79
80GlibMutex::Lock::Lock(GlibMutex& x) : m_mutex(x.m_p){ m_mutex->lock(); }
81GlibMutex::Lock::~Lock() { m_mutex->unlock(); }
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85namespace
86{
87GPrivate null_gprivate = G_PRIVATE_INIT(nullptr);
88}
89
95{
96 public:
97 Impl() ARCCORE_NOEXCEPT
98 : m_private(nullptr)
99 {
100 m_private_instance = null_gprivate;
101 m_private = &m_private_instance;
102 }
103 ~Impl()
104 {
105 }
106 void create()
107 {
108 }
109 void setValue(void* value)
110 {
111 g_private_set(m_private,value);
112 }
113 void* getValue()
114 {
115 return g_private_get(m_private);
116 }
117 private:
118 GPrivate m_private_instance;
119 GPrivate* m_private;
120};
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
125GlibPrivate::
126GlibPrivate()
127: m_p(new Impl())
128{
129}
130
131GlibPrivate::
132~GlibPrivate()
133{
134 delete m_p;
135}
136
137void GlibPrivate::create() { m_p->create(); }
138void GlibPrivate::setValue(void* value) { m_p->setValue(value); }
139void* GlibPrivate::getValue() { return m_p->getValue(); }
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
145{
146 public:
147 Impl() : m_cond(nullptr)
148 {
149 m_cond = &m_cond_instance;
150 g_cond_init(m_cond);
151 }
152 ~Impl()
153 {
154 g_cond_clear(m_cond);
155 }
156 public:
157 void broadcast() { g_cond_broadcast(m_cond); }
158 void wait(GlibMutex::Impl* mutex) { g_cond_wait(m_cond,mutex->value()); }
159 private:
160 GCond m_cond_instance;
161 GCond* m_cond;
162};
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167GlibCond::GlibCond() : m_p(new GlibCond::Impl()){}
168GlibCond::~GlibCond() { delete m_p; }
169void GlibCond::broadcast() { m_p->broadcast(); }
170void GlibCond::wait(GlibMutex* mutex) { m_p->wait(mutex->m_p); }
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175} // End namespace Arccore
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
Encapsule un GCond de la glib.
Definition GlibAdapter.h:87
GMutex de la glib.
GPrivate de la glib.
Espace de nom de Arccore.
Definition ArcaneTypes.h:24