1 /**
2 * \file render_queue.c
3 * \brief Manage a queue of rendered backbuffers to display
4 *
5 * \author David Hogan <david.q.hogan@gmail.com>
6 */
7
8 /* This file is part of VICE, the Versatile Commodore Emulator.
9 * See README for copyright notice.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 * 02111-1307 USA.
25 *
26 */
27
28 #include "vice.h"
29 #include "render_thread.h"
30
31 #include <glib.h>
32 #include <pthread.h>
33 #include <stdbool.h>
34 #include <string.h>
35
36 #include "archdep.h"
37 #include "lib.h"
38 #include "log.h"
39
40 struct render_thread_s {
41 int index;
42 GThreadPool *executor;
43 bool is_shutdown_initiated;
44 bool is_shut_down;
45 };
46
47 #define RENDER_THREAD_MAX 2 /* Current max of two windows (x128) */
48
49 static struct render_thread_s threads[RENDER_THREAD_MAX];
50 static int thread_count;
51
52 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
53
54 #define LOCK() pthread_mutex_lock(&lock)
55 #define UNLOCK() pthread_mutex_unlock(&lock)
56
render_thread_create(render_thread_callback_t callback,void * thread_context)57 render_thread_t render_thread_create(render_thread_callback_t callback, void *thread_context)
58 {
59 render_thread_t thread;
60
61 LOCK();
62
63 if (thread_count == RENDER_THREAD_MAX) {
64 log_error(LOG_ERR, "Reach maximum render thread count (%d), cannot create another", RENDER_THREAD_MAX);
65 UNLOCK();
66 archdep_vice_exit(-1);
67 }
68
69 thread = threads + thread_count;
70 memset(thread, 0, sizeof(struct render_thread_s));
71 thread->index = thread_count++;
72
73 thread->executor = g_thread_pool_new(callback, thread_context, 1, TRUE, NULL);
74
75 /* Schedule the init job */
76 g_thread_pool_push(thread->executor, (void *)render_thread_init, NULL);
77
78 UNLOCK();
79
80 log_message(LOG_DEFAULT, "Created render thread %d", thread->index);
81
82 return thread;
83 }
84
render_thread_initiate_shutdown(render_thread_t thread)85 void render_thread_initiate_shutdown(render_thread_t thread)
86 {
87 LOCK();
88
89 if (thread->is_shutdown_initiated) {
90 UNLOCK();
91 return;
92 }
93
94 log_message(LOG_DEFAULT, "Initiating render thread %d shutdown", thread->index);
95 thread->is_shutdown_initiated = true;
96
97 /* Schedule the shutdown job */
98 g_thread_pool_push(thread->executor, (void *)render_thread_shutdown, NULL);
99
100 UNLOCK();
101 }
102
render_thread_join(render_thread_t thread)103 void render_thread_join(render_thread_t thread)
104 {
105 log_message(LOG_DEFAULT, "Joining render thread %d ...", thread->index);
106
107 /* TODO: We should block until all jobs are done - but there's a race condition deadlock outcome here. Fix needed */
108 g_thread_pool_free(thread->executor, TRUE, TRUE);
109
110 LOCK();
111 thread->is_shut_down = true;
112 UNLOCK();
113
114 log_message(LOG_DEFAULT, "Joined render thread %d.", thread->index);
115 }
116
render_thread_shutdown_and_join_all(void)117 void render_thread_shutdown_and_join_all(void)
118 {
119 for (int i = 0; i < thread_count; i++) {
120 render_thread_initiate_shutdown(threads + i);
121 }
122
123 for (int i = 0; i < thread_count; i++) {
124 render_thread_join(threads + i);
125 }
126 }
127
render_thread_push_job(render_thread_t thread,render_job_t job)128 void render_thread_push_job(render_thread_t thread, render_job_t job)
129 {
130 LOCK();
131
132 if (thread->is_shutdown_initiated)
133 {
134 log_message(LOG_DEFAULT, "Ignoring new render job as render thread %d %s down", thread->index, thread->is_shut_down ? "has shut" : "is shutting");
135 UNLOCK();
136 return;
137 }
138
139 g_thread_pool_push(thread->executor, (void *)job, NULL);
140
141 UNLOCK();
142 }
143
144
145
146
147