+# ifndef UNDER_CE
+# define VLC_CANCEL_INIT { NULL, true, false }
+# else
+# define VLC_CANCEL_INIT { NULL, true, false, NULL }
+# endif
+#endif
+
+#ifdef UNDER_CE
+static void CALLBACK vlc_cancel_self (ULONG_PTR dummy);
+
+static DWORD vlc_cancelable_wait (DWORD count, const HANDLE *handles,
+ DWORD delay)
+{
+ vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
+ if (nfo == NULL)
+ {
+ /* Main thread - cannot be cancelled anyway */
+ return WaitForMultipleObjects (count, handles, FALSE, delay);
+ }
+ HANDLE new_handles[count + 1];
+ memcpy(new_handles, handles, count * sizeof(HANDLE));
+ new_handles[count] = nfo->cancel_event;
+ DWORD result = WaitForMultipleObjects (count + 1, new_handles, FALSE,
+ delay);
+ if (result == WAIT_OBJECT_0 + count)
+ {
+ vlc_cancel_self (NULL);
+ return WAIT_IO_COMPLETION;
+ }
+ else
+ {
+ return result;
+ }
+}
+
+DWORD SleepEx (DWORD dwMilliseconds, BOOL bAlertable)
+{
+ if (bAlertable)
+ {
+ DWORD result = vlc_cancelable_wait (0, NULL, dwMilliseconds);
+ return (result == WAIT_TIMEOUT) ? 0 : WAIT_IO_COMPLETION;
+ }
+ else
+ {
+ Sleep(dwMilliseconds);
+ return 0;
+ }
+}
+
+DWORD WaitForSingleObjectEx (HANDLE hHandle, DWORD dwMilliseconds,
+ BOOL bAlertable)
+{
+ if (bAlertable)
+ {
+ /* The MSDN documentation specifies different return codes,
+ * but in practice they are the same. We just check that it
+ * remains so. */
+#if WAIT_ABANDONED != WAIT_ABANDONED_0
+# error Windows headers changed, code needs to be rewritten!
+#endif
+ return vlc_cancelable_wait (1, &hHandle, dwMilliseconds);
+ }
+ else
+ {
+ return WaitForSingleObject (hHandle, dwMilliseconds);
+ }
+}
+
+DWORD WaitForMultipleObjectsEx (DWORD nCount, const HANDLE *lpHandles,
+ BOOL bWaitAll, DWORD dwMilliseconds,
+ BOOL bAlertable)
+{
+ if (bAlertable)
+ {
+ /* We do not support the bWaitAll case */
+ assert (! bWaitAll);
+ return vlc_cancelable_wait (nCount, lpHandles, dwMilliseconds);
+ }
+ else
+ {
+ return WaitForMultipleObjects (nCount, lpHandles, bWaitAll,
+ dwMilliseconds);
+ }
+}