Skip to content

Commit

Permalink
fixed bugs of acl_fiber_cond_timedwait in acl_fiber_cond.c
Browse files Browse the repository at this point in the history
  • Loading branch information
zsx committed Dec 25, 2018
1 parent 2dddbec commit a60f84a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changes.txt
@@ -1,4 +1,7 @@

95) 2018.12.25
95.1) bugfix: fixed bugs of acl_fiber_cond_timedwait in acl_fiber_cond.c

94) 2018.11.29
94.1) bugfix: fixed bugs in fbase_event.c/fiber_event.c

Expand Down
10 changes: 8 additions & 2 deletions include/fiber/fiber_base.h
Expand Up @@ -25,9 +25,15 @@ FIBER_API ACL_FIBER* acl_fiber_create(void (*fn)(ACL_FIBER*, void*),

/**
* get the fibers count in deading status
* @retur {int}
* @return {unsigned}
*/
FIBER_API int acl_fiber_ndead(void);
FIBER_API unsigned acl_fiber_ndead(void);

/**
* get the fibers count in aliving status
* @return {unsigned}
*/
FIBER_API unsigned acl_fiber_number(void);

/**
* create one fiber in background for freeing the dead fibers, specify the
Expand Down
12 changes: 10 additions & 2 deletions src/fiber.c
Expand Up @@ -492,12 +492,20 @@ int acl_fiber_yield(void)
#endif
}

int acl_fiber_ndead(void)
unsigned acl_fiber_ndead(void)
{
if (__thread_fiber == NULL) {
return 0;
}
return ring_size(&__thread_fiber->dead);
return (unsigned) ring_size(&__thread_fiber->dead);
}

unsigned acl_fiber_number(void)
{
if (__thread_fiber == NULL) {
return 0;
}
return __thread_fiber->slot;
}

static void fbase_init(FIBER_BASE *fbase, int flag)
Expand Down
34 changes: 31 additions & 3 deletions src/fiber_cond.c
Expand Up @@ -157,16 +157,44 @@ int acl_fiber_cond_timedwait(ACL_FIBER_COND *cond, ACL_FIBER_EVENT *event,
return EINVAL;
}

if (read_wait(fbase->event_in, delay_ms) == -1) {
DETACHE;
return ETIMEDOUT;
while (1) {
if (read_wait(fbase->event_in, delay_ms) == -1) {
if (acl_fiber_event_wait(event) == -1) {
msg_fatal("%s(%d), %s: wait event error",
__FILE__, __LINE__, __FUNCTION__);
}
DETACHE;
return ETIMEDOUT;
}

__ll_lock(cond);
if (atomic_int64_cas(cond->atomic, 0, 1) == 0) {
break;
}
__ll_unlock(cond);
}

if (fbase_event_wait(fbase) == -1) {
if (atomic_int64_cas(cond->atomic, 1, 0) != 1) {
msg_fatal("%s(%d), %s: cond corrupt",
__FILE__, __LINE__, __FUNCTION__);
}
__ll_unlock(cond);

if (acl_fiber_event_wait(event) == -1) {
msg_fatal("%s(%d), %s: wait event error",
__FILE__, __LINE__, __FUNCTION__);
}
DETACHE;
return EINVAL;
}

if (atomic_int64_cas(cond->atomic, 1, 0) != 1) {
msg_fatal("%s(%d), %s: cond corrupt",
__FILE__, __LINE__, __FUNCTION__);
}
__ll_unlock(cond);

if (acl_fiber_event_wait(event) == -1) {
DETACHE;
msg_error("acl_fiber_event_wait error");
Expand Down

0 comments on commit a60f84a

Please sign in to comment.