1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/* GSequencer - Advanced GTK Sequencer
* Copyright (C) 2005-2023 Joël Krähemann
*
* This file is part of GSequencer.
*
* GSequencer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GSequencer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GSequencer. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ags/thread/ags_returnable_thread.h>
#include <ags/object/ags_connectable.h>
#include <ags/thread/ags_thread_pool.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>
#include <ags/i18n.h>
void ags_returnable_thread_class_init(AgsReturnableThreadClass *returnable_thread);
void ags_returnable_thread_init(AgsReturnableThread *returnable_thread);
void ags_returnable_thread_set_property(GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *param_spec);
void ags_returnable_thread_get_property(GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *param_spec);
void ags_returnable_thread_dispose(GObject *gobject);
void ags_returnable_thread_finalize(GObject *gobject);
void ags_returnable_thread_start(AgsThread *thread);
void ags_returnable_thread_run(AgsThread *thread);
void ags_returnable_thread_stop(AgsThread *thread);
void ags_returnable_thread_resume(AgsThread *thread);
/**
* SECTION:ags_returnable_thread
* @short_description: returnable thread
* @title: AgsReturnableThread
* @section_id:
* @include: ags/thread/ags_returnable_thread.h
*
* The #AgsReturnableThread acts as thread. It should return after a short
* while because of limited thread pool.
*/
enum{
PROP_0,
PROP_THREAD_POOL,
PROP_SAFE_DATA,
};
enum{
SAFE_RUN,
LAST_SIGNAL,
};
static gpointer ags_returnable_thread_parent_class = NULL;
static guint returnable_thread_signals[LAST_SIGNAL];
GType
ags_returnable_thread_get_type()
{
static gsize g_define_type_id__static = 0;
if(g_once_init_enter(&g_define_type_id__static)){
GType ags_type_returnable_thread = 0;
static const GTypeInfo ags_returnable_thread_info = {
sizeof (AgsReturnableThreadClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) ags_returnable_thread_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (AgsReturnableThread),
0, /* n_preallocs */
(GInstanceInitFunc) ags_returnable_thread_init,
};
ags_type_returnable_thread = g_type_register_static(AGS_TYPE_THREAD,
"AgsReturnableThread",
&ags_returnable_thread_info,
0);
g_once_init_leave(&g_define_type_id__static, ags_type_returnable_thread);
}
return(g_define_type_id__static);
}
GType
ags_returnable_thread_flags_get_type()
{
static gsize g_flags_type_id__static;
if(g_once_init_enter(&g_flags_type_id__static)){
static const GFlagsValue values[] = {
{ AGS_RETURNABLE_THREAD_IN_USE, "AGS_RETURNABLE_THREAD_IN_USE", "returnable-thread-in-use" },
{ AGS_RETURNABLE_THREAD_RESET, "AGS_RETURNABLE_THREAD_RESET", "returnable-thread-reset" },
{ AGS_RETURNABLE_THREAD_RUN_ONCE, "AGS_RETURNABLE_THREAD_RUN_ONCE", "returnable-thread-run-once" },
{ 0, NULL, NULL }
};
GType g_flags_type_id = g_flags_register_static(g_intern_static_string("AgsReturnableThreadFlags"), values);
g_once_init_leave(&g_flags_type_id__static, g_flags_type_id);
}
return(g_flags_type_id__static);
}
void
ags_returnable_thread_class_init(AgsReturnableThreadClass *returnable_thread)
{
AgsThreadClass *thread;
GObjectClass *gobject;
GParamSpec *param_spec;
ags_returnable_thread_parent_class = g_type_class_peek_parent(returnable_thread);
/* GObjectClass */
gobject = (GObjectClass *) returnable_thread;
gobject->set_property = ags_returnable_thread_set_property;
gobject->get_property = ags_returnable_thread_get_property;
gobject->dispose = ags_returnable_thread_dispose;
gobject->finalize = ags_returnable_thread_finalize;
/* properties */
/**
* AgsReturnableThread:thread-pool:
*
* The assigned #AgsThreadPool providing default settings.
*
* Since: 3.0.0
*/
param_spec = g_param_spec_object("thread-pool",
i18n_pspec("assigned thread pool"),
i18n_pspec("The thread pool it is assigned with"),
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_WRITABLE);
g_object_class_install_property(gobject,
PROP_THREAD_POOL,
param_spec);
/* AgsThreadClass */
thread = (AgsThreadClass *) returnable_thread;
thread->start = ags_returnable_thread_start;
thread->run = ags_returnable_thread_run;
thread->stop = ags_returnable_thread_stop;
/* AgsReturnableThreadClass */
returnable_thread->safe_run = NULL;
/* signals */
/**
* AgsReturnableThread::safe-run:
* @returnable_thread: the #AgsReturnableThread
*
* The ::safe-run is invoked durin AgsThread::run as
* a context safe wrapper.
*
* Since: 3.0.0
*/
returnable_thread_signals[SAFE_RUN] =
g_signal_new("safe-run",
G_TYPE_FROM_CLASS (returnable_thread),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (AgsReturnableThreadClass, safe_run),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
void
ags_returnable_thread_init(AgsReturnableThread *returnable_thread)
{
AgsThread *thread;
thread = AGS_THREAD(returnable_thread);
ags_thread_set_flags(thread, AGS_THREAD_UNREF_ON_EXIT);
g_object_set(thread,
"frequency", AGS_RETURNABLE_THREAD_DEFAULT_JIFFIE,
NULL);
ags_atomic_int_set(&(returnable_thread->flags),
AGS_RETURNABLE_THREAD_RUN_ONCE);
returnable_thread->thread_pool = NULL;
g_rec_mutex_init(&(returnable_thread->reset_mutex));
ags_atomic_pointer_set(&(returnable_thread->safe_data),
NULL);
returnable_thread->handler = 0;
}
void
ags_returnable_thread_set_property(GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *param_spec)
{
AgsReturnableThread *returnable_thread;
returnable_thread = AGS_RETURNABLE_THREAD(gobject);
switch(prop_id){
case PROP_THREAD_POOL:
{
GObject *thread_pool;
thread_pool = g_value_get_object(value);
if(returnable_thread->thread_pool == thread_pool)
return;
if(returnable_thread->thread_pool != NULL){
g_object_unref(returnable_thread->thread_pool);
}
if(thread_pool != NULL){
g_object_ref(thread_pool);
}
returnable_thread->thread_pool = thread_pool;
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
break;
}
}
void
ags_returnable_thread_get_property(GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *param_spec)
{
AgsReturnableThread *returnable_thread;
returnable_thread = AGS_RETURNABLE_THREAD(gobject);
switch(prop_id){
case PROP_THREAD_POOL:
{
g_value_set_object(value, returnable_thread->thread_pool);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
break;
}
}
void
ags_returnable_thread_dispose(GObject *gobject)
{
AgsReturnableThread *returnable_thread;
returnable_thread = (AgsReturnableThread *) gobject;
/* thread pool */
if(returnable_thread->thread_pool != NULL){
g_object_unref(returnable_thread->thread_pool);
returnable_thread->thread_pool = NULL;
}
/* call parent */
G_OBJECT_CLASS(ags_returnable_thread_parent_class)->dispose(gobject);
}
void
ags_returnable_thread_finalize(GObject *gobject)
{
AgsReturnableThread *returnable_thread;
returnable_thread = (AgsReturnableThread *) gobject;
/* thread pool */
if(returnable_thread->thread_pool != NULL){
g_object_unref(returnable_thread->thread_pool);
}
/* call parent */
G_OBJECT_CLASS(ags_returnable_thread_parent_class)->finalize(gobject);
}
void
ags_returnable_thread_start(AgsThread *thread)
{
AGS_THREAD_CLASS(ags_returnable_thread_parent_class)->start(thread);
}
void
ags_returnable_thread_run(AgsThread *thread)
{
AgsThreadPool *thread_pool;
AgsReturnableThread *returnable_thread;
GRecMutex *thread_mutex;
// g_message("reset:0");
/* retrieve some variables */
returnable_thread = AGS_RETURNABLE_THREAD(thread);
/* safe run */
if((AGS_RETURNABLE_THREAD_IN_USE & (ags_atomic_int_get(&(returnable_thread->flags)))) != 0){
ags_returnable_thread_safe_run(returnable_thread);
if((AGS_RETURNABLE_THREAD_RUN_ONCE & (ags_atomic_int_get(&(returnable_thread->flags)))) != 0){
ags_atomic_int_and(&(returnable_thread->flags),
(~AGS_RETURNABLE_THREAD_IN_USE));
/* return to thread pool */
thread_mutex = AGS_THREAD_GET_OBJ_MUTEX(thread);
g_rec_mutex_lock(thread_mutex);
thread_pool = (AgsThreadPool *) returnable_thread->thread_pool;
g_rec_mutex_unlock(thread_mutex);
/* give returnable thread back to thread pool */
ags_atomic_pointer_set(&(returnable_thread->safe_data),
NULL);
ags_returnable_thread_disconnect_safe_run(returnable_thread);
ags_atomic_pointer_set(&(thread_pool->returnable_thread),
g_list_prepend(ags_atomic_pointer_get(&(thread_pool->returnable_thread)),
returnable_thread));
}
}
}
void
ags_returnable_thread_safe_run(AgsReturnableThread *returnable_thread)
{
guint returnable_thread_signal;
returnable_thread_signal = returnable_thread_signals[SAFE_RUN];
g_return_if_fail(AGS_IS_RETURNABLE_THREAD(returnable_thread));
g_object_ref(returnable_thread);
g_signal_emit(returnable_thread,
returnable_thread_signal, 0);
g_object_unref(returnable_thread);
}
void
ags_returnable_thread_stop(AgsThread *thread)
{
AGS_THREAD_CLASS(ags_returnable_thread_parent_class)->stop(thread);
}
void
ags_returnable_thread_resume(AgsThread *thread)
{
/* empty */
}
/**
* ags_returnable_thread_test_flags:
* @returnable_thread: the #AgsReturnableThread
* @flags: the flags
*
* Test @flags to be set on @returnable_thread.
*
* Returns: %TRUE if flags are set, else %FALSE
*
* Since: 3.0.0
*/
gboolean
ags_returnable_thread_test_flags(AgsReturnableThread *returnable_thread,
AgsReturnableThreadFlags flags)
{
gboolean retval;
if(!AGS_IS_RETURNABLE_THREAD(returnable_thread)){
return(FALSE);
}
retval = ((flags & (ags_atomic_int_get(&(returnable_thread->flags)))) != 0) ? TRUE: FALSE;
return(retval);
}
/**
* ags_returnable_thread_set_flags:
* @returnable_thread: the #AgsReturnableThread
* @flags: the flags
*
* Set flags.
*
* Since: 3.0.0
*/
void
ags_returnable_thread_set_flags(AgsReturnableThread *returnable_thread,
AgsReturnableThreadFlags flags)
{
if(!AGS_IS_RETURNABLE_THREAD(returnable_thread)){
return;
}
ags_atomic_int_or(&(returnable_thread->flags), flags);
}
/**
* ags_returnable_thread_unset_flags:
* @returnable_thread: the #AgsReturnableThread
* @flags: the flags
*
* Unset flags.
*
* Since: 3.0.0
*/
void
ags_returnable_thread_unset_flags(AgsReturnableThread *returnable_thread,
AgsReturnableThreadFlags flags)
{
if(!AGS_IS_RETURNABLE_THREAD(returnable_thread)){
return;
}
ags_atomic_int_and(&(returnable_thread->flags), (~flags));
}
/**
* ags_returnable_thread_connect_safe_run:
* @returnable_thread: the thread to connect
* @callback: (scope call): the callback
*
* Connects @callback to @thread.
*
* Since: 3.0.0
*/
void
ags_returnable_thread_connect_safe_run(AgsReturnableThread *returnable_thread, AgsReturnableThreadCallback callback)
{
if(returnable_thread->handler > 0){
return;
}
returnable_thread->handler = g_signal_connect(G_OBJECT(returnable_thread), "safe-run",
G_CALLBACK(callback), returnable_thread);
}
/**
* ags_returnable_thread_disconnect_safe_run:
* @returnable_thread: the thread to disconnect
*
* Disconnects callback of @thread.
*
* Since: 3.0.0
*/
void
ags_returnable_thread_disconnect_safe_run(AgsReturnableThread *returnable_thread)
{
if(returnable_thread->handler == 0){
return;
}
g_signal_handler_disconnect(G_OBJECT(returnable_thread),
returnable_thread->handler);
returnable_thread->handler = 0;
}
/**
* ags_returnable_thread_new:
* @thread_pool: the #AgsThreadPool
*
* Create a new instance of #AgsReturnableThread.
*
* Returns: the new #AgsReturnableThread
*
* Since: 3.0.0
*/
AgsReturnableThread*
ags_returnable_thread_new(GObject *thread_pool)
{
AgsReturnableThread *returnable_thread;
returnable_thread = (AgsReturnableThread *) g_object_new(AGS_TYPE_RETURNABLE_THREAD,
"thread-pool", thread_pool,
NULL);
return(returnable_thread);
}
|