comparison stream/cache2.c @ 31146:78d11e186bfc

Use an extra define to simplify ifdefs
author reimar
date Sun, 23 May 2010 22:09:40 +0000
parents 47d2e52f61b4
children 97660f5d9aef
comparison
equal deleted inserted replaced
31145:47d2e52f61b4 31146:78d11e186bfc
51 #elif defined(PTHREAD_CACHE) 51 #elif defined(PTHREAD_CACHE)
52 #include <pthread.h> 52 #include <pthread.h>
53 static void *ThreadProc(void *s); 53 static void *ThreadProc(void *s);
54 #else 54 #else
55 #include <sys/wait.h> 55 #include <sys/wait.h>
56 #define FORKED_CACHE 1
57 #endif
58 #ifndef FORKED_CACHE
59 #define FORKED_CACHE 0
56 #endif 60 #endif
57 61
58 #include "mp_msg.h" 62 #include "mp_msg.h"
59 #include "help_mp.h" 63 #include "help_mp.h"
60 64
94 98
95 int cache_fill_status=0; 99 int cache_fill_status=0;
96 100
97 static void cache_wakeup(stream_t *s) 101 static void cache_wakeup(stream_t *s)
98 { 102 {
99 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 103 #if FORKED_CACHE
100 // signal process to wake up immediately 104 // signal process to wake up immediately
101 kill(s->cache_pid, SIGUSR1); 105 kill(s->cache_pid, SIGUSR1);
102 #endif 106 #endif
103 } 107 }
104 108
275 return 1; 279 return 1;
276 } 280 }
277 281
278 static cache_vars_t* cache_init(int size,int sector){ 282 static cache_vars_t* cache_init(int size,int sector){
279 int num; 283 int num;
280 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 284 #if FORKED_CACHE
281 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t)); 285 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
282 #else 286 #else
283 cache_vars_t* s=malloc(sizeof(cache_vars_t)); 287 cache_vars_t* s=malloc(sizeof(cache_vars_t));
284 #endif 288 #endif
285 if(s==NULL) return NULL; 289 if(s==NULL) return NULL;
289 if(num < 16){ 293 if(num < 16){
290 num = 16; 294 num = 16;
291 }//32kb min_size 295 }//32kb min_size
292 s->buffer_size=num*sector; 296 s->buffer_size=num*sector;
293 s->sector_size=sector; 297 s->sector_size=sector;
294 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 298 #if FORKED_CACHE
295 s->buffer=shmem_alloc(s->buffer_size); 299 s->buffer=shmem_alloc(s->buffer_size);
296 #else 300 #else
297 s->buffer=malloc(s->buffer_size); 301 s->buffer=malloc(s->buffer_size);
298 #endif 302 #endif
299 303
300 if(s->buffer == NULL){ 304 if(s->buffer == NULL){
301 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 305 #if FORKED_CACHE
302 shmem_free(s,sizeof(cache_vars_t)); 306 shmem_free(s,sizeof(cache_vars_t));
303 #else 307 #else
304 free(s); 308 free(s);
305 #endif 309 #endif
306 return NULL; 310 return NULL;
312 } 316 }
313 317
314 void cache_uninit(stream_t *s) { 318 void cache_uninit(stream_t *s) {
315 cache_vars_t* c = s->cache_data; 319 cache_vars_t* c = s->cache_data;
316 if(s->cache_pid) { 320 if(s->cache_pid) {
317 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) 321 #if !FORKED_CACHE
318 cache_do_control(s, -2, NULL); 322 cache_do_control(s, -2, NULL);
319 #else 323 #else
320 kill(s->cache_pid,SIGKILL); 324 kill(s->cache_pid,SIGKILL);
321 waitpid(s->cache_pid,NULL,0); 325 waitpid(s->cache_pid,NULL,0);
322 #endif 326 #endif
323 s->cache_pid = 0; 327 s->cache_pid = 0;
324 } 328 }
325 if(!c) return; 329 if(!c) return;
326 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) 330 #if !FORKED_CACHE
327 free(c->buffer); 331 free(c->buffer);
328 c->buffer = NULL; 332 c->buffer = NULL;
329 c->stream = NULL; 333 c->stream = NULL;
330 free(s->cache_data); 334 free(s->cache_data);
331 #else 335 #else
389 } 393 }
390 if (min > s->buffer_size - s->fill_limit) { 394 if (min > s->buffer_size - s->fill_limit) {
391 min = s->buffer_size - s->fill_limit; 395 min = s->buffer_size - s->fill_limit;
392 } 396 }
393 397
394 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 398 #if FORKED_CACHE
395 if((stream->cache_pid=fork())){ 399 if((stream->cache_pid=fork())){
396 if ((pid_t)stream->cache_pid == -1) 400 if ((pid_t)stream->cache_pid == -1)
397 stream->cache_pid = 0; 401 stream->cache_pid = 0;
398 #else 402 #else
399 { 403 {
437 err_out: 441 err_out:
438 cache_uninit(stream); 442 cache_uninit(stream);
439 return res; 443 return res;
440 } 444 }
441 445
442 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 446 #if FORKED_CACHE
443 #ifdef CONFIG_GUI 447 #ifdef CONFIG_GUI
444 use_gui = 0; // mp_msg may not use gui stuff in forked code 448 use_gui = 0; // mp_msg may not use gui stuff in forked code
445 #endif 449 #endif
446 signal(SIGTERM,exit_sighandler); // kill 450 signal(SIGTERM,exit_sighandler); // kill
447 signal(SIGUSR1, dummy_sighandler); // wakeup 451 signal(SIGUSR1, dummy_sighandler); // wakeup
449 // make sure forked code never leaves this function 453 // make sure forked code never leaves this function
450 exit(0); 454 exit(0);
451 #endif 455 #endif
452 } 456 }
453 457
454 #ifdef PTHREAD_CACHE 458 #if !FORKED_CACHE
459 #if defined(__MINGW32__) || defined(__OS2__)
460 static void ThreadProc( void *s ){
461 cache_mainloop(s);
462 _endthread();
463 }
464 #else
455 static void *ThreadProc( void *s ){ 465 static void *ThreadProc( void *s ){
456 cache_mainloop(s); 466 cache_mainloop(s);
457 return NULL; 467 return NULL;
458 } 468 }
459 #elif defined(__MINGW32__) || defined(__OS2__) 469 #endif
460 static void ThreadProc( void *s ){
461 cache_mainloop(s);
462 _endthread();
463 }
464 #endif 470 #endif
465 471
466 int cache_stream_fill_buffer(stream_t *s){ 472 int cache_stream_fill_buffer(stream_t *s){
467 int len; 473 int len;
468 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; } 474 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }