diff options
author | Ben Gamari <ben@smart-cactus.org> | 2015-11-26 12:02:33 +0100 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2015-11-26 14:48:48 +0100 |
commit | c4308b468c7fe723b6f0a1009b233a55260de503 (patch) | |
tree | e1bd38e290aa9700d59b4393bba15e721f4a6f71 /rts/Pool.c | |
parent | d2a2d5ebb08caedcd2a2a7a9e06ed6f4cbba96e7 (diff) | |
download | haskell-c4308b468c7fe723b6f0a1009b233a55260de503.tar.gz |
rts/Pool: Add poolTryTake
Diffstat (limited to 'rts/Pool.c')
-rw-r--r-- | rts/Pool.c | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/rts/Pool.c b/rts/Pool.c index 6c238075ef..b3e3446647 100644 --- a/rts/Pool.c +++ b/rts/Pool.c @@ -111,19 +111,40 @@ nat poolGetDesiredSize(Pool *pool) { return pool->desired_size; } +// Try taking a PoolEntry with an item from a pool, +// returning NULL if no items are available. +static PoolEntry *poolTryTake_(Pool *pool) { + PoolEntry *ent = NULL; + if (pool->available != NULL) { + ent = pool->available; + pool->available = ent->next; + } else if (pool->current_size < pool->max_size) { + ent = stgMallocBytes(sizeof(PoolEntry), "pool_take"); + ent->flags = 0; + ent->thing = pool->alloc_fn(); + pool->current_size++; + } else { + return NULL; + } + + ent->next = pool->taken; + pool->taken = ent; + return ent; +} + +void *poolTryTake(Pool *pool) { + ACQUIRE_LOCK(&pool->mutex); + PoolEntry *ent = poolTryTake_(pool); + RELEASE_LOCK(&pool->mutex); + return ent ? ent->thing : NULL; +} + void *poolTake(Pool *pool) { PoolEntry *ent = NULL; ACQUIRE_LOCK(&pool->mutex); while (ent == NULL) { - if (pool->available != NULL) { - ent = pool->available; - pool->available = ent->next; - } else if (pool->current_size < pool->max_size) { - ent = stgMallocBytes(sizeof(PoolEntry), "pool_take"); - ent->flags = 0; - ent->thing = pool->alloc_fn(); - pool->current_size++; - } else { + ent = poolTryTake_(pool); + if (!ent) { #ifdef THREADED_RTS waitCondition(&pool->cond, &pool->mutex); #else @@ -132,8 +153,6 @@ void *poolTake(Pool *pool) { } } - ent->next = pool->taken; - pool->taken = ent; RELEASE_LOCK(&pool->mutex); return ent->thing; } |