Skip to content

Commit

Permalink
Threads-Log: Refine API and main workflow.
Browse files Browse the repository at this point in the history
1. Use SrsThreadPool to execute the hybrid server.
2. Right now, directly run in primordial thread, that is no threads.
3. Define the main APIs of SrsThreadPool.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent bbdb4a3 commit 831b77b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion trunk/configure
Expand Up @@ -274,7 +274,7 @@ MODULE_FILES=("srs_app_server" "srs_app_conn" "srs_app_rtmp_conn" "srs_app_sourc
"srs_app_mpegts_udp" "srs_app_rtsp" "srs_app_listener" "srs_app_async_call"
"srs_app_caster_flv" "srs_app_process" "srs_app_ng_exec"
"srs_app_hourglass" "srs_app_dash" "srs_app_fragment" "srs_app_dvr"
"srs_app_coworkers" "srs_app_hybrid")
"srs_app_coworkers" "srs_app_hybrid" "srs_app_threads")
if [[ $SRS_RTC == YES ]]; then
MODULE_FILES+=("srs_app_rtc_conn" "srs_app_rtc_dtls" "srs_app_rtc_sdp"
"srs_app_rtc_queue" "srs_app_rtc_server" "srs_app_rtc_source" "srs_app_rtc_api")
Expand Down
58 changes: 58 additions & 0 deletions trunk/src/app/srs_app_threads.cpp
@@ -0,0 +1,58 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2020 Winlin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <srs_app_threads.hpp>

#include <srs_kernel_error.hpp>

SrsThreadPool::SrsThreadPool()
{
}

SrsThreadPool::~SrsThreadPool()
{
}

srs_error_t SrsThreadPool::initialize()
{
srs_error_t err = srs_success;
return err;
}

srs_error_t SrsThreadPool::execute(srs_error_t (*start)(void* arg), void* arg)
{
srs_error_t err = start(arg);
return err;
}

srs_error_t SrsThreadPool::run()
{
srs_error_t err = srs_success;
return err;
}

void SrsThreadPool::stop()
{
}

SrsThreadPool* _srs_thread_pool = new SrsThreadPool();
50 changes: 50 additions & 0 deletions trunk/src/app/srs_app_threads.hpp
@@ -0,0 +1,50 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2020 Winlin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef SRS_APP_THREADS_HPP
#define SRS_APP_THREADS_HPP

#include <srs_core.hpp>

// Allocate a(or almost) fixed thread poll to execute tasks,
// so that we can take the advantage of multiple CPUs.
class SrsThreadPool
{
public:
SrsThreadPool();
virtual ~SrsThreadPool();
public:
// Initialize the thread pool.
srs_error_t initialize();
// Execute start function in thread.
srs_error_t execute(srs_error_t (*start)(void* arg), void* arg);
// Run in the primordial thread, util stop or quit.
srs_error_t run();
// Stop the thread pool and quit the primordial thread.
void stop();
};

// The global thread pool.
extern SrsThreadPool* _srs_thread_pool;

#endif
25 changes: 21 additions & 4 deletions trunk/src/main/srs_main_server.cpp
Expand Up @@ -54,6 +54,7 @@ using namespace std;
#include <srs_core_autofree.hpp>
#include <srs_kernel_file.hpp>
#include <srs_app_hybrid.hpp>
#include <srs_app_threads.hpp>
#ifdef SRS_RTC
#include <srs_app_rtc_conn.hpp>
#include <srs_app_rtc_server.hpp>
Expand All @@ -65,7 +66,7 @@ using namespace std;

// pre-declare
srs_error_t run_directly_or_daemon();
srs_error_t run_hybrid_server();
srs_error_t run_in_thread_pool();
void show_macro_features();

// @global log and context.
Expand Down Expand Up @@ -415,7 +416,7 @@ srs_error_t run_directly_or_daemon()

// If not daemon, directly run hybrid server.
if (!run_as_daemon) {
if ((err = run_hybrid_server()) != srs_success) {
if ((err = run_in_thread_pool()) != srs_success) {
return srs_error_wrap(err, "run hybrid");
}
return srs_success;
Expand Down Expand Up @@ -452,14 +453,30 @@ srs_error_t run_directly_or_daemon()
// son
srs_trace("son(daemon) process running.");

if ((err = run_hybrid_server()) != srs_success) {
if ((err = run_in_thread_pool()) != srs_success) {
return srs_error_wrap(err, "daemon run hybrid");
}

return err;
}

srs_error_t run_hybrid_server()
srs_error_t run_hybrid_server(void* arg);
srs_error_t run_in_thread_pool()
{
srs_error_t err = srs_success;

if ((err = _srs_thread_pool->initialize()) != srs_success) {
return srs_error_wrap(err, "init thread pool");
}

if ((err = _srs_thread_pool->execute(run_hybrid_server, NULL)) != srs_success) {
return srs_error_wrap(err, "run hybrid server");
}

return _srs_thread_pool->run();
}

srs_error_t run_hybrid_server(void* arg)
{
srs_error_t err = srs_success;

Expand Down

0 comments on commit 831b77b

Please sign in to comment.