2 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include "anv_private.h"
30 #define NUM_THREADS 16
31 #define STATES_PER_THREAD 1024
37 struct anv_state_pool
*pool
;
38 uint32_t offsets
[STATES_PER_THREAD
];
41 pthread_barrier_t barrier
;
43 static void *alloc_states(void *_job
)
45 struct job
*job
= _job
;
47 pthread_barrier_wait(&barrier
);
49 for (unsigned i
= 0; i
< STATES_PER_THREAD
; i
++) {
50 struct anv_state state
= anv_state_pool_alloc(job
->pool
, 16, 16);
51 job
->offsets
[i
] = state
.offset
;
57 static void run_test()
59 struct anv_physical_device physical_device
= { };
60 struct anv_device device
= {
61 .physical
= &physical_device
,
63 struct anv_state_pool state_pool
;
65 pthread_mutex_init(&device
.mutex
, NULL
);
66 anv_bo_cache_init(&device
.bo_cache
);
67 anv_state_pool_init(&state_pool
, &device
, 4096, 0, 64);
69 pthread_barrier_init(&barrier
, NULL
, NUM_THREADS
);
71 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
72 jobs
[i
].pool
= &state_pool
;
74 pthread_create(&jobs
[i
].thread
, NULL
, alloc_states
, &jobs
[i
]);
77 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
78 pthread_join(jobs
[i
].thread
, NULL
);
80 /* A list of indices, one per thread */
81 unsigned next
[NUM_THREADS
];
82 memset(next
, 0, sizeof(next
));
86 /* First, we find which thread has the highest next element */
88 int max_thread_idx
= -1;
89 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
90 if (next
[i
] >= STATES_PER_THREAD
)
93 if (thread_max
< jobs
[i
].offsets
[next
[i
]]) {
94 thread_max
= jobs
[i
].offsets
[next
[i
]];
99 /* The only way this can happen is if all of the next[] values are at
100 * BLOCKS_PER_THREAD, in which case, we're done.
102 if (thread_max
== -1)
105 /* That next element had better be higher than the previous highest */
106 assert(jobs
[max_thread_idx
].offsets
[next
[max_thread_idx
]] > highest
);
108 highest
= jobs
[max_thread_idx
].offsets
[next
[max_thread_idx
]];
109 next
[max_thread_idx
]++;
112 anv_state_pool_finish(&state_pool
);
113 pthread_mutex_destroy(&device
.mutex
);
116 int main(int argc
, char **argv
)
118 for (unsigned i
= 0; i
< NUM_RUNS
; i
++)