static struct pvr2_context *pvr2_context_notify_last;
 static DEFINE_MUTEX(pvr2_context_mutex);
 static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_sync_data);
+static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_cleanup_data);
+static int pvr2_context_cleanup_flag;
+static int pvr2_context_cleaned_flag;
 static struct task_struct *pvr2_context_thread_ptr;
 
 
 
 static int pvr2_context_shutok(void)
 {
-       return kthread_should_stop() && (pvr2_context_exist_first == NULL);
+       return pvr2_context_cleanup_flag && (pvr2_context_exist_first == NULL);
 }
 
 
                         pvr2_context_shutok()));
        } while (!pvr2_context_shutok());
 
+       pvr2_context_cleaned_flag = !0;
+       wake_up(&pvr2_context_cleanup_data);
+
+       pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread cleaned up");
+
+       wait_event_interruptible(
+               pvr2_context_sync_data,
+               kthread_should_stop());
+
        pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread end");
 
        return 0;
 
 void pvr2_context_global_done(void)
 {
+       pvr2_context_cleanup_flag = !0;
+       wake_up(&pvr2_context_sync_data);
+       wait_event_interruptible(
+               pvr2_context_cleanup_data,
+               pvr2_context_cleaned_flag);
        kthread_stop(pvr2_context_thread_ptr);
 }