From cc23568629d12c3eae7e4064773cf0b86cc54311 Mon Sep 17 00:00:00 2001 From: Bahadir Balban Date: Sat, 9 Feb 2008 14:30:03 +0000 Subject: [PATCH] Added locking to id pool allocation and free. --- include/l4/lib/idpool.h | 2 ++ src/lib/idpool.c | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/l4/lib/idpool.h b/include/l4/lib/idpool.h index c3d9668..619f237 100644 --- a/include/l4/lib/idpool.h +++ b/include/l4/lib/idpool.h @@ -2,8 +2,10 @@ #define __IDPOOL_H__ #include +#include struct id_pool { + struct spinlock lock; int nwords; u32 bitmap[]; }; diff --git a/src/lib/idpool.c b/src/lib/idpool.c index 3745dd3..59f033e 100644 --- a/src/lib/idpool.c +++ b/src/lib/idpool.c @@ -1,7 +1,7 @@ /* * Used for thread and space ids. * - * Copyright (C) 2007 Bahadir Balban + * Copyright (C) 2007, 2008 Bahadir Balban */ #include #include @@ -13,21 +13,31 @@ struct id_pool *id_pool_new_init(int totalbits) int nwords = BITWISE_GETWORD(totalbits); struct id_pool *new = kzalloc((nwords * SZ_WORD) + sizeof(struct id_pool)); + spin_lock_init(&new->lock); new->nwords = nwords; return new; } int id_new(struct id_pool *pool) { - int id = find_and_set_first_free_bit(pool->bitmap, + int id; + + spin_lock(&pool->lock); + id = find_and_set_first_free_bit(pool->bitmap, pool->nwords * WORD_BITS); + spin_unlock(&pool->lock); BUG_ON(id < 0); + return id; } int id_del(struct id_pool *pool, int id) { - int ret = check_and_clear_bit(pool->bitmap, id); + int ret; + + spin_lock(&pool->lock); + ret = check_and_clear_bit(pool->bitmap, id); + spin_unlock(&pool->lock); BUG_ON(ret < 0); return ret; @@ -36,7 +46,11 @@ int id_del(struct id_pool *pool, int id) /* Return a specific id, if available */ int id_get(struct id_pool *pool, int id) { - int ret = check_and_set_bit(pool->bitmap, id); + int ret; + + spin_lock(&pool->lock); + ret = check_and_set_bit(pool->bitmap, id); + spin_unlock(&pool->lock); if (ret < 0) return ret;