comparison stream/cache2.c @ 31144:c2d5a1f6360b

Extract the cache main loop into a separate function.
author reimar
date Sun, 23 May 2010 21:58:50 +0000
parents 355302f83219
children 47d2e52f61b4
comparison
equal deleted inserted replaced
31143:48622875aecc 31144:c2d5a1f6360b
343 343
344 static void dummy_sighandler(int x) { 344 static void dummy_sighandler(int x) {
345 } 345 }
346 346
347 /** 347 /**
348 * Main loop of the cache process or thread.
349 */
350 static void cache_mainloop(cache_vars_t *s) {
351 int sleep_count = 0;
352 do {
353 if (!cache_fill(s)) {
354 if (sleep_count < INITIAL_FILL_USLEEP_COUNT) {
355 sleep_count++;
356 usec_sleep(INITIAL_FILL_USLEEP_TIME);
357 } else
358 usec_sleep(FILL_USLEEP_TIME); // idle
359 } else
360 sleep_count = 0;
361 // cache_stats(s->cache_data);
362 } while (cache_execute_control(s));
363 }
364
365 /**
348 * \return 1 on success, 0 if the function was interrupted and -1 on error 366 * \return 1 on success, 0 if the function was interrupted and -1 on error
349 */ 367 */
350 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){ 368 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
351 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE; 369 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
352 int res = -1; 370 int res = -1;
434 use_gui = 0; // mp_msg may not use gui stuff in forked code 452 use_gui = 0; // mp_msg may not use gui stuff in forked code
435 #endif 453 #endif
436 // cache thread mainloop: 454 // cache thread mainloop:
437 signal(SIGTERM,exit_sighandler); // kill 455 signal(SIGTERM,exit_sighandler); // kill
438 signal(SIGUSR1, dummy_sighandler); // wakeup 456 signal(SIGUSR1, dummy_sighandler); // wakeup
439 { 457 cache_mainloop(s);
440 int sleep_count = 0;
441 do {
442 if(!cache_fill(s)){
443 if (sleep_count < INITIAL_FILL_USLEEP_COUNT) {
444 sleep_count++;
445 usec_sleep(INITIAL_FILL_USLEEP_TIME);
446 } else
447 usec_sleep(FILL_USLEEP_TIME); // idle
448 } else
449 sleep_count = 0;
450 // cache_stats(s->cache_data);
451 } while (cache_execute_control(s));
452 #if defined(__MINGW32__) || defined(__OS2__) 458 #if defined(__MINGW32__) || defined(__OS2__)
453 _endthread(); 459 _endthread();
454 #elif defined(PTHREAD_CACHE) 460 #elif defined(PTHREAD_CACHE)
455 return NULL; 461 return NULL;
456 #else 462 #else
457 // make sure forked code never leaves this function 463 // make sure forked code never leaves this function
458 exit(0); 464 exit(0);
459 #endif 465 #endif
460 }
461 } 466 }
462 467
463 int cache_stream_fill_buffer(stream_t *s){ 468 int cache_stream_fill_buffer(stream_t *s){
464 int len; 469 int len;
465 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; } 470 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }