mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 19:57:43 +08:00
Make app().run() method callable on a non-main thread (#457)
This commit is contained in:
parent
d4d5adf88b
commit
ecb3d3f74f
@ -73,8 +73,8 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
/**
|
||||
* Calling this method starts the IO event loops and the main loop of the
|
||||
* application;
|
||||
* This method MUST be called in the main thread.
|
||||
* This method blocks the main thread until the main event loop exits.
|
||||
* This method can be called in the main thread or any other thread.
|
||||
* This method blocks the current thread until the main event loop exits.
|
||||
*/
|
||||
virtual void run() = 0;
|
||||
|
||||
@ -85,7 +85,8 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
/**
|
||||
* Calling this method results in stopping all network IO in the
|
||||
* framework and interrupting the blocking of the run() method. Usually,
|
||||
* after calling this method, the application exits.
|
||||
* after calling this method, the application exits (when the run() method
|
||||
* is called in the main thread).
|
||||
*
|
||||
* @note
|
||||
* This method can be called in any thread and anywhere.
|
||||
@ -1081,10 +1082,11 @@ class HttpAppFramework : public trantor::NonCopyable
|
||||
* @brief Get the Current Thread Index whose range is [0, the total number
|
||||
* of IO threads]
|
||||
*
|
||||
* @return size_t If the current thread is the main thread, the number of
|
||||
* the IO threads is returned. If the current thread is a network IO thread,
|
||||
* the index of it in the range [0, the number of IO threads) is returned.
|
||||
* otherwise the maximum value of type size_t is returned.
|
||||
* @return size_t If the current thread is the main EventLoop thread (in
|
||||
* which the app().run() is called), the number of the IO threads is
|
||||
* returned. If the current thread is a network IO thread, the index of it
|
||||
* in the range [0, the number of IO threads) is returned. otherwise the
|
||||
* maximum value of type size_t is returned.
|
||||
*
|
||||
* @note Basically this method is used for storing thread-related various in
|
||||
* an array and users can use indexes returned by this method to access
|
||||
|
@ -365,7 +365,10 @@ HttpAppFramework &HttpAppFrameworkImpl::setSSLFiles(const std::string &certPath,
|
||||
|
||||
void HttpAppFrameworkImpl::run()
|
||||
{
|
||||
//
|
||||
if (!getLoop()->isInLoopThread())
|
||||
{
|
||||
getLoop()->moveToCurrentThread();
|
||||
}
|
||||
LOG_TRACE << "Start to run...";
|
||||
trantor::AsyncFileLogger asyncFileLogger;
|
||||
// Create dirs for cache files
|
||||
|
@ -329,7 +329,7 @@ bool HttpRequestParser::parseRequest(MsgBuffer *buf)
|
||||
return ok;
|
||||
}
|
||||
|
||||
void HttpRequestParser::pushRquestToPipelining(const HttpRequestPtr &req)
|
||||
void HttpRequestParser::pushRequestToPipelining(const HttpRequestPtr &req)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
auto conn = conn_.lock();
|
||||
|
@ -72,7 +72,7 @@ class HttpRequestParser : public trantor::NonCopyable,
|
||||
websockConnPtr_ = conn;
|
||||
}
|
||||
// to support request pipelining(rfc2616-8.1.2.2)
|
||||
void pushRquestToPipelining(const HttpRequestPtr &req);
|
||||
void pushRequestToPipelining(const HttpRequestPtr &req);
|
||||
HttpRequestPtr getFirstRequest() const;
|
||||
std::pair<HttpResponsePtr, bool> getFirstResponse() const;
|
||||
void popFirstRequest();
|
||||
|
@ -326,7 +326,7 @@ void HttpServer::onRequests(
|
||||
bool syncFlag = false;
|
||||
if (!requestParser->emptyPipelining())
|
||||
{
|
||||
requestParser->pushRquestToPipelining(req);
|
||||
requestParser->pushRequestToPipelining(req);
|
||||
syncFlag = true;
|
||||
}
|
||||
if (!syncAdvices_.empty())
|
||||
@ -477,7 +477,7 @@ void HttpServer::onRequests(
|
||||
});
|
||||
if (syncFlag == false)
|
||||
{
|
||||
requestParser->pushRquestToPipelining(req);
|
||||
requestParser->pushRequestToPipelining(req);
|
||||
}
|
||||
}
|
||||
*loopFlagPtr = false;
|
||||
|
@ -9,6 +9,7 @@ add_executable(http_full_date_test HttpFullDateTest.cc)
|
||||
add_executable(gzip_test GzipTest.cc)
|
||||
add_executable(url_codec_test UrlCodecTest.cc)
|
||||
add_executable(main_loop_test MainLoopTest.cc)
|
||||
add_executable(main_loop_test2 MainLoopTest2.cc)
|
||||
|
||||
set(test_targets
|
||||
cache_map_test
|
||||
@ -19,7 +20,8 @@ set(test_targets
|
||||
http_full_date_test
|
||||
gzip_test
|
||||
url_codec_test
|
||||
main_loop_test)
|
||||
main_loop_test
|
||||
main_loop_test2)
|
||||
|
||||
set_property(TARGET ${test_targets}
|
||||
PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD})
|
||||
|
21
lib/tests/MainLoopTest2.cc
Normal file
21
lib/tests/MainLoopTest2.cc
Normal file
@ -0,0 +1,21 @@
|
||||
#include <drogon/drogon.h>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
/**
|
||||
* @brief This test program tests to call the app().run() in another thread.
|
||||
*
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
std::thread([]() {
|
||||
drogon::app().getLoop()->runEvery(1, []() {
|
||||
std::cout << "!" << std::endl;
|
||||
});
|
||||
}).detach();
|
||||
|
||||
std::thread l([]() { drogon::app().run(); });
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
trantor::EventLoop loop;
|
||||
l.join();
|
||||
}
|
2
trantor
2
trantor
@ -1 +1 @@
|
||||
Subproject commit 880ca0acbe89c91b557b93c711dd9624e922c209
|
||||
Subproject commit 55232baacebb77036babfeb8c652420032241b01
|
Loading…
Reference in New Issue
Block a user