2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-01-01 13:37:28 +00:00
|
|
|
* Copyright (c) 2013-2019 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-03-01 02:14:25 +00:00
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_st.hpp>
|
2014-03-01 02:14:25 +00:00
|
|
|
|
2017-05-30 01:05:02 +00:00
|
|
|
#include <st.h>
|
2015-10-13 09:37:59 +00:00
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
2014-10-08 05:27:48 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_log.hpp>
|
2015-10-13 09:59:51 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2016-09-05 06:16:24 +00:00
|
|
|
#include <srs_app_log.hpp>
|
2014-10-08 05:27:48 +00:00
|
|
|
|
2017-05-29 09:19:06 +00:00
|
|
|
ISrsCoroutineHandler::ISrsCoroutineHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ISrsCoroutineHandler::~ISrsCoroutineHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
SrsCoroutine::SrsCoroutine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsCoroutine::~SrsCoroutine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsDummyCoroutine::SrsDummyCoroutine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsDummyCoroutine::~SrsDummyCoroutine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsDummyCoroutine::start()
|
2017-06-04 11:13:56 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
return srs_error_new(ERROR_THREAD_DUMMY, "dummy coroutine");
|
2017-06-04 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsDummyCoroutine::stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsDummyCoroutine::interrupt()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsDummyCoroutine::pull()
|
2017-06-04 11:13:56 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
return srs_error_new(ERROR_THREAD_DUMMY, "dummy pull");
|
2017-06-04 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SrsDummyCoroutine::cid()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsSTCoroutine::SrsSTCoroutine(const string& n, ISrsCoroutineHandler* h, int cid)
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
|
|
|
name = n;
|
|
|
|
handler = h;
|
|
|
|
context = cid;
|
|
|
|
trd = NULL;
|
2017-06-11 10:44:20 +00:00
|
|
|
trd_err = srs_success;
|
2017-05-29 09:19:06 +00:00
|
|
|
started = interrupted = disposed = false;
|
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
SrsSTCoroutine::~SrsSTCoroutine()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
|
|
|
stop();
|
2017-06-11 10:44:20 +00:00
|
|
|
|
|
|
|
srs_freep(trd_err);
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsSTCoroutine::start()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-05-29 09:19:06 +00:00
|
|
|
|
|
|
|
if (started || disposed) {
|
2019-04-07 05:52:16 +00:00
|
|
|
if (disposed) {
|
|
|
|
err = srs_error_new(ERROR_THREAD_DISPOSED, "disposed");
|
|
|
|
} else {
|
|
|
|
err = srs_error_new(ERROR_THREAD_STARTED, "started");
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if (trd_err == srs_success) {
|
|
|
|
trd_err = srs_error_copy(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 04:59:37 +00:00
|
|
|
if ((trd = (srs_thread_t)st_thread_create(pfn, this, 1, 0)) == NULL) {
|
2017-06-11 10:44:20 +00:00
|
|
|
err = srs_error_new(ERROR_ST_CREATE_CYCLE_THREAD, "create failed");
|
|
|
|
|
|
|
|
srs_freep(trd_err);
|
|
|
|
trd_err = srs_error_copy(err);
|
|
|
|
|
|
|
|
return err;
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
return err;
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
void SrsSTCoroutine::stop()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
2019-04-07 04:59:37 +00:00
|
|
|
if (disposed) {
|
2017-05-29 09:19:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
|
|
|
|
interrupt();
|
|
|
|
|
|
|
|
void* res = NULL;
|
2019-04-07 04:59:37 +00:00
|
|
|
// When not started, the rd is NULL.
|
|
|
|
if (trd) {
|
|
|
|
int r0 = st_thread_join((st_thread_t)trd, &res);
|
|
|
|
srs_assert(!r0);
|
|
|
|
}
|
2017-06-11 10:44:20 +00:00
|
|
|
|
|
|
|
// Always override the error by the error from worker.
|
2019-04-06 08:10:33 +00:00
|
|
|
srs_error_t err_res = (srs_error_t)res;
|
|
|
|
if (err_res != srs_success && trd_err != err_res) {
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_freep(trd_err);
|
2019-04-06 08:10:33 +00:00
|
|
|
// It's ok to directly use it, because it's returned by st_thread_join.
|
|
|
|
trd_err = err_res;
|
2017-06-11 10:44:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-05-29 09:19:06 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
// If there's no error occur from worker, try to set to interrupted error.
|
|
|
|
if (trd_err == srs_success) {
|
|
|
|
trd_err = srs_error_new(ERROR_THREAD_TERMINATED, "terminated");
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
void SrsSTCoroutine::interrupt()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
|
|
|
if (!started || interrupted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
interrupted = true;
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if (trd_err == srs_success) {
|
|
|
|
trd_err = srs_error_new(ERROR_THREAD_INTERRUPED, "interrupted");
|
|
|
|
}
|
|
|
|
|
2017-05-30 01:05:02 +00:00
|
|
|
st_thread_interrupt((st_thread_t)trd);
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsSTCoroutine::pull()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
return srs_error_copy(trd_err);
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
int SrsSTCoroutine::cid()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsSTCoroutine::cycle()
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
2017-05-29 12:33:32 +00:00
|
|
|
if (_srs_context) {
|
|
|
|
if (context) {
|
|
|
|
_srs_context->set_id(context);
|
|
|
|
} else {
|
|
|
|
context = _srs_context->generate_id();
|
|
|
|
}
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = handler->cycle();
|
|
|
|
if (err != srs_success) {
|
|
|
|
return srs_error_wrap(err, "coroutine cycle");
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
void* SrsSTCoroutine::pfn(void* arg)
|
2017-05-29 09:19:06 +00:00
|
|
|
{
|
2017-06-04 11:13:56 +00:00
|
|
|
SrsSTCoroutine* p = (SrsSTCoroutine*)arg;
|
2019-04-06 08:10:33 +00:00
|
|
|
|
|
|
|
srs_error_t err = p->cycle();
|
|
|
|
|
|
|
|
// Set the err for function pull to fetch it.
|
|
|
|
// @see https://github.com/ossrs/srs/pull/1304#issuecomment-480484151
|
|
|
|
if (err != srs_success) {
|
|
|
|
srs_freep(p->trd_err);
|
|
|
|
// It's ok to directly use it, because it's returned by st_thread_join.
|
|
|
|
p->trd_err = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*)err;
|
2017-05-29 09:19:06 +00:00
|
|
|
}
|
|
|
|
|