Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
GlibAdapter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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-2025 */
9/* */
10/* Utility classes to adapt to different versions of 'glib'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/concurrency/GlibAdapter.h"
15
16#include <glib.h>
17
18// Starting from 2.32, 'glib' uses a new mechanism to manage
19// everything related to multi-threading. In particular, all
20// creation/destruction functions change.
21
22// Eventually, these features will be available in the C++ standard
23// and when recent compilers can be used, they should
24// no longer be used.
25
26#define ARCCORE_GLIB_HAS_NEW_THREAD
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37/*!
38 * \internal
39 * \brief Glib mutex.
40 */
42{
43 public:
44
45 Impl() ARCCORE_NOEXCEPT
46 : m_mutex(nullptr)
47 {
48 m_mutex = &m_mutex_instance;
49 g_mutex_init(m_mutex);
50 }
51 ~Impl()
52 {
53 g_mutex_clear(m_mutex);
54 }
55
56 public:
57
58 GMutex* value() const { return m_mutex; }
59 void lock() { g_mutex_lock(m_mutex); }
60 void unlock() { g_mutex_unlock(m_mutex); }
61
62 private:
63
64 GMutex m_mutex_instance;
65 GMutex* m_mutex;
66};
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71GlibMutex::
72GlibMutex() ARCCORE_NOEXCEPT
73: m_p(new Impl())
74{
75}
76
77GlibMutex::
78~GlibMutex()
79{
80 delete m_p;
81}
82
83void GlibMutex::lock()
84{
85 m_p->lock();
86}
87void GlibMutex::unlock()
88{
89 m_p->unlock();
90}
91
92GlibMutex::Lock::Lock(GlibMutex& x)
93: m_mutex(x.m_p)
94{
95 m_mutex->lock();
96}
97GlibMutex::Lock::~Lock()
98{
99 m_mutex->unlock();
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104namespace
105{
106 GPrivate null_gprivate = G_PRIVATE_INIT(nullptr);
107}
108
109/*!
110 * \internal
111 * \brief Glib private.
112 */
114{
115 public:
116
117 Impl() ARCCORE_NOEXCEPT
118 : m_private(nullptr)
119 {
120 m_private_instance = null_gprivate;
121 m_private = &m_private_instance;
122 }
123 ~Impl()
124 {
125 }
126 void create()
127 {
128 }
129 void setValue(void* value)
130 {
131 g_private_set(m_private, value);
132 }
133 void* getValue()
134 {
135 return g_private_get(m_private);
136 }
137
138 private:
139
140 GPrivate m_private_instance;
141 GPrivate* m_private;
142};
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147GlibPrivate::
148GlibPrivate()
149: m_p(new Impl())
150{
151}
152
153GlibPrivate::
154~GlibPrivate()
155{
156 delete m_p;
157}
158
159void GlibPrivate::create()
160{
161 m_p->create();
162}
163void GlibPrivate::setValue(void* value)
164{
165 m_p->setValue(value);
166}
167void* GlibPrivate::getValue()
168{
169 return m_p->getValue();
170}
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
176{
177 public:
178
179 Impl()
180 : m_cond(nullptr)
181 {
182 m_cond = &m_cond_instance;
183 g_cond_init(m_cond);
184 }
185 ~Impl()
186 {
187 g_cond_clear(m_cond);
188 }
189
190 public:
191
192 void broadcast() { g_cond_broadcast(m_cond); }
193 void wait(GlibMutex::Impl* mutex) { g_cond_wait(m_cond, mutex->value()); }
194
195 private:
196
197 GCond m_cond_instance;
198 GCond* m_cond;
199};
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204GlibCond::GlibCond()
205: m_p(new GlibCond::Impl())
206{}
207GlibCond::~GlibCond()
208{
209 delete m_p;
210}
211void GlibCond::broadcast()
212{
213 m_p->broadcast();
214}
215void GlibCond::wait(GlibMutex* mutex)
216{
217 m_p->wait(mutex->m_p);
218}
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
222
223} // namespace Arcane
224
225/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --