File: test3.c

package info (click to toggle)
quesoglc 0.7.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,192 kB
  • sloc: ansic: 14,620; sh: 9,718; makefile: 283; python: 50
file content (146 lines) | stat: -rw-r--r-- 4,220 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* QuesoGLC
 * A free implementation of the OpenGL Character Renderer (GLC)
 * Copyright (c) 2002, 2004-2007, Bertrand Coconnier
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* $Id: test3.c 679 2007-12-20 21:42:13Z bcoconni $ */

/** \file
 * The purpose of this test is to check that a context can be bound to only
 * one thread at a time. This test checks that if a context is current to
 * a thread other than the issuing thread then a GLC_STATE_ERROR is raised.
 * Pending deletion of contexts is also tested.
 */

#include "GL/glc.h"
#include <pthread.h>
#include <stdio.h>
#if defined __APPLE__ && defined __MACH__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

GLint ctx;
int magic = 0xdeadbeef;

void* da_thread(void *arg)
{
  GLCenum err;

  glutCreateWindow("test3.1");

  glcContext(ctx);
  err = glcGetError();
  if (err != GLC_STATE_ERROR) {
    printf("Thread 2 : Unexpected error : 0x%X\n", (int)err);
    return &magic;
  }

  /* Ask GLC to delete this context as soon as it is not current any thread */
  glcDeleteContext(ctx);
  err = glcGetError();
  if (err) {
    printf("Thread 2 : Unexpected error : 0x%X\n", (int)err);
    return &magic;
  }
  /* Check that the context has not been deleted yet. */
  if (!glcIsContext(ctx)) {
    printf("Thread 2 : Unexpected deletion of context %d\n", (int)ctx);
    return &magic;
  }

  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t thread;
  GLCenum err;
  void *return_value = NULL;
  GLint ctx2;

  /* Needed to initialize an OpenGL context */
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutCreateWindow("test3");

  ctx2 = glcGenContext();
  ctx = glcGenContext();
  glcContext(ctx);
  err = glcGetError();
  if (err) {
    printf("Main Thread : Unexpected error : 0x%X\n", (int)err);
    return -1;
  }

  if (pthread_create(&thread, NULL, da_thread, NULL)) {
    printf("Main Thread : Failed to create pthread\n");
    return -1;
  }

  if (pthread_join(thread, &return_value)) {
    printf("Main Thread : Failed to join Thread 2\n");
    return -1;
  }

  if (return_value) {
    printf("Main Thread : An error occured in Thread 2\n");
    return -1;
  }

  /* Check that although the thread has requested a deletion of the context,
   * the context 'ctx' still exists.
   */
  if (!glcIsContext(ctx)) {
    printf("Main Thread : Unexpected deletion of context 'ctx'\n");
    return -1;
  }

  /* Change the current context. The pending deletion should be executed
   * at this step.
   */
  glcContext(ctx2);
  /* Verify that the context has been deleted */
  if (glcIsContext(ctx)) {
    printf("Main Thread : Pending deletion of context 'ctx' has not been executed\n");
    return -1;
  }

  /* Same as above (pending deletion of a context) but a little different :
   * - The deletion is now requested in the thread that owns the context
   * - glcContext(0) is called which means that the current context will be
   *   released and no other context will be made current (hence a different
   *   branch of code will be executed in glcContext().
   */
  glcDeleteContext(ctx2);
  err = glcGetError();
  if (err) {
    printf("Main Thread : Unexpected GLC error 0x%x\n", (int)err);
    return -1;
  }

  /* Release the current context */
  glcContext(0);
  /* Check that 'ctx2' has been deleted */
  if (glcIsContext(ctx2)) {
    printf("Main Thread : Pending deletion of context 'ctx2' has not been executed\n");
    return -1;
  }

  printf("Test successful!\n");
  return 0;
}