changeset 27894:d06d8e459ae1

Use pthreads for the cache on Cygwin, since _beginthread is not available and the previous CreateThread method would probably leak memory here, too. Also pthreads seems to be the official Cygwin threading API.
author reimar
date Sat, 15 Nov 2008 19:08:50 +0000
parents 002c69b829a5
children 69b4b8d73b5e
files stream/cache2.c
diffstat 1 files changed, 27 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/stream/cache2.c	Sat Nov 15 19:06:25 2008 +0000
+++ b/stream/cache2.c	Sat Nov 15 19:08:50 2008 +0000
@@ -16,15 +16,22 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#ifdef __CYGWIN__
+#define PTHREAD_CACHE 1
+#endif
+
 #include "osdep/shmem.h"
 #include "osdep/timer.h"
-#if defined(__MINGW32__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
 #include <windows.h>
 static void ThreadProc( void *s );
 #elif defined(__OS2__)
 #define INCL_DOS
 #include <os2.h>
 static void ThreadProc( void *s );
+#elif defined(PTHREAD_CACHE)
+#include <pthread.h>
+static void *ThreadProc(void *s);
 #else
 #include <sys/wait.h>
 #endif
@@ -245,7 +252,7 @@
 
 cache_vars_t* cache_init(int size,int sector){
   int num;
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
 #else
   cache_vars_t* s=malloc(sizeof(cache_vars_t));
@@ -259,14 +266,14 @@
   }//32kb min_size
   s->buffer_size=num*sector;
   s->sector_size=sector;
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   s->buffer=shmem_alloc(s->buffer_size);
 #else
   s->buffer=malloc(s->buffer_size);
 #endif
 
   if(s->buffer == NULL){
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
     shmem_free(s,sizeof(cache_vars_t));
 #else
     free(s);
@@ -282,14 +289,14 @@
 void cache_uninit(stream_t *s) {
   cache_vars_t* c = s->cache_data;
   if(!s->cache_pid) return;
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
   cache_do_control(s, -2, NULL);
 #else
   kill(s->cache_pid,SIGKILL);
   waitpid(s->cache_pid,NULL,0);
 #endif
   if(!c) return;
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
   free(c->stream);
   free(c->buffer);
   free(s->cache_data);
@@ -330,17 +337,19 @@
      min = s->buffer_size - s->fill_limit;
   }
   
-#if !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__OS2__)
+#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
   if((stream->cache_pid=fork())){
 #else
   {
     stream_t* stream2=malloc(sizeof(stream_t));
     memcpy(stream2,s->stream,sizeof(stream_t));
     s->stream=stream2;
-#if defined(__MINGW32__) || defined(__CYGWIN__)
+#if defined(__MINGW32__)
     stream->cache_pid = _beginthread( ThreadProc, 0, s );
+#elif defined(__OS2__)
+    stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
 #else
-    stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
+    pthread_create(&stream->cache_pid, NULL, ThreadProc, s);
 #endif
 #endif
     // wait until cache is filled at least prefill_init %
@@ -359,10 +368,14 @@
     return 1; // parent exits
   }
   
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
 }
+#ifdef PTHREAD_CACHE
+static void *ThreadProc( void *s ){
+#else
 static void ThreadProc( void *s ){
 #endif
+#endif
   
 #ifdef CONFIG_GUI
   use_gui = 0; // mp_msg may not use gui stuff in forked code
@@ -375,9 +388,12 @@
     }
 //	 cache_stats(s->cache_data);
   } while (cache_execute_control(s));
-#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+#if defined(__MINGW32__) || defined(__OS2__)
   _endthread();
 #endif
+#ifdef PTHREAD_CACHE
+  return NULL;
+#endif
 }
 
 int cache_stream_fill_buffer(stream_t *s){