From 95f946275df6fa1a1d362725aa1bd5ee609004c5 Mon Sep 17 00:00:00 2001
From: Geoffroy Couprie <geo.couprie@gmail.com>
Date: Thu, 4 Sep 2008 13:15:06 +0200
Subject: [PATCH] WinCE: keep old threading and timing system (need rework)

---
 include/vlc_threads.h |   27 +++++++++++++++++++++++----
 src/misc/mtime.c      |    5 +++++
 src/misc/threads.c    |   24 ++++++++++++++++++------
 3 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 6970358..8b620a7 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -34,10 +34,12 @@
  *
  */
 
+#if defined( WIN32 )
 #if defined( UNDER_CE )
-                                                                /* WinCE API */
-#elif defined( WIN32 )
+#   include <winbase.h>                                         /* WinCE API */
+#elif
 #   include <process.h>                                         /* Win32 API */
+#endif
 #   include <errno.h>
 
 #elif defined( SYS_BEOS )                                            /* BeOS */
@@ -88,7 +90,7 @@
 #   define VLC_THREAD_PRIORITY_OUTPUT  15
 #   define VLC_THREAD_PRIORITY_HIGHEST 20
 
-#elif defined(WIN32) || defined(UNDER_CE)
+#elif defined(WIN32) && !defined(UNDER_CE)
 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
 #   define VLC_THREAD_PRIORITY_LOW 0
 #   define VLC_THREAD_PRIORITY_INPUT \
@@ -102,6 +104,15 @@
 #   define VLC_THREAD_PRIORITY_HIGHEST \
         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
 
+#elif defined UNDER_CE
+#   define VLC_THREAD_PRIORITY_LOW      0
+#   define VLC_THREAD_PRIORITY_INPUT    THREAD_PRIORITY_ABOVE_NORMAL
+#   define VLC_THREAD_PRIORITY_AUDIO    THREAD_PRIORITY_HIGHEST
+#   define VLC_THREAD_PRIORITY_VIDEO    0
+#   define VLC_THREAD_PRIORITY_OUTPUT   THREAD_PRIORITY_ABOVE_NORMAL
+#   define VLC_THREAD_PRIORITY_HIGHEST  THREAD_PRIORITY_TIME_CRITICAL
+
+
 #else
 #   define VLC_THREAD_PRIORITY_LOW 0
 #   define VLC_THREAD_PRIORITY_INPUT 0
@@ -130,7 +141,14 @@ typedef struct
     void  *data;
 } *vlc_thread_t;
 
+# ifdef UNDER_CE
+typedef struct
+{
+    CRITICAL_SECTION csection;
+} vlc_mutex_t;
+# elif
 typedef HANDLE  vlc_mutex_t;
+# endif
 typedef HANDLE  vlc_cond_t;
 typedef DWORD   vlc_threadvar_t;
 
@@ -454,6 +472,7 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
 
     /* Reacquire the mutex before returning. */
     vlc_mutex_lock( p_mutex );
+    (void)psz_file; (void)i_line;
 
 #elif defined( WIN32 )
     DWORD result;
@@ -635,7 +654,7 @@ static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
     pthread_spin_destroy (spin);
 }
 
-#elif defined( WIN32 )
+#elif defined( WIN32 ) && !defined (UNDER_CE)
 
 typedef CRITICAL_SECTION vlc_spinlock_t;
 
diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index 0d1e062..ef47a0e 100644
--- a/src/misc/mtime.c
+++ b/src/misc/mtime.c
@@ -348,7 +348,12 @@ void mwait( mtime_t date )
     {
         DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
         vlc_testcancel ();
+#ifndef UNDER_CE
         SleepEx (delay, TRUE);
+#else
+        /* TODO: Will need an reimplementation of SleepEx*/
+        Sleep (delay);
+#endif
     }
     vlc_testcancel ();
 
diff --git a/src/misc/threads.c b/src/misc/threads.c
index 7d62c3f..d4e24a8 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -296,6 +296,9 @@ int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
     i_result = pthread_mutex_init( p_mutex, &attr );
     pthread_mutexattr_destroy( &attr );
     return( i_result );
+#elif defined( UNDER_CE )
+	InitializeCriticalSection( &p_mutex->csection );
+	return 0;
 #elif defined( WIN32 )
     /* Create mutex returns a recursive mutex */
     *p_mutex = CreateMutex( 0, FALSE, 0 );
@@ -415,8 +418,8 @@ int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
 
 #if defined( LIBVLC_USE_PTHREAD )
     i_ret =  pthread_key_create( p_tls, destr );
-#elif defined( UNDER_CE )
-    i_ret = ENOSYS;
+/*#elif defined( UNDER_CE )
+    i_ret = ENOSYS;*/
 #elif defined( WIN32 )
     /* FIXME: remember/use the destr() callback and stop leaking whatever */
     *p_tls = TlsAlloc();
@@ -431,7 +434,7 @@ void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
 {
 #if defined( LIBVLC_USE_PTHREAD )
     pthread_key_delete (*p_tls);
-#elif defined( UNDER_CE )
+//#elif defined( UNDER_CE )
 #elif defined( WIN32 )
     TlsFree (*p_tls);
 #else
@@ -583,7 +586,7 @@ void vlc_cancel (vlc_thread_t thread_id)
 {
 #if defined (LIBVLC_USE_PTHREAD_CANCEL)
     pthread_cancel (thread_id);
-#elif defined (WIN32)
+#elif defined (WIN32) && !defined (UNDER_CE)
     QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
 #else
 #   warning vlc_cancel is not implemented!
@@ -606,11 +609,16 @@ void vlc_join (vlc_thread_t handle, void **result)
     if (val)
         vlc_pthread_fatal ("joining thread", val, __FILE__, __LINE__);
 
-#elif defined( UNDER_CE ) || defined( WIN32 )
+#elif defined( WIN32 )
     do
         vlc_testcancel ();
+#ifndef UNDER_CE
     while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
                                                         == WAIT_IO_COMPLETION);
+#else
+    while (WaitForSingleObject (handle->handle, INFINITE)
+           == WAIT_IO_COMPLETION);
+#endif
 
     CloseHandle (handle->handle);
     if (result)
@@ -752,7 +760,7 @@ int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
         }
     }
 
-#elif defined( WIN32 ) || defined( UNDER_CE )
+#elif defined( WIN32 )
     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
 
     if( !SetThreadPriority(p_priv->thread_id->handle, i_priority) )
@@ -890,7 +898,11 @@ void vlc_control_cancel (int cmd, ...)
 #if defined (LIBVLC_USE_PTHREAD)
                 pthread_exit (PTHREAD_CANCELLED);
 #elif defined (WIN32)
+# ifndef UNDER_CE
                 _endthread ();
+# else
+# warning no thread cancellation on WinCE (yet)
+# endif
 #else
 # error Not implemented!
 #endif
-- 
1.5.4.3

