Blame view

trunk/src/app/srs_app_thread.cpp 5.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(const char* name, ISrsThreadHandler* thread_handler, int64_t interval_us, bool joinable)
58
{
59
    _name = name;
60 61 62 63 64
    handler = thread_handler;
    cycle_interval_us = interval_us;
    
    tid = NULL;
    loop = false;
65
    _cid = -1;
66
    _joinable = joinable;
67 68 69 70 71 72
    
    // 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.
    // @see https://github.com/winlinvip/simple-rtmp-server/issues/110
    // thread will set _cid, callback on_thread_start(), then wait for the can_run signal.
    can_run = false;
73 74 75 76
}

SrsThread::~SrsThread()
{
77
    stop();
78 79
}
80 81 82 83 84
int SrsThread::cid()
{
    return _cid;
}
85 86
int SrsThread::start()
{
87 88
    int ret = ERROR_SUCCESS;
    
89
    if(tid) {
90
        srs_info("thread %s already running.", _name);
91 92 93
        return ret;
    }
    
94
    if((tid = st_thread_create(thread_fun, this, (_joinable? 1:0), 0)) == NULL){
95
        ret = ERROR_ST_CREATE_CYCLE_THREAD;
96 97 98 99
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
100 101 102
    // we set to loop to true for thread to run.
    loop = true;
    
103
    // wait for cid to ready, for parent thread to get the cid.
104
    while (_cid < 0 && loop) {
105
        st_usleep(10 * 1000);
106 107
    }
    
108 109 110
    // now, cycle thread can run.
    can_run = true;
    
111 112 113 114 115
    return ret;
}

void SrsThread::stop()
{
116 117 118 119 120 121 122 123 124 125 126 127
    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;
    }
128 129
}
130 131
bool SrsThread::can_loop()
{
132
    return loop;
133 134
}
135 136 137 138 139
void SrsThread::stop_loop()
{
    loop = false;
}
140 141
void SrsThread::thread_cycle()
{
142 143 144
    int ret = ERROR_SUCCESS;
    
    _srs_context->generate_id();
145
    srs_info("thread %s cycle start", _name);
winlin authored
146 147 148 149
    
    _cid = _srs_context->get_id();
    
    srs_assert(handler);
150
    handler->on_thread_start();
151
    
152 153
    // wait for cid to ready, for parent thread to get the cid.
    while (!can_run && loop) {
154
        st_usleep(10 * 1000);
155 156
    }
    
157 158
    while (loop) {
        if ((ret = handler->on_before_cycle()) != ERROR_SUCCESS) {
159
            srs_warn("thread %s on before cycle failed, ignored and retry, ret=%d", _name, ret);
160 161
            goto failed;
        }
162
        srs_info("thread %s on before cycle success");
163 164
        
        if ((ret = handler->cycle()) != ERROR_SUCCESS) {
165
            if (!srs_is_client_gracefully_close(ret)) {
166
                srs_warn("thread cycle failed, ignored and retry, ret=%d", _name, ret);
167
            }
168 169
            goto failed;
        }
170
        srs_info("thread %s cycle success", _name);
171 172
        
        if ((ret = handler->on_end_cycle()) != ERROR_SUCCESS) {
173
            srs_warn("thread %s on end cycle failed, ignored and retry, ret=%d", _name, ret);
174 175
            goto failed;
        }
176
        srs_info("thread %s on end cycle success", _name);
177 178

failed:
179 180 181 182
        if (!loop) {
            break;
        }
        
183 184
        // to improve performance, donot sleep when interval is zero.
        // @see: https://github.com/winlinvip/simple-rtmp-server/issues/237
185
        if (cycle_interval_us != 0) {
186 187
            st_usleep(cycle_interval_us);
        }
188 189
    }
    
190
    handler->on_thread_stop();
191
    srs_info("thread %s cycle finished", _name);
192 193 194 195
}

void* SrsThread::thread_fun(void* arg)
{
196 197 198 199
    SrsThread* obj = (SrsThread*)arg;
    srs_assert(obj);
    
    obj->thread_cycle();
200
201
    st_thread_exit(NULL);
202 203
    
    return NULL;
204
}
205