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
|
/* GSequencer - Advanced GTK Sequencer
* Copyright (C) 2005-2022 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_worker_thread.h>
#include <stdlib.h>
void ags_worker_thread_class_init(AgsWorkerThreadClass *worker_thread);
void ags_worker_thread_init(AgsWorkerThread *worker_thread);
void ags_worker_thread_finalize(GObject *gobject);
void ags_worker_thread_start(AgsThread *thread);
void ags_worker_thread_run(AgsThread *thread);
void ags_worker_thread_stop(AgsThread *thread);
void* ags_woker_thread_do_poll_loop(void *ptr);
/**
* SECTION:ags_worker_thread
* @short_description: worker thread class discarding base frequency
* @title: AgsWorkerThread
* @section_id:
* @include: ags/thread/ags_worker_thread.h
*
* The #AgsWorkerThread does non-realtime work. You might want
* to synchronize to the run signal within your ::do_poll() method.
*
* You usually connect to AgsThread::do_poll() event. The poll event
* is invoked as long as %AGS_WORKER_THREAD_STATUS_RUNNING status flag is set.
*
* You might want to inject to a thread tree using a #AgsTask implementation.
*/
enum{
DO_POLL,
LAST_SIGNAL,
};
static gpointer ags_worker_thread_parent_class = NULL;
static guint worker_thread_signals[LAST_SIGNAL];
GType
ags_worker_thread_get_type()
{
static gsize g_define_type_id__static = 0;
if(g_once_init_enter(&g_define_type_id__static)){
GType ags_type_worker_thread = 0;
static const GTypeInfo ags_worker_thread_info = {
sizeof (AgsWorkerThreadClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) ags_worker_thread_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (AgsWorkerThread),
0, /* n_preallocs */
(GInstanceInitFunc) ags_worker_thread_init,
};
ags_type_worker_thread = g_type_register_static(AGS_TYPE_THREAD,
"AgsWorkerThread",
&ags_worker_thread_info,
0);
g_once_init_leave(&g_define_type_id__static, ags_type_worker_thread);
}
return(g_define_type_id__static);
}
GType
ags_worker_thread_status_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_WORKER_THREAD_STATUS_RUNNING, "AGS_WORKER_THREAD_STATUS_RUNNING", "worker-thread-status-running" },
{ AGS_WORKER_THREAD_STATUS_RUN_WAIT, "AGS_WORKER_THREAD_STATUS_RUN_WAIT", "worker-thread-status-run-wait" },
{ AGS_WORKER_THREAD_STATUS_RUN_DONE, "AGS_WORKER_THREAD_STATUS_RUN_DONE", "worker-thread-status-run-done" },
{ AGS_WORKER_THREAD_STATUS_RUN_SYNC, "AGS_WORKER_THREAD_STATUS_RUN_SYNC", "worker-thread-status-run-sync" },
{ 0, NULL, NULL }
};
GType g_flags_type_id = g_flags_register_static(g_intern_static_string("AgsWorkerThreadStatusFlags"), values);
g_once_init_leave(&g_flags_type_id__static, g_flags_type_id);
}
return(g_flags_type_id__static);
}
void
ags_worker_thread_class_init(AgsWorkerThreadClass *worker_thread)
{
GObjectClass *gobject;
AgsThreadClass *thread;
ags_worker_thread_parent_class = g_type_class_peek_parent(worker_thread);
/* GObject */
gobject = (GObjectClass *) worker_thread;
gobject->finalize = ags_worker_thread_finalize;
/* AgsThread */
thread = (AgsThreadClass *) worker_thread;
thread->start = ags_worker_thread_start;
thread->run = ags_worker_thread_run;
thread->stop = ags_worker_thread_stop;
/* AgsWorkerThread */
worker_thread->do_poll = NULL;
/* signals */
/**
* AgsWorkerThread::do-poll:
* @thread: the #AgsWorkerThread
*
* The ::do_poll() signal runs independently of ::run() but
* might be synchronized using a conditional lock.
*
* Since: 3.0.0
*/
worker_thread_signals[DO_POLL] =
g_signal_new("do-poll",
G_TYPE_FROM_CLASS (thread),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (AgsWorkerThreadClass, do_poll),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
void
ags_worker_thread_init(AgsWorkerThread *worker_thread)
{
AgsThread *thread;
thread = (AgsThread *) worker_thread;
g_object_set(thread,
"frequency", AGS_WORKER_THREAD_DEFAULT_JIFFIE,
NULL);
ags_atomic_int_set(&(worker_thread->status_flags),
0);
/* synchronization */
g_mutex_init(&(worker_thread->run_mutex));
g_cond_init(&(worker_thread->run_cond));
/* worker thread */
worker_thread->worker_thread = NULL;
}
void
ags_worker_thread_finalize(GObject *gobject)
{
AgsWorkerThread *worker_thread;
gboolean running;
gboolean do_exit;
worker_thread = AGS_WORKER_THREAD(gobject);
if(worker_thread == ags_thread_self()){
do_exit = TRUE;
}else{
do_exit = FALSE;
}
running = ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING);
/* call parent */
G_OBJECT_CLASS(ags_worker_thread_parent_class)->finalize(gobject);
if(do_exit){
g_thread_exit(NULL);
}
}
void
ags_worker_thread_start(AgsThread *thread)
{
AgsWorkerThread *worker_thread;
worker_thread = AGS_WORKER_THREAD(thread);
/* */
AGS_THREAD_CLASS(ags_worker_thread_parent_class)->start(thread);
ags_worker_thread_set_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING);
worker_thread->worker_thread = g_thread_new("Advanced Gtk+ Sequencer - worker",
ags_woker_thread_do_poll_loop,
worker_thread);
}
void
ags_worker_thread_run(AgsThread *thread)
{
AgsWorkerThread *worker_thread;
worker_thread = AGS_WORKER_THREAD(thread);
/* synchronization point */
if(ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUN_WAIT) &&
!ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUN_DONE)){
g_mutex_lock(&(worker_thread->run_mutex));
ags_worker_thread_set_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUN_SYNC);
while(ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUN_WAIT) &&
!ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUN_DONE)){
g_cond_wait(&(worker_thread->run_cond),
&(worker_thread->run_mutex));
}
ags_worker_thread_set_status_flags(worker_thread, (AGS_WORKER_THREAD_STATUS_RUN_WAIT |
AGS_WORKER_THREAD_STATUS_RUN_DONE));
g_mutex_unlock(&(worker_thread->run_mutex));
}
}
void
ags_worker_thread_stop(AgsThread *thread)
{
AgsWorkerThread *worker_thread;
worker_thread = AGS_WORKER_THREAD(thread);
ags_worker_thread_unset_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING);
/* call parent */
AGS_THREAD_CLASS(ags_worker_thread_parent_class)->stop(thread);
}
/**
* ags_worker_thread_test_status_flags:
* @worker_thread: the #AgsWorkerThread
* @status_flags: status flags
*
* Test @status_flags of @worker_thread.
*
* Returns: %TRUE if status flags set, otherwise %FALSE
*
* Since: 3.0.0
*/
gboolean
ags_worker_thread_test_status_flags(AgsWorkerThread *worker_thread, guint status_flags)
{
gboolean retval;
if(!AGS_IS_WORKER_THREAD(worker_thread)){
return(FALSE);
}
retval = ((status_flags & (ags_atomic_int_get(&(worker_thread->status_flags)))) != 0) ? TRUE: FALSE;
return(retval);
}
/**
* ags_worker_thread_set_status_flags:
* @worker_thread: the #AgsWorkerThread
* @status_flags: status flags
*
* Set status flags.
*
* Since: 3.0.0
*/
void
ags_worker_thread_set_status_flags(AgsWorkerThread *worker_thread, guint status_flags)
{
if(!AGS_IS_WORKER_THREAD(worker_thread)){
return;
}
ags_atomic_int_or(&(worker_thread->status_flags),
status_flags);
}
/**
* ags_worker_thread_unset_status_flags:
* @worker_thread: the #AgsWorkerThread
* @status_flags: status flags
*
* Unset status flags.
*
* Since: 3.0.0
*/
void
ags_worker_thread_unset_status_flags(AgsWorkerThread *worker_thread, guint status_flags)
{
if(!AGS_IS_WORKER_THREAD(worker_thread)){
return;
}
ags_atomic_int_and(&(worker_thread->status_flags),
(~status_flags));
}
/**
* ags_woker_thread_do_poll_loop:
* @ptr: the #AgsWorkerThread
*
* Do loop and invoke ags_worker_thread_do_poll() unless flag
* AGS_WORKER_THREAD_RUNNING was unset.
*
* Since: 3.0.0
*/
void*
ags_woker_thread_do_poll_loop(void *ptr)
{
AgsWorkerThread *worker_thread;
worker_thread = (AgsWorkerThread *) ptr;
while(ags_worker_thread_test_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING)){
ags_worker_thread_do_poll(worker_thread);
}
g_thread_exit(NULL);
return(NULL);
}
/**
* ags_worker_thread_do_poll:
* @worker_thread: the #AgsWorkerThread
*
* Do poll your work. It is called of the worker thread.
*
* Since: 3.0.0
*/
void
ags_worker_thread_do_poll(AgsWorkerThread *worker_thread)
{
g_return_if_fail(AGS_IS_WORKER_THREAD(worker_thread));
g_object_ref(worker_thread);
g_signal_emit(worker_thread,
worker_thread_signals[DO_POLL], 0);
g_object_unref(worker_thread);
}
/**
* ags_worker_thread_new:
*
* Create a new instance of #AgsWorkerThread.
*
* Returns: the new #AgsWorkerThread
*
* Since: 3.0.0
*/
AgsWorkerThread*
ags_worker_thread_new()
{
AgsWorkerThread *worker_thread;
worker_thread = (AgsWorkerThread *) g_object_new(AGS_TYPE_WORKER_THREAD,
NULL);
return(worker_thread);
}
|