Skip to content

Commit

Permalink
Code review for spin_pdr_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
klueska committed Dec 16, 2014
1 parent 4f1f689 commit db452e4
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
6 changes: 3 additions & 3 deletions cpu_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void cpu_util_init(struct cpu_util *c)
sprintf(proc_stat_file, "/proc/%d/stat", getpid());
c->proc_stat_fd = open(proc_stat_file, O_RDONLY);
memset(c->buffer, 0, sizeof(c->buffer));
spinlock_init(&c->lock);
spin_pdr_init(&c->lock);
__set_cpu_time(c, &c->initial_stats);
__set_proc_time(c, &c->initial_stats);
}
Expand All @@ -75,13 +75,13 @@ void cpu_util_fini(struct cpu_util *c)
struct cpu_util_stats cpu_util_get_stats(struct cpu_util *c)
{
struct cpu_util_stats s;
spinlock_lock(&c->lock);
spin_pdr_lock(&c->lock);
__set_cpu_time(c, &s);
__set_proc_time(c, &s);
s.cpu_time -= c->initial_stats.cpu_time;
s.proc_user_time -= c->initial_stats.proc_user_time;
s.proc_sys_time -= c->initial_stats.proc_sys_time;
spinlock_unlock(&c->lock);
spin_pdr_unlock(&c->lock);
return s;
}

Expand Down
2 changes: 1 addition & 1 deletion cpu_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct cpu_util {
int stat_fd;
int proc_stat_fd;
char buffer[1024];
spinlock_t lock;
spin_pdr_lock_t lock;
struct cpu_util_stats initial_stats;
};

Expand Down
28 changes: 14 additions & 14 deletions kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ void kqueue_init(struct kqueue *q, int item_size)
q->ids = 0;

STAILQ_INIT(&q->queue);
spinlock_init(&q->lock);
spin_pdr_init(&q->lock);
q->qstats.size = 0;
q->qstats.size_sum = 0;
q->qstats.total_enqueued = 0;
q->qstats.total_dequeued = 0;
q->qstats.wait_time_sum = 0;

STAILQ_INIT(&q->zombie_queue);
spinlock_init(&q->zombie_lock);
spin_pdr_init(&q->zombie_lock);
q->zombie_qstats.size = 0;
q->zombie_qstats.size_sum = 0;
q->zombie_qstats.total_enqueued = 0;
Expand All @@ -26,7 +26,7 @@ void kqueue_init(struct kqueue *q, int item_size)

void *kqueue_create_item(struct kqueue *q)
{
spinlock_lock(&q->zombie_lock);
spin_pdr_lock(&q->zombie_lock);
struct kitem *r = STAILQ_FIRST(&q->zombie_queue);
if(r) {
STAILQ_REMOVE_HEAD(&q->zombie_queue, link);
Expand All @@ -35,7 +35,7 @@ void *kqueue_create_item(struct kqueue *q)
q->zombie_qstats.wait_time_sum += (r->dequeue_time - r->enqueue_time);
q->zombie_qstats.size--;
}
spinlock_unlock(&q->zombie_lock);
spin_pdr_unlock(&q->zombie_lock);

if(r == NULL) {
r = malloc(q->item_size);
Expand All @@ -48,37 +48,37 @@ void *kqueue_create_item(struct kqueue *q)

void kqueue_destroy_item(struct kqueue *q, struct kitem *r)
{
spinlock_lock(&q->zombie_lock);
spin_pdr_lock(&q->zombie_lock);
q->zombie_qstats.size_sum += q->zombie_qstats.size++;
q->zombie_qstats.total_enqueued++;
r->enqueue_time = read_tsc();
STAILQ_INSERT_HEAD(&q->zombie_queue, r, link);
spinlock_unlock(&q->zombie_lock);
spin_pdr_unlock(&q->zombie_lock);
}

void kqueue_enqueue_item_head(struct kqueue *q, struct kitem *r)
{
spinlock_lock(&q->lock);
spin_pdr_lock(&q->lock);
q->qstats.size_sum += q->qstats.size++;
q->qstats.total_enqueued++;
r->enqueue_time = read_tsc();
STAILQ_INSERT_HEAD(&q->queue, r, link);
spinlock_unlock(&q->lock);
spin_pdr_unlock(&q->lock);
}

void kqueue_enqueue_item_tail(struct kqueue *q, struct kitem *r)
{
spinlock_lock(&q->lock);
spin_pdr_lock(&q->lock);
q->qstats.size_sum += q->qstats.size++;
q->qstats.total_enqueued++;
r->enqueue_time = read_tsc();
STAILQ_INSERT_TAIL(&q->queue, r, link);
spinlock_unlock(&q->lock);
spin_pdr_unlock(&q->lock);
}

struct kitem *kqueue_dequeue_item(struct kqueue *q)
{
spinlock_lock(&q->lock);
spin_pdr_lock(&q->lock);
struct kitem *r = STAILQ_FIRST(&q->queue);
if(r) {
STAILQ_REMOVE_HEAD(&q->queue, link);
Expand All @@ -87,15 +87,15 @@ struct kitem *kqueue_dequeue_item(struct kqueue *q)
q->qstats.wait_time_sum += (r->dequeue_time - r->enqueue_time);
q->qstats.size--;
}
spinlock_unlock(&q->lock);
spin_pdr_unlock(&q->lock);
return r;
}

struct kqueue_stats kqueue_get_stats(struct kqueue *q)
{
spinlock_lock(&q->lock);
spin_pdr_lock(&q->lock);
struct kqueue_stats s = q->qstats;
spinlock_unlock(&q->lock);
spin_pdr_unlock(&q->lock);
return s;
}

Expand Down
4 changes: 2 additions & 2 deletions kqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ struct kqueue {
int item_size;
unsigned long ids;

spinlock_t lock;
spin_pdr_lock_t lock;
struct __kqueue queue;
struct kqueue_stats qstats;

spinlock_t zombie_lock;
spin_pdr_lock_t zombie_lock;
struct __kqueue zombie_queue;
struct kqueue_stats zombie_qstats;
};
Expand Down
12 changes: 6 additions & 6 deletions ktimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ static void *__ktimer(void *arg)
for(;;) {
usleep(1000*t->period_ms);

spinlock_lock(&t->lock);
spin_pdr_lock(&t->lock);
if(t->state == S_TIMER_STOPPING)
break;
spinlock_unlock(&t->lock);
spin_pdr_unlock(&t->lock);
t->callback(t->callback_arg);
}
t->state = S_TIMER_STOPPED;
spinlock_unlock(&t->lock);
spin_pdr_unlock(&t->lock);
}

void ktimer_init(struct ktimer *t, void (*callback)(void*), void* arg)
{
t->state = S_TIMER_STOPPED;
t->callback = callback;
t->callback_arg = arg;
spinlock_init(&t->lock);
spin_pdr_init(&t->lock);
}

int ktimer_start(struct ktimer *t, unsigned int period_ms)
Expand All @@ -55,9 +55,9 @@ int ktimer_stop(struct ktimer *t)
if(t->state != S_TIMER_STARTED)
return -1;

spinlock_lock(&t->lock);
spin_pdr_lock(&t->lock);
t->state = S_TIMER_STOPPING;
spinlock_unlock(&t->lock);
spin_pdr_unlock(&t->lock);
while(t->state != S_TIMER_STOPPED) {
pthread_yield();
cpu_relax();
Expand Down
2 changes: 1 addition & 1 deletion ktimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct ktimer {
unsigned int period_ms;
void (*callback)(void*);
void *callback_arg;
spinlock_t lock;
spin_pdr_lock_t lock;
};

void ktimer_init(struct ktimer *t, void (*callback)(void*), void* arg);
Expand Down
7 changes: 6 additions & 1 deletion spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ static void spinlock_lock(spinlock_t *lock)
cpu_relax();
}

static void spinlock_unlock(spinlock_t *lock)
static inline void spinlock_unlock(spinlock_t *lock)
{
__sync_lock_release(&lock->lock, 0);
}

#define spin_pdr_lock_t spinlock_t
#define spin_pdr_init spinlock_init
#define spin_pdr_lock spinlock_lock
#define spin_pdr_unlock spinlock_unlock

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions tpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ static void *__thread_wrapper(void *arg)

while(1) {
i = NULL;
spinlock_lock(&t->lock);
spin_pdr_lock(&t->lock);
if(t->stats.active_threads < t->nprocs)
i = kqueue_dequeue_item(t->q);
if(i)
__sync_fetch_and_add(&t->stats.active_threads, 1);
else
total_enqueued = t->q->qstats.total_enqueued;
spinlock_unlock(&t->lock);
spin_pdr_unlock(&t->lock);

if(i) {
__sync_fetch_and_add(&t->stats.active_threads_sum, t->stats.active_threads);
Expand Down Expand Up @@ -68,7 +68,7 @@ int tpool_init(struct tpool *t, int size, struct kqueue *q,
t->q = q;
t->func = func;
t->nprocs = get_nprocs();
spinlock_init(&t->lock);
spin_pdr_init(&t->lock);
t->stats.active_threads = 0;
t->stats.blocked_threads = 0;
t->stats.active_threads_sum = 0;
Expand Down
2 changes: 1 addition & 1 deletion tpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct tpool {
struct kqueue *q;
int nprocs;
void (*func)(struct kqueue *, struct kitem *);
spinlock_t lock;
spin_pdr_lock_t lock;
struct tpool_stats stats;
size_t stacksize;
};
Expand Down

0 comments on commit db452e4

Please sign in to comment.