libMesh
thread_buffered_syncbuf.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_THREAD_BUFFERED_SYNCBUF_H
19 #define LIBMESH_THREAD_BUFFERED_SYNCBUF_H
20 
21 #include <streambuf>
22 #include <string>
23 #include <memory>
24 
25 namespace libMesh
26 {
27 namespace Threads
28 {
29 class spin_mutex;
30 }
31 
32 class ThreadBufferedSyncbuf : public std::streambuf
33 {
34 public:
35  explicit ThreadBufferedSyncbuf(std::streambuf & sink, bool flush_on_newline = true);
36 
41 
42 protected:
43  // Single-char path
44  int_type overflow(int_type ch) override;
45 
46  // Bulk write path
47  std::streamsize xsputn(const char * s, std::streamsize n) override;
48 
49  // Flush path (std::flush / std::endl / ostream::flush)
50  int sync() override;
51 
52 private:
58  {
59  public:
60  explicit ThreadLocalBuffer(ThreadBufferedSyncbuf & owner) : _owner(owner) {}
61 
66 
68  std::string buf;
69 
70  private:
73  };
74 
79 
83  void emit_from_thread_local_buffer(std::string & b, bool force_flush);
84 
86  std::streambuf & _sink;
88  const bool _flush_on_newline;
89 
91  std::unique_ptr<Threads::spin_mutex> _mu;
92 };
93 
94 }
95 #endif // LIBMESH_THREAD_BUFFERED_SYNCBUF_H
ThreadBufferedSyncbuf(std::streambuf &sink, bool flush_on_newline=true)
const bool _flush_on_newline
Whether to flush our sink to terminal/file on terminating new-line characters ( ) ...
The libMesh namespace provides an interface to certain functionality in the library.
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.
~ThreadLocalBuffer()
Ensures we write to our sink upon destruction if our buf is non-empty.
std::unique_ptr< Threads::spin_mutex > _mu
Serialization for commits to the shared sink.
std::streambuf & _sink
Wrapped output sink.
int_type overflow(int_type ch) override
ThreadBufferedSyncbuf & _owner
owning syncing stream buffer
~ThreadBufferedSyncbuf()
Defaulted destructor defined in implementation so we don&#39;t need to include threads.h.
std::streamsize xsputn(const char *s, std::streamsize n) override
tbb::spin_mutex spin_mutex
Spin mutex.
Definition: threads_tbb.h:167
A class that wraps a thread-local string.