glx-multithread-clearbuffer.c 2.92 KB
Newer Older
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
/*
 * Copyright (c) 2017 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * Create multiple GLX contexts and concurrently create, clear, and destroy
 * buffers and flush the context.
 *
 * This reproduces a deadlock with the radeonsi command submission thread
 * queue.
 */

#include "piglit-util-gl.h"
#include "piglit-glx-util.h"
#include "pthread.h"

static pthread_mutex_t mutex;
static bool dispatch_ready = false;

static void *
thread_func(void *arg)
{
	Display *dpy;
	XVisualInfo *visinfo;
	Window win;
	unsigned i;

	dpy = piglit_get_glx_display();
	visinfo = piglit_get_glx_visual(dpy);
	win = piglit_get_glx_window(dpy, visinfo);

	GLXContext ctx;

	ctx = piglit_get_glx_context(dpy, visinfo);
	glXMakeCurrent(dpy, win, ctx);

	pthread_mutex_lock(&mutex);
	if (!dispatch_ready) {
		piglit_dispatch_default_init(PIGLIT_DISPATCH_GL);
		dispatch_ready = true;
		piglit_require_gl_version(30);
		piglit_require_extension("GL_ARB_clear_buffer_object");
	}
	pthread_mutex_unlock(&mutex);

	for (i = 0; i < 1000; ++i) {
		GLuint buf;
		glGenBuffers(1, &buf);
		glBindBuffer(GL_ARRAY_BUFFER, buf);
		glBufferData(GL_ARRAY_BUFFER, 512, NULL, GL_STATIC_DRAW);
		glClearBufferSubData(GL_ARRAY_BUFFER, GL_R32UI, 0, 512, GL_RED_INTEGER, GL_UNSIGNED_INT, &buf);
		glDeleteBuffers(1, &buf);
		glFlush();
		piglit_check_gl_error(0);
	}

	glXDestroyContext(dpy, ctx);
	return NULL;
}

int
main(int argc, char **argv)
{
	/* Need at least 16 contexts to congest the thread queue. */
	pthread_t thread[16];
85
	bool pass = true;
86 87 88 89 90 91 92 93

	XInitThreads();

	pthread_mutex_init(&mutex, NULL);

	for (int i = 0; i < ARRAY_SIZE(thread); i++)
		pthread_create(&thread[i], NULL, thread_func, NULL);

94 95 96 97
	for (int i = 0; i < ARRAY_SIZE(thread); i++) {
		if (pthread_join(thread[i], NULL) != 0)
			pass = false;
	}
98 99 100

	pthread_mutex_destroy(&mutex);

101
	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
102 103
	return 0;
}