1 /* $Id: glthreads.c,v 1.2 2002/03/08 19:44:28 brianp Exp $ */
4 * Copyright (C) 2000 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * This program tests GLX thread safety.
27 * Command line options:
28 * -n <num threads> Number of threads to create (default is 2)
29 * -display <display name> Specify X display (default is :0.0)
31 * Brian Paul 20 July 2000
35 #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 GLboolean ExitFlag
= GL_FALSE
;
69 Error(const char *msg
)
71 fprintf(stderr
, "Error: %s\n", msg
);
76 /* draw a colored cube */
81 glScalef(0.75, 0.75, 0.75);
85 glVertex3f(1, -1, -1);
93 glVertex3f(-1, -1, -1);
94 glVertex3f(-1, 1, -1);
96 glVertex3f(-1, -1, 1);
101 glVertex3f(-1, 1, -1);
102 glVertex3f( 1, 1, -1);
103 glVertex3f( 1, 1, 1);
104 glVertex3f(-1, 1, 1);
109 glVertex3f(-1, -1, -1);
110 glVertex3f( 1, -1, -1);
111 glVertex3f( 1, -1, 1);
112 glVertex3f(-1, -1, 1);
117 glVertex3f(-1, -1, 1);
118 glVertex3f( 1, -1, 1);
119 glVertex3f( 1, 1, 1);
120 glVertex3f(-1, 1, 1);
125 glVertex3f(-1, -1, -1);
126 glVertex3f( 1, -1, -1);
127 glVertex3f( 1, 1, -1);
128 glVertex3f(-1, 1, -1);
134 /* signal resize of given window */
136 resize(struct winthread
*wt
, int w
, int h
)
138 wt
->NewSize
= GL_TRUE
;
145 * We have an instance of this for each thread.
148 draw_loop(struct winthread
*wt
)
152 glXMakeCurrent(wt
->Dpy
, wt
->Win
, wt
->Context
);
154 glEnable(GL_DEPTH_TEST
);
157 GLfloat w
= (float) wt
->WinWidth
/ (float) wt
->WinHeight
;
158 glViewport(0, 0, wt
->WinWidth
, wt
->WinHeight
);
159 glMatrixMode(GL_PROJECTION
);
161 glFrustum(-w
, w
, -1.0, 1.0, 1.5, 10);
162 glMatrixMode(GL_MODELVIEW
);
164 glTranslatef(0, 0, -2.5);
165 wt
->NewSize
= GL_FALSE
;
168 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
171 glRotatef(wt
->Angle
, 0, 0, 1);
172 glRotatef(wt
->Angle
, 1, 0, 0);
173 glScalef(0.7, 0.7, 0.7);
177 glXSwapBuffers(wt
->Dpy
, wt
->Win
);
185 * The main process thread runs this loop.
188 event_loop(Display
*dpy
)
191 static long mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
195 for (i
= 0; i
< NumWinThreads
; i
++) {
196 struct winthread
*wt
= &WinThreads
[i
];
197 while (XCheckWindowEvent(dpy
, wt
->Win
, mask
, &event
)) {
198 if (event
.xany
.window
== wt
->Win
) {
199 switch (event
.type
) {
200 case ConfigureNotify
:
201 resize(wt
, event
.xconfigure
.width
,
202 event
.xconfigure
.height
);
205 /* tell all threads to exit */
207 /*printf("exit draw_loop %d\n", wt->Index);*/
214 printf("window mismatch\n");
223 * we'll call this once for each thread, before the threads are created.
226 create_window(struct winthread
*wt
)
230 int attrib
[] = { GLX_RGBA
,
238 XSetWindowAttributes attr
;
241 XVisualInfo
*visinfo
;
242 int width
= 80, height
= 80;
243 int xpos
= (wt
->Index
% 10) * 90;
244 int ypos
= (wt
->Index
/ 10) * 100;
246 scrnum
= DefaultScreen(wt
->Dpy
);
247 root
= RootWindow(wt
->Dpy
, scrnum
);
249 visinfo
= glXChooseVisual(wt
->Dpy
, scrnum
, attrib
);
251 Error("Unable to find RGB, Z, double-buffered visual");
254 /* window attributes */
255 attr
.background_pixel
= 0;
256 attr
.border_pixel
= 0;
257 attr
.colormap
= XCreateColormap(wt
->Dpy
, root
, visinfo
->visual
, AllocNone
);
258 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
259 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
261 win
= XCreateWindow(wt
->Dpy
, root
, xpos
, ypos
, width
, height
,
262 0, visinfo
->depth
, InputOutput
,
263 visinfo
->visual
, mask
, &attr
);
265 Error("Couldn't create window");
269 XSizeHints sizehints
;
272 sizehints
.width
= width
;
273 sizehints
.height
= height
;
274 sizehints
.flags
= USSize
| USPosition
;
275 XSetNormalHints(wt
->Dpy
, win
, &sizehints
);
276 XSetStandardProperties(wt
->Dpy
, win
, "glthreads", "glthreads",
277 None
, (char **)NULL
, 0, &sizehints
);
281 ctx
= glXCreateContext(wt
->Dpy
, visinfo
, NULL
, True
);
283 Error("Couldn't create GLX context");
286 XMapWindow(wt
->Dpy
, win
);
289 /* save the info for this window/context */
293 wt
->WinWidth
= width
;
294 wt
->WinHeight
= height
;
295 wt
->NewSize
= GL_TRUE
;
300 * Called by pthread_create()
303 thread_function(void *p
)
305 struct winthread
*wt
= (struct winthread
*) p
;
312 * called before exit to wait for all threads to finish
319 /* wait for threads to finish */
320 for (i
= 0; i
< NumWinThreads
; i
++) {
321 pthread_join(WinThreads
[i
].Thread
, NULL
);
324 for (i
= 0; i
< NumWinThreads
; i
++) {
325 glXDestroyContext(WinThreads
[i
].Dpy
, WinThreads
[i
].Context
);
326 XDestroyWindow(WinThreads
[i
].Dpy
, WinThreads
[i
].Win
);
333 main(int argc
, char *argv
[])
335 char *displayName
= ":0.0";
342 printf("threadgl: test of GL thread safety (any key = exit)\n");
344 printf(" threadgl [-display dpyName] [-n numthreads]\n");
348 for (i
= 1; i
< argc
; i
++) {
349 if (strcmp(argv
[i
], "-display") == 0 && i
+ 1 < argc
) {
350 displayName
= argv
[i
+ 1];
353 else if (strcmp(argv
[i
], "-n") == 0 && i
+ 1 < argc
) {
354 numThreads
= atoi(argv
[i
+ 1]);
357 else if (numThreads
> MAX_WINTHREADS
)
358 numThreads
= MAX_WINTHREADS
;
365 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
367 threadStat
= XInitThreads();
369 printf("XInitThreads() returned %d (success)\n", (int) threadStat
);
372 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
376 dpy
= XOpenDisplay(displayName
);
378 fprintf(stderr
, "Unable to open display %s\n", displayName
);
382 NumWinThreads
= numThreads
;
384 /* Create the GLX windows and contexts */
385 for (i
= 0; i
< numThreads
; i
++) {
386 WinThreads
[i
].Dpy
= dpy
;
387 WinThreads
[i
].Index
= i
;
388 create_window(&WinThreads
[i
]);
391 /* Create the threads */
392 for (i
= 0; i
< numThreads
; i
++) {
393 pthread_create(&WinThreads
[i
].Thread
, NULL
, thread_function
,
394 (void*) &WinThreads
[i
]);
395 printf("Created Thread %p\n", WinThreads
[i
].Thread
);
414 main(int argc
, char *argv
[])
416 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
421 #endif /* PTHREADS */