-#if defined(LIBVLC_USE_PTHREAD)
- int val = pthread_cond_wait( p_condvar, p_mutex );
- VLC_THREAD_ASSERT ("waiting on condition");
-
-#elif defined( UNDER_CE )
- LeaveCriticalSection( &p_mutex->csection );
- WaitForSingleObject( *p_condvar, INFINITE );
-
- /* Reacquire the mutex before returning. */
- vlc_mutex_lock( p_mutex );
-
-#elif defined( WIN32 )
- (void)psz_file; (void)i_line;
-
- /* Increase our wait count */
- SignalObjectAndWait( *p_mutex, *p_condvar, INFINITE, FALSE );
-
- /* Reacquire the mutex before returning. */
- vlc_mutex_lock( p_mutex );
-
-#elif defined( SYS_BEOS )
- /* The p_condvar->thread var is initialized before the unlock because
- * it enables to identify when the thread is interrupted beetwen the
- * unlock line and the suspend_thread line */
- p_condvar->thread = find_thread( NULL );
- vlc_mutex_unlock( p_mutex );
- suspend_thread( p_condvar->thread );
- p_condvar->thread = -1;
-
- vlc_mutex_lock( p_mutex );
-
-#endif
-}
-
-
-/*****************************************************************************
- * vlc_cond_timedwait: wait until condition completion or expiration
- *****************************************************************************
- * Returns 0 if object signaled, an error code in case of timeout or error.
- *****************************************************************************/
-#define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
- __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
-
-static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
- vlc_cond_t *p_condvar,
- vlc_mutex_t *p_mutex,
- mtime_t deadline )
+ vlc_cleanup_t *next;
+ void (*proc) (void *);
+ void *data;
+};
+
+/* This macros opens a code block on purpose. This is needed for multiple
+ * calls within a single function. This also prevent Win32 developpers from
+ * writing code that would break on POSIX (POSIX opens a block as well). */
+# define vlc_cleanup_push( routine, arg ) \
+ do { \
+ vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
+ vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
+
+# define vlc_cleanup_pop( ) \
+ vlc_control_cancel (VLC_CLEANUP_POP); \
+ } while (0)
+
+# define vlc_cleanup_run( ) \
+ vlc_control_cancel (VLC_CLEANUP_POP); \
+ vlc_cleanup_data.proc (vlc_cleanup_data.data); \
+ } while (0)
+
+#endif /* LIBVLC_USE_PTHREAD_CANCEL */
+
+static inline void vlc_cleanup_lock (void *lock)