File: pull_thread.c.in

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 (94 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2005-2021 Joël Krähemann
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

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

#include <ags/libags.h>

void setup_callback(AgsApplicationContext *application_context, gpointer data);
void thread_run_callback(AgsThread *thread, gpointer data);

gchar *thread_0_str = "thread 0";
gchar *thread_1_str = "thread 1";

void
setup_callback(AgsApplicationContext *application_context, gpointer data)
{
  AgsThread *main_loop;
  AgsThread *thread_0, *thread_1;
  AgsThreadPool *thread_pool;
  
  main_loop = ags_concurrency_provider_get_main_loop(AGS_CONCURRENCY_PROVIDER(application_context));

  thread_pool = ags_thread_pool_new(main_loop);
  ags_concurrency_provider_set_thread_pool(AGS_CONCURRENCY_PROVIDER(application_context),
					   thread_pool);

  ags_thread_pool_start(thread_pool);
  
  /* pull thread 0 */
  thread_0 = ags_thread_pool_pull(thread_pool);
  
  g_rec_mutex_lock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_0));

  g_atomic_pointer_set(&(AGS_RETURNABLE_THREAD(thread_0)->safe_data),
                       thread_0_str);

  ags_returnable_thread_connect_safe_run(AGS_RETURNABLE_THREAD(thread_0),
                                         thread_run_callback);

  ags_returnable_thread_set_flags(thread_0,
				  AGS_RETURNABLE_THREAD_IN_USE);
    
  g_rec_mutex_unlock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_0));

  /* pull thread 1 */
  thread_1 = ags_thread_pool_pull(thread_pool);
  
  g_rec_mutex_lock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_1));

  g_atomic_pointer_set(&(AGS_RETURNABLE_THREAD(thread_1)->safe_data),
                       thread_1_str);

  ags_returnable_thread_connect_safe_run(AGS_RETURNABLE_THREAD(thread_1),
                                         thread_run_callback);

  ags_returnable_thread_set_flags(thread_1,
				  AGS_RETURNABLE_THREAD_IN_USE);
    
  g_rec_mutex_unlock(AGS_RETURNABLE_THREAD_GET_RESET_MUTEX(thread_1));
}

void
thread_run_callback(AgsThread *thread, gpointer data)
{
  g_message("%s", (gchar *) data);
}

int
main(int argc, char **argv)
{
  AgsApplicationContext *application_context;

  application_context = ags_thread_application_context_new();
  g_object_ref(application_context);
  
  g_signal_connect_after(application_context, "setup",
			 G_CALLBACK(setup_callback), NULL);

  ags_application_context_prepare(application_context);
  ags_application_context_setup(application_context);
  
  /* main loop run */
  g_main_loop_run(g_main_loop_new(g_main_context_default(),
				  TRUE));
  
  return(0);
}