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 * -n <num threads> Number of threads to create (default is 2)
27 * -display <display name> Specify X display (default is :0.0)
29 * Brian Paul 20 July 2000
33 #if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
45 * Each window/thread/context:
54 int WinWidth
, WinHeight
;
59 #define MAX_WINTHREADS 100
60 static struct winthread WinThreads
[MAX_WINTHREADS
];
61 static int NumWinThreads
= 0;
62 static volatile GLboolean ExitFlag
= GL_FALSE
;
67 Error(const char *msg
)
69 fprintf(stderr
, "Error: %s\n", msg
);
74 /* draw a colored cube */
79 glScalef(0.75, 0.75, 0.75);
83 glVertex3f(1, -1, -1);
91 glVertex3f(-1, -1, -1);
92 glVertex3f(-1, 1, -1);
94 glVertex3f(-1, -1, 1);
99 glVertex3f(-1, 1, -1);
100 glVertex3f( 1, 1, -1);
101 glVertex3f( 1, 1, 1);
102 glVertex3f(-1, 1, 1);
107 glVertex3f(-1, -1, -1);
108 glVertex3f( 1, -1, -1);
109 glVertex3f( 1, -1, 1);
110 glVertex3f(-1, -1, 1);
115 glVertex3f(-1, -1, 1);
116 glVertex3f( 1, -1, 1);
117 glVertex3f( 1, 1, 1);
118 glVertex3f(-1, 1, 1);
123 glVertex3f(-1, -1, -1);
124 glVertex3f( 1, -1, -1);
125 glVertex3f( 1, 1, -1);
126 glVertex3f(-1, 1, -1);
132 /* signal resize of given window */
134 resize(struct winthread
*wt
, int w
, int h
)
136 wt
->NewSize
= GL_TRUE
;
143 * We have an instance of this for each thread.
146 draw_loop(struct winthread
*wt
)
150 glXMakeCurrent(wt
->Dpy
, wt
->Win
, wt
->Context
);
152 glEnable(GL_DEPTH_TEST
);
155 GLfloat w
= (float) wt
->WinWidth
/ (float) wt
->WinHeight
;
156 glViewport(0, 0, wt
->WinWidth
, wt
->WinHeight
);
157 glMatrixMode(GL_PROJECTION
);
159 glFrustum(-w
, w
, -1.0, 1.0, 1.5, 10);
160 glMatrixMode(GL_MODELVIEW
);
162 glTranslatef(0, 0, -2.5);
163 wt
->NewSize
= GL_FALSE
;
166 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
169 glRotatef(wt
->Angle
, 0, 0, 1);
170 glRotatef(wt
->Angle
, 1, 0, 0);
171 glScalef(0.7, 0.7, 0.7);
175 glXSwapBuffers(wt
->Dpy
, wt
->Win
);
183 * The main process thread runs this loop.
186 event_loop(Display
*dpy
)
192 XNextEvent(dpy
, &event
);
193 switch (event
.type
) {
194 case ConfigureNotify
:
195 /* Find winthread for this event's window */
196 for (i
= 0; i
< NumWinThreads
; i
++) {
197 struct winthread
*wt
= &WinThreads
[i
];
198 if (event
.xconfigure
.window
== wt
->Win
) {
199 resize(wt
, event
.xconfigure
.width
,
200 event
.xconfigure
.height
);
206 /* tell all threads to exit */
208 /*printf("exit draw_loop %d\n", wt->Index);*/
218 * we'll call this once for each thread, before the threads are created.
221 create_window(struct winthread
*wt
)
225 int attrib
[] = { GLX_RGBA
,
233 XSetWindowAttributes attr
;
236 XVisualInfo
*visinfo
;
237 int width
= 80, height
= 80;
238 int xpos
= (wt
->Index
% 10) * 90;
239 int ypos
= (wt
->Index
/ 10) * 100;
241 scrnum
= DefaultScreen(wt
->Dpy
);
242 root
= RootWindow(wt
->Dpy
, scrnum
);
244 visinfo
= glXChooseVisual(wt
->Dpy
, scrnum
, attrib
);
246 Error("Unable to find RGB, Z, double-buffered visual");
249 /* window attributes */
250 attr
.background_pixel
= 0;
251 attr
.border_pixel
= 0;
252 attr
.colormap
= XCreateColormap(wt
->Dpy
, root
, visinfo
->visual
, AllocNone
);
253 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
254 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
256 win
= XCreateWindow(wt
->Dpy
, root
, xpos
, ypos
, width
, height
,
257 0, visinfo
->depth
, InputOutput
,
258 visinfo
->visual
, mask
, &attr
);
260 Error("Couldn't create window");
264 XSizeHints sizehints
;
267 sizehints
.width
= width
;
268 sizehints
.height
= height
;
269 sizehints
.flags
= USSize
| USPosition
;
270 XSetNormalHints(wt
->Dpy
, win
, &sizehints
);
271 XSetStandardProperties(wt
->Dpy
, win
, "glthreads", "glthreads",
272 None
, (char **)NULL
, 0, &sizehints
);
276 ctx
= glXCreateContext(wt
->Dpy
, visinfo
, NULL
, True
);
278 Error("Couldn't create GLX context");
281 XMapWindow(wt
->Dpy
, win
);
284 /* save the info for this window/context */
288 wt
->WinWidth
= width
;
289 wt
->WinHeight
= height
;
290 wt
->NewSize
= GL_TRUE
;
295 * Called by pthread_create()
298 thread_function(void *p
)
300 struct winthread
*wt
= (struct winthread
*) p
;
307 * called before exit to wait for all threads to finish
314 /* wait for threads to finish */
315 for (i
= 0; i
< NumWinThreads
; i
++) {
316 pthread_join(WinThreads
[i
].Thread
, NULL
);
319 for (i
= 0; i
< NumWinThreads
; i
++) {
320 glXDestroyContext(WinThreads
[i
].Dpy
, WinThreads
[i
].Context
);
321 XDestroyWindow(WinThreads
[i
].Dpy
, WinThreads
[i
].Win
);
328 main(int argc
, char *argv
[])
330 char *displayName
= ":0.0";
337 printf("threadgl: test of GL thread safety (any key = exit)\n");
339 printf(" threadgl [-display dpyName] [-n numthreads]\n");
343 for (i
= 1; i
< argc
; i
++) {
344 if (strcmp(argv
[i
], "-display") == 0 && i
+ 1 < argc
) {
345 displayName
= argv
[i
+ 1];
348 else if (strcmp(argv
[i
], "-n") == 0 && i
+ 1 < argc
) {
349 numThreads
= atoi(argv
[i
+ 1]);
352 else if (numThreads
> MAX_WINTHREADS
)
353 numThreads
= MAX_WINTHREADS
;
360 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
362 threadStat
= XInitThreads();
364 printf("XInitThreads() returned %d (success)\n", (int) threadStat
);
367 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
371 dpy
= XOpenDisplay(displayName
);
373 fprintf(stderr
, "Unable to open display %s\n", displayName
);
377 NumWinThreads
= numThreads
;
379 /* Create the GLX windows and contexts */
380 for (i
= 0; i
< numThreads
; i
++) {
381 WinThreads
[i
].Dpy
= dpy
;
382 WinThreads
[i
].Index
= i
;
383 create_window(&WinThreads
[i
]);
386 /* Create the threads */
387 for (i
= 0; i
< numThreads
; i
++) {
388 pthread_create(&WinThreads
[i
].Thread
, NULL
, thread_function
,
389 (void*) &WinThreads
[i
]);
390 printf("Created Thread %d\n", (int) WinThreads
[i
].Thread
);
409 main(int argc
, char *argv
[])
411 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
416 #endif /* PTHREADS */