Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:11:35

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file celeritas/io/OffloadWriter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <mutex>
0011 
0012 #include "EventIOInterface.hh"
0013 
0014 namespace celeritas
0015 {
0016 //---------------------------------------------------------------------------//
0017 /*!
0018  * Dump primaries to a shared file, one event per flush.
0019  *
0020  * This thin class simply adds a mutex to the output writer for thread safety.
0021  */
0022 class OffloadWriter
0023 {
0024   public:
0025     //!@{
0026     //! \name Type aliases
0027     using UPWriter = std::unique_ptr<EventWriterInterface>;
0028     using argument_type = EventWriterInterface::argument_type;
0029     //!@}
0030 
0031   public:
0032     // Construct from a writer interface
0033     inline explicit OffloadWriter(UPWriter&& writer);
0034 
0035     // Write primaries
0036     inline void operator()(argument_type);
0037 
0038   private:
0039     std::mutex write_mutex_;
0040     UPWriter write_event_;
0041 };
0042 
0043 //---------------------------------------------------------------------------//
0044 // INLINE DEFINITIONS
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * Construct from a writer.
0048  */
0049 OffloadWriter::OffloadWriter(UPWriter&& writer)
0050     : write_event_{std::move(writer)}
0051 {
0052 }
0053 
0054 //---------------------------------------------------------------------------//
0055 /*!
0056  * Write primaries using a mutex.
0057  */
0058 void OffloadWriter::operator()(argument_type primaries)
0059 {
0060     std::lock_guard scoped_lock{write_mutex_};
0061     (*write_event_)(primaries);
0062 }
0063 
0064 //---------------------------------------------------------------------------//
0065 }  // namespace celeritas