Blame view

trunk/src/app/srs_app_thread.cpp 3.7 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_app_thread.hpp>
25
26
#include <srs_kernel_error.hpp>
27
#include <srs_kernel_log.hpp>
28 29 30 31 32 33 34 35 36

ISrsThreadHandler::ISrsThreadHandler()
{
}

ISrsThreadHandler::~ISrsThreadHandler()
{
}
37
void ISrsThreadHandler::on_thread_start()
38 39 40 41 42
{
}

int ISrsThreadHandler::on_before_cycle()
{
43 44
    int ret = ERROR_SUCCESS;
    return ret;
45 46 47 48
}

int ISrsThreadHandler::on_end_cycle()
{
49 50
    int ret = ERROR_SUCCESS;
    return ret;
51 52
}
53
void ISrsThreadHandler::on_thread_stop()
54 55 56
{
}
57
SrsThread::SrsThread(ISrsThreadHandler* thread_handler, int64_t interval_us)
58
{
59 60 61 62 63
    handler = thread_handler;
    cycle_interval_us = interval_us;
    
    tid = NULL;
    loop = false;
64 65 66 67
}

SrsThread::~SrsThread()
{
68
    stop();
69 70 71 72
}

int SrsThread::start()
{
73 74
    int ret = ERROR_SUCCESS;
    
75 76 77 78 79 80
    if(tid) {
        srs_info("thread already running.");
        return ret;
    }
    
    if((tid = st_thread_create(thread_fun, this, 1, 0)) == NULL){
81
        ret = ERROR_ST_CREATE_CYCLE_THREAD;
82 83 84 85 86 87 88 89 90
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
    return ret;
}

void SrsThread::stop()
{
91 92 93 94 95 96 97 98 99 100 101 102
    if (tid) {
        loop = false;
        
        // the interrupt will cause the socket to read/write error,
        // which will terminate the cycle thread.
        st_thread_interrupt(tid);
        
        // wait the thread to exit.
        st_thread_join(tid, NULL);
        
        tid = NULL;
    }
103 104
}
105 106
bool SrsThread::can_loop()
{
107
    return loop;
108 109
}
110 111
void SrsThread::thread_cycle()
{
112 113 114 115 116
    int ret = ERROR_SUCCESS;
    
    srs_assert(handler);
    
    _srs_context->generate_id();
117
    srs_info("thread cycle start");
118
119
    handler->on_thread_start();
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    
    loop = true;
    while (loop) {
        if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) {
            srs_warn("thread on before cycle failed, ignored and retry, ret=%d", ret);
            goto failed;
        }
        srs_info("thread on before cycle success");
        
        if ((ret = handler->cycle()) != ERROR_SUCCESS) {
            srs_warn("thread cycle failed, ignored and retry, ret=%d", ret);
            goto failed;
        }
        srs_info("thread cycle success");
        
        if ((ret = handler->on_end_cycle()) != ERROR_SUCCESS) {
            srs_warn("thread on end cycle failed, ignored and retry, ret=%d", ret);
            goto failed;
        }
        srs_info("thread on end cycle success");
140 141

failed:
142 143 144 145 146 147 148
        if (!loop) {
            break;
        }
        
        st_usleep(cycle_interval_us);
    }
    
149
    handler->on_thread_stop();
150
    srs_trace("thread cycle finished");
151 152 153 154
}

void* SrsThread::thread_fun(void* arg)
{
155 156 157 158 159 160
    SrsThread* obj = (SrsThread*)arg;
    srs_assert(obj);
    
    obj->thread_cycle();
    
    return NULL;
161
}