mesa/glthread: restore the dispatch table when incompatible gl calls are detected
[mesa.git] / src / mesa / main / glthread.c
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file glthread.c
25 *
26 * Support functions for the glthread feature of Mesa.
27 *
28 * In multicore systems, many applications end up CPU-bound with about half
29 * their time spent inside their rendering thread and half inside Mesa. To
30 * alleviate this, we put a shim layer in Mesa at the GL dispatch level that
31 * quickly logs the GL commands to a buffer to be processed by a worker
32 * thread.
33 */
34
35 #include "main/mtypes.h"
36 #include "main/glthread.h"
37 #include "main/marshal.h"
38 #include "main/marshal_generated.h"
39
40 #ifdef HAVE_PTHREAD
41
42 static void
43 glthread_allocate_batch(struct gl_context *ctx)
44 {
45 struct glthread_state *glthread = ctx->GLThread;
46
47 /* TODO: handle memory allocation failure. */
48 glthread->batch = malloc(sizeof(*glthread->batch));
49 if (!glthread->batch)
50 return;
51 memset(glthread->batch, 0, offsetof(struct glthread_batch, buffer));
52 }
53
54 static void
55 glthread_unmarshal_batch(struct gl_context *ctx, struct glthread_batch *batch)
56 {
57 size_t pos = 0;
58
59 _glapi_set_dispatch(ctx->CurrentServerDispatch);
60
61 while (pos < batch->used)
62 pos += _mesa_unmarshal_dispatch_cmd(ctx, &batch->buffer[pos]);
63
64 assert(pos == batch->used);
65
66 free(batch);
67 }
68
69 static void *
70 glthread_worker(void *data)
71 {
72 struct gl_context *ctx = data;
73 struct glthread_state *glthread = ctx->GLThread;
74
75 ctx->Driver.SetBackgroundContext(ctx);
76 _glapi_set_context(ctx);
77
78 pthread_mutex_lock(&glthread->mutex);
79
80 while (true) {
81 struct glthread_batch *batch;
82
83 /* Block (dropping the lock) until new work arrives for us. */
84 while (!glthread->batch_queue && !glthread->shutdown) {
85 pthread_cond_broadcast(&glthread->work_done);
86 pthread_cond_wait(&glthread->new_work, &glthread->mutex);
87 }
88
89 batch = glthread->batch_queue;
90
91 if (glthread->shutdown && !batch) {
92 pthread_cond_broadcast(&glthread->work_done);
93 pthread_mutex_unlock(&glthread->mutex);
94 return NULL;
95 }
96 glthread->batch_queue = batch->next;
97 if (glthread->batch_queue_tail == &batch->next)
98 glthread->batch_queue_tail = &glthread->batch_queue;
99
100 glthread->busy = true;
101 pthread_mutex_unlock(&glthread->mutex);
102
103 glthread_unmarshal_batch(ctx, batch);
104
105 pthread_mutex_lock(&glthread->mutex);
106 glthread->busy = false;
107 }
108
109 /* UNREACHED */
110 return NULL;
111 }
112
113 void
114 _mesa_glthread_init(struct gl_context *ctx)
115 {
116 struct glthread_state *glthread = calloc(1, sizeof(*glthread));
117
118 if (!glthread)
119 return;
120
121 ctx->MarshalExec = _mesa_create_marshal_table(ctx);
122 if (!ctx->MarshalExec) {
123 free(glthread);
124 return;
125 }
126
127 ctx->CurrentClientDispatch = ctx->MarshalExec;
128
129 pthread_mutex_init(&glthread->mutex, NULL);
130 pthread_cond_init(&glthread->new_work, NULL);
131 pthread_cond_init(&glthread->work_done, NULL);
132
133 glthread->batch_queue_tail = &glthread->batch_queue;
134 ctx->GLThread = glthread;
135
136 glthread_allocate_batch(ctx);
137
138 pthread_create(&glthread->thread, NULL, glthread_worker, ctx);
139 }
140
141 void
142 _mesa_glthread_destroy(struct gl_context *ctx)
143 {
144 struct glthread_state *glthread = ctx->GLThread;
145
146 if (!glthread)
147 return;
148
149 _mesa_glthread_flush_batch(ctx);
150
151 pthread_mutex_lock(&glthread->mutex);
152 glthread->shutdown = true;
153 pthread_cond_broadcast(&glthread->new_work);
154 pthread_mutex_unlock(&glthread->mutex);
155
156 /* Since this waits for the thread to exit, it means that all queued work
157 * will have been completed.
158 */
159 pthread_join(glthread->thread, NULL);
160
161 pthread_cond_destroy(&glthread->new_work);
162 pthread_cond_destroy(&glthread->work_done);
163 pthread_mutex_destroy(&glthread->mutex);
164
165 /* Due to the join above, there should be one empty batch allocated at this
166 * point, and no batches queued.
167 */
168 assert(!glthread->batch->used);
169 assert(!glthread->batch->next);
170 free(glthread->batch);
171 assert(!glthread->batch_queue);
172
173 free(glthread);
174 ctx->GLThread = NULL;
175
176 _mesa_glthread_restore_dispatch(ctx);
177 }
178
179 void
180 _mesa_glthread_restore_dispatch(struct gl_context *ctx)
181 {
182 /* Remove ourselves from the dispatch table except if another ctx/thread
183 * already installed a new dispatch table.
184 *
185 * Typically glxMakeCurrent will bind a new context (install new table) then
186 * old context might be deleted.
187 */
188 if (_glapi_get_dispatch() == ctx->MarshalExec) {
189 ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
190 _glapi_set_dispatch(ctx->CurrentClientDispatch);
191 }
192 }
193
194 void
195 _mesa_glthread_flush_batch(struct gl_context *ctx)
196 {
197 struct glthread_state *glthread = ctx->GLThread;
198 struct glthread_batch *batch;
199
200 if (!glthread)
201 return;
202
203 batch = glthread->batch;
204 if (!batch->used)
205 return;
206
207 /* Immediately reallocate a new batch, since the next marshalled call would
208 * just do it.
209 */
210 glthread_allocate_batch(ctx);
211
212 /* Debug: execute the batch immediately from this thread.
213 *
214 * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
215 * need to restore it when it returns.
216 */
217 if (false) {
218 glthread_unmarshal_batch(ctx, batch);
219 _glapi_set_dispatch(ctx->CurrentClientDispatch);
220 return;
221 }
222
223 pthread_mutex_lock(&glthread->mutex);
224 *glthread->batch_queue_tail = batch;
225 glthread->batch_queue_tail = &batch->next;
226 pthread_cond_broadcast(&glthread->new_work);
227 pthread_mutex_unlock(&glthread->mutex);
228 }
229
230 /**
231 * Waits for all pending batches have been unmarshaled.
232 *
233 * This can be used by the main thread to synchronize access to the context,
234 * since the worker thread will be idle after this.
235 */
236 void
237 _mesa_glthread_finish(struct gl_context *ctx)
238 {
239 struct glthread_state *glthread = ctx->GLThread;
240
241 if (!glthread)
242 return;
243
244 /* If this is called from the worker thread, then we've hit a path that
245 * might be called from either the main thread or the worker (such as some
246 * dri interface entrypoints), in which case we don't need to actually
247 * synchronize against ourself.
248 */
249 if (pthread_self() == glthread->thread)
250 return;
251
252 _mesa_glthread_flush_batch(ctx);
253
254 pthread_mutex_lock(&glthread->mutex);
255
256 while (glthread->batch_queue || glthread->busy)
257 pthread_cond_wait(&glthread->work_done, &glthread->mutex);
258
259 pthread_mutex_unlock(&glthread->mutex);
260 }
261
262 #endif