1 /* $Id: glthreads.c,v 1.1 2000/07/20 20:12:17 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 */
46 * Each window/thread/context:
55 int WinWidth
, WinHeight
;
60 #define MAX_WINTHREADS 100
61 static struct winthread WinThreads
[MAX_WINTHREADS
];
62 static int NumWinThreads
= 0;
63 static GLboolean ExitFlag
= GL_FALSE
;
68 Error(const char *msg
)
70 fprintf(stderr
, "Error: %s\n", msg
);
75 /* draw a colored cube */
80 glScalef(0.75, 0.75, 0.75);
84 glVertex3f(1, -1, -1);
92 glVertex3f(-1, -1, -1);
93 glVertex3f(-1, 1, -1);
95 glVertex3f(-1, -1, 1);
100 glVertex3f(-1, 1, -1);
101 glVertex3f( 1, 1, -1);
102 glVertex3f( 1, 1, 1);
103 glVertex3f(-1, 1, 1);
108 glVertex3f(-1, -1, -1);
109 glVertex3f( 1, -1, -1);
110 glVertex3f( 1, -1, 1);
111 glVertex3f(-1, -1, 1);
116 glVertex3f(-1, -1, 1);
117 glVertex3f( 1, -1, 1);
118 glVertex3f( 1, 1, 1);
119 glVertex3f(-1, 1, 1);
124 glVertex3f(-1, -1, -1);
125 glVertex3f( 1, -1, -1);
126 glVertex3f( 1, 1, -1);
127 glVertex3f(-1, 1, -1);
133 /* signal resize of given window */
135 resize(struct winthread
*wt
, int w
, int h
)
137 wt
->NewSize
= GL_TRUE
;
144 * We have an instance of this for each thread.
147 draw_loop(struct winthread
*wt
)
151 glXMakeCurrent(wt
->Dpy
, wt
->Win
, wt
->Context
);
153 glEnable(GL_DEPTH_TEST
);
156 GLfloat w
= (float) wt
->WinWidth
/ (float) wt
->WinHeight
;
157 glViewport(0, 0, wt
->WinWidth
, wt
->WinHeight
);
158 glMatrixMode(GL_PROJECTION
);
160 glFrustum(-w
, w
, -1.0, 1.0, 1.5, 10);
161 glMatrixMode(GL_MODELVIEW
);
163 glTranslatef(0, 0, -2.5);
164 wt
->NewSize
= GL_FALSE
;
167 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
170 glRotatef(wt
->Angle
, 0, 0, 1);
171 glRotatef(wt
->Angle
, 1, 0, 0);
172 glScalef(0.7, 0.7, 0.7);
176 glXSwapBuffers(wt
->Dpy
, wt
->Win
);
184 * The main process thread runs this loop.
187 event_loop(Display
*dpy
)
190 static long mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
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
);
204 /* tell all threads to exit */
206 /*printf("exit draw_loop %d\n", wt->Index);*/
213 printf("window mismatch\n");
222 * we'll call this once for each thread, before the threads are created.
225 create_window(struct winthread
*wt
)
229 int attrib
[] = { GLX_RGBA
,
237 XSetWindowAttributes attr
;
240 XVisualInfo
*visinfo
;
241 int width
= 80, height
= 80;
242 int xpos
= (wt
->Index
% 10) * 90;
243 int ypos
= (wt
->Index
/ 10) * 100;
245 scrnum
= DefaultScreen(wt
->Dpy
);
246 root
= RootWindow(wt
->Dpy
, scrnum
);
248 visinfo
= glXChooseVisual(wt
->Dpy
, scrnum
, attrib
);
250 Error("Unable to find RGB, Z, double-buffered visual");
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
;
260 win
= XCreateWindow(wt
->Dpy
, root
, xpos
, ypos
, width
, height
,
261 0, visinfo
->depth
, InputOutput
,
262 visinfo
->visual
, mask
, &attr
);
264 Error("Couldn't create window");
268 XSizeHints sizehints
;
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
);
280 ctx
= glXCreateContext(wt
->Dpy
, visinfo
, NULL
, True
);
282 Error("Couldn't create GLX context");
285 XMapWindow(wt
->Dpy
, win
);
288 /* save the info for this window/context */
292 wt
->WinWidth
= width
;
293 wt
->WinHeight
= height
;
294 wt
->NewSize
= GL_TRUE
;
299 * Called by pthread_create()
302 thread_function(void *p
)
304 struct winthread
*wt
= (struct winthread
*) p
;
311 * called before exit to wait for all threads to finish
318 /* wait for threads to finish */
319 for (i
= 0; i
< NumWinThreads
; i
++) {
320 pthread_join(WinThreads
[i
].Thread
, NULL
);
323 for (i
= 0; i
< NumWinThreads
; i
++) {
324 glXDestroyContext(WinThreads
[i
].Dpy
, WinThreads
[i
].Context
);
325 XDestroyWindow(WinThreads
[i
].Dpy
, WinThreads
[i
].Win
);
332 main(int argc
, char *argv
[])
334 char *displayName
= ":0.0";
341 printf("threadgl: test of GL thread safety (any key = exit)\n");
343 printf(" threadgl [-display dpyName] [-n numthreads]\n");
347 for (i
= 1; i
< argc
; i
++) {
348 if (strcmp(argv
[i
], "-display") == 0 && i
+ 1 < argc
) {
349 displayName
= argv
[i
+ 1];
352 else if (strcmp(argv
[i
], "-n") == 0 && i
+ 1 < argc
) {
353 numThreads
= atoi(argv
[i
+ 1]);
356 else if (numThreads
> MAX_WINTHREADS
)
357 numThreads
= MAX_WINTHREADS
;
364 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
366 threadStat
= XInitThreads();
368 printf("XInitThreads() returned %d (success)\n", (int) threadStat
);
371 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
375 dpy
= XOpenDisplay(displayName
);
377 fprintf(stderr
, "Unable to open display %s\n", displayName
);
381 NumWinThreads
= numThreads
;
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
]);
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
);
413 main(int argc
, char *argv
[])
415 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
420 #endif /* PTHREADS */