LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
async_write.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #include "engineimp.h"
18 #include "writer/writer.h"
19 static void write_data(std::shared_ptr<writer> dat, std::ostream& wfile){
20  dat->write(wfile);
21  wfile << "&&\n";
22 }
23 static void write_individual_dat(const std::list<std::shared_ptr<writer>>& dats, size_t ind, std::ostream& out_stream){
24  out_stream << "index: " << ind << "\n";
25  for(auto& dat : dats){
26  write_data(dat, out_stream);
27  }
28  out_stream << "&\n";
29 }
30 static size_t write_dat(std::map<size_t, std::list<std::shared_ptr<writer>>>& writers, FILE* ofile){
31  std::stringstream out_stream;
32  for(auto& writes : writers){
33  write_individual_dat(writes.second, writes.first, out_stream);
34  }
35  const auto& o_str = out_stream.str();
36  fwrite(o_str.c_str(), sizeof(char), o_str.size(), ofile);
37  writers.clear();
38  return o_str.size();
39 }
40 //This is somewhat of a mess of mutexes, condition variables, and atomic conditions.
41 //I'm this can be cleaned up but it allows the engine to add data for writing while data is being currently written
42 //and the thread will never exit until the engine ends.
44 void write_data(std::mutex& wait_m, std::mutex& data,
45  std::condition_variable& write_notify, data_io_info invals){
46  FILE* ofile = invals.file;
47  auto* writers = invals.writers;
48  volatile std::atomic_size_t* datas_in_queue = invals.datas_queued;
49  volatile std::atomic_char* is_over = invals.is_over;
50  while(true){
51  //obtain a lock on the data structure
52  std::unique_lock<std::mutex> data_lock(data);
53  //wait until more data exists to write
54  while(writers->empty()){
55  //unblock data access
56  data_lock.unlock();
57  //if waiting for data and engine ends, just quit
58  if(*is_over == 1){
59  return;
60  }
61  //no data available, wait for next data run
62  std::unique_lock<std::mutex> lock(wait_m);
63  write_notify.wait(lock, [writers](){return !writers->empty();});
64  //relinquish access to notifier lock
65  lock.unlock();
66  //re-obtain data access
67  data_lock.lock();
68  }
69  if(*is_over == 1 && writers->empty()){
70  return;
71  }
72  //move smeantics since this value is being destroyed anyways
73  auto val_from_front = std::move(writers->front());
74  writers->pop_front();
75  //relinquish access to data, it isn't needed anymore
76  data_lock.unlock();
77  //perform data io and subtract total from
78  write_dat(val_from_front, ofile);
79  (*datas_in_queue)--;
80  }
81 }