changeset 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 15df1f217e6d
files stream/cache2.c
diffstat 1 files changed, 18 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/stream/cache2.c	Sun Oct 19 17:28:23 2008 +0000
+++ b/stream/cache2.c	Sun Oct 19 18:05:45 2008 +0000
@@ -20,7 +20,7 @@
 #include "osdep/timer.h"
 #if defined(__MINGW32__) || defined(__CYGWIN__)
 #include <windows.h>
-static DWORD WINAPI ThreadProc(void* s);
+static void ThreadProc( void *s );
 #elif defined(__OS2__)
 #define INCL_DOS
 #include <os2.h>
@@ -198,14 +198,15 @@
   
 }
 
-static void cache_execute_control(cache_vars_t *s) {
+static int cache_execute_control(cache_vars_t *s) {
+  int res = 1;
   static unsigned last;
   if (!s->stream->control) {
     s->stream_time_length = 0;
     s->control_new_pos = 0;
     s->control_res = STREAM_UNSUPPORTED;
     s->control = -1;
-    return;
+    return res;
   }
   if (GetTimerMS() - last > 99) {
     double len;
@@ -215,7 +216,7 @@
       s->stream_time_length = 0;
     last = GetTimerMS();
   }
-  if (s->control == -1) return;
+  if (s->control == -1) return res;
   switch (s->control) {
     case STREAM_CTRL_GET_CURRENT_TIME:
     case STREAM_CTRL_SEEK_TO_TIME:
@@ -230,12 +231,15 @@
     case STREAM_CTRL_SET_ANGLE:
       s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
       break;
+    case -2:
+      res = 0;
     default:
       s->control_res = STREAM_UNSUPPORTED;
       break;
   }
   s->control_new_pos = s->stream->pos;
   s->control = -1;
+  return res;
 }
 
 cache_vars_t* cache_init(int size,int sector){
@@ -277,11 +281,8 @@
 void cache_uninit(stream_t *s) {
   cache_vars_t* c = s->cache_data;
   if(!s->cache_pid) return;
-#if defined(__MINGW32__) || defined(__CYGWIN__)
-  TerminateThread((HANDLE)s->cache_pid,0);
-#elif defined(__OS2__)
-  DosKillThread( s->cache_pid );
-  DosWaitThread( &s->cache_pid, DCWW_WAIT );
+#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+  cache_do_control(s, -2, NULL);
 #else
   kill(s->cache_pid,SIGKILL);
   waitpid(s->cache_pid,NULL,0);
@@ -332,15 +333,12 @@
   if((stream->cache_pid=fork())){
 #else
   {
-#if defined(__MINGW32__) || defined(__CYGWIN__)
-    DWORD threadId;
-#endif
     stream_t* stream2=malloc(sizeof(stream_t));
     memcpy(stream2,s->stream,sizeof(stream_t));
     s->stream=stream2;
 #if defined(__MINGW32__) || defined(__CYGWIN__)
-    stream->cache_pid = CreateThread(NULL,0,ThreadProc,s,0,&threadId);
-#else   // OS2
+    stream->cache_pid = _beginthread( ThreadProc, 0, s );
+#else
     stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
 #endif
 #endif
@@ -362,25 +360,23 @@
   
 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
 }
-#if defined(__MINGW32__) || defined(__CYGWIN__)
-static DWORD WINAPI ThreadProc(void*s){
-#else   // OS2
 static void ThreadProc( void *s ){
 #endif
-#endif
   
 #ifdef CONFIG_GUI
   use_gui = 0; // mp_msg may not use gui stuff in forked code
 #endif
 // cache thread mainloop:
   signal(SIGTERM,exit_sighandler); // kill
-  while(1){
+  do {
     if(!cache_fill(s)){
 	 usec_sleep(FILL_USLEEP_TIME); // idle
     }
-    cache_execute_control(s);
 //	 cache_stats(s->cache_data);
-  }
+  } while (cache_execute_control(s));
+#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
+  _endthread();
+#endif
 }
 
 int cache_stream_fill_buffer(stream_t *s){
@@ -455,6 +451,7 @@
     case STREAM_CTRL_GET_ASPECT_RATIO:
     case STREAM_CTRL_GET_NUM_ANGLES:
     case STREAM_CTRL_GET_ANGLE:
+    case -2:
       s->control = cmd;
       break;
     default: