Skip to content

Commit

Permalink
automatic start fiber when calling acl_fiber_schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntu14 committed Jul 18, 2016
1 parent 36585ef commit 65300d7
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 48 deletions.
Binary file added doc/acl_fiber.pptx
Binary file not shown.
4 changes: 3 additions & 1 deletion lib_fiber/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ all:
@(cd cpp; make)
@(cd samples; make)

clean:
clean cl:
@(cd c; make clean)
@(cd cpp; make clean)
@(cd samples; make clean)

rebuild rb: cl all
5 changes: 3 additions & 2 deletions lib_fiber/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ CFLAGS = -c -g -W \
-D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS \
-D_USE_FAST_MACRO \
-Wno-long-long \
-DUSE_VALGRIND
-Wno-long-long
#-DUSE_VALGRIND
#-Wno-clobbered
#-O3

#-pedantic
# -Wcast-align
Expand Down
8 changes: 5 additions & 3 deletions lib_fiber/c/src/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct {

static FIBER_TLS *__main_fiber = NULL;
static __thread FIBER_TLS *__thread_fiber = NULL;
__thread int acl_var_hook_sys_api = 1;
__thread int acl_var_hook_sys_api = 0;

static acl_pthread_key_t __fiber_key;

Expand Down Expand Up @@ -366,6 +366,8 @@ void acl_fiber_schedule(void)
ACL_FIBER *fiber;
ACL_RING *head;

acl_fiber_hook_api(1);

for (;;) {
head = acl_ring_pop_head(&__thread_fiber->queue);
if (head == NULL) {
Expand All @@ -383,14 +385,14 @@ void acl_fiber_schedule(void)
__thread_fiber->running = NULL;
}

// release dead fiber
/* release dead fiber */
for (;;) {
head = acl_ring_pop_head(&__thread_fiber->dead);
if (head == NULL)
break;

fiber = ACL_RING_TO_APPL(head, ACL_FIBER, me);
fiber_free (fiber);
fiber_free(fiber);
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib_fiber/samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
all:
@(cd dns; make)
@(cd fiber; make)
@(cd fiber sem; make)
@(cd read; make)
@(cd httpd; make)
@(cd httpd2; make)
Expand Down Expand Up @@ -31,6 +32,7 @@ all:
cl clean:
@(cd dns; make clean)
@(cd fiber; make clean)
@(cd fiber sem; make clean)
@(cd read; make clean)
@(cd httpd; make clean)
@(cd httpd2; make clean)
Expand All @@ -56,3 +58,5 @@ cl clean:
@(cd https_client; make clean)
@(cd https_server; make clean)
@(cd mysql; make clean)

rebuild rb: clean all
4 changes: 3 additions & 1 deletion lib_fiber/samples/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ $(OBJ_PATH)/%.o: %.c
$(COMPILE) $< -o $@
RM:
rm -f $(PROG)
clean:
clean cl:
rm -f $(PROG)
rm -f $(OBJ)

rebuild rb: clean all
###########################################################
5 changes: 4 additions & 1 deletion lib_fiber/samples/Makefile_cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ all: RM $(OBJ)
@echo ""
RM:
rm -f $(PROG)
clean:
clean cl:
rm -f $(PROG)
rm -f $(OBJ)

rebuild rb: clean all

install:
cp $(PROG) ../../../dist/master/libexec/$(RPATH)/
cp $(PROG).cf ../../../dist/master/conf/service/
Expand Down
13 changes: 10 additions & 3 deletions lib_fiber/samples/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ static void echo_client(ACL_VSTREAM *cstream)

if (!__read_data) {
__total_count++;
if (__total_count % 10000 == 0) {
printf("fiber-%d: total %lld, curr %d\r\n",
acl_fiber_self(), __total_count, i);
acl_fiber_yield();
}
continue;
}

Expand All @@ -58,12 +63,14 @@ static void fiber_connect(ACL_FIBER *fiber acl_unused, void *ctx)
ACL_VSTREAM *cstream = acl_vstream_connect(addr, ACL_BLOCKING,
__conn_timeout, __rw_timeout, 4096);
if (cstream == NULL) {
printf("connect %s error %s\r\n", addr, acl_last_serror());
printf("fiber-%d: connect %s error %s\r\n",
acl_fiber_self(), addr, acl_last_serror());
__total_error_clients++;
} else {
__total_clients++;
printf("connect %s ok, clients: %d, fd: %d\r\n",
addr, __total_clients, ACL_VSTREAM_SOCK(cstream));
printf("fiber-%d: connect %s ok, clients: %d, fd: %d\r\n",
acl_fiber_self(), addr, __total_clients,
ACL_VSTREAM_SOCK(cstream));
echo_client(cstream);
}

Expand Down
6 changes: 3 additions & 3 deletions lib_fiber/samples/fiber/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void *thread_main(void *ctx acl_unused)

static void usage(const char *procname)
{
printf("usage: %s -h [help] -n max_loop -m max_fiber -t max_threads\r\n", procname);
printf("usage: %s -h [help] -n max_loop -c max_fiber -t max_threads\r\n", procname);
}

int main(int argc, char *argv[])
Expand All @@ -64,15 +64,15 @@ int main(int argc, char *argv[])
acl_pthread_attr_t attr;
acl_pthread_t *tids;

while ((ch = getopt(argc, argv, "hn:m:t:")) > 0) {
while ((ch = getopt(argc, argv, "hn:c:t:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'n':
__max_loop = atoi(optarg);
break;
case 'm':
case 'c':
__max_fiber = atoi(optarg);
break;
case 't':
Expand Down
74 changes: 40 additions & 34 deletions lib_fiber/samples/mysql/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include <stdio.h>
#include <stdlib.h>

//#define USE_THREADS

class mysql_oper
{
public:
Expand Down Expand Up @@ -104,7 +102,8 @@ class mysql_oper
}
};

#ifdef USE_THREADS
//////////////////////////////////////////////////////////////////////////////
// mysql thread class

class mysql_thread : public acl::thread
{
Expand All @@ -116,8 +115,6 @@ class mysql_thread : public acl::thread
protected:
void* run(void)
{
acl::fiber::hook_api(false);

mysql_oper dboper(dbp_, id_);

if (oper_.equal("add", false))
Expand All @@ -137,7 +134,8 @@ class mysql_thread : public acl::thread
int count_;
};

#else
//////////////////////////////////////////////////////////////////////////////
// mysql fiber class

static int __max_fibers = 2;
static int __cur_fibers = 2;
Expand Down Expand Up @@ -182,11 +180,12 @@ class mysql_fiber : public acl::fiber
int count_;
};

#endif
//////////////////////////////////////////////////////////////////////////////

static void usage(const char* procname)
{
printf("usage: %s -h [help] -c cocurrent\r\n"
" -t [use threads mode]\r\n"
" -n oper_count\r\n"
" -o db_oper[add|get]\r\n"
" -f mysqlclient_path\r\n"
Expand All @@ -203,11 +202,12 @@ int main(int argc, char *argv[])
acl::string mysql_path("../../lib/libmysqlclient_r.so");
acl::string dbaddr("127.0.0.1:3306"), dbname("acl_db");
acl::string dbuser("root"), dbpass(""), oper("get");
bool use_threads = false;

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

while ((ch = getopt(argc, argv, "hc:n:f:u:o:p:C:R:")) > 0)
while ((ch = getopt(argc, argv, "hc:tn:f:u:o:p:C:R:")) > 0)
{
switch (ch)
{
Expand All @@ -217,6 +217,9 @@ int main(int argc, char *argv[])
case 'c':
cocurrent = atoi(optarg);
break;
case 't':
use_threads = true;
break;
case 'n':
count = atoi(optarg);
break;
Expand All @@ -243,52 +246,55 @@ int main(int argc, char *argv[])
}
}


// setup libmysqlclient_r.so path
acl::db_handle::set_loadpath(mysql_path);

// init mysql connection configure
acl::mysql_conf dbconf(dbaddr, dbname);
dbconf.set_dbuser(dbuser)
.set_dbpass(dbpass)
.set_dblimit(cocurrent)
.set_conn_timeout(conn_timeout)
.set_rw_timeout(rw_timeout);

// init mysql connections pool
acl::mysql_pool dbpool(dbconf);

#ifdef USE_THREADS
if (use_threads)
{
std::vector<acl::thread*> threads;

std::vector<acl::thread*> threads;
for (int i = 0; i < cocurrent; i++)
{
acl::thread* thread = new
mysql_thread(i, dbpool, oper, count);

for (int i = 0; i < cocurrent; i++)
{
acl::thread* thread = new mysql_thread(i, dbpool, oper, count);
thread->set_detachable(false);
threads.push_back(thread);
thread->start();
}

thread->set_detachable(false);
threads.push_back(thread);
thread->start();
for (std::vector<acl::thread*>::iterator it = threads.begin();
it != threads.end(); ++it)
{
(*it)->wait(NULL);
delete (*it);
}
}

for (std::vector<acl::thread*>::iterator it = threads.begin();
it != threads.end(); ++it)
else
{
(*it)->wait(NULL);
delete (*it);
}
#else
__max_fibers = cocurrent;
__cur_fibers = __max_fibers;

__max_fibers = cocurrent;
__cur_fibers = __max_fibers;
for (int i = 0; i < __max_fibers; i++)
{
acl::fiber* f = new mysql_fiber(i, dbpool, oper, count);
f->start();
}

for (int i = 0; i < __max_fibers; i++)
{
acl::fiber* f = new mysql_fiber(i, dbpool, oper, count);
f->start();
acl::fiber::schedule();
}

acl::fiber::schedule();

#endif

printf("---- exit now ----\r\n");

return 0;
Expand Down

0 comments on commit 65300d7

Please sign in to comment.