Index: cpukit/rtems/src/semdelete.c
===================================================================
--- cpukit/rtems/src/semdelete.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/rtems/src/semdelete.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -93,5 +93,5 @@
         _CORE_mutex_Flush(
           &the_semaphore->Core_control.mutex,
-          CORE_MUTEX_WAS_DELETED,
+          _CORE_mutex_Was_deleted,
           _Semaphore_MP_Send_object_was_deleted,
           id
Index: cpukit/rtems/src/semflush.c
===================================================================
--- cpukit/rtems/src/semflush.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/rtems/src/semflush.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -54,5 +54,5 @@
         _CORE_mutex_Flush(
           &the_semaphore->Core_control.mutex,
-          CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT,
+          _CORE_mutex_Unsatisfied_nowait,
           _Semaphore_MP_Send_object_was_deleted,
           id
Index: cpukit/score/include/rtems/score/corebarrierimpl.h
===================================================================
--- cpukit/score/include/rtems/score/corebarrierimpl.h	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/include/rtems/score/corebarrierimpl.h	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -194,4 +194,10 @@
 #endif
 
+Thread_Control *_CORE_barrier_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
 /* Must be a macro due to the multiprocessing dependent parameters */
 #define _CORE_barrier_Flush( \
@@ -200,11 +206,19 @@
   mp_id \
 ) \
-  _Thread_queue_Flush( \
-    &( the_barrier )->Wait_queue, \
-    CORE_BARRIER_TQ_OPERATIONS, \
-    CORE_BARRIER_WAS_DELETED, \
-    mp_callout, \
-    mp_id \
-  )
+  do { \
+    ISR_lock_Context _core_barrier_flush_lock_context; \
+    _Thread_queue_Acquire( \
+      &( the_barrier )->Wait_queue, \
+      &_core_barrier_flush_lock_context \
+    ); \
+    _Thread_queue_Flush_critical( \
+      &( the_barrier )->Wait_queue.Queue, \
+      CORE_BARRIER_TQ_OPERATIONS, \
+      _CORE_barrier_Was_deleted, \
+      mp_callout, \
+      mp_id, \
+      &_core_barrier_flush_lock_context \
+    ); \
+  } while ( 0 )
 
 /**
Index: cpukit/score/include/rtems/score/coremuteximpl.h
===================================================================
--- cpukit/score/include/rtems/score/coremuteximpl.h	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/include/rtems/score/coremuteximpl.h	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -339,18 +339,38 @@
 #endif
 
+Thread_Control *_CORE_mutex_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
+Thread_Control *_CORE_mutex_Unsatisfied_nowait(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
 /* Must be a macro due to the multiprocessing dependent parameters */
 #define _CORE_mutex_Flush( \
   the_mutex, \
-  status, \
+  filter, \
   mp_callout, \
   mp_id \
 ) \
-  _Thread_queue_Flush( \
-    &( the_mutex )->Wait_queue, \
-    ( the_mutex )->operations, \
-    status, \
-    mp_callout, \
-    mp_id \
-  )
+  do { \
+    ISR_lock_Context _core_mutex_flush_lock_context; \
+    _Thread_queue_Acquire( \
+      &( the_mutex )->Wait_queue, \
+      &_core_mutex_flush_lock_context \
+    ); \
+    _Thread_queue_Flush_critical( \
+      &( the_mutex )->Wait_queue.Queue, \
+      ( the_mutex )->operations, \
+      filter, \
+      mp_callout, \
+      mp_id, \
+      &_core_mutex_flush_lock_context \
+    ); \
+  } while ( 0 )
 
 /**
Index: cpukit/score/include/rtems/score/coresemimpl.h
===================================================================
--- cpukit/score/include/rtems/score/coresemimpl.h	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/include/rtems/score/coresemimpl.h	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -87,4 +87,16 @@
 );
 
+Thread_Control *_CORE_semaphore_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
+Thread_Control *_CORE_semaphore_Unsatisfied_nowait(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
 #define _CORE_semaphore_Destroy( \
   the_semaphore, \
@@ -93,10 +105,16 @@
 ) \
   do { \
-    _Thread_queue_Flush( \
+    ISR_lock_Context _core_semaphore_destroy_lock_context; \
+    _Thread_queue_Acquire( \
       &( the_semaphore )->Wait_queue, \
+      &_core_semaphore_destroy_lock_context \
+    ); \
+    _Thread_queue_Flush_critical( \
+      &( the_semaphore )->Wait_queue.Queue, \
       ( the_semaphore )->operations, \
-      CORE_SEMAPHORE_WAS_DELETED, \
+      _CORE_semaphore_Was_deleted, \
       mp_callout, \
-      mp_id \
+      mp_id, \
+      &_core_semaphore_destroy_lock_context \
     ); \
     _Thread_queue_Destroy( &( the_semaphore )->Wait_queue ); \
@@ -193,11 +211,19 @@
   mp_id \
 ) \
-  _Thread_queue_Flush( \
-    &( the_semaphore )->Wait_queue, \
-    ( the_semaphore )->operations, \
-    CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT, \
-    mp_callout, \
-    mp_id \
-  )
+  do { \
+    ISR_lock_Context _core_semaphore_flush_lock_context; \
+    _Thread_queue_Acquire( \
+      &( the_semaphore )->Wait_queue, \
+      &_core_semaphore_flush_lock_context \
+    ); \
+    _Thread_queue_Flush_critical( \
+      &( the_semaphore )->Wait_queue.Queue, \
+      ( the_semaphore )->operations, \
+      _CORE_semaphore_Unsatisfied_nowait, \
+      mp_callout, \
+      mp_id, \
+      &_core_semaphore_flush_lock_context \
+    ); \
+  } while ( 0 )
 
 /**
Index: cpukit/score/include/rtems/score/threadqimpl.h
===================================================================
--- cpukit/score/include/rtems/score/threadqimpl.h	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/include/rtems/score/threadqimpl.h	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -591,55 +591,94 @@
 );
 
-void _Thread_queue_Do_flush(
-  Thread_queue_Control          *the_thread_queue,
+/**
+ * @brief Thread queue flush filter function.
+ *
+ * Called under protection of the thread queue lock by
+ * _Thread_queue_Flush_critical() to optionally alter the thread wait
+ * information and control the iteration.
+ *
+ * @param the_thread The thread to extract.  This is the first parameter to
+ *   optimize for architectures that use the same register for the first
+ *   parameter and the return value.
+ * @param queue The actual thread queue.
+ * @param lock_context The lock context of the lock acquire.  May be used to
+ *   pass additional data to the filter function via an overlay structure.  The
+ *   filter function should not release or acquire the thread queue lock.
+ *
+ * @retval the_thread Extract this thread.
+ * @retval NULL Do not extract this thread and stop the thread queue flush
+ *   operation.  Threads that are already extracted will complete the flush
+ *   operation.
+ */
+typedef Thread_Control *( *Thread_queue_Flush_filter )(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+);
+
+size_t _Thread_queue_Do_flush_critical(
+  Thread_queue_Queue            *queue,
   const Thread_queue_Operations *operations,
-  uint32_t                       status
-#if defined(RTEMS_MULTIPROCESSING)
-  ,
+  Thread_queue_Flush_filter      filter,
+#if defined(RTEMS_MULTIPROCESSING)
   Thread_queue_MP_callout        mp_callout,
-  Objects_Id                     mp_id
-#endif
-);
-
-/**
- * @brief Unblocks all threads blocked on the thread queue.
- *
- * The thread timers of the threads are cancelled.
- *
- * @param the_thread_queue The thread queue.
+  Objects_Id                     mp_id,
+#endif
+  ISR_lock_Context              *lock_context
+);
+
+/**
+ * @brief Unblocks all threads enqueued on the thread queue.
+ *
+ * This function iteratively extracts the first enqueued thread of the thread
+ * queue until the thread queue is empty or the filter function indicates a
+ * stop.  The thread timers of the extracted threads are cancelled.  The
+ * extracted threads are unblocked.
+ *
+ * @param queue The actual thread queue.
  * @param operations The thread queue operations.
- * @param status The return status for the threads.
+ * @param filter The filter functions is called for each thread to extract from
+ *   the thread queue.  It may be used to alter the thread under protection of
+ *   the thread queue lock, for example to set the thread wait return code.
+ *   The return value of the filter function controls if the thread queue flush
+ *   operation should stop or continue.
  * @param mp_callout Callout to extract the proxy of a remote thread.  This
  *   parameter is only used on multiprocessing configurations.
  * @param mp_id Object identifier of the object containing the thread queue.
  *   This parameter is only used on multiprocessing configurations.
- */
-#if defined(RTEMS_MULTIPROCESSING)
-  #define _Thread_queue_Flush( \
-    the_thread_queue, \
+ *
+ * @return The count of extracted threads.
+ */
+#if defined(RTEMS_MULTIPROCESSING)
+  #define _Thread_queue_Flush_critical( \
+    queue, \
     operations, \
-    status, \
-    mp_callout, \
-    mp_id \
-  ) \
-    _Thread_queue_Do_flush( \
-      the_thread_queue, \
+    filter, \
+    mp_callout, \
+    mp_id, \
+    lock_context \
+  ) \
+    _Thread_queue_Do_flush_critical( \
+      queue, \
       operations, \
-      status, \
+      filter, \
       mp_callout, \
-      mp_id \
-    )
-#else
-  #define _Thread_queue_Flush( \
-    the_thread_queue, \
+      mp_id, \
+      lock_context \
+    )
+#else
+  #define _Thread_queue_Flush_critical( \
+    queue, \
     operations, \
-    status, \
-    mp_callout, \
-    mp_id \
-  ) \
-    _Thread_queue_Do_flush( \
-      the_thread_queue, \
+    filter, \
+    mp_callout, \
+    mp_id, \
+    lock_context \
+  ) \
+    _Thread_queue_Do_flush_critical( \
+      queue, \
       operations, \
-      status \
+      filter, \
+      lock_context \
     )
 #endif
Index: cpukit/score/src/corebarrier.c
===================================================================
--- cpukit/score/src/corebarrier.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/corebarrier.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -20,5 +20,4 @@
 
 #include <rtems/score/corebarrierimpl.h>
-#include <rtems/score/threadqimpl.h>
 
 void _CORE_barrier_Initialize(
@@ -33,2 +32,13 @@
   _Thread_queue_Initialize( &the_barrier->Wait_queue );
 }
+
+Thread_Control *_CORE_barrier_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_BARRIER_WAS_DELETED;
+
+  return the_thread;
+}
Index: cpukit/score/src/coremsgclose.c
===================================================================
--- cpukit/score/src/coremsgclose.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/coremsgclose.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -22,4 +22,15 @@
 #include <rtems/score/wkspace.h>
 
+static Thread_Control *_CORE_message_queue_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_MESSAGE_QUEUE_STATUS_WAS_DELETED;
+
+  return the_thread;
+}
+
 void _CORE_message_queue_Do_close(
   CORE_message_queue_Control *the_message_queue
@@ -31,4 +42,6 @@
 )
 {
+  ISR_lock_Context lock_context;
+
   /*
    *  This will flush blocked threads whether they were blocked on
@@ -36,10 +49,12 @@
    */
 
-  _Thread_queue_Flush(
-    &the_message_queue->Wait_queue,
+  _CORE_message_queue_Acquire( the_message_queue, &lock_context );
+  _Thread_queue_Flush_critical(
+    &the_message_queue->Wait_queue.Queue,
     the_message_queue->operations,
-    CORE_MESSAGE_QUEUE_STATUS_WAS_DELETED,
+    _CORE_message_queue_Was_deleted,
     mp_callout,
-    mp_id
+    mp_id,
+    &lock_context
   );
 
Index: cpukit/score/src/coremsgflush.c
===================================================================
--- cpukit/score/src/coremsgflush.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/coremsgflush.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -42,5 +42,5 @@
    *  (1) The thread queue of pending senders is a logical extension
    *  of the pending message queue.  In this case, it should be
-   *  flushed using the _Thread_queue_Flush() service with a status
+   *  flushed using the _Thread_queue_Flush_critical() service with a status
    *  such as CORE_MESSAGE_QUEUE_SENDER_FLUSHED (which currently does
    *  not exist).  This can be implemented without changing the "big-O"
Index: cpukit/score/src/coremutex.c
===================================================================
--- cpukit/score/src/coremutex.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/coremutex.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -97,2 +97,24 @@
   return CORE_MUTEX_STATUS_SUCCESSFUL;
 }
+
+Thread_Control *_CORE_mutex_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_MUTEX_WAS_DELETED;
+
+  return the_thread;
+}
+
+Thread_Control *_CORE_mutex_Unsatisfied_nowait(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
+
+  return the_thread;
+}
Index: cpukit/score/src/coresem.c
===================================================================
--- cpukit/score/src/coresem.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/coresem.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -37,2 +37,24 @@
   }
 }
+
+Thread_Control *_CORE_semaphore_Was_deleted(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_SEMAPHORE_WAS_DELETED;
+
+  return the_thread;
+}
+
+Thread_Control *_CORE_semaphore_Unsatisfied_nowait(
+  Thread_Control     *the_thread,
+  Thread_queue_Queue *queue,
+  ISR_lock_Context   *lock_context
+)
+{
+  the_thread->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT;
+
+  return the_thread;
+}
Index: cpukit/score/src/threadqflush.c
===================================================================
--- cpukit/score/src/threadqflush.c	(revision 48b04fc388a60fc6621233ddbb7cd65d89bb63d8)
+++ cpukit/score/src/threadqflush.c	(revision adbedd10cfe5259018b1682d903ab40f6005b3f0)
@@ -19,45 +19,81 @@
 #endif
 
-#include <rtems/score/threadqimpl.h>
-#include <rtems/score/objectimpl.h>
+#include <rtems/score/threadimpl.h>
 
-void _Thread_queue_Do_flush(
-  Thread_queue_Control          *the_thread_queue,
+size_t _Thread_queue_Do_flush_critical(
+  Thread_queue_Queue            *queue,
   const Thread_queue_Operations *operations,
-  uint32_t                       status
+  Thread_queue_Flush_filter      filter,
 #if defined(RTEMS_MULTIPROCESSING)
-  ,
   Thread_queue_MP_callout        mp_callout,
-  Objects_Id                     mp_id
+  Objects_Id                     mp_id,
 #endif
+  ISR_lock_Context              *lock_context
 )
 {
-  ISR_lock_Context  lock_context;
-  Thread_Control   *the_thread;
+  size_t         flushed;
+  Chain_Control  unblock;
+  Chain_Node    *node;
+  Chain_Node    *tail;
 
-  _Thread_queue_Acquire( the_thread_queue, &lock_context );
+  flushed = 0;
+  _Chain_Initialize_empty( &unblock );
 
-  while (
-    (
-      the_thread = _Thread_queue_First_locked(
-        the_thread_queue,
-        operations
-      )
-    )
-  ) {
-    the_thread->Wait.return_code = status;
+  while ( true ) {
+    Thread_queue_Heads *heads;
+    Thread_Control     *first;
+    bool                do_unblock;
 
-    _Thread_queue_Extract_critical(
-      &the_thread_queue->Queue,
+    heads = queue->heads;
+    if ( heads == NULL ) {
+      break;
+    }
+
+    first = ( *operations->first )( heads );
+    first = ( *filter )( first, queue, lock_context );
+    if ( first == NULL ) {
+      break;
+    }
+
+    do_unblock = _Thread_queue_Extract_locked(
+      queue,
       operations,
-      the_thread,
+      first,
       mp_callout,
-      mp_id,
-      &lock_context
+      mp_id
     );
+    if ( do_unblock ) {
+      _Chain_Append_unprotected( &unblock, &first->Wait.Node.Chain );
+    }
 
-    _Thread_queue_Acquire( the_thread_queue, &lock_context );
+    ++flushed;
   }
 
-  _Thread_queue_Release( the_thread_queue, &lock_context );
+  node = _Chain_First( &unblock );
+  tail = _Chain_Tail( &unblock );
+
+  if ( node != tail ) {
+    Per_CPU_Control *cpu_self;
+
+    cpu_self = _Thread_Dispatch_disable_critical( lock_context );
+    _Thread_queue_Queue_release( queue, lock_context );
+
+    do {
+      Thread_Control *the_thread;
+      Chain_Node     *next;
+
+      next = _Chain_Next( node );
+      the_thread = THREAD_CHAIN_NODE_TO_THREAD( node );
+      _Thread_Timer_remove( the_thread );
+      _Thread_Unblock( the_thread );
+
+      node = next;
+    } while ( node != tail );
+
+    _Thread_Dispatch_enable( cpu_self );
+  } else {
+    _Thread_queue_Queue_release( queue, lock_context );
+  }
+
+  return flushed;
 }
