Skip to content

Commit

Permalink
added on lfqueue feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Taymindis committed Sep 15, 2018
1 parent dd17590 commit df6fba6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lfqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,20 @@ lfqueue_deq(lfqueue_t *lfqueue) {
__LFQ_FETCH_AND_ADD(&lfqueue->size, -1);
return v;
}
// Rest the thread for other thread, to avoid keep looping force
lfqueue_sleep(1);
return NULL;
}

void*
lfqueue_deq_must(lfqueue_t *lfqueue) {
void *v;
while ( !(v = _dequeue(lfqueue)) ) {
// Rest the thread for other thread, to avoid keep looping force
lfqueue_sleep(1);
}
__LFQ_FETCH_AND_ADD(&lfqueue->size, -1);
return v;
}

/**This is only applicable when only single thread consume only**/
void*
lfqueue_single_deq(lfqueue_t *lfqueue) {
Expand All @@ -319,17 +328,27 @@ lfqueue_single_deq(lfqueue_t *lfqueue) {
__LFQ_FETCH_AND_ADD(&lfqueue->size, -1);
return v;
}
// Rest the thread for other thread, to avoid keep looping force
lfqueue_sleep(1);
return NULL;
}

/**This is only applicable when only single thread consume only**/
void*
lfqueue_single_deq_must(lfqueue_t *lfqueue) {
void *v;
while ( !(v = _single_dequeue(lfqueue)) ) {
// Rest the thread for other thread, to avoid keep looping force
lfqueue_sleep(1);
}
__LFQ_FETCH_AND_ADD(&lfqueue->size, -1);
return v;
}

size_t
lfqueue_size(lfqueue_t *lfqueue) {
return __LFQ_ADD_AND_FETCH(&lfqueue->size, 0);
}

void
void
lfqueue_sleep(unsigned int milisec) {
#if defined __GNUC__ || defined __CYGWIN__ || defined __MINGW32__ || defined __APPLE__
#pragma GCC diagnostic push
Expand Down
5 changes: 5 additions & 0 deletions lfqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ extern int lfqueue_init(lfqueue_t *lfqueue);
extern int lfqueue_enq(lfqueue_t *lfqueue, void *value);
extern void* lfqueue_deq(lfqueue_t *lfqueue);
extern void* lfqueue_single_deq(lfqueue_t *lfqueue);

/** loop until value been dequeue, it sleeps 1ms if not found to reduce cpu high usage **/
extern void* lfqueue_deq_must(lfqueue_t *lfqueue);
extern void* lfqueue_single_deq_must(lfqueue_t *lfqueue);

extern void lfqueue_destroy(lfqueue_t *lfqueue);
extern size_t lfqueue_size(lfqueue_t *lfqueue);
extern void lfqueue_sleep(unsigned int milisec);
Expand Down

0 comments on commit df6fba6

Please sign in to comment.