From 7b2b11e932032b2bd5033ca2af4368bb87e794db Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 17 Sep 2015 11:57:17 +0800 Subject: [PATCH] async call worker fast execute tasks. --- trunk/auto/auto_headers.sh | 2 ++ trunk/src/app/srs_app_async_call.cpp | 34 ++++++++++++++++++++-------- trunk/src/app/srs_app_async_call.hpp | 3 +++ 3 files changed, 30 insertions(+), 9 deletions(-) mode change 100755 => 100644 trunk/auto/auto_headers.sh diff --git a/trunk/auto/auto_headers.sh b/trunk/auto/auto_headers.sh old mode 100755 new mode 100644 index 8723824a5..d9c723014 --- a/trunk/auto/auto_headers.sh +++ b/trunk/auto/auto_headers.sh @@ -257,7 +257,9 @@ echo "\"" >> $SRS_AUTO_HEADERS_H # new empty line to auto headers file. echo "" >> $SRS_AUTO_HEADERS_H +##################################################################################### # auto header EOF. +##################################################################################### echo "#endif" >> $SRS_AUTO_HEADERS_H echo "" >> $SRS_AUTO_HEADERS_H diff --git a/trunk/src/app/srs_app_async_call.cpp b/trunk/src/app/srs_app_async_call.cpp index 28968de71..c626e4f9c 100644 --- a/trunk/src/app/srs_app_async_call.cpp +++ b/trunk/src/app/srs_app_async_call.cpp @@ -42,6 +42,7 @@ ISrsAsyncCallTask::~ISrsAsyncCallTask() SrsAsyncCallWorker::SrsAsyncCallWorker() { pthread = new SrsReusableThread("async", this, SRS_AUTO_ASYNC_CALLBACL_SLEEP_US); + wait = st_cond_new(); } SrsAsyncCallWorker::~SrsAsyncCallWorker() @@ -54,6 +55,8 @@ SrsAsyncCallWorker::~SrsAsyncCallWorker() srs_freep(task); } tasks.clear(); + + st_cond_destroy(wait); } int SrsAsyncCallWorker::execute(ISrsAsyncCallTask* t) @@ -61,10 +64,16 @@ int SrsAsyncCallWorker::execute(ISrsAsyncCallTask* t) int ret = ERROR_SUCCESS; tasks.push_back(t); + st_cond_signal(wait); return ret; } +int SrsAsyncCallWorker::count() +{ + return (int)tasks.size(); +} + int SrsAsyncCallWorker::start() { return pthread->start(); @@ -72,23 +81,30 @@ int SrsAsyncCallWorker::start() void SrsAsyncCallWorker::stop() { + st_cond_signal(wait); pthread->stop(); } int SrsAsyncCallWorker::cycle() { int ret = ERROR_SUCCESS; - - std::vector copies = tasks; - tasks.clear(); - std::vector::iterator it; - for (it = copies.begin(); it != copies.end(); ++it) { - ISrsAsyncCallTask* task = *it; - if ((ret = task->call()) != ERROR_SUCCESS) { - srs_warn("ignore async callback %s, ret=%d", task->to_string().c_str(), ret); + while (pthread->can_loop()) { + if (tasks.empty()) { + st_cond_wait(wait); + } + + std::vector copies = tasks; + tasks.clear(); + + std::vector::iterator it; + for (it = copies.begin(); it != copies.end(); ++it) { + ISrsAsyncCallTask* task = *it; + if ((ret = task->call()) != ERROR_SUCCESS) { + srs_warn("ignore async callback %s, ret=%d", task->to_string().c_str(), ret); + } + srs_freep(task); } - srs_freep(task); } return ret; diff --git a/trunk/src/app/srs_app_async_call.hpp b/trunk/src/app/srs_app_async_call.hpp index c20ac76bb..81bcdf71e 100644 --- a/trunk/src/app/srs_app_async_call.hpp +++ b/trunk/src/app/srs_app_async_call.hpp @@ -70,12 +70,15 @@ class SrsAsyncCallWorker : public ISrsReusableThreadHandler { private: SrsReusableThread* pthread; +protected: std::vector tasks; + st_cond_t wait; public: SrsAsyncCallWorker(); virtual ~SrsAsyncCallWorker(); public: virtual int execute(ISrsAsyncCallTask* t); + virtual int count(); public: virtual int start(); virtual void stop();