1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

fix the forwarder dead when st_thread interrupt at st_usleep, check thread->can_loop().

This commit is contained in:
winlin 2014-01-01 16:27:45 +08:00
parent fabdf9507e
commit 99b9fa0c25
3 changed files with 29 additions and 1 deletions

View file

@ -290,7 +290,7 @@ int SrsForwarder::forward()
SrsPithyPrint pithy_print(SRS_STAGE_FORWARDER); SrsPithyPrint pithy_print(SRS_STAGE_FORWARDER);
while (true) { while (pthread->can_loop()) {
// switch to other st-threads. // switch to other st-threads.
st_usleep(0); st_usleep(0);

View file

@ -102,6 +102,11 @@ void SrsThread::stop()
} }
} }
bool SrsThread::can_loop()
{
return loop;
}
void SrsThread::thread_cycle() void SrsThread::thread_cycle()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;

View file

@ -43,6 +43,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* when stop, the thread will interrupt the st_thread, * when stop, the thread will interrupt the st_thread,
* which will cause the socket to return error and * which will cause the socket to return error and
* terminate the cycle thread. * terminate the cycle thread.
*
* when thread interrupt, the socket maybe not got EINT,
* espectially on st_usleep(), so the cycle must check the loop,
* when handler->cycle() has loop itself, for example:
* handler->cycle() is:
* while (true):
* st_usleep(0);
* if (read_from_socket(skt) < 0) break;
* if thread stop when read_from_socket, it's ok, the loop will break,
* but when thread stop interrupt the s_usleep(0), then the loop is
* death loop.
* in a word, the handler->cycle() must:
* handler->cycle() is:
* while (pthread->can_loop()):
* st_usleep(0);
* if (read_from_socket(skt) < 0) break;
* check the loop, then it works.
*/ */
class ISrsThreadHandler class ISrsThreadHandler
{ {
@ -90,6 +107,12 @@ public:
* @remark user can stop multiple times, ignore if already stopped. * @remark user can stop multiple times, ignore if already stopped.
*/ */
virtual void stop(); virtual void stop();
/**
* whether the thread should loop,
* used for handler->cycle() which has a loop method,
* to check this method, break if false.
*/
virtual bool can_loop();
private: private:
virtual void thread_cycle(); virtual void thread_cycle();
static void* thread_fun(void* arg); static void* thread_fun(void* arg);