File: ags_destroy_worker.c

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 (292 lines) | stat: -rw-r--r-- 7,266 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
/* GSequencer - Advanced GTK Sequencer
 * Copyright (C) 2005-2019 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_destroy_worker.h>

#include <ags/lib/ags_time.h>

#include <ags/object/ags_connectable.h>

#include <stdlib.h>

void ags_destroy_worker_class_init(AgsDestroyWorkerClass *destroy_worker);
void ags_destroy_worker_init(AgsDestroyWorker *destroy_worker);
void ags_destroy_worker_finalize(GObject *gobject);

void ags_destroy_worker_start(AgsThread *thread);
void ags_destroy_worker_stop(AgsThread *thread);

void ags_destroy_worker_do_poll(AgsWorkerThread *worker_thread);

/**
 * SECTION:ags_destroy_worker
 * @short_description: destroy worker
 * @title: AgsDestroyWorker
 * @section_id:
 * @include: ags/worker/ags_destroy_worker.h
 *
 * The #AgsDestroyWorker does non-realtime work. You might want
 * give it the responsibility to destroy your objects.
 */

static gpointer ags_destroy_worker_parent_class = NULL;

AgsDestroyWorker *ags_destroy_worker = NULL;

GType
ags_destroy_worker_get_type()
{
  static gsize g_define_type_id__static = 0;

  if(g_once_init_enter(&g_define_type_id__static)){
    GType ags_type_destroy_worker = 0;

    static const GTypeInfo ags_destroy_worker_info = {
      sizeof (AgsDestroyWorkerClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) ags_destroy_worker_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof (AgsDestroyWorker),
      0,    /* n_preallocs */
      (GInstanceInitFunc) ags_destroy_worker_init,
    };

    ags_type_destroy_worker = g_type_register_static(AGS_TYPE_WORKER_THREAD,
						     "AgsDestroyWorker",
						     &ags_destroy_worker_info,
						     0);    

    g_once_init_leave(&g_define_type_id__static, ags_type_destroy_worker);
  }

  return(g_define_type_id__static);
}

void
ags_destroy_worker_class_init(AgsDestroyWorkerClass *destroy_worker)
{
  GObjectClass *gobject;
  AgsThreadClass *thread;
  AgsWorkerThreadClass *worker_thread;

  ags_destroy_worker_parent_class = g_type_class_peek_parent(destroy_worker);

  /* GObject */
  gobject = (GObjectClass *) destroy_worker;

  gobject->finalize = ags_destroy_worker_finalize;

  /* AgsThread */
  thread = (AgsThreadClass *) destroy_worker;

  thread->start = ags_destroy_worker_start;
  thread->run = NULL;
  thread->stop = ags_destroy_worker_stop;

  /* AgsDestroyWorker */
  worker_thread = (AgsWorkerThreadClass *) destroy_worker;

  worker_thread->do_poll = ags_destroy_worker_do_poll;
}

void
ags_destroy_worker_init(AgsDestroyWorker *destroy_worker)
{
  destroy_worker->destroy_interval = (struct timespec *) malloc(sizeof(struct timespec));

  destroy_worker->destroy_interval->tv_sec = 1;
  destroy_worker->destroy_interval->tv_nsec = 0;
  
  /* lock destroy list */
  g_rec_mutex_init(&(destroy_worker->destroy_mutex));

  /* destroy list */
  destroy_worker->destroy_list = NULL;
}

void
ags_destroy_worker_finalize(GObject *gobject)
{
  AgsDestroyWorker *destroy_worker;

  destroy_worker = AGS_DESTROY_WORKER(gobject);

  if(destroy_worker->destroy_interval != NULL){
    free(destroy_worker->destroy_interval);
  }
  
  /* call parent */
  G_OBJECT_CLASS(ags_destroy_worker_parent_class)->finalize(gobject);
}

void
ags_destroy_worker_start(AgsThread *thread)
{
  AgsWorkerThread *worker_thread;

  worker_thread = AGS_WORKER_THREAD(thread);

  ags_worker_thread_set_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING);

  worker_thread->worker_thread = g_thread_new("Advanced Gtk+ Sequencer - destroy worker",
					      ags_woker_thread_do_poll_loop,
					      worker_thread);
}

void
ags_destroy_worker_stop(AgsThread *thread)
{
  AgsWorkerThread *worker_thread;

  worker_thread = AGS_WORKER_THREAD(thread);

  ags_worker_thread_set_status_flags(worker_thread, AGS_WORKER_THREAD_STATUS_RUNNING);
}

void
ags_destroy_worker_do_poll(AgsWorkerThread *worker_thread)
{
  AgsDestroyWorker *destroy_worker;

  GList *list, *list_start;
  
  destroy_worker = AGS_DESTROY_WORKER(worker_thread);

  g_rec_mutex_lock(&(destroy_worker->destroy_mutex));

  list_start =
    list = destroy_worker->destroy_list;
  destroy_worker->destroy_list = NULL;
  
  g_rec_mutex_unlock(&(destroy_worker->destroy_mutex));

  while(list != NULL){
    AGS_DESTROY_ENTRY(list->data)->destroy_func(AGS_DESTROY_ENTRY(list->data)->ptr);

    list = list->next;
  }

  g_list_free_full(list_start,
		   g_free);

  ags_time_nanosleep(destroy_worker->destroy_interval);
}

/**
 * ags_destroy_entry_alloc:
 * @ptr: a pointer
 * @destroy_func: (scope call): the @ptr's destroy function
 * 
 * Allocated a destroy entry.
 * 
 * Returns: (type gpointer) (transfer none): the allocated #AgsDestroyEntry
 * 
 * Since: 3.0.0
 */
AgsDestroyEntry*
ags_destroy_entry_alloc(gpointer ptr, AgsDestroyFunc destroy_func)
{
  AgsDestroyEntry *destroy_entry;

  destroy_entry = (AgsDestroyEntry *) malloc(sizeof(AgsDestroyEntry));

  destroy_entry->ptr = ptr;
  destroy_entry->destroy_func = destroy_func;

  return(destroy_entry);
}

/**
 * ags_destroy_worker_add:
 * @destroy_worker: (type gpointer): the #AgsDestroyWorker
 * @ptr: the gpointer to destroy
 * @destroy_func: (scope call): the AgsDestroyFunc
 * 
 * Add @ptr for destruction using @destroy_func.
 * 
 * Since: 3.0.0
 */
void
ags_destroy_worker_add(AgsDestroyWorker *destroy_worker,
		       gpointer ptr, AgsDestroyFunc destroy_func)
{
  AgsDestroyEntry *destroy_entry;

  if(!AGS_IS_DESTROY_WORKER(destroy_worker) ||
     ptr == NULL ||
     destroy_func == NULL){
    return;
  }

  destroy_entry = ags_destroy_entry_alloc(ptr, destroy_func);
  
  g_rec_mutex_lock(&(destroy_worker->destroy_mutex));

  destroy_worker->destroy_list = g_list_prepend(destroy_worker->destroy_list,
						destroy_entry);
  
  g_rec_mutex_unlock(&(destroy_worker->destroy_mutex));
}

/**
 * ags_destroy_worker_get_instance:
 * 
 * Get your destroy worker instance.
 *
 * Returns: (transfer none): the #AgsDestroyWorker instance
 *
 * Since: 3.0.0
 */
AgsDestroyWorker*
ags_destroy_worker_get_instance()
{
  static GMutex mutex;

  g_mutex_lock(&mutex);
  
  if(ags_destroy_worker == NULL){
    ags_destroy_worker = ags_destroy_worker_new();
  }

  g_mutex_unlock(&mutex);
  
  return(ags_destroy_worker);
}

/**
 * ags_destroy_worker_new:
 *
 * Create a new #AgsDestroyWorker.
 *
 * Returns: the new #AgsDestroyWorker
 *
 * Since: 3.0.0
 */
AgsDestroyWorker*
ags_destroy_worker_new()
{
  AgsDestroyWorker *destroy_worker;
  
  destroy_worker = (AgsDestroyWorker *) g_object_new(AGS_TYPE_DESTROY_WORKER,
						     NULL);

  return(destroy_worker);
}