Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 1 | //======================================================================== |
| 2 | // Multithreading test |
| 3 | // Copyright (c) Camilla Berglund <elmindreda@elmindreda.org> |
| 4 | // |
| 5 | // This software is provided 'as-is', without any express or implied |
| 6 | // warranty. In no event will the authors be held liable for any damages |
| 7 | // arising from the use of this software. |
| 8 | // |
| 9 | // Permission is granted to anyone to use this software for any purpose, |
| 10 | // including commercial applications, and to alter it and redistribute it |
| 11 | // freely, subject to the following restrictions: |
| 12 | // |
| 13 | // 1. The origin of this software must not be misrepresented; you must not |
| 14 | // claim that you wrote the original software. If you use this software |
| 15 | // in a product, an acknowledgment in the product documentation would |
| 16 | // be appreciated but is not required. |
| 17 | // |
| 18 | // 2. Altered source versions must be plainly marked as such, and must not |
| 19 | // be misrepresented as being the original software. |
| 20 | // |
| 21 | // 3. This notice may not be removed or altered from any source |
| 22 | // distribution. |
| 23 | // |
| 24 | //======================================================================== |
| 25 | // |
| 26 | // This test is intended to verify whether the OpenGL context part of |
| 27 | // the GLFW API is able to be used from multiple threads |
| 28 | // |
| 29 | //======================================================================== |
| 30 | |
Camilla Berglund | 6338395 | 2012-09-13 00:05:54 +0200 | [diff] [blame] | 31 | #include "tinycthread.h" |
| 32 | |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 33 | #include <GL/glfw3.h> |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <math.h> |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 38 | #include <assert.h> |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 39 | |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 40 | typedef struct |
| 41 | { |
| 42 | GLFWwindow window; |
| 43 | const char* title; |
| 44 | float r, g, b; |
Camilla Berglund | 12e0087 | 2012-08-15 20:26:09 +0200 | [diff] [blame] | 45 | thrd_t id; |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 46 | } Thread; |
| 47 | |
Camilla Berglund | c594bb4 | 2012-08-12 22:36:10 +0200 | [diff] [blame] | 48 | static volatile GLboolean running = GL_TRUE; |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 49 | |
Camilla Berglund | 12e0087 | 2012-08-15 20:26:09 +0200 | [diff] [blame] | 50 | static int thread_main(void* data) |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 51 | { |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 52 | const Thread* thread = (const Thread*) data; |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 53 | |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 54 | glfwMakeContextCurrent(thread->window); |
| 55 | assert(glfwGetCurrentContext() == thread->window); |
| 56 | |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 57 | glfwSwapInterval(1); |
| 58 | |
| 59 | while (running) |
| 60 | { |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 61 | const float v = (float) fabs(sin(glfwGetTime() * 2.f)); |
| 62 | glClearColor(thread->r * v, thread->g * v, thread->b * v, 0.f); |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 63 | |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 64 | glClear(GL_COLOR_BUFFER_BIT); |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 65 | glfwSwapBuffers(thread->window); |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | glfwMakeContextCurrent(NULL); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int main(void) |
| 73 | { |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 74 | int i, result; |
| 75 | Thread threads[] = |
| 76 | { |
| 77 | { NULL, "Red", 1.f, 0.f, 0.f, 0 }, |
| 78 | { NULL, "Green", 0.f, 1.f, 0.f, 0 }, |
| 79 | { NULL, "Blue", 0.f, 0.f, 1.f, 0 } |
| 80 | }; |
| 81 | const int count = sizeof(threads) / sizeof(Thread); |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 82 | |
| 83 | if (!glfwInit()) |
| 84 | { |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 85 | fprintf(stderr, "Failed to initialize GLFW: %s\n", |
| 86 | glfwErrorString(glfwGetError())); |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 87 | exit(EXIT_FAILURE); |
| 88 | } |
| 89 | |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 90 | for (i = 0; i < count; i++) |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 91 | { |
m@bitsnbites.eu | c9f4ded | 2012-10-28 00:23:28 +0200 | [diff] [blame] | 92 | glfwWindowHint(GLFW_POSITION_X, 200 + 250 * i); |
| 93 | glfwWindowHint(GLFW_POSITION_Y, 200); |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 94 | threads[i].window = glfwCreateWindow(200, 200, |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 95 | threads[i].title, |
Camilla Berglund | 1be1636 | 2012-09-27 21:37:36 +0200 | [diff] [blame] | 96 | NULL, NULL); |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 97 | if (!threads[i].window) |
| 98 | { |
| 99 | fprintf(stderr, "Failed to open GLFW window: %s\n", |
| 100 | glfwErrorString(glfwGetError())); |
| 101 | exit(EXIT_FAILURE); |
| 102 | } |
| 103 | |
Camilla Berglund | 12e0087 | 2012-08-15 20:26:09 +0200 | [diff] [blame] | 104 | if (thrd_create(&threads[i].id, thread_main, threads + i) != |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 105 | thrd_success) |
| 106 | { |
| 107 | fprintf(stderr, "Failed to create secondary thread\n"); |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 112 | while (running) |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 113 | { |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 114 | assert(glfwGetCurrentContext() == NULL); |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 115 | |
Camilla Berglund | 4057885 | 2012-08-12 16:32:54 +0200 | [diff] [blame] | 116 | glfwWaitEvents(); |
| 117 | |
Camilla Berglund | 86bcfb3 | 2012-08-13 19:37:39 +0200 | [diff] [blame] | 118 | for (i = 0; i < count; i++) |
| 119 | { |
| 120 | if (glfwGetWindowParam(threads[i].window, GLFW_CLOSE_REQUESTED)) |
| 121 | running = GL_FALSE; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | for (i = 0; i < count; i++) |
Camilla Berglund | 12e0087 | 2012-08-15 20:26:09 +0200 | [diff] [blame] | 126 | thrd_join(threads[i].id, &result); |
Camilla Berglund | 4057885 | 2012-08-12 16:32:54 +0200 | [diff] [blame] | 127 | |
Camilla Berglund | 5b95715 | 2012-08-12 15:05:18 +0200 | [diff] [blame] | 128 | exit(EXIT_SUCCESS); |
| 129 | } |
| 130 | |