2 * Copyright (C) 2000 Brian Paul All Rights Reserved.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * This program tests GLX thread safety.
25 * Command line options:
26 * -p Open a display connection for each thread
27 * -n <num threads> Number of threads to create (default is 2)
28 * -display <display name> Specify X display (default is :0.0)
30 * Brian Paul 20 July 2000
34 #if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
47 * Each window/thread/context:
56 int WinWidth
, WinHeight
;
61 #define MAX_WINTHREADS 100
62 static struct winthread WinThreads
[MAX_WINTHREADS
];
63 static int NumWinThreads
= 0;
64 static volatile GLboolean ExitFlag
= GL_FALSE
;
66 static GLboolean MultiDisplays
= 0;
67 static GLboolean Locking
= 0;
69 static pthread_mutex_t Mutex
;
73 Error(const char *msg
)
75 fprintf(stderr
, "Error: %s\n", msg
);
80 /* draw a colored cube */
85 glScalef(0.75, 0.75, 0.75);
89 glVertex3f(1, -1, -1);
97 glVertex3f(-1, -1, -1);
98 glVertex3f(-1, 1, -1);
100 glVertex3f(-1, -1, 1);
105 glVertex3f(-1, 1, -1);
106 glVertex3f( 1, 1, -1);
107 glVertex3f( 1, 1, 1);
108 glVertex3f(-1, 1, 1);
113 glVertex3f(-1, -1, -1);
114 glVertex3f( 1, -1, -1);
115 glVertex3f( 1, -1, 1);
116 glVertex3f(-1, -1, 1);
121 glVertex3f(-1, -1, 1);
122 glVertex3f( 1, -1, 1);
123 glVertex3f( 1, 1, 1);
124 glVertex3f(-1, 1, 1);
129 glVertex3f(-1, -1, -1);
130 glVertex3f( 1, -1, -1);
131 glVertex3f( 1, 1, -1);
132 glVertex3f(-1, 1, -1);
138 /* signal resize of given window */
140 resize(struct winthread
*wt
, int w
, int h
)
142 wt
->NewSize
= GL_TRUE
;
149 * We have an instance of this for each thread.
152 draw_loop(struct winthread
*wt
)
154 GLboolean firstIter
= GL_TRUE
;
159 pthread_mutex_lock(&Mutex
);
161 glXMakeCurrent(wt
->Dpy
, wt
->Win
, wt
->Context
);
163 printf("glthreads: %d: GL_RENDERER = %s\n", wt
->Index
,
164 (char *) glGetString(GL_RENDERER
));
165 firstIter
= GL_FALSE
;
169 pthread_mutex_unlock(&Mutex
);
171 glEnable(GL_DEPTH_TEST
);
174 GLfloat w
= (float) wt
->WinWidth
/ (float) wt
->WinHeight
;
175 glViewport(0, 0, wt
->WinWidth
, wt
->WinHeight
);
176 glMatrixMode(GL_PROJECTION
);
178 glFrustum(-w
, w
, -1.0, 1.0, 1.5, 10);
179 glMatrixMode(GL_MODELVIEW
);
181 glTranslatef(0, 0, -2.5);
182 wt
->NewSize
= GL_FALSE
;
185 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
188 glRotatef(wt
->Angle
, 0, 0, 1);
189 glRotatef(wt
->Angle
, 1, 0, 0);
190 glScalef(0.7, 0.7, 0.7);
195 pthread_mutex_lock(&Mutex
);
197 glXSwapBuffers(wt
->Dpy
, wt
->Win
);
200 pthread_mutex_unlock(&Mutex
);
209 * The main process thread runs this loop.
210 * Single display connection for all threads.
213 event_loop(Display
*dpy
)
218 assert(!MultiDisplays
);
225 pthread_mutex_lock(&Mutex
);
228 XNextEvent(dpy
, &event
);
229 pthread_mutex_unlock(&Mutex
);
232 pthread_mutex_unlock(&Mutex
);
237 XNextEvent(dpy
, &event
);
240 switch (event
.type
) {
241 case ConfigureNotify
:
242 /* Find winthread for this event's window */
243 for (i
= 0; i
< NumWinThreads
; i
++) {
244 struct winthread
*wt
= &WinThreads
[i
];
245 if (event
.xconfigure
.window
== wt
->Win
) {
246 resize(wt
, event
.xconfigure
.width
,
247 event
.xconfigure
.height
);
253 /* tell all threads to exit */
255 /*printf("exit draw_loop %d\n", wt->Index);*/
265 * Separate display connection for each thread.
268 event_loop_multi(void)
273 assert(MultiDisplays
);
276 struct winthread
*wt
= &WinThreads
[w
];
277 if (XPending(wt
->Dpy
)) {
278 XNextEvent(wt
->Dpy
, &event
);
279 switch (event
.type
) {
280 case ConfigureNotify
:
281 resize(wt
, event
.xconfigure
.width
, event
.xconfigure
.height
);
284 /* tell all threads to exit */
286 /*printf("exit draw_loop %d\n", wt->Index);*/
292 w
= (w
+ 1) % NumWinThreads
;
300 * we'll call this once for each thread, before the threads are created.
303 create_window(struct winthread
*wt
)
307 int attrib
[] = { GLX_RGBA
,
315 XSetWindowAttributes attr
;
318 XVisualInfo
*visinfo
;
319 int width
= 80, height
= 80;
320 int xpos
= (wt
->Index
% 10) * 90;
321 int ypos
= (wt
->Index
/ 10) * 100;
323 scrnum
= DefaultScreen(wt
->Dpy
);
324 root
= RootWindow(wt
->Dpy
, scrnum
);
326 visinfo
= glXChooseVisual(wt
->Dpy
, scrnum
, attrib
);
328 Error("Unable to find RGB, Z, double-buffered visual");
331 /* window attributes */
332 attr
.background_pixel
= 0;
333 attr
.border_pixel
= 0;
334 attr
.colormap
= XCreateColormap(wt
->Dpy
, root
, visinfo
->visual
, AllocNone
);
335 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
336 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
338 win
= XCreateWindow(wt
->Dpy
, root
, xpos
, ypos
, width
, height
,
339 0, visinfo
->depth
, InputOutput
,
340 visinfo
->visual
, mask
, &attr
);
342 Error("Couldn't create window");
346 XSizeHints sizehints
;
349 sizehints
.width
= width
;
350 sizehints
.height
= height
;
351 sizehints
.flags
= USSize
| USPosition
;
352 XSetNormalHints(wt
->Dpy
, win
, &sizehints
);
353 XSetStandardProperties(wt
->Dpy
, win
, "glthreads", "glthreads",
354 None
, (char **)NULL
, 0, &sizehints
);
358 ctx
= glXCreateContext(wt
->Dpy
, visinfo
, NULL
, True
);
360 Error("Couldn't create GLX context");
363 XMapWindow(wt
->Dpy
, win
);
366 /* save the info for this window/context */
370 wt
->WinWidth
= width
;
371 wt
->WinHeight
= height
;
372 wt
->NewSize
= GL_TRUE
;
377 * Called by pthread_create()
380 thread_function(void *p
)
382 struct winthread
*wt
= (struct winthread
*) p
;
389 * called before exit to wait for all threads to finish
396 /* wait for threads to finish */
397 for (i
= 0; i
< NumWinThreads
; i
++) {
398 pthread_join(WinThreads
[i
].Thread
, NULL
);
401 for (i
= 0; i
< NumWinThreads
; i
++) {
402 glXDestroyContext(WinThreads
[i
].Dpy
, WinThreads
[i
].Context
);
403 XDestroyWindow(WinThreads
[i
].Dpy
, WinThreads
[i
].Win
);
410 main(int argc
, char *argv
[])
412 char *displayName
= ":0.0";
419 printf("glthreads: test of GL thread safety (any key = exit)\n");
421 printf(" glthreads [-display dpyName] [-n numthreads]\n");
425 for (i
= 1; i
< argc
; i
++) {
426 if (strcmp(argv
[i
], "-display") == 0 && i
+ 1 < argc
) {
427 displayName
= argv
[i
+ 1];
430 else if (strcmp(argv
[i
], "-p") == 0) {
433 else if (strcmp(argv
[i
], "-l") == 0) {
436 else if (strcmp(argv
[i
], "-n") == 0 && i
+ 1 < argc
) {
437 numThreads
= atoi(argv
[i
+ 1]);
440 else if (numThreads
> MAX_WINTHREADS
)
441 numThreads
= MAX_WINTHREADS
;
445 fprintf(stderr
, "glthreads: unexpected flag: %s\n", argv
[i
]);
451 printf("glthreads: Using explict locks around Xlib calls.\n");
453 printf("glthreads: No explict locking.\n");
456 printf("glthreads: Per-thread display connections.\n");
458 printf("glthreads: Single display connection.\n");
461 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
463 if (!MultiDisplays
) {
465 threadStat
= XInitThreads();
467 printf("XInitThreads() returned %d (success)\n", (int) threadStat
);
470 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
474 dpy
= XOpenDisplay(displayName
);
476 fprintf(stderr
, "Unable to open display %s\n", XDisplayName(displayName
));
482 pthread_mutex_init(&Mutex
, NULL
);
485 printf("glthreads: creating windows\n");
487 NumWinThreads
= numThreads
;
489 /* Create the GLX windows and contexts */
490 for (i
= 0; i
< numThreads
; i
++) {
492 WinThreads
[i
].Dpy
= XOpenDisplay(displayName
);
493 assert(WinThreads
[i
].Dpy
);
496 WinThreads
[i
].Dpy
= dpy
;
498 WinThreads
[i
].Index
= i
;
499 create_window(&WinThreads
[i
]);
502 printf("glthreads: creating threads\n");
504 /* Create the threads */
505 for (i
= 0; i
< numThreads
; i
++) {
506 pthread_create(&WinThreads
[i
].Thread
, NULL
, thread_function
,
507 (void*) &WinThreads
[i
]);
508 printf("glthreads: Created thread %u\n", (unsigned int) WinThreads
[i
].Thread
);
519 for (i
= 0; i
< numThreads
; i
++) {
520 XCloseDisplay(WinThreads
[i
].Dpy
);
537 main(int argc
, char *argv
[])
539 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
544 #endif /* PTHREADS */