mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine the thread, set to disposed when terminate normally
This commit is contained in:
parent
83a9ff9f5d
commit
eb578b4a39
3 changed files with 50 additions and 34 deletions
|
@ -66,6 +66,7 @@ namespace internal {
|
||||||
really_terminated = true;
|
really_terminated = true;
|
||||||
_cid = -1;
|
_cid = -1;
|
||||||
_joinable = joinable;
|
_joinable = joinable;
|
||||||
|
disposed = false;
|
||||||
|
|
||||||
// in start(), the thread cycle method maybe stop and remove the thread itself,
|
// in start(), the thread cycle method maybe stop and remove the thread itself,
|
||||||
// and the thread start() is waiting for the _cid, and segment fault then.
|
// and the thread start() is waiting for the _cid, and segment fault then.
|
||||||
|
@ -115,38 +116,15 @@ namespace internal {
|
||||||
|
|
||||||
void SrsThread::stop()
|
void SrsThread::stop()
|
||||||
{
|
{
|
||||||
if (tid) {
|
if (!tid) {
|
||||||
loop = false;
|
return;
|
||||||
|
|
||||||
// the interrupt will cause the socket to read/write error,
|
|
||||||
// which will terminate the cycle thread.
|
|
||||||
st_thread_interrupt(tid);
|
|
||||||
|
|
||||||
// when joinable, wait util quit.
|
|
||||||
if (_joinable) {
|
|
||||||
// wait the thread to exit.
|
|
||||||
int ret = st_thread_join(tid, NULL);
|
|
||||||
if (ret) {
|
|
||||||
srs_warn("core: ignore join thread failed.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait the thread actually terminated.
|
|
||||||
// sometimes the thread join return -1, for example,
|
|
||||||
// when thread use st_recvfrom, the thread join return -1.
|
|
||||||
// so here, we use a variable to ensure the thread stopped.
|
|
||||||
// @remark even the thread not joinable, we must ensure the thread stopped when stop.
|
|
||||||
while (!really_terminated) {
|
|
||||||
st_usleep(10 * 1000);
|
|
||||||
|
|
||||||
if (really_terminated) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
srs_warn("core: wait thread to actually terminated");
|
|
||||||
}
|
|
||||||
|
|
||||||
tid = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loop = false;
|
||||||
|
|
||||||
|
dispose();
|
||||||
|
|
||||||
|
tid = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SrsThread::can_loop()
|
bool SrsThread::can_loop()
|
||||||
|
@ -159,6 +137,42 @@ namespace internal {
|
||||||
loop = false;
|
loop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SrsThread::dispose()
|
||||||
|
{
|
||||||
|
if (disposed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the interrupt will cause the socket to read/write error,
|
||||||
|
// which will terminate the cycle thread.
|
||||||
|
st_thread_interrupt(tid);
|
||||||
|
|
||||||
|
// when joinable, wait util quit.
|
||||||
|
if (_joinable) {
|
||||||
|
// wait the thread to exit.
|
||||||
|
int ret = st_thread_join(tid, NULL);
|
||||||
|
if (ret) {
|
||||||
|
srs_warn("core: ignore join thread failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait the thread actually terminated.
|
||||||
|
// sometimes the thread join return -1, for example,
|
||||||
|
// when thread use st_recvfrom, the thread join return -1.
|
||||||
|
// so here, we use a variable to ensure the thread stopped.
|
||||||
|
// @remark even the thread not joinable, we must ensure the thread stopped when stop.
|
||||||
|
while (!really_terminated) {
|
||||||
|
st_usleep(10 * 1000);
|
||||||
|
|
||||||
|
if (really_terminated) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
srs_warn("core: wait thread to actually terminated");
|
||||||
|
}
|
||||||
|
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
|
|
||||||
void SrsThread::thread_cycle()
|
void SrsThread::thread_cycle()
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
@ -218,8 +232,8 @@ namespace internal {
|
||||||
handler->on_thread_stop();
|
handler->on_thread_stop();
|
||||||
srs_info("thread %s cycle finished", _name);
|
srs_info("thread %s cycle finished", _name);
|
||||||
|
|
||||||
// when thread terminated normally, set the tid to NULL.
|
// when thread terminated normally, also disposed.
|
||||||
tid = NULL;
|
disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* SrsThread::thread_fun(void* arg)
|
void* SrsThread::thread_fun(void* arg)
|
||||||
|
|
|
@ -100,6 +100,7 @@ namespace internal {
|
||||||
bool really_terminated;
|
bool really_terminated;
|
||||||
bool _joinable;
|
bool _joinable;
|
||||||
const char* _name;
|
const char* _name;
|
||||||
|
bool disposed;
|
||||||
private:
|
private:
|
||||||
ISrsThreadHandler* handler;
|
ISrsThreadHandler* handler;
|
||||||
int64_t cycle_interval_us;
|
int64_t cycle_interval_us;
|
||||||
|
@ -154,6 +155,7 @@ namespace internal {
|
||||||
*/
|
*/
|
||||||
virtual void stop_loop();
|
virtual void stop_loop();
|
||||||
private:
|
private:
|
||||||
|
virtual void dispose();
|
||||||
virtual void thread_cycle();
|
virtual void thread_cycle();
|
||||||
static void* thread_fun(void* arg);
|
static void* thread_fun(void* arg);
|
||||||
};
|
};
|
||||||
|
|
|
@ -905,7 +905,7 @@ namespace _srs_internal
|
||||||
}
|
}
|
||||||
|
|
||||||
// client c1 time and version
|
// client c1 time and version
|
||||||
time = ::time(NULL);
|
time = (int32_t)::time(NULL);
|
||||||
version = 0x80000702; // client c1 version
|
version = 0x80000702; // client c1 version
|
||||||
|
|
||||||
// generate signature by schema
|
// generate signature by schema
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue