File: ags_thread.h

package info (click to toggle)
gsequencer 8.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 74,272 kB
  • sloc: ansic: 1,195,333; xml: 31,048; cpp: 9,749; sh: 5,798; makefile: 4,024; perl: 536; sed: 16; python: 11
file content (321 lines) | stat: -rw-r--r-- 11,904 bytes parent folder | download | duplicates (3)
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
/* 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/>.
 */

#ifndef __AGS_THREAD_H__
#define __AGS_THREAD_H__

#include <glib.h>
#include <glib-object.h>

#include <ags/lib/ags_uuid.h>
#include <ags/lib/ags_time.h>

#include <ags/thread/ags_atomic.h>

#include <time.h>

G_BEGIN_DECLS

#define AGS_TYPE_THREAD                (ags_thread_get_type())
#define AGS_TYPE_THREAD_FLAGS          (ags_thread_flags_get_type())
#define AGS_TYPE_THREAD_STATUS_FLAGS   (ags_thread_status_flags_get_type())
#define AGS_TYPE_THREAD_SYNC_TIC_FLAGS (ags_thread_sync_tic_flags_get_type())
#define AGS_THREAD(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), AGS_TYPE_THREAD, AgsThread))
#define AGS_THREAD_CLASS(class)        (G_TYPE_CHECK_CLASS_CAST(class, AGS_TYPE_THREAD, AgsThreadClass))
#define AGS_IS_THREAD(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AGS_TYPE_THREAD))
#define AGS_IS_THREAD_CLASS(class)     (G_TYPE_CHECK_CLASS_TYPE ((class), AGS_TYPE_THREAD))
#define AGS_THREAD_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS(obj, AGS_TYPE_THREAD, AgsThreadClass))

#define AGS_THREAD_GET_OBJ_MUTEX(obj) (&(((AgsThread *) obj)->obj_mutex))

#define AGS_THREAD_GET_WAIT_MUTEX(obj) (&(((AgsThread *) obj)->wait_mutex))
#define AGS_THREAD_GET_WAIT_COND(obj) (&(((AgsThread *) obj)->wait_cond))
#define AGS_THREAD_GET_TIC_MUTEX(obj) (&(((AgsThread *) obj)->tic_mutex))
#define AGS_THREAD_GET_TIC_COND(obj) (&(((AgsThread *) obj)->tic_cond))
#define AGS_THREAD_GET_START_MUTEX(obj) (&(((AgsThread *) obj)->start_mutex))
#define AGS_THREAD_GET_START_COND(obj) (&(((AgsThread *) obj)->start_cond))

#define AGS_THREAD_HERTZ_JIFFIE (1000.0)
#define AGS_THREAD_YIELD_JIFFIE (2.0)

#define AGS_THREAD_DEFAULT_JIFFIE (250.0)
#define AGS_THREAD_DEFAULT_MAX_PRECISION (1000.0)

#define AGS_THREAD_MAX_PRECISION (1000.0)

#define AGS_THREAD_DEFAULT_ATTACK (1.0)

#define AGS_THREAD_TOLERANCE (0.0)

typedef struct _AgsThread AgsThread;
typedef struct _AgsThreadClass AgsThreadClass;

/**
 * AgsThreadFlags:
 * @AGS_THREAD_UNREF_ON_EXIT: call g_object_unref() before g_thread_exit()
 * @AGS_THREAD_IMMEDIATE_SYNC: do sync immediately
 * @AGS_THREAD_INTERMEDIATE_PRE_SYNC: intermediate pre sync to parent thread
 * @AGS_THREAD_INTERMEDIATE_POST_SYNC: intermediate post sync to parent thread
 * @AGS_THREAD_START_SYNCED_FREQ: sync frequency as starting thread
 * @AGS_THREAD_MARK_SYNCED: mark thread synced
 * @AGS_THREAD_TIME_ACCOUNTING: time accounting causes to track time
 *
 * Enum values to control the behavior or indicate internal state of #AgsThread by
 * enable/disable as flags.
 */
typedef enum{
  AGS_THREAD_UNREF_ON_EXIT           = 1,
  AGS_THREAD_IMMEDIATE_SYNC          = 1 <<  1,
  AGS_THREAD_INTERMEDIATE_PRE_SYNC   = 1 <<  2,
  AGS_THREAD_INTERMEDIATE_POST_SYNC  = 1 <<  3,
  AGS_THREAD_START_SYNCED_FREQ       = 1 <<  4,
  AGS_THREAD_MARK_SYNCED             = 1 <<  5,
  AGS_THREAD_TIME_ACCOUNTING         = 1 <<  6,
}AgsThreadFlags;

/**
 * AgsThreadStatusFlags:
 * @AGS_THREAD_STATUS_RT_SETUP: realtime setup was performed
 * @AGS_THREAD_STATUS_INITIAL_SYNC: initial sync indicates the thread wasn't synced before
 * @AGS_THREAD_STATUS_INITIAL_RUN: the first call to #AgsThread:run()
 * @AGS_THREAD_STATUS_IS_CHAOS_TREE: the thread is not synced
 * @AGS_THREAD_STATUS_START_WAIT: the thread start is waiting
 * @AGS_THREAD_STATUS_START_DONE: the thread start is done
 * @AGS_THREAD_STATUS_READY: the thread is ready
 * @AGS_THREAD_STATUS_WAITING: the thread is waiting
 * @AGS_THREAD_STATUS_RUNNING: the thread is running
 * @AGS_THREAD_STATUS_LOCKED: the thread is locked
 * @AGS_THREAD_STATUS_BUSY: the thread is busy
 * @AGS_THREAD_STATUS_SYNCED: the thread joined the tic based system, it is synced
 * @AGS_THREAD_STATUS_SYNCED_FREQ: the frequency was synced
 *
 * Enum values to control the behavior or indicate internal state of #AgsThread by
 * enable/disable as status flags.
 */
typedef enum{
  AGS_THREAD_STATUS_RT_SETUP                = 1,
  AGS_THREAD_STATUS_INITIAL_SYNC            = 1 <<  1,
  AGS_THREAD_STATUS_INITIAL_RUN             = 1 <<  2,
  AGS_THREAD_STATUS_IS_CHAOS_TREE           = 1 <<  3,
  AGS_THREAD_STATUS_START_WAIT              = 1 <<  4,
  AGS_THREAD_STATUS_START_DONE              = 1 <<  5,
  AGS_THREAD_STATUS_READY                   = 1 <<  6,
  AGS_THREAD_STATUS_WAITING                 = 1 <<  7,
  AGS_THREAD_STATUS_RUNNING                 = 1 <<  8,
  AGS_THREAD_STATUS_LOCKED                  = 1 <<  9,
  AGS_THREAD_STATUS_BUSY                    = 1 << 10,
  AGS_THREAD_STATUS_SYNCED                  = 1 << 11,
  AGS_THREAD_STATUS_SYNCED_FREQ             = 1 << 12,
}AgsThreadStatusFlags;

/**
 * AgsThreadSyncTicFlags:
 * @AGS_THREAD_SYNC_TIC_WAIT_0: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_0: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_1: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_1: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_2: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_2: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_3: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_3: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_4: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_4: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_5: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_5: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_6: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_6: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_7: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_7: done tree to be synced
 * @AGS_THREAD_SYNC_TIC_WAIT_8: wait tree to be synced
 * @AGS_THREAD_SYNC_TIC_DONE_8: done tree to be synced
 *
 * Enum values to control the behavior or indicate internal state of #AgsThread by
 * enable/disable as sync tic flags.
 */
typedef enum{
  AGS_THREAD_SYNC_TIC_WAIT_0                  = 1,
  AGS_THREAD_SYNC_TIC_DONE_0                  = 1 <<  1,
  AGS_THREAD_SYNC_TIC_WAIT_1                  = 1 <<  2,
  AGS_THREAD_SYNC_TIC_DONE_1                  = 1 <<  3,
  AGS_THREAD_SYNC_TIC_WAIT_2                  = 1 <<  4,
  AGS_THREAD_SYNC_TIC_DONE_2                  = 1 <<  5,
  AGS_THREAD_SYNC_TIC_WAIT_3                  = 1 <<  6,
  AGS_THREAD_SYNC_TIC_DONE_3                  = 1 <<  7,
  AGS_THREAD_SYNC_TIC_WAIT_4                  = 1 <<  8,
  AGS_THREAD_SYNC_TIC_DONE_4                  = 1 <<  9,
  AGS_THREAD_SYNC_TIC_WAIT_5                  = 1 << 10,
  AGS_THREAD_SYNC_TIC_DONE_5                  = 1 << 11,
  AGS_THREAD_SYNC_TIC_WAIT_6                  = 1 << 12,
  AGS_THREAD_SYNC_TIC_DONE_6                  = 1 << 13,
  AGS_THREAD_SYNC_TIC_WAIT_7                  = 1 << 14,
  AGS_THREAD_SYNC_TIC_DONE_7                  = 1 << 15,
  AGS_THREAD_SYNC_TIC_WAIT_8                  = 1 << 16,
  AGS_THREAD_SYNC_TIC_DONE_8                  = 1 << 17,
}AgsThreadSyncTicFlags;

struct _AgsThread
{
  GObject gobject;

  guint my_flags;
  guint connectable_flags;
  _Atomic guint status_flags;
  _Atomic guint sync_tic_flags;

  _Atomic gint is_running;
  
  GRecMutex obj_mutex;

  AgsUUID *uuid;

  _Atomic guint current_sync_tic;

  gdouble delay;
  gdouble tic_delay;
  
  gdouble frequency;
  gdouble max_precision;

  gint64 last_run_start;
  gint64 last_run_end;
  
  GThread *thread;

  GMutex wait_mutex;
  GCond wait_cond;
  
  GMutex tic_mutex;
  GCond tic_cond;

  GList *start_queue;
  
  GMutex start_mutex;
  GCond start_cond;
  
  AgsThread *parent;

  AgsThread *next;
  AgsThread *prev;

  AgsThread *children;
};

struct _AgsThreadClass
{
  GObjectClass gobject;

  guint (*clock)(AgsThread *thread);
  
  void (*start)(AgsThread *thread);
  void (*run)(AgsThread *thread);
  void (*stop)(AgsThread *thread);

  void (*recover_dead_lock)(AgsThread *thread);
};

GType ags_thread_get_type();
GType ags_thread_flags_get_type();
GType ags_thread_status_flags_get_type();
GType ags_thread_sync_tic_flags_get_type();

gboolean ags_thread_global_get_use_sync_counter();

gboolean ags_thread_test_flags(AgsThread *thread,
			       AgsThreadFlags flags);
void ags_thread_set_flags(AgsThread *thread,
			  AgsThreadFlags flags);
void ags_thread_unset_flags(AgsThread *thread,
			    AgsThreadFlags flags);

gboolean ags_thread_test_status_flags(AgsThread *thread,
				      AgsThreadStatusFlags status_flags);
void ags_thread_set_status_flags(AgsThread *thread,
				 AgsThreadStatusFlags status_flags);
void ags_thread_unset_status_flags(AgsThread *thread,
				   AgsThreadStatusFlags status_flags);
void ags_thread_clear_status_flags(AgsThread *thread);

gboolean ags_thread_test_sync_tic_flags(AgsThread *thread,
					AgsThreadSyncTicFlags sync_tic_flags);
void ags_thread_set_sync_tic_flags(AgsThread *thread,
				   AgsThreadSyncTicFlags sync_tic_flags);
void ags_thread_unset_sync_tic_flags(AgsThread *thread,
				     AgsThreadSyncTicFlags sync_tic_flags);
void ags_thread_clear_sync_tic_flags(AgsThread *thread);

void ags_thread_set_current_sync_tic(AgsThread *thread, guint current_sync_tic);
guint ags_thread_get_current_sync_tic(AgsThread *thread);

void ags_thread_set_delay(AgsThread *thread, gdouble delay);
gdouble ags_thread_get_delay(AgsThread *thread);

void ags_thread_set_frequency(AgsThread *thread, gdouble frequency);
gdouble ags_thread_get_frequency(AgsThread *thread);

void ags_thread_set_max_precision(AgsThread *thread, gdouble max_precision);
gdouble ags_thread_get_max_precision(AgsThread *thread);

AgsThread* ags_thread_find_type(AgsThread *thread, GType gtype);
AgsThread* ags_thread_self(void);

AgsThread* ags_thread_parent(AgsThread *thread);
AgsThread* ags_thread_next(AgsThread *thread);
AgsThread* ags_thread_prev(AgsThread *thread);
AgsThread* ags_thread_children(AgsThread *thread);

AgsThread* ags_thread_get_toplevel(AgsThread *thread);
AgsThread* ags_thread_first(AgsThread *thread);
AgsThread* ags_thread_last(AgsThread *thread);

void ags_thread_lock(AgsThread *thread);
gboolean ags_thread_trylock(AgsThread *thread);
void ags_thread_unlock(AgsThread *thread);

void ags_thread_remove_child(AgsThread *thread, AgsThread *child);
void ags_thread_add_child(AgsThread *thread, AgsThread *child);
void ags_thread_add_child_extended(AgsThread *thread, AgsThread *child,
				   gboolean no_start, gboolean no_wait);

gboolean ags_thread_is_current_ready(AgsThread *current, guint current_sync_tic);
gboolean ags_thread_is_tree_ready_recursive(AgsThread *thread, guint current_sync_tic);

void ags_thread_prepare_current_sync(AgsThread *current, guint current_sync_tic);
void ags_thread_prepare_tree_sync_recursive(AgsThread *thread, guint current_sync_tic);

void ags_thread_set_current_sync(AgsThread *current, guint current_sync_tic);
void ags_thread_set_tree_sync_recursive(AgsThread *thread, guint current_sync_tic);

guint ags_thread_clock(AgsThread *thread);

void ags_thread_add_start_queue(AgsThread *thread,
				AgsThread *child);
void ags_thread_add_start_queue_all(AgsThread *thread,
				    GList *child);

void ags_thread_start(AgsThread *thread);
void ags_thread_run(AgsThread *thread);
void ags_thread_stop(AgsThread *thread);

void ags_thread_recover_dead_lock(AgsThread *thread);

AgsThread* ags_thread_new();

G_END_DECLS

#endif /*__AGS_THREAD_H__*/