Skip to content

Commit

Permalink
Add buffer count for fiber_sem to improve the performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Dec 29, 2024
1 parent dc4aa9b commit fe42748
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib_fiber/c/include/fiber/fiber_sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ typedef struct ACL_FIBER_SEM ACL_FIBER_SEM;
FIBER_API ACL_FIBER_SEM* acl_fiber_sem_create2(int num, unsigned flags);
#define ACL_FIBER_SEM_F_ASYNC (1 << 0) /* If notifying in async mode */

/**
* Create one fiber semaphore with the specified buff count and flags.
* @param num {int} the initial value of the semaphore, must >= 0
* @param buf {int} the buffed count before signal the waiters.
* @param flags {unsigned} the flags defined as ACL_FIBER_SEM_F_XXX
*/
FIBER_API ACL_FIBER_SEM* acl_fiber_sem_create3(int num, int buf, unsigned flags);

FIBER_API ACL_FIBER_SEM* acl_fiber_sem_create(int num);

/**
Expand Down
13 changes: 12 additions & 1 deletion lib_fiber/c/src/sync/fiber_sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct ACL_FIBER_SEM {
int num;
int buf;
unsigned flags;
RING waiting;
unsigned long tid;
Expand All @@ -17,11 +18,18 @@ ACL_FIBER_SEM *acl_fiber_sem_create(int num)
}

ACL_FIBER_SEM *acl_fiber_sem_create2(int num, unsigned flags)
{
int buf = (flags & ACL_FIBER_SEM_F_ASYNC) ? 50000000 : 0;
return acl_fiber_sem_create3(num, buf, flags);
}

ACL_FIBER_SEM *acl_fiber_sem_create3(int num, int buf, unsigned flags)
{
ACL_FIBER_SEM *sem = (ACL_FIBER_SEM *) mem_malloc(sizeof(ACL_FIBER_SEM));

sem->tid = 0;
sem->num = num;
sem->buf = buf;
sem->flags = flags;
ring_init(&sem->waiting);
return sem;
Expand Down Expand Up @@ -178,6 +186,9 @@ int acl_fiber_sem_post(ACL_FIBER_SEM *sem)
sem->num++;

if ((ready = FIRST_FIBER(&sem->waiting)) == NULL) {
if (sem->num >= sem->buf) {
acl_fiber_yield();
}
return sem->num;
}

Expand All @@ -187,7 +198,7 @@ int acl_fiber_sem_post(ACL_FIBER_SEM *sem)
/* Help the fiber to be wakeup to decrease the sem number. */
num = sem->num--;

if (!(sem->flags & ACL_FIBER_SEM_F_ASYNC)) {
if (num >= sem->buf) {
acl_fiber_yield();
}

Expand Down
8 changes: 8 additions & 0 deletions lib_fiber/cpp/include/fiber/fiber_sem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef enum {
class FIBER_CPP_API fiber_sem {
public:
explicit fiber_sem(int max, fiber_sem_attr_t attr = fiber_sem_t_async);
explicit fiber_sem(int max, int buf);
~fiber_sem();

int wait(int ms = -1);
Expand Down Expand Up @@ -55,6 +56,10 @@ class fiber_sbox : public box<T> {
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync)
, free_obj_(free_obj) {}

explicit fiber_sbox(int buf, bool free_obj = true)
: sem_(0, buf)
, free_obj_(free_obj) {}

~fiber_sbox() { clear(free_obj_); }

// @override
Expand Down Expand Up @@ -143,6 +148,9 @@ class fiber_sbox2 : public box2<T> {
explicit fiber_sbox2(bool async = true)
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync) {}

explicit fiber_sbox2(int buf)
: sem_(0, buf) {}

~fiber_sbox2() {}

// @override
Expand Down
5 changes: 5 additions & 0 deletions lib_fiber/cpp/src/fiber_sem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ fiber_sem::fiber_sem(int max, fiber_sem_attr_t attr)
sem_ = acl_fiber_sem_create2(max, flags);
}

fiber_sem::fiber_sem(int max, int buf)
{
sem_ = acl_fiber_sem_create3(max, buf, 0);
}

fiber_sem::~fiber_sem()
{
acl_fiber_sem_free(sem_);
Expand Down
15 changes: 7 additions & 8 deletions lib_fiber/samples-c++1x/fiber_box/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ static void usage(const char* procname)
" -n count\r\n"
" -c fibers\r\n"
" -t box_type[sbox2|tbox2]"
" -S [if in sync mode for sbox2]\r\n", procname);
" -b buf_count\r\n", procname);
}

int main(int argc, char *argv[])
{
int ch, n = 10, c = 1;
int ch, n = 10, c = 1, buf = 0;
std::string type = "sbox2";
bool sync = false;

acl::acl_cpp_init();
acl::log::stdout_open(true);

while ((ch = getopt(argc, argv, "hn:t:c:S")) > 0) {
while ((ch = getopt(argc, argv, "hn:t:c:b:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
Expand All @@ -38,8 +37,8 @@ int main(int argc, char *argv[])
case 'c':
c = atoi(optarg);
break;
case 'S':
sync = true;
case 'b':
buf = atoi(optarg);
break;
default:
break;
Expand All @@ -49,7 +48,7 @@ int main(int argc, char *argv[])
std::shared_ptr<acl::box2<int>> box;

if (type == "sbox2") {
box = std::make_shared<acl::fiber_sbox2<int>>(!sync);
box = std::make_shared<acl::fiber_sbox2<int>>(buf);
} else if (type == "tbox2") {
box = std::make_shared<acl::fiber_tbox2<int>>();
} else {
Expand Down Expand Up @@ -83,7 +82,7 @@ int main(int argc, char *argv[])
};
}

acl::fiber::schedule();
acl::fiber::schedule();

struct timeval end;
gettimeofday(&end, nullptr);
Expand Down

0 comments on commit fe42748

Please sign in to comment.