1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #906, #902, refine thread object.

This commit is contained in:
winlin 2017-05-29 17:19:06 +08:00
parent 5792c462b8
commit fc380fe48d
4 changed files with 255 additions and 38 deletions

View file

@ -31,6 +31,116 @@ using namespace std;
#include <srs_app_utility.hpp>
#include <srs_app_log.hpp>
ISrsCoroutineHandler::ISrsCoroutineHandler()
{
}
ISrsCoroutineHandler::~ISrsCoroutineHandler()
{
}
SrsCoroutine::SrsCoroutine(const string& n, ISrsCoroutineHandler* h, int cid)
{
name = n;
handler = h;
context = cid;
trd = NULL;
err = ERROR_SUCCESS;
started = interrupted = disposed = false;
}
SrsCoroutine::~SrsCoroutine()
{
stop();
}
int SrsCoroutine::start()
{
int ret = ERROR_SUCCESS;
if (started || disposed) {
ret = ERROR_THREAD_DISPOSED;
err = (err == ERROR_SUCCESS? ret:err);
srs_error("Thread.start: Failed, started=%d, disposed=%d, ret=%d", started, disposed, ret);
return ret;
}
if((trd = st_thread_create(pfn, this, 1, 0)) == NULL){
ret = ERROR_ST_CREATE_CYCLE_THREAD;
srs_error("Thread.start: Create thread failed. ret=%d", ret);
return ret;
}
started = true;
return ret;
}
void SrsCoroutine::stop()
{
if (!started || disposed) {
return;
}
disposed = true;
interrupt();
void* res = NULL;
int ret = st_thread_join(trd, &res);
srs_trace("Thread.stop: Terminated, ret=%d, err=%d", ret, err);
srs_assert(!ret);
// Always override the error by the worker.
if (!res) {
err = (int)(uint64_t)res;
} else {
err = ERROR_THREAD_TERMINATED;
}
return;
}
void SrsCoroutine::interrupt()
{
if (!started || interrupted) {
return;
}
interrupted = true;
srs_trace("Thread.interrupt: Interrupt thread, err=%d", err);
err = (err == ERROR_SUCCESS? ERROR_THREAD_INTERRUPED:err);
st_thread_interrupt(trd);
}
int SrsCoroutine::pull()
{
return err;
}
int SrsCoroutine::cid()
{
return context;
}
int SrsCoroutine::cycle()
{
if (!context && _srs_context) {
context = _srs_context->generate_id();
}
srs_trace("Thread.cycle: Start with cid=%d, err=%d", context, err);
int ret = handler->cycle();
srs_trace("Thread.cycle: Finished with ret=%d, err=%d", ret, err);
return ret;
}
void* SrsCoroutine::pfn(void* arg)
{
SrsCoroutine* p = (SrsCoroutine*)arg;
void*res = (void*)(uint64_t)p->cycle();
return res;
}
namespace internal
{
ISrsThreadHandler::ISrsThreadHandler()

View file

@ -31,6 +31,110 @@
#include <srs_service_st.hpp>
#include <srs_protocol_io.hpp>
/**
* Each ST-coroutine must implements this interface,
* to do the cycle job and handle some events.
*
* Thread do a job then terminated normally, it's a SrsOneCycleThread:
* class SrsOneCycleThread : public ISrsCoroutineHandler {
* public: SrsCoroutine trd;
* public: virtual int cycle() {
* // Do something, then return this cycle and thread terminated normally.
* }
* };
*
* Thread has its inside loop, such as the RTMP receive thread:
* class SrsReceiveThread : public ISrsCoroutineHandler {
* public: SrsCoroutine trd;
* public: virtual int cycle() {
* while (!trd.pull()) { // Check whether thread interrupted.
* // Do something, such as st_read() packets, it'll be wakeup
* // when user stop or interrupt the thread.
* }
* }
* };
*/
class ISrsCoroutineHandler
{
public:
ISrsCoroutineHandler();
virtual ~ISrsCoroutineHandler();
public:
/**
* Do the work. The ST-coroutine will terminated normally if it returned.
* @remark If the cycle has its own loop, it must check the thread pull.
*/
virtual int cycle() = 0;
};
/**
* A ST-coroutine is a lightweight thread, just like the goroutine.
* But the goroutine maybe run on different thread, while ST-coroutine only
* run in single thread, because it use setjmp and longjmp, so it may cause
* problem in multiple threads. For SRS, we only use single thread module,
* like NGINX to get very high performance, with asynchronous and non-blocking
* sockets.
* @reamrk For multiple processes, please use go-oryx to fork many SRS processes.
* Please read https://github.com/ossrs/go-oryx
* @remark For debugging of ST-coroutine, read _st_iterate_threads_flag of ST/README
* https://github.com/ossrs/state-threads/blob/st-1.9/README#L115
* @remark We always create joinable thread, so we must join it or memory leak,
* Please read https://github.com/ossrs/srs/issues/78
*/
class SrsCoroutine
{
private:
std::string name;
ISrsCoroutineHandler* handler;
private:
st_thread_t trd;
int context;
int err;
private:
bool started;
bool interrupted;
bool disposed;
public:
// Create a thread with name n and handler h.
// @remark User can specify a cid for thread to use, or we will allocate a new one.
SrsCoroutine(const std::string& n, ISrsCoroutineHandler* h, int cid = 0);
virtual ~SrsCoroutine();
public:
/**
* Start the thread.
* @remark Should never start it when stopped or terminated.
*/
virtual int start();
/**
* Interrupt the thread then wait to terminated.
* @remark If user want to notify thread to quit async, for example if there are
* many threads to stop like the encoder, use the interrupt to notify all threads
* to terminate then use stop to wait for each to terminate.
*/
virtual void stop();
/**
* Interrupt the thread and notify it to terminate, it will be wakeup if it's blocked
* in some IO operations, such as st_read or st_write, then it will found should quit,
* finally the thread should terminated normally, user can use the stop to join it.
*/
virtual void interrupt();
/**
* Check whether thread is terminated normally or error(stopped or termianted with error),
* and the thread should be running if it return ERROR_SUCCESS.
* @remark Return specified error when thread terminated normally with error.
* @remark Return ERROR_THREAD_TERMINATED when thread terminated normally without error.
* @remark Return ERROR_THREAD_INTERRUPED when thread is interrupted.
*/
virtual int pull();
/**
* Get the context id of thread.
*/
virtual int cid();
private:
virtual int cycle();
static void* pfn(void* arg);
};
// the internal classes, user should never use it.
// user should use the public classes at the bellow:
// @see SrsEndlessThread, SrsOneCycleThread, SrsReusableThread

View file

@ -103,6 +103,9 @@
#define ERROR_SYSTEM_DNS_RESOLVE 1066
#define ERROR_SYSTEM_FRAGMENT_UNLINK 1067
#define ERROR_SYSTEM_FRAGMENT_RENAME 1068
#define ERROR_THREAD_DISPOSED 1069
#define ERROR_THREAD_INTERRUPED 1070
#define ERROR_THREAD_TERMINATED 1071
///////////////////////////////////////////////////////
// RTMP protocol error.