Path: blob/21.2-virgl/src/gallium/tests/unit/pipe_barrier_test.c
4565 views
/**************************************************************************1*2* Copyright 2009-2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728/*29* Test case for util_barrier.30*31* The test succeeds if no thread exits before all the other threads reach32* the barrier.33*/343536#include <stdio.h>37#include <stdlib.h>38#include <string.h>3940#include "util/os_time.h"41#include "util/u_atomic.h"42#include "util/u_thread.h"434445#define NUM_THREADS 104647static int verbosity = 0;4849static thrd_t threads[NUM_THREADS];50static util_barrier barrier;51static int thread_ids[NUM_THREADS];5253static volatile int waiting = 0;54static volatile int proceeded = 0;555657#define LOG(fmt, ...) \58if (verbosity > 0) { \59fprintf(stdout, fmt, ##__VA_ARGS__); \60}6162#define CHECK(_cond) \63if (!(_cond)) { \64fprintf(stderr, "%s:%u: `%s` failed\n", __FILE__, __LINE__, #_cond); \65_exit(EXIT_FAILURE); \66}676869static int70thread_function(void *thread_data)71{72int thread_id = *((int *) thread_data);7374LOG("thread %d starting\n", thread_id);75os_time_sleep(thread_id * 100 * 1000);76LOG("thread %d before barrier\n", thread_id);7778CHECK(p_atomic_read(&proceeded) == 0);79p_atomic_inc(&waiting);8081util_barrier_wait(&barrier);8283CHECK(p_atomic_read(&waiting) == NUM_THREADS);8485p_atomic_inc(&proceeded);8687LOG("thread %d exiting\n", thread_id);8889return 0;90}919293int main(int argc, char *argv[])94{95int i;9697for (i = 1; i < argc; ++i) {98const char *arg = argv[i];99if (strcmp(arg, "-v") == 0) {100++verbosity;101} else {102fprintf(stderr, "error: unrecognized option `%s`\n", arg);103exit(EXIT_FAILURE);104}105}106107// Disable buffering108setbuf(stdout, NULL);109110LOG("pipe_barrier_test starting\n");111112util_barrier_init(&barrier, NUM_THREADS);113114for (i = 0; i < NUM_THREADS; i++) {115thread_ids[i] = i;116threads[i] = u_thread_create(thread_function, (void *) &thread_ids[i]);117}118119for (i = 0; i < NUM_THREADS; i++ ) {120thrd_join(threads[i], NULL);121}122123CHECK(p_atomic_read(&proceeded) == NUM_THREADS);124125util_barrier_destroy(&barrier);126127LOG("pipe_barrier_test exiting\n");128129return 0;130}131132133