Merge branch 'nir-spirv' into vulkan
[mesa.git] / src / vulkan / tests / block_pool_no_free.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <pthread.h>
25
26 #include "anv_private.h"
27
28 #define NUM_THREADS 16
29 #define BLOCKS_PER_THREAD 1024
30 #define NUM_RUNS 64
31
32 struct job {
33 pthread_t thread;
34 unsigned id;
35 struct anv_block_pool *pool;
36 uint32_t blocks[BLOCKS_PER_THREAD];
37 } jobs[NUM_THREADS];
38
39 static void *alloc_blocks(void *_job)
40 {
41 struct job *job = _job;
42
43 for (unsigned i = 0; i < BLOCKS_PER_THREAD; i++)
44 job->blocks[i] = anv_block_pool_alloc(job->pool);
45
46 return NULL;
47 }
48
49 static void run_test()
50 {
51 struct anv_device device;
52 struct anv_block_pool pool;
53
54 anv_block_pool_init(&pool, &device, 16);
55
56 for (unsigned i = 0; i < NUM_THREADS; i++) {
57 jobs[i].pool = &pool;
58 jobs[i].id = i;
59 pthread_create(&jobs[i].thread, NULL, alloc_blocks, &jobs[i]);
60 }
61
62 for (unsigned i = 0; i < NUM_THREADS; i++)
63 pthread_join(jobs[i].thread, NULL);
64
65 /* A list of indices, one per thread */
66 unsigned next[NUM_THREADS];
67 memset(next, 0, sizeof(next));
68
69 int highest = -1;
70 while (true) {
71 /* First, we find which thread has the highest next element */
72 int thread_max = -1;
73 int max_thread_idx = -1;
74 for (unsigned i = 0; i < NUM_THREADS; i++) {
75 if (next[i] >= BLOCKS_PER_THREAD)
76 continue;
77
78 if (thread_max < jobs[i].blocks[next[i]]) {
79 thread_max = jobs[i].blocks[next[i]];
80 max_thread_idx = i;
81 }
82 }
83
84 /* The only way this can happen is if all of the next[] values are at
85 * BLOCKS_PER_THREAD, in which case, we're done.
86 */
87 if (thread_max == -1)
88 break;
89
90 /* That next element had better be higher than the previous highest */
91 assert(jobs[max_thread_idx].blocks[next[max_thread_idx]] > highest);
92
93 highest = jobs[max_thread_idx].blocks[next[max_thread_idx]];
94 next[max_thread_idx]++;
95 }
96
97 anv_block_pool_finish(&pool);
98 }
99
100 int main(int argc, char **argv)
101 {
102 for (unsigned i = 0; i < NUM_RUNS; i++)
103 run_test();
104 }