forked from kmsmith137/ch_vdif_assembler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk_writer_thread.cpp
69 lines (51 loc) · 1.68 KB
/
disk_writer_thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "ch_vdif_assembler_internals.hpp"
using namespace std;
namespace ch_vdif_assembler {
#if 0
}; // pacify emacs c-mode!
#endif
struct disk_writer_thread : public thread_base {
shared_ptr<assembler_nerve_center> nc;
string outdir;
int thread_id;
bool mkdir_called;
disk_writer_thread(const shared_ptr<assembler_nerve_center> &nc_, const string &outdir_, int thread_id_)
: thread_base("disk writer thread " + to_string(thread_id_)),
nc(nc_), outdir(outdir_), thread_id(thread_id_), mkdir_called(false)
{
xassert(thread_id >= 0);
xassert(thread_id < constants::num_disks);
}
virtual ~disk_writer_thread() { }
// Devirtualize thread_base
virtual void thread_body()
{
// Kill the assembler if we throw an exception before the end
assembler_killer k(nc, "disk_writer thread threw exception");
for (;;) {
shared_ptr<vdif_chunk> data = nc->disk_writer_get_chunk(thread_id, timer);
if (!data)
break;
if (!mkdir_called) {
xmkdir(outdir);
cout << (name + string(": created directory ") + outdir + "\n") << flush;
mkdir_called = true;
}
// Make filename
stringstream ss;
ss << "/" << setfill('0') << setw(7) << data->seq_id << ".dat";
string basename = ss.str();
string filename = outdir + basename;
data->write(filename);
cout << (name + string(": wrote ") + filename + "\n") << flush;
}
k.let_live();
}
};
void spawn_disk_writer_thread(const shared_ptr<assembler_nerve_center> &nc, const string &data_dir, int ithread)
{
xassert(nc);
nc->check_alive();
spawn_thread<disk_writer_thread> (nc, data_dir, ithread);
}
} // namespace ch_vdif_assembler