@ -12,6 +12,7 @@
# include <spdlog/details/circular_q.h>
# include <spdlog/details/circular_q.h>
# include <atomic>
# include <condition_variable>
# include <condition_variable>
# include <mutex>
# include <mutex>
@ -49,6 +50,28 @@ public:
push_cv_ . notify_one ( ) ;
push_cv_ . notify_one ( ) ;
}
}
void enqueue_if_have_room ( T & & item )
{
bool pushed = false ;
{
std : : unique_lock < std : : mutex > lock ( queue_mutex_ ) ;
if ( ! q_ . full ( ) )
{
q_ . push_back ( std : : move ( item ) ) ;
pushed = true ;
}
}
if ( pushed )
{
push_cv_ . notify_one ( ) ;
}
else
{
+ + discard_counter_ ;
}
}
// dequeue with a timeout.
// dequeue with a timeout.
// Return true, if succeeded dequeue item, false otherwise
// Return true, if succeeded dequeue item, false otherwise
bool dequeue_for ( T & popped_item , std : : chrono : : milliseconds wait_duration )
bool dequeue_for ( T & popped_item , std : : chrono : : milliseconds wait_duration )
@ -99,6 +122,26 @@ public:
push_cv_ . notify_one ( ) ;
push_cv_ . notify_one ( ) ;
}
}
void enqueue_if_have_room ( T & & item )
{
bool pushed = false ;
std : : unique_lock < std : : mutex > lock ( queue_mutex_ ) ;
if ( ! q_ . full ( ) )
{
q_ . push_back ( std : : move ( item ) ) ;
pushed = true ;
}
if ( pushed )
{
push_cv_ . notify_one ( ) ;
}
else
{
+ + discard_counter_ ;
}
}
// dequeue with a timeout.
// dequeue with a timeout.
// Return true, if succeeded dequeue item, false otherwise
// Return true, if succeeded dequeue item, false otherwise
bool dequeue_for ( T & popped_item , std : : chrono : : milliseconds wait_duration )
bool dequeue_for ( T & popped_item , std : : chrono : : milliseconds wait_duration )
@ -132,6 +175,11 @@ public:
return q_ . overrun_counter ( ) ;
return q_ . overrun_counter ( ) ;
}
}
size_t discard_counter ( )
{
return discard_counter_ . load ( std : : memory_order_relaxed ) ;
}
size_t size ( )
size_t size ( )
{
{
std : : unique_lock < std : : mutex > lock ( queue_mutex_ ) ;
std : : unique_lock < std : : mutex > lock ( queue_mutex_ ) ;
@ -144,11 +192,17 @@ public:
q_ . reset_overrun_counter ( ) ;
q_ . reset_overrun_counter ( ) ;
}
}
void reset_discard_counter ( )
{
discard_counter_ . store ( 0 , std : : memory_order_relaxed ) ;
}
private :
private :
std : : mutex queue_mutex_ ;
std : : mutex queue_mutex_ ;
std : : condition_variable push_cv_ ;
std : : condition_variable push_cv_ ;
std : : condition_variable pop_cv_ ;
std : : condition_variable pop_cv_ ;
spdlog : : details : : circular_q < T > q_ ;
spdlog : : details : : circular_q < T > q_ ;
std : : atomic < size_t > discard_counter_ { 0 } ;
} ;
} ;
} // namespace details
} // namespace details
} // namespace spdlog
} // namespace spdlog