comparison stream/cache2.c @ 30351:b985db55e78a

Check for fork failing and make sure cache_uninit always frees the cache data even if fork failed.
author reimar
date Sat, 23 Jan 2010 10:50:50 +0000
parents 5c39c41f38e8
children dff74677e2fd
comparison
equal deleted inserted replaced
30350:956cc6a392d7 30351:b985db55e78a
13 #include <stdlib.h> 13 #include <stdlib.h>
14 #include <string.h> 14 #include <string.h>
15 #include <signal.h> 15 #include <signal.h>
16 #include <sys/types.h> 16 #include <sys/types.h>
17 #include <unistd.h> 17 #include <unistd.h>
18 #include <errno.h>
18 19
19 #include "osdep/shmem.h" 20 #include "osdep/shmem.h"
20 #include "osdep/timer.h" 21 #include "osdep/timer.h"
21 #if defined(__MINGW32__) 22 #if defined(__MINGW32__)
22 #include <windows.h> 23 #include <windows.h>
282 return s; 283 return s;
283 } 284 }
284 285
285 void cache_uninit(stream_t *s) { 286 void cache_uninit(stream_t *s) {
286 cache_vars_t* c = s->cache_data; 287 cache_vars_t* c = s->cache_data;
287 if(!s->cache_pid) return; 288 if(s->cache_pid) {
288 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) 289 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
289 cache_do_control(s, -2, NULL); 290 cache_do_control(s, -2, NULL);
290 #else 291 #else
291 kill(s->cache_pid,SIGKILL); 292 kill(s->cache_pid,SIGKILL);
292 waitpid(s->cache_pid,NULL,0); 293 waitpid(s->cache_pid,NULL,0);
293 #endif 294 #endif
295 }
294 if(!c) return; 296 if(!c) return;
295 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) 297 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
296 free(c->stream); 298 free(c->stream);
297 free(c->buffer); 299 free(c->buffer);
298 free(s->cache_data); 300 free(s->cache_data);
332 min = s->buffer_size - s->fill_limit; 334 min = s->buffer_size - s->fill_limit;
333 } 335 }
334 336
335 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) 337 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
336 if((stream->cache_pid=fork())){ 338 if((stream->cache_pid=fork())){
339 if ((pid_t)stream->cache_pid == -1)
340 stream->cache_pid = 0;
337 #else 341 #else
338 { 342 {
339 stream_t* stream2=malloc(sizeof(stream_t)); 343 stream_t* stream2=malloc(sizeof(stream_t));
340 memcpy(stream2,s->stream,sizeof(stream_t)); 344 memcpy(stream2,s->stream,sizeof(stream_t));
341 s->stream=stream2; 345 s->stream=stream2;
349 pthread_create(&tid, NULL, ThreadProc, s); 353 pthread_create(&tid, NULL, ThreadProc, s);
350 stream->cache_pid = 1; 354 stream->cache_pid = 1;
351 } 355 }
352 #endif 356 #endif
353 #endif 357 #endif
358 if (!stream->cache_pid) {
359 mp_msg(MSGT_CACHE, MSGL_ERR,
360 "Starting cache process/thread failed: %s.\n", strerror(errno));
361 return 0;
362 }
354 // wait until cache is filled at least prefill_init % 363 // wait until cache is filled at least prefill_init %
355 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n", 364 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
356 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof); 365 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
357 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){ 366 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
358 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill, 367 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill,