1 // SPDX-License-Identifier: GPL-2.0
2 #include "sharded_mutex.h"
6 struct sharded_mutex *sharded_mutex__new(size_t num_shards)
8 struct sharded_mutex *result;
12 for (bits = 0; ((size_t)1 << bits) < num_shards; bits++)
15 size = sizeof(*result) + sizeof(struct mutex) * (1 << bits);
16 result = malloc(size);
20 result->cap_bits = bits;
21 for (size_t i = 0; i < ((size_t)1 << bits); i++)
22 mutex_init(&result->mutexes[i]);
27 void sharded_mutex__delete(struct sharded_mutex *sm)
29 for (size_t i = 0; i < ((size_t)1 << sm->cap_bits); i++)
30 mutex_destroy(&sm->mutexes[i]);