diff options
author | Mattias Andrée <maandree@kth.se> | 2020-05-11 19:41:16 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2020-05-11 19:41:16 +0200 |
commit | 80bcfd86b16b990cac7c172d4f55740b27a1510b (patch) | |
tree | fbad87ac55543fc5f5563983b516325a1f9e07c6 /libaxl_generate_id.c | |
parent | m (diff) | |
download | libaxl-80bcfd86b16b990cac7c172d4f55740b27a1510b.tar.gz libaxl-80bcfd86b16b990cac7c172d4f55740b27a1510b.tar.bz2 libaxl-80bcfd86b16b990cac7c172d4f55740b27a1510b.tar.xz |
Documentation, use of liberror, and implement libaxl_deallocate_id
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libaxl_generate_id.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/libaxl_generate_id.c b/libaxl_generate_id.c index 232ef4d..91b5aa6 100644 --- a/libaxl_generate_id.c +++ b/libaxl_generate_id.c @@ -5,8 +5,34 @@ libaxl_id_t libaxl_generate_id(LIBAXL_CONTEXT *ctx) { LIBAXL_CONNECTION *conn = ctx->conn; + struct id_pool *pool = NULL, *next, *last; uint32_t id; + pool = atomic_exchange(&conn->xid_pool, pool); + if (pool) { + id = pool->first; + pool->first += (uint32_t)1 << conn->xid_shift; + if (id == pool->last) { + next = pool->next; + if (next) { + free(pool); + pool = next; + } + next = NULL; + if (atomic_compare_exchange_strong(&conn->xid_pool, &next, pool)) + return id; + if (!pool->next) { + free(pool); + return id; + } + } + while ((pool = atomic_exchange(&conn->xid_pool, pool))) { + for (last = pool; last->next; last = last->next); + last->next = atomic_exchange(&conn->xid_pool, NULL); + } + return id; + } + id = atomic_fetch_add(&conn->xid_last, 1); if (id <= conn->xid_max) return (id << conn->xid_shift) | conn->xid_base; |