libMesh
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
libMesh::ThreadBufferedSyncbuf Class Reference

#include <thread_buffered_syncbuf.h>

Inheritance diagram for libMesh::ThreadBufferedSyncbuf:
[legend]

Classes

class  ThreadLocalBuffer
 A class that wraps a thread-local string. More...
 

Public Member Functions

 ThreadBufferedSyncbuf (std::streambuf &sink, bool flush_on_newline=true)
 
 ~ThreadBufferedSyncbuf ()
 Defaulted destructor defined in implementation so we don't need to include threads.h.
 

Protected Member Functions

int_type overflow (int_type ch) override
 
std::streamsize xsputn (const char *s, std::streamsize n) override
 
int sync () override
 

Private Member Functions

ThreadLocalBufferthread_local_buffer ()
 One ThreadLocalBuffer instance per thread, constructed on first use with *this.
 
void emit_from_thread_local_buffer (std::string &b, bool force_flush)
 Emit from the thread local buffer to our wrapped _sink.
 

Private Attributes

std::streambuf & _sink
 Wrapped output sink.
 
const bool _flush_on_newline
 Whether to flush our sink to terminal/file on terminating new-line characters (
)
 
std::unique_ptr< Threads::spin_mutex_mu
 Serialization for commits to the shared sink.
 

Detailed Description

Definition at line 30 of file thread_buffered_syncbuf.h.

Constructor & Destructor Documentation

◆ ThreadBufferedSyncbuf()

libMesh::ThreadBufferedSyncbuf::ThreadBufferedSyncbuf ( std::streambuf &  sink,
bool  flush_on_newline = true 
)
explicit

Definition at line 23 of file thread_buffered_syncbuf.C.

24 : _sink(sink), _flush_on_newline(flush_on_newline), _mu(std::make_unique<Threads::spin_mutex>())
25{
26}
std::unique_ptr< Threads::spin_mutex > _mu
Serialization for commits to the shared sink.
std::streambuf & _sink
Wrapped output sink.
const bool _flush_on_newline
Whether to flush our sink to terminal/file on terminating new-line characters ( )

◆ ~ThreadBufferedSyncbuf()

libMesh::ThreadBufferedSyncbuf::~ThreadBufferedSyncbuf ( )
default

Defaulted destructor defined in implementation so we don't need to include threads.h.

Member Function Documentation

◆ emit_from_thread_local_buffer()

void libMesh::ThreadBufferedSyncbuf::emit_from_thread_local_buffer ( std::string &  b,
bool  force_flush 
)
private

Emit from the thread local buffer to our wrapped _sink.

Definition at line 67 of file thread_buffered_syncbuf.C.

68{
69 if (b.empty())
70 {
71 if (force_flush)
72 {
73 Threads::spin_mutex::scoped_lock lk(*_mu);
74 _sink.pubsync();
75 }
76 return;
77 }
78
79 {
80 Threads::spin_mutex::scoped_lock lk(*_mu);
81 _sink.sputn(b.data(), static_cast<std::streamsize>(b.size()));
82 if (force_flush)
83 _sink.pubsync();
84 }
85 b.clear();
86}
static const Real b

References _mu, _sink, and b.

Referenced by overflow(), sync(), xsputn(), and libMesh::ThreadBufferedSyncbuf::ThreadLocalBuffer::~ThreadLocalBuffer().

◆ overflow()

ThreadBufferedSyncbuf::int_type libMesh::ThreadBufferedSyncbuf::overflow ( int_type  ch)
overrideprotected

Definition at line 30 of file thread_buffered_syncbuf.C.

31{
32 if (traits_type::eq_int_type(ch, traits_type::eof()))
33 return traits_type::not_eof(ch);
34
35 auto & t = this->thread_local_buffer();
36 t.buf.push_back(traits_type::to_char_type(ch));
37
38 if (_flush_on_newline && ch == '\n')
39 this->emit_from_thread_local_buffer(t.buf, /*force_flush=*/false);
40
41 return ch;
42}
ThreadLocalBuffer & thread_local_buffer()
One ThreadLocalBuffer instance per thread, constructed on first use with *this.
void emit_from_thread_local_buffer(std::string &b, bool force_flush)
Emit from the thread local buffer to our wrapped _sink.

References _flush_on_newline, emit_from_thread_local_buffer(), and thread_local_buffer().

◆ sync()

int libMesh::ThreadBufferedSyncbuf::sync ( )
overrideprotected

Definition at line 55 of file thread_buffered_syncbuf.C.

56{
57 this->emit_from_thread_local_buffer(this->thread_local_buffer().buf, /*force_flush=*/true);
58 return 0;
59}

References emit_from_thread_local_buffer(), and thread_local_buffer().

◆ thread_local_buffer()

ThreadBufferedSyncbuf::ThreadLocalBuffer & libMesh::ThreadBufferedSyncbuf::thread_local_buffer ( )
private

One ThreadLocalBuffer instance per thread, constructed on first use with *this.

Definition at line 61 of file thread_buffered_syncbuf.C.

62{
63 static thread_local ThreadLocalBuffer t(*this);
64 return t;
65}

Referenced by overflow(), sync(), and xsputn().

◆ xsputn()

std::streamsize libMesh::ThreadBufferedSyncbuf::xsputn ( const char *  s,
std::streamsize  n 
)
overrideprotected

Definition at line 44 of file thread_buffered_syncbuf.C.

45{
46 auto & t = this->thread_local_buffer();
47 t.buf.append(s, static_cast<size_t>(n));
48
49 if (_flush_on_newline && !t.buf.empty() && t.buf.back() == '\n')
50 this->emit_from_thread_local_buffer(t.buf, /*force_flush=*/false);
51
52 return n;
53}

References _flush_on_newline, emit_from_thread_local_buffer(), and thread_local_buffer().

Member Data Documentation

◆ _flush_on_newline

const bool libMesh::ThreadBufferedSyncbuf::_flush_on_newline
private

Whether to flush our sink to terminal/file on terminating new-line characters (
)

Definition at line 86 of file thread_buffered_syncbuf.h.

Referenced by overflow(), and xsputn().

◆ _mu

std::unique_ptr<Threads::spin_mutex> libMesh::ThreadBufferedSyncbuf::_mu
private

Serialization for commits to the shared sink.

Definition at line 89 of file thread_buffered_syncbuf.h.

Referenced by emit_from_thread_local_buffer().

◆ _sink

std::streambuf& libMesh::ThreadBufferedSyncbuf::_sink
private

Wrapped output sink.

Definition at line 84 of file thread_buffered_syncbuf.h.

Referenced by emit_from_thread_local_buffer().


The documentation for this class was generated from the following files: