comparison stream/cache2.c @ 27770:c8d4cace053d

Avoid CreateThread and especially TerminateThread since they cause a memleak.
author reimar
date Sun, 19 Oct 2008 18:05:45 +0000
parents 80d3dab37bb2
children 298a3cd86bbb
comparison
equal deleted inserted replaced
27769:80d3dab37bb2 27770:c8d4cace053d
18 18
19 #include "osdep/shmem.h" 19 #include "osdep/shmem.h"
20 #include "osdep/timer.h" 20 #include "osdep/timer.h"
21 #if defined(__MINGW32__) || defined(__CYGWIN__) 21 #if defined(__MINGW32__) || defined(__CYGWIN__)
22 #include <windows.h> 22 #include <windows.h>
23 static DWORD WINAPI ThreadProc(void* s); 23 static void ThreadProc( void *s );
24 #elif defined(__OS2__) 24 #elif defined(__OS2__)
25 #define INCL_DOS 25 #define INCL_DOS
26 #include <os2.h> 26 #include <os2.h>
27 static void ThreadProc( void *s ); 27 static void ThreadProc( void *s );
28 #else 28 #else
196 196
197 return len; 197 return len;
198 198
199 } 199 }
200 200
201 static void cache_execute_control(cache_vars_t *s) { 201 static int cache_execute_control(cache_vars_t *s) {
202 int res = 1;
202 static unsigned last; 203 static unsigned last;
203 if (!s->stream->control) { 204 if (!s->stream->control) {
204 s->stream_time_length = 0; 205 s->stream_time_length = 0;
205 s->control_new_pos = 0; 206 s->control_new_pos = 0;
206 s->control_res = STREAM_UNSUPPORTED; 207 s->control_res = STREAM_UNSUPPORTED;
207 s->control = -1; 208 s->control = -1;
208 return; 209 return res;
209 } 210 }
210 if (GetTimerMS() - last > 99) { 211 if (GetTimerMS() - last > 99) {
211 double len; 212 double len;
212 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK) 213 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
213 s->stream_time_length = len; 214 s->stream_time_length = len;
214 else 215 else
215 s->stream_time_length = 0; 216 s->stream_time_length = 0;
216 last = GetTimerMS(); 217 last = GetTimerMS();
217 } 218 }
218 if (s->control == -1) return; 219 if (s->control == -1) return res;
219 switch (s->control) { 220 switch (s->control) {
220 case STREAM_CTRL_GET_CURRENT_TIME: 221 case STREAM_CTRL_GET_CURRENT_TIME:
221 case STREAM_CTRL_SEEK_TO_TIME: 222 case STREAM_CTRL_SEEK_TO_TIME:
222 case STREAM_CTRL_GET_ASPECT_RATIO: 223 case STREAM_CTRL_GET_ASPECT_RATIO:
223 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg); 224 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
228 case STREAM_CTRL_GET_NUM_ANGLES: 229 case STREAM_CTRL_GET_NUM_ANGLES:
229 case STREAM_CTRL_GET_ANGLE: 230 case STREAM_CTRL_GET_ANGLE:
230 case STREAM_CTRL_SET_ANGLE: 231 case STREAM_CTRL_SET_ANGLE:
231 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg); 232 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
232 break; 233 break;
234 case -2:
235 res = 0;
233 default: 236 default:
234 s->control_res = STREAM_UNSUPPORTED; 237 s->control_res = STREAM_UNSUPPORTED;
235 break; 238 break;
236 } 239 }
237 s->control_new_pos = s->stream->pos; 240 s->control_new_pos = s->stream->pos;
238 s->control = -1; 241 s->control = -1;
242 return res;
239 } 243 }
240 244
241 cache_vars_t* cache_init(int size,int sector){ 245 cache_vars_t* cache_init(int size,int sector){
242 int num; 246 int num;
243 #if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__) 247 #if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
275 } 279 }
276 280
277 void cache_uninit(stream_t *s) { 281 void cache_uninit(stream_t *s) {
278 cache_vars_t* c = s->cache_data; 282 cache_vars_t* c = s->cache_data;
279 if(!s->cache_pid) return; 283 if(!s->cache_pid) return;
280 #if defined(__MINGW32__) || defined(__CYGWIN__) 284 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
281 TerminateThread((HANDLE)s->cache_pid,0); 285 cache_do_control(s, -2, NULL);
282 #elif defined(__OS2__)
283 DosKillThread( s->cache_pid );
284 DosWaitThread( &s->cache_pid, DCWW_WAIT );
285 #else 286 #else
286 kill(s->cache_pid,SIGKILL); 287 kill(s->cache_pid,SIGKILL);
287 waitpid(s->cache_pid,NULL,0); 288 waitpid(s->cache_pid,NULL,0);
288 #endif 289 #endif
289 if(!c) return; 290 if(!c) return;
330 331
331 #if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__) 332 #if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
332 if((stream->cache_pid=fork())){ 333 if((stream->cache_pid=fork())){
333 #else 334 #else
334 { 335 {
335 #if defined(__MINGW32__) || defined(__CYGWIN__)
336 DWORD threadId;
337 #endif
338 stream_t* stream2=malloc(sizeof(stream_t)); 336 stream_t* stream2=malloc(sizeof(stream_t));
339 memcpy(stream2,s->stream,sizeof(stream_t)); 337 memcpy(stream2,s->stream,sizeof(stream_t));
340 s->stream=stream2; 338 s->stream=stream2;
341 #if defined(__MINGW32__) || defined(__CYGWIN__) 339 #if defined(__MINGW32__) || defined(__CYGWIN__)
342 stream->cache_pid = CreateThread(NULL,0,ThreadProc,s,0,&threadId); 340 stream->cache_pid = _beginthread( ThreadProc, 0, s );
343 #else // OS2 341 #else
344 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s ); 342 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
345 #endif 343 #endif
346 #endif 344 #endif
347 // wait until cache is filled at least prefill_init % 345 // wait until cache is filled at least prefill_init %
348 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n", 346 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
360 return 1; // parent exits 358 return 1; // parent exits
361 } 359 }
362 360
363 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__) 361 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
364 } 362 }
365 #if defined(__MINGW32__) || defined(__CYGWIN__)
366 static DWORD WINAPI ThreadProc(void*s){
367 #else // OS2
368 static void ThreadProc( void *s ){ 363 static void ThreadProc( void *s ){
369 #endif
370 #endif 364 #endif
371 365
372 #ifdef CONFIG_GUI 366 #ifdef CONFIG_GUI
373 use_gui = 0; // mp_msg may not use gui stuff in forked code 367 use_gui = 0; // mp_msg may not use gui stuff in forked code
374 #endif 368 #endif
375 // cache thread mainloop: 369 // cache thread mainloop:
376 signal(SIGTERM,exit_sighandler); // kill 370 signal(SIGTERM,exit_sighandler); // kill
377 while(1){ 371 do {
378 if(!cache_fill(s)){ 372 if(!cache_fill(s)){
379 usec_sleep(FILL_USLEEP_TIME); // idle 373 usec_sleep(FILL_USLEEP_TIME); // idle
380 } 374 }
381 cache_execute_control(s);
382 // cache_stats(s->cache_data); 375 // cache_stats(s->cache_data);
383 } 376 } while (cache_execute_control(s));
377 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
378 _endthread();
379 #endif
384 } 380 }
385 381
386 int cache_stream_fill_buffer(stream_t *s){ 382 int cache_stream_fill_buffer(stream_t *s){
387 int len; 383 int len;
388 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; } 384 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
453 case STREAM_CTRL_GET_NUM_CHAPTERS: 449 case STREAM_CTRL_GET_NUM_CHAPTERS:
454 case STREAM_CTRL_GET_CURRENT_CHAPTER: 450 case STREAM_CTRL_GET_CURRENT_CHAPTER:
455 case STREAM_CTRL_GET_ASPECT_RATIO: 451 case STREAM_CTRL_GET_ASPECT_RATIO:
456 case STREAM_CTRL_GET_NUM_ANGLES: 452 case STREAM_CTRL_GET_NUM_ANGLES:
457 case STREAM_CTRL_GET_ANGLE: 453 case STREAM_CTRL_GET_ANGLE:
454 case -2:
458 s->control = cmd; 455 s->control = cmd;
459 break; 456 break;
460 default: 457 default:
461 return STREAM_UNSUPPORTED; 458 return STREAM_UNSUPPORTED;
462 } 459 }