For #906, #902, refine thread object.

pull/902/merge
winlin 8 years ago
parent 5792c462b8
commit fc380fe48d

@ -105,14 +105,6 @@
3CA432A81E3F46DD001DA0C6 /* srs_kernel_io.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CA432A61E3F46DD001DA0C6 /* srs_kernel_io.cpp */; };
3CA432AB1E40AEBC001DA0C6 /* Makefile in Sources */ = {isa = PBXBuildFile; fileRef = 3CA432A91E40AEBC001DA0C6 /* Makefile */; };
3CA432AC1E40AEBC001DA0C6 /* srs_ingest_mp4.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA432AA1E40AEBC001DA0C6 /* srs_ingest_mp4.c */; };
3CA5F1411E65543700E442C7 /* event.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F1371E65543700E442C7 /* event.c */; };
3CA5F1421E65543700E442C7 /* io.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F1381E65543700E442C7 /* io.c */; };
3CA5F1431E65543700E442C7 /* key.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F1391E65543700E442C7 /* key.c */; };
3CA5F1441E65543700E442C7 /* Makefile in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F13A1E65543700E442C7 /* Makefile */; };
3CA5F1451E65543700E442C7 /* md.S in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F13C1E65543700E442C7 /* md.S */; };
3CA5F1461E65543700E442C7 /* sched.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F13E1E65543700E442C7 /* sched.c */; };
3CA5F1471E65543700E442C7 /* stk.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F13F1E65543700E442C7 /* stk.c */; };
3CA5F1481E65543700E442C7 /* sync.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CA5F1401E65543700E442C7 /* sync.c */; };
3CB25C2A1BB269FD00C97A63 /* jmp_sp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CB25C291BB269FD00C97A63 /* jmp_sp.cpp */; };
3CC52DD81ACE4023006FEB01 /* srs_utest_amf0.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CC52DCA1ACE4023006FEB01 /* srs_utest_amf0.cpp */; };
3CC52DD91ACE4023006FEB01 /* srs_utest_config.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CC52DCC1ACE4023006FEB01 /* srs_utest_config.cpp */; };
@ -132,6 +124,14 @@
3CE893C51E876C39000B742D /* srs_service_rtmp_conn.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CE893C31E876C39000B742D /* srs_service_rtmp_conn.cpp */; };
3CE893C81E876D04000B742D /* srs_service_utility.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CE893C61E876D04000B742D /* srs_service_utility.cpp */; };
3CE893CB1E8770E2000B742D /* srs_service_conn.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CE893C91E8770E2000B742D /* srs_service_conn.cpp */; };
3CECAF991EDC100F00C50501 /* event.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF8F1EDC100F00C50501 /* event.c */; };
3CECAF9A1EDC100F00C50501 /* io.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF901EDC100F00C50501 /* io.c */; };
3CECAF9B1EDC100F00C50501 /* key.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF911EDC100F00C50501 /* key.c */; };
3CECAF9C1EDC100F00C50501 /* Makefile in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF921EDC100F00C50501 /* Makefile */; };
3CECAF9D1EDC100F00C50501 /* md.S in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF941EDC100F00C50501 /* md.S */; };
3CECAF9E1EDC100F00C50501 /* sched.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF961EDC100F00C50501 /* sched.c */; };
3CECAF9F1EDC100F00C50501 /* stk.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF971EDC100F00C50501 /* stk.c */; };
3CECAFA01EDC100F00C50501 /* sync.c in Sources */ = {isa = PBXBuildFile; fileRef = 3CECAF981EDC100F00C50501 /* sync.c */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -397,17 +397,6 @@
3CA432A71E3F46DD001DA0C6 /* srs_kernel_io.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_kernel_io.hpp; path = ../../../src/kernel/srs_kernel_io.hpp; sourceTree = "<group>"; };
3CA432A91E40AEBC001DA0C6 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; name = Makefile; path = ../../../research/librtmp/Makefile; sourceTree = "<group>"; };
3CA432AA1E40AEBC001DA0C6 /* srs_ingest_mp4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = srs_ingest_mp4.c; path = ../../../research/librtmp/srs_ingest_mp4.c; sourceTree = "<group>"; };
3CA5F1361E65543700E442C7 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = common.h; path = "../../../objs/st-1.9/common.h"; sourceTree = "<group>"; };
3CA5F1371E65543700E442C7 /* event.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = event.c; path = "../../../objs/st-1.9/event.c"; sourceTree = "<group>"; };
3CA5F1381E65543700E442C7 /* io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = io.c; path = "../../../objs/st-1.9/io.c"; sourceTree = "<group>"; };
3CA5F1391E65543700E442C7 /* key.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = key.c; path = "../../../objs/st-1.9/key.c"; sourceTree = "<group>"; };
3CA5F13A1E65543700E442C7 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; name = Makefile; path = "../../../objs/st-1.9/Makefile"; sourceTree = "<group>"; };
3CA5F13B1E65543700E442C7 /* md.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = md.h; path = "../../../objs/st-1.9/md.h"; sourceTree = "<group>"; };
3CA5F13C1E65543700E442C7 /* md.S */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = md.S; path = "../../../objs/st-1.9/md.S"; sourceTree = "<group>"; };
3CA5F13D1E65543700E442C7 /* public.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = public.h; path = "../../../objs/st-1.9/public.h"; sourceTree = "<group>"; };
3CA5F13E1E65543700E442C7 /* sched.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sched.c; path = "../../../objs/st-1.9/sched.c"; sourceTree = "<group>"; };
3CA5F13F1E65543700E442C7 /* stk.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = stk.c; path = "../../../objs/st-1.9/stk.c"; sourceTree = "<group>"; };
3CA5F1401E65543700E442C7 /* sync.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sync.c; path = "../../../objs/st-1.9/sync.c"; sourceTree = "<group>"; };
3CB25C281BB2596300C97A63 /* setup_variables.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = setup_variables.sh; path = ../../../auto/setup_variables.sh; sourceTree = "<group>"; };
3CB25C291BB269FD00C97A63 /* jmp_sp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = jmp_sp.cpp; path = ../../../research/arm/jmp_sp.cpp; sourceTree = "<group>"; };
3CC52DCA1ACE4023006FEB01 /* srs_utest_amf0.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_utest_amf0.cpp; path = ../../src/utest/srs_utest_amf0.cpp; sourceTree = "<group>"; };
@ -445,6 +434,17 @@
3CE893C71E876D04000B742D /* srs_service_utility.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_service_utility.hpp; path = ../../../src/service/srs_service_utility.hpp; sourceTree = "<group>"; };
3CE893C91E8770E2000B742D /* srs_service_conn.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_service_conn.cpp; path = ../../../src/service/srs_service_conn.cpp; sourceTree = "<group>"; };
3CE893CA1E8770E2000B742D /* srs_service_conn.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_service_conn.hpp; path = ../../../src/service/srs_service_conn.hpp; sourceTree = "<group>"; };
3CECAF8E1EDC100F00C50501 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = common.h; path = "../../../objs/state-threads-1.9.1/common.h"; sourceTree = "<group>"; };
3CECAF8F1EDC100F00C50501 /* event.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = event.c; path = "../../../objs/state-threads-1.9.1/event.c"; sourceTree = "<group>"; };
3CECAF901EDC100F00C50501 /* io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = io.c; path = "../../../objs/state-threads-1.9.1/io.c"; sourceTree = "<group>"; };
3CECAF911EDC100F00C50501 /* key.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = key.c; path = "../../../objs/state-threads-1.9.1/key.c"; sourceTree = "<group>"; };
3CECAF921EDC100F00C50501 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; name = Makefile; path = "../../../objs/state-threads-1.9.1/Makefile"; sourceTree = "<group>"; };
3CECAF931EDC100F00C50501 /* md.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = md.h; path = "../../../objs/state-threads-1.9.1/md.h"; sourceTree = "<group>"; };
3CECAF941EDC100F00C50501 /* md.S */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = md.S; path = "../../../objs/state-threads-1.9.1/md.S"; sourceTree = "<group>"; };
3CECAF951EDC100F00C50501 /* public.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = public.h; path = "../../../objs/state-threads-1.9.1/public.h"; sourceTree = "<group>"; };
3CECAF961EDC100F00C50501 /* sched.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sched.c; path = "../../../objs/state-threads-1.9.1/sched.c"; sourceTree = "<group>"; };
3CECAF971EDC100F00C50501 /* stk.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = stk.c; path = "../../../objs/state-threads-1.9.1/stk.c"; sourceTree = "<group>"; };
3CECAF981EDC100F00C50501 /* sync.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sync.c; path = "../../../objs/state-threads-1.9.1/sync.c"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -883,17 +883,17 @@
3C7175A61E1DEA0500E8C49F /* st */ = {
isa = PBXGroup;
children = (
3CA5F1361E65543700E442C7 /* common.h */,
3CA5F1371E65543700E442C7 /* event.c */,
3CA5F1381E65543700E442C7 /* io.c */,
3CA5F1391E65543700E442C7 /* key.c */,
3CA5F13A1E65543700E442C7 /* Makefile */,
3CA5F13B1E65543700E442C7 /* md.h */,
3CA5F13C1E65543700E442C7 /* md.S */,
3CA5F13D1E65543700E442C7 /* public.h */,
3CA5F13E1E65543700E442C7 /* sched.c */,
3CA5F13F1E65543700E442C7 /* stk.c */,
3CA5F1401E65543700E442C7 /* sync.c */,
3CECAF8E1EDC100F00C50501 /* common.h */,
3CECAF8F1EDC100F00C50501 /* event.c */,
3CECAF901EDC100F00C50501 /* io.c */,
3CECAF911EDC100F00C50501 /* key.c */,
3CECAF921EDC100F00C50501 /* Makefile */,
3CECAF931EDC100F00C50501 /* md.h */,
3CECAF941EDC100F00C50501 /* md.S */,
3CECAF951EDC100F00C50501 /* public.h */,
3CECAF961EDC100F00C50501 /* sched.c */,
3CECAF971EDC100F00C50501 /* stk.c */,
3CECAF981EDC100F00C50501 /* sync.c */,
);
name = st;
sourceTree = "<group>";
@ -1020,24 +1020,23 @@
3CD247C31BB3F14100DC1922 /* srs_kernel_balance.cpp in Sources */,
3C1232271AAE814D00CE8F6C /* srs_kernel_log.cpp in Sources */,
3C1232A81AAE81D900CE8F6C /* srs_app_log.cpp in Sources */,
3CECAF9D1EDC100F00C50501 /* md.S in Sources */,
3C1232A41AAE81D900CE8F6C /* srs_app_ingest.cpp in Sources */,
3CE893B51E87508D000B742D /* srs_app_dvr.cpp in Sources */,
3C0D422E1B87165900C2508B /* srs_protocol_json.cpp in Sources */,
3C1232B41AAE81D900CE8F6C /* srs_app_st.cpp in Sources */,
3C9F82221E4ECA8200F5B2D2 /* srs_app_dash.cpp in Sources */,
3C1232481AAE81A400CE8F6C /* srs_rtmp_stack.cpp in Sources */,
3CA5F1451E65543700E442C7 /* md.S in Sources */,
3C1232B01AAE81D900CE8F6C /* srs_app_security.cpp in Sources */,
3C12322B1AAE814D00CE8F6C /* srs_kernel_utility.cpp in Sources */,
3CA5F1411E65543700E442C7 /* event.c in Sources */,
3C12324A1AAE81A400CE8F6C /* srs_rtsp_stack.cpp in Sources */,
3CE893BF1E876A97000B742D /* srs_service_http_client.cpp in Sources */,
3C36DB5D1ABD1CB90066CCAF /* srs_librtmp.cpp in Sources */,
3CA5F1421E65543700E442C7 /* io.c in Sources */,
3C12329F1AAE81D900CE8F6C /* srs_app_http_api.cpp in Sources */,
3CECAF991EDC100F00C50501 /* event.c in Sources */,
3C1EE6AE1AB1055800576EE9 /* srs_app_hds.cpp in Sources */,
3C663F101AB0155100286D8B /* srs_audio_raw_publish.c in Sources */,
3CA5F1431E65543700E442C7 /* key.c in Sources */,
3CECAFA01EDC100F00C50501 /* sync.c in Sources */,
3C663F111AB0155100286D8B /* srs_bandwidth_check.c in Sources */,
3CC52DDE1ACE4023006FEB01 /* srs_utest.cpp in Sources */,
3C1232A11AAE81D900CE8F6C /* srs_app_http_conn.cpp in Sources */,
@ -1056,7 +1055,6 @@
3C663F151AB0155100286D8B /* srs_h264_raw_publish.c in Sources */,
3C1231F61AAE652D00CE8F6C /* srs_core_autofree.cpp in Sources */,
3C5265B41B241BF0009CA186 /* srs_core_mem_watch.cpp in Sources */,
3CA5F1481E65543700E442C7 /* sync.c in Sources */,
3C1EE6D71AB1367D00576EE9 /* README.md in Sources */,
3C82802C1BAFF8CC004A1794 /* srs_kafka_stack.cpp in Sources */,
3C1232411AAE81A400CE8F6C /* srs_raw_avc.cpp in Sources */,
@ -1068,10 +1066,11 @@
3C12329B1AAE81D900CE8F6C /* srs_app_ffmpeg.cpp in Sources */,
3C1232421AAE81A400CE8F6C /* srs_protocol_amf0.cpp in Sources */,
3C4AB9331B8C9148006627D3 /* srs_app_ng_exec.cpp in Sources */,
3CA5F1471E65543700E442C7 /* stk.c in Sources */,
3C1232AA1AAE81D900CE8F6C /* srs_app_pithy_print.cpp in Sources */,
3CECAF9F1EDC100F00C50501 /* stk.c in Sources */,
3C12329C1AAE81D900CE8F6C /* srs_app_forward.cpp in Sources */,
3C1232251AAE814D00CE8F6C /* srs_kernel_file.cpp in Sources */,
3CECAF9A1EDC100F00C50501 /* io.c in Sources */,
3C1232AD1AAE81D900CE8F6C /* srs_app_reload.cpp in Sources */,
3CE893C81E876D04000B742D /* srs_service_utility.cpp in Sources */,
3C1231F81AAE652D00CE8F6C /* srs_core.cpp in Sources */,
@ -1087,6 +1086,7 @@
3C1232B21AAE81D900CE8F6C /* srs_app_source.cpp in Sources */,
3C1231F71AAE652D00CE8F6C /* srs_core_performance.cpp in Sources */,
3C4D184C1E73F133008806F7 /* srs_app_fragment.cpp in Sources */,
3CECAF9B1EDC100F00C50501 /* key.c in Sources */,
3CE893BC1E875108000B742D /* srs_service_st.cpp in Sources */,
3CC52DD81ACE4023006FEB01 /* srs_utest_amf0.cpp in Sources */,
3C4F97121B8B466D00FF0E46 /* srs_app_process.cpp in Sources */,
@ -1095,7 +1095,6 @@
3C1232461AAE81A400CE8F6C /* srs_rtmp_msg_array.cpp in Sources */,
3C1232A71AAE81D900CE8F6C /* srs_app_listener.cpp in Sources */,
3C1232261AAE814D00CE8F6C /* srs_kernel_flv.cpp in Sources */,
3CA5F1441E65543700E442C7 /* Makefile in Sources */,
3C036B551B2D0AC10078E2E0 /* srs_app_http_static.cpp in Sources */,
3C663F1A1AB0155100286D8B /* srs_rtmp_dump.c in Sources */,
3CE6CD311AE4AFB800706E07 /* srs_main_ingest_hls.cpp in Sources */,
@ -1110,9 +1109,11 @@
3C1232441AAE81A400CE8F6C /* srs_rtmp_handshake.cpp in Sources */,
3C1232291AAE814D00CE8F6C /* srs_kernel_buffer.cpp in Sources */,
3C663F181AB0155100286D8B /* srs_play.c in Sources */,
3CECAF9E1EDC100F00C50501 /* sched.c in Sources */,
3C1232B61AAE81D900CE8F6C /* srs_app_thread.cpp in Sources */,
3C1232A91AAE81D900CE8F6C /* srs_app_mpegts_udp.cpp in Sources */,
3CE893B91E8750A9000B742D /* srs_service_log.cpp in Sources */,
3CECAF9C1EDC100F00C50501 /* Makefile in Sources */,
3C1232AE1AAE81D900CE8F6C /* srs_app_rtmp_conn.cpp in Sources */,
3C1232B51AAE81D900CE8F6C /* srs_app_statistic.cpp in Sources */,
3C663F161AB0155100286D8B /* srs_ingest_flv.c in Sources */,
@ -1128,7 +1129,6 @@
3C068D6A1B10149F00AA722C /* srs_protocol_kbps.cpp in Sources */,
3C12329A1AAE81D900CE8F6C /* srs_app_encoder.cpp in Sources */,
3C44AACF1E3AF50200D4ABC3 /* srs_kernel_mp4.cpp in Sources */,
3CA5F1461E65543700E442C7 /* sched.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -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()

@ -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

@ -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.

Loading…
Cancel
Save