Skip to content

Commit

Permalink
pool: don't try to steal from own thread
Browse files Browse the repository at this point in the history
steal id = (rand (mod n-1)) + id + 1 (mod n)
Current implementation has a lot of repeated calls to lc_pool_get_local
that the compiler doesn't seem to fully get rid of.
Maybe marking it with __attribute__((pure)) will help?
Need to re-read docs to ensure we don't violate its constraints.
  • Loading branch information
omor1 committed Jun 5, 2020
1 parent 51ef528 commit 461b6ed
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions include/lc/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,21 @@ LC_INLINE int32_t lc_pool_get_local(struct lc_pool* pool)
return pid;
}

LC_INLINE void* lc_pool_get_slow(struct lc_pool* pool) {
void* elm = NULL;
while (!elm) {
int steal = rand() % (pool->npools);
LC_INLINE int32_t lc_pool_get_steal(struct lc_pool* pool)
{
int32_t pid = lc_pool_get_local(pool);
int32_t npools = pool->npools;
int32_t r = rand() % (npools - 1);
return (r + pid + 1) % npools;
}

LC_INLINE void* lc_pool_steal(struct lc_pool* pool)
{
void* elm = NULL;
int32_t steal = lc_pool_get_steal(pool);
if (likely(pool->lpools[steal] != NULL))
elm = dq_pop_bot(pool->lpools[steal]);
}
return elm;
return elm;
}

LC_INLINE void lc_pool_put(struct lc_pool* pool, void* elm) {
Expand All @@ -111,26 +118,19 @@ LC_INLINE void lc_pool_put_to(struct lc_pool* pool, void* elm, int32_t pid) {
dq_push_top(lpool, elm);
}

LC_INLINE void* lc_pool_get(struct lc_pool* pool) {
LC_INLINE void* lc_pool_get_nb(struct lc_pool* pool) {
int32_t pid = lc_pool_get_local(pool);
struct dequeue* lpool = pool->lpools[pid];
void *elm = NULL;
elm = dq_pop_top(lpool);
void* elm = dq_pop_top(lpool);
if (elm == NULL)
elm = lc_pool_get_slow(pool);
elm = lc_pool_steal(pool);
return elm;
}

LC_INLINE void* lc_pool_get_nb(struct lc_pool* pool) {
int32_t pid = lc_pool_get_local(pool);
struct dequeue* lpool = pool->lpools[pid];
LC_INLINE void* lc_pool_get(struct lc_pool* pool) {
void* elm = NULL;
elm = dq_pop_top(lpool);
if (elm == NULL) {
int steal = rand() % (pool->npools);
if (likely(pool->lpools[steal] != NULL))
elm = dq_pop_bot(pool->lpools[steal]);
}
while (elm == NULL)
elm = lc_pool_get_nb(pool);
return elm;
}

Expand Down

0 comments on commit 461b6ed

Please sign in to comment.