Blame view

trunk/src/app/srs_app_thread.cpp 4.0 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
    _cid = -1;
65 66 67 68
}

SrsThread::~SrsThread()
{
69
    stop();
70 71
}
72 73 74 75 76
int SrsThread::cid()
{
    return _cid;
}
77 78
int SrsThread::start()
{
79 80
    int ret = ERROR_SUCCESS;
    
81 82 83 84 85 86
    if(tid) {
        srs_info("thread already running.");
        return ret;
    }
    
    if((tid = st_thread_create(thread_fun, this, 1, 0)) == NULL){
87
        ret = ERROR_ST_CREATE_CYCLE_THREAD;
88 89 90 91
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
92 93 94
    // we set to loop to true for thread to run.
    loop = true;
    
95 96 97 98 99
    // wait for cid to ready, for parent thread to get the cid.
    while (_cid < 0) {
        st_usleep(10 * SRS_TIME_MILLISECONDS);
    }
    
100 101 102 103 104
    return ret;
}

void SrsThread::stop()
{
105 106 107 108 109 110 111 112 113 114 115 116
    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;
    }
117 118
}
119 120
bool SrsThread::can_loop()
{
121
    return loop;
122 123
}
124 125
void SrsThread::thread_cycle()
{
126 127 128
    int ret = ERROR_SUCCESS;
    
    _srs_context->generate_id();
129
    srs_info("thread cycle start");
winlin authored
130 131 132 133
    
    _cid = _srs_context->get_id();
    
    srs_assert(handler);
134
    handler->on_thread_start();
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    
    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");
154 155

failed:
156 157 158 159 160 161 162
        if (!loop) {
            break;
        }
        
        st_usleep(cycle_interval_us);
    }
    
163
    handler->on_thread_stop();
164
    srs_trace("thread cycle finished");
165 166 167 168
}

void* SrsThread::thread_fun(void* arg)
{
169 170 171 172 173 174
    SrsThread* obj = (SrsThread*)arg;
    srs_assert(obj);
    
    obj->thread_cycle();
    
    return NULL;
175
}