winlin

refine the thread, set to disposed when terminate normally

@@ -66,6 +66,7 @@ namespace internal { @@ -66,6 +66,7 @@ namespace internal {
66 really_terminated = true; 66 really_terminated = true;
67 _cid = -1; 67 _cid = -1;
68 _joinable = joinable; 68 _joinable = joinable;
  69 + disposed = false;
69 70
70 // in start(), the thread cycle method maybe stop and remove the thread itself, 71 // in start(), the thread cycle method maybe stop and remove the thread itself,
71 // and the thread start() is waiting for the _cid, and segment fault then. 72 // and the thread start() is waiting for the _cid, and segment fault then.
@@ -115,38 +116,15 @@ namespace internal { @@ -115,38 +116,15 @@ namespace internal {
115 116
116 void SrsThread::stop() 117 void SrsThread::stop()
117 { 118 {
118 - if (tid) {  
119 - loop = false;  
120 -  
121 - // the interrupt will cause the socket to read/write error,  
122 - // which will terminate the cycle thread.  
123 - st_thread_interrupt(tid);  
124 -  
125 - // when joinable, wait util quit.  
126 - if (_joinable) {  
127 - // wait the thread to exit.  
128 - int ret = st_thread_join(tid, NULL);  
129 - if (ret) {  
130 - srs_warn("core: ignore join thread failed.");  
131 - }  
132 - }  
133 -  
134 - // wait the thread actually terminated.  
135 - // sometimes the thread join return -1, for example,  
136 - // when thread use st_recvfrom, the thread join return -1.  
137 - // so here, we use a variable to ensure the thread stopped.  
138 - // @remark even the thread not joinable, we must ensure the thread stopped when stop.  
139 - while (!really_terminated) {  
140 - st_usleep(10 * 1000);  
141 -  
142 - if (really_terminated) {  
143 - break;  
144 - }  
145 - srs_warn("core: wait thread to actually terminated");  
146 - }  
147 -  
148 - tid = NULL; 119 + if (!tid) {
  120 + return;
149 } 121 }
  122 +
  123 + loop = false;
  124 +
  125 + dispose();
  126 +
  127 + tid = NULL;
150 } 128 }
151 129
152 bool SrsThread::can_loop() 130 bool SrsThread::can_loop()
@@ -159,6 +137,42 @@ namespace internal { @@ -159,6 +137,42 @@ namespace internal {
159 loop = false; 137 loop = false;
160 } 138 }
161 139
  140 + void SrsThread::dispose()
  141 + {
  142 + if (disposed) {
  143 + return;
  144 + }
  145 +
  146 + // the interrupt will cause the socket to read/write error,
  147 + // which will terminate the cycle thread.
  148 + st_thread_interrupt(tid);
  149 +
  150 + // when joinable, wait util quit.
  151 + if (_joinable) {
  152 + // wait the thread to exit.
  153 + int ret = st_thread_join(tid, NULL);
  154 + if (ret) {
  155 + srs_warn("core: ignore join thread failed.");
  156 + }
  157 + }
  158 +
  159 + // wait the thread actually terminated.
  160 + // sometimes the thread join return -1, for example,
  161 + // when thread use st_recvfrom, the thread join return -1.
  162 + // so here, we use a variable to ensure the thread stopped.
  163 + // @remark even the thread not joinable, we must ensure the thread stopped when stop.
  164 + while (!really_terminated) {
  165 + st_usleep(10 * 1000);
  166 +
  167 + if (really_terminated) {
  168 + break;
  169 + }
  170 + srs_warn("core: wait thread to actually terminated");
  171 + }
  172 +
  173 + disposed = true;
  174 + }
  175 +
162 void SrsThread::thread_cycle() 176 void SrsThread::thread_cycle()
163 { 177 {
164 int ret = ERROR_SUCCESS; 178 int ret = ERROR_SUCCESS;
@@ -218,8 +232,8 @@ namespace internal { @@ -218,8 +232,8 @@ namespace internal {
218 handler->on_thread_stop(); 232 handler->on_thread_stop();
219 srs_info("thread %s cycle finished", _name); 233 srs_info("thread %s cycle finished", _name);
220 234
221 - // when thread terminated normally, set the tid to NULL.  
222 - tid = NULL; 235 + // when thread terminated normally, also disposed.
  236 + disposed = true;
223 } 237 }
224 238
225 void* SrsThread::thread_fun(void* arg) 239 void* SrsThread::thread_fun(void* arg)
@@ -100,6 +100,7 @@ namespace internal { @@ -100,6 +100,7 @@ namespace internal {
100 bool really_terminated; 100 bool really_terminated;
101 bool _joinable; 101 bool _joinable;
102 const char* _name; 102 const char* _name;
  103 + bool disposed;
103 private: 104 private:
104 ISrsThreadHandler* handler; 105 ISrsThreadHandler* handler;
105 int64_t cycle_interval_us; 106 int64_t cycle_interval_us;
@@ -154,6 +155,7 @@ namespace internal { @@ -154,6 +155,7 @@ namespace internal {
154 */ 155 */
155 virtual void stop_loop(); 156 virtual void stop_loop();
156 private: 157 private:
  158 + virtual void dispose();
157 virtual void thread_cycle(); 159 virtual void thread_cycle();
158 static void* thread_fun(void* arg); 160 static void* thread_fun(void* arg);
159 }; 161 };
@@ -905,7 +905,7 @@ namespace _srs_internal @@ -905,7 +905,7 @@ namespace _srs_internal
905 } 905 }
906 906
907 // client c1 time and version 907 // client c1 time and version
908 - time = ::time(NULL); 908 + time = (int32_t)::time(NULL);
909 version = 0x80000702; // client c1 version 909 version = 0x80000702; // client c1 version
910 910
911 // generate signature by schema 911 // generate signature by schema