86d1a76151f6c648d280336effaeea2b49ce0f42
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
26 #include "anv_private.h"
28 #define NUM_THREADS 16
29 #define BLOCKS_PER_THREAD 1024
35 struct anv_block_pool
*pool
;
36 uint32_t blocks
[BLOCKS_PER_THREAD
];
37 uint32_t back_blocks
[BLOCKS_PER_THREAD
];
41 static void *alloc_blocks(void *_job
)
43 struct job
*job
= _job
;
46 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
47 block
= anv_block_pool_alloc(job
->pool
);
48 data
= job
->pool
->map
+ block
;
51 job
->blocks
[i
] = block
;
53 block
= anv_block_pool_alloc_back(job
->pool
);
54 data
= job
->pool
->map
+ block
;
57 job
->back_blocks
[i
] = -block
;
60 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
61 block
= job
->blocks
[i
];
62 data
= job
->pool
->map
+ block
;
63 assert(*data
== block
);
65 block
= -job
->back_blocks
[i
];
66 data
= job
->pool
->map
+ block
;
67 assert(*data
== block
);
73 static void validate_monotonic(uint32_t **blocks
)
75 /* A list of indices, one per thread */
76 unsigned next
[NUM_THREADS
];
77 memset(next
, 0, sizeof(next
));
81 /* First, we find which thread has the highest next element */
83 int max_thread_idx
= -1;
84 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
85 if (next
[i
] >= BLOCKS_PER_THREAD
)
88 if (thread_max
< blocks
[i
][next
[i
]]) {
89 thread_max
= blocks
[i
][next
[i
]];
94 /* The only way this can happen is if all of the next[] values are at
95 * BLOCKS_PER_THREAD, in which case, we're done.
100 /* That next element had better be higher than the previous highest */
101 assert(blocks
[max_thread_idx
][next
[max_thread_idx
]] > highest
);
103 highest
= blocks
[max_thread_idx
][next
[max_thread_idx
]];
104 next
[max_thread_idx
]++;
108 static void run_test()
110 struct anv_device device
;
111 struct anv_block_pool pool
;
113 pthread_mutex_init(&device
.mutex
, NULL
);
114 anv_block_pool_init(&pool
, &device
, 16);
116 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
117 jobs
[i
].pool
= &pool
;
119 pthread_create(&jobs
[i
].thread
, NULL
, alloc_blocks
, &jobs
[i
]);
122 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
123 pthread_join(jobs
[i
].thread
, NULL
);
125 /* Validate that the block allocations were monotonic */
126 uint32_t *block_ptrs
[NUM_THREADS
];
127 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
128 block_ptrs
[i
] = jobs
[i
].blocks
;
129 validate_monotonic(block_ptrs
);
131 /* Validate that the back block allocations were monotonic */
132 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
133 block_ptrs
[i
] = jobs
[i
].back_blocks
;
134 validate_monotonic(block_ptrs
);
136 anv_block_pool_finish(&pool
);
137 pthread_mutex_destroy(&device
.mutex
);
140 int main(int argc
, char **argv
)
142 for (unsigned i
= 0; i
< NUM_RUNS
; i
++)