blob: 63a94c55681327e7e60ebe6a2967c050ee808a9c [file] [log] [blame]
Brian Paulc7d14442000-07-20 20:12:17 +00001
2/*
3 * Copyright (C) 2000 Brian Paul All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23
24/*
25 * This program tests GLX thread safety.
26 * Command line options:
27 * -n <num threads> Number of threads to create (default is 2)
28 * -display <display name> Specify X display (default is :0.0)
29 *
30 * Brian Paul 20 July 2000
31 */
32
33
34#if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
35
36#include <GL/gl.h>
37#include <GL/glx.h>
38#include <stdio.h>
39#include <stdlib.h>
Brian Paula3e44f42002-03-08 19:44:28 +000040#include <string.h>
Brian Paulc7d14442000-07-20 20:12:17 +000041#include <unistd.h>
42#include <pthread.h>
43
44
45/*
46 * Each window/thread/context:
47 */
48struct winthread {
49 Display *Dpy;
50 int Index;
51 pthread_t Thread;
52 Window Win;
53 GLXContext Context;
54 float Angle;
55 int WinWidth, WinHeight;
56 GLboolean NewSize;
57};
58
59
60#define MAX_WINTHREADS 100
61static struct winthread WinThreads[MAX_WINTHREADS];
62static int NumWinThreads = 0;
63static GLboolean ExitFlag = GL_FALSE;
64
65
66
67static void
68Error(const char *msg)
69{
70 fprintf(stderr, "Error: %s\n", msg);
71 exit(1);
72}
73
74
75/* draw a colored cube */
76static void
77draw_object(void)
78{
79 glPushMatrix();
80 glScalef(0.75, 0.75, 0.75);
81
82 glColor3f(1, 0, 0);
83 glBegin(GL_POLYGON);
84 glVertex3f(1, -1, -1);
85 glVertex3f(1, 1, -1);
86 glVertex3f(1, 1, 1);
87 glVertex3f(1, -1, 1);
88 glEnd();
89
90 glColor3f(0, 1, 1);
91 glBegin(GL_POLYGON);
92 glVertex3f(-1, -1, -1);
93 glVertex3f(-1, 1, -1);
94 glVertex3f(-1, 1, 1);
95 glVertex3f(-1, -1, 1);
96 glEnd();
97
98 glColor3f(0, 1, 0);
99 glBegin(GL_POLYGON);
100 glVertex3f(-1, 1, -1);
101 glVertex3f( 1, 1, -1);
102 glVertex3f( 1, 1, 1);
103 glVertex3f(-1, 1, 1);
104 glEnd();
105
106 glColor3f(1, 0, 1);
107 glBegin(GL_POLYGON);
108 glVertex3f(-1, -1, -1);
109 glVertex3f( 1, -1, -1);
110 glVertex3f( 1, -1, 1);
111 glVertex3f(-1, -1, 1);
112 glEnd();
113
114 glColor3f(0, 0, 1);
115 glBegin(GL_POLYGON);
116 glVertex3f(-1, -1, 1);
117 glVertex3f( 1, -1, 1);
118 glVertex3f( 1, 1, 1);
119 glVertex3f(-1, 1, 1);
120 glEnd();
121
122 glColor3f(1, 1, 0);
123 glBegin(GL_POLYGON);
124 glVertex3f(-1, -1, -1);
125 glVertex3f( 1, -1, -1);
126 glVertex3f( 1, 1, -1);
127 glVertex3f(-1, 1, -1);
128 glEnd();
129 glPopMatrix();
130}
131
132
133/* signal resize of given window */
134static void
135resize(struct winthread *wt, int w, int h)
136{
137 wt->NewSize = GL_TRUE;
138 wt->WinWidth = w;
139 wt->WinHeight = h;
140}
141
142
143/*
144 * We have an instance of this for each thread.
145 */
146static void
147draw_loop(struct winthread *wt)
148{
149 while (!ExitFlag) {
150
151 glXMakeCurrent(wt->Dpy, wt->Win, wt->Context);
152
153 glEnable(GL_DEPTH_TEST);
154
155 if (wt->NewSize) {
156 GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
157 glViewport(0, 0, wt->WinWidth, wt->WinHeight);
158 glMatrixMode(GL_PROJECTION);
159 glLoadIdentity();
160 glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
161 glMatrixMode(GL_MODELVIEW);
162 glLoadIdentity();
163 glTranslatef(0, 0, -2.5);
164 wt->NewSize = GL_FALSE;
165 }
166
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
168
169 glPushMatrix();
170 glRotatef(wt->Angle, 0, 0, 1);
171 glRotatef(wt->Angle, 1, 0, 0);
172 glScalef(0.7, 0.7, 0.7);
173 draw_object();
174 glPopMatrix();
175
176 glXSwapBuffers(wt->Dpy, wt->Win);
177
178 wt->Angle += 1.0;
179 }
180}
181
182
183/*
184 * The main process thread runs this loop.
185 */
186static void
187event_loop(Display *dpy)
188{
189 while (!ExitFlag) {
190 static long mask = StructureNotifyMask | ExposureMask | KeyPressMask;
191 XEvent event;
192 int i;
193
194 for (i = 0; i < NumWinThreads; i++) {
195 struct winthread *wt = &WinThreads[i];
196 while (XCheckWindowEvent(dpy, wt->Win, mask, &event)) {
197 if (event.xany.window == wt->Win) {
198 switch (event.type) {
199 case ConfigureNotify:
200 resize(wt, event.xconfigure.width,
201 event.xconfigure.height);
202 break;
203 case KeyPress:
204 /* tell all threads to exit */
205 ExitFlag = GL_TRUE;
206 /*printf("exit draw_loop %d\n", wt->Index);*/
207 return;
208 default:
209 /*no-op*/ ;
210 }
211 }
212 else {
213 printf("window mismatch\n");
214 }
215 }
216 }
217 }
218}
219
220
221/*
222 * we'll call this once for each thread, before the threads are created.
223 */
224static void
225create_window(struct winthread *wt)
226{
227 Window win;
228 GLXContext ctx;
229 int attrib[] = { GLX_RGBA,
230 GLX_RED_SIZE, 1,
231 GLX_GREEN_SIZE, 1,
232 GLX_BLUE_SIZE, 1,
233 GLX_DEPTH_SIZE, 1,
234 GLX_DOUBLEBUFFER,
235 None };
236 int scrnum;
237 XSetWindowAttributes attr;
238 unsigned long mask;
239 Window root;
240 XVisualInfo *visinfo;
241 int width = 80, height = 80;
242 int xpos = (wt->Index % 10) * 90;
243 int ypos = (wt->Index / 10) * 100;
244
245 scrnum = DefaultScreen(wt->Dpy);
246 root = RootWindow(wt->Dpy, scrnum);
247
248 visinfo = glXChooseVisual(wt->Dpy, scrnum, attrib);
249 if (!visinfo) {
250 Error("Unable to find RGB, Z, double-buffered visual");
251 }
252
253 /* window attributes */
254 attr.background_pixel = 0;
255 attr.border_pixel = 0;
256 attr.colormap = XCreateColormap(wt->Dpy, root, visinfo->visual, AllocNone);
257 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
258 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
259
260 win = XCreateWindow(wt->Dpy, root, xpos, ypos, width, height,
261 0, visinfo->depth, InputOutput,
262 visinfo->visual, mask, &attr);
263 if (!win) {
264 Error("Couldn't create window");
265 }
266
267 {
268 XSizeHints sizehints;
269 sizehints.x = xpos;
270 sizehints.y = ypos;
271 sizehints.width = width;
272 sizehints.height = height;
273 sizehints.flags = USSize | USPosition;
274 XSetNormalHints(wt->Dpy, win, &sizehints);
275 XSetStandardProperties(wt->Dpy, win, "glthreads", "glthreads",
276 None, (char **)NULL, 0, &sizehints);
277 }
278
279
280 ctx = glXCreateContext(wt->Dpy, visinfo, NULL, True);
281 if (!ctx) {
282 Error("Couldn't create GLX context");
283 }
284
285 XMapWindow(wt->Dpy, win);
286 XSync(wt->Dpy, 0);
287
288 /* save the info for this window/context */
289 wt->Win = win;
290 wt->Context = ctx;
291 wt->Angle = 0.0;
292 wt->WinWidth = width;
293 wt->WinHeight = height;
294 wt->NewSize = GL_TRUE;
295}
296
297
298/*
299 * Called by pthread_create()
300 */
301static void *
302thread_function(void *p)
303{
304 struct winthread *wt = (struct winthread *) p;
305 draw_loop(wt);
306 return NULL;
307}
308
309
310/*
311 * called before exit to wait for all threads to finish
312 */
313static void
314clean_up(void)
315{
316 int i;
317
318 /* wait for threads to finish */
319 for (i = 0; i < NumWinThreads; i++) {
320 pthread_join(WinThreads[i].Thread, NULL);
321 }
322
323 for (i = 0; i < NumWinThreads; i++) {
324 glXDestroyContext(WinThreads[i].Dpy, WinThreads[i].Context);
325 XDestroyWindow(WinThreads[i].Dpy, WinThreads[i].Win);
326 }
327}
328
329
330
331int
332main(int argc, char *argv[])
333{
334 char *displayName = ":0.0";
335 int numThreads = 2;
336 Display *dpy;
337 int i;
338 Status threadStat;
339
340 if (argc == 1) {
341 printf("threadgl: test of GL thread safety (any key = exit)\n");
342 printf("Usage:\n");
343 printf(" threadgl [-display dpyName] [-n numthreads]\n");
344 }
345 else {
346 int i;
347 for (i = 1; i < argc; i++) {
348 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
349 displayName = argv[i + 1];
350 i++;
351 }
352 else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
353 numThreads = atoi(argv[i + 1]);
354 if (numThreads < 1)
355 numThreads = 1;
356 else if (numThreads > MAX_WINTHREADS)
357 numThreads = MAX_WINTHREADS;
358 i++;
359 }
360 }
361 }
362
363 /*
364 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
365 */
366 threadStat = XInitThreads();
367 if (threadStat) {
368 printf("XInitThreads() returned %d (success)\n", (int) threadStat);
369 }
370 else {
371 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
372 }
373
374
375 dpy = XOpenDisplay(displayName);
376 if (!dpy) {
377 fprintf(stderr, "Unable to open display %s\n", displayName);
378 return -1;
379 }
380
381 NumWinThreads = numThreads;
382
383 /* Create the GLX windows and contexts */
384 for (i = 0; i < numThreads; i++) {
385 WinThreads[i].Dpy = dpy;
386 WinThreads[i].Index = i;
387 create_window(&WinThreads[i]);
388 }
389
390 /* Create the threads */
391 for (i = 0; i < numThreads; i++) {
392 pthread_create(&WinThreads[i].Thread, NULL, thread_function,
393 (void*) &WinThreads[i]);
394 printf("Created Thread %p\n", WinThreads[i].Thread);
395 }
396
397 event_loop(dpy);
398
399 clean_up();
400
401 XCloseDisplay(dpy);
402
403 return 0;
404}
405
406
407#else /* PTHREADS */
408
409
410#include <stdio.h>
411
412int
413main(int argc, char *argv[])
414{
415 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
416 return 0;
417}
418
419
420#endif /* PTHREADS */