modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / aio-test / nsync.h
blobeb74cca7d73aa1d0683258370bf93369d642ec05
1 /*
2 * Asynchronous IO buffer adapter
4 * This library is under no license, hence,
5 * you can do whatever you wanna do on this
6 * library.
8 * Created on: Jul 5, 2010
9 * Author: Aqua
12 #ifndef _NSYNC_H_AQUA
13 #define _NSYNC_H_AQUA
16 * Here is a illustration on you use this lib,
17 * this lib requires you to define a Routine that
18 * flush the data from buffer and do what you wanna
19 * ----------------------------------------------------------------------
20 void Routine(nSyncBuf_s & buf, size_t size)
22 cout<<string(*buf, size);
23 buf.release();
26 int main(int argc, char ** argv)
28 nSyncBuf_s buf(Routine, true);
29 cerr<<"[ImportFileList]:"<<ImportFileList(buf, argv[1])<<endl;
30 nSync(buf);
32 * ----------------------------------------------------------------------
33 nSyncBuf_s buf(coutRoutine, true):
34 "nSyncBuf_s" is a class that contains the buffer
35 and linked to boost::asio for asynchronous stream.
36 "Routine" is the Routine that handles the data.
37 You can pass the second parameter as "true" if the
38 next run of "Routine" would depend on the accomplishment
39 of the last run, thus to say, read and process sequentially,
40 or "false" if "Routine"s are absolutely independent.
41 PLEASE NOTE THE, for passing "true", it's your duty to
42 "release" when the previous Routine was done. You can
43 see "buf.release();" after "cout<<string(*buf, size);".
44 Deadlock would occur if you forget to release when "true";
45 cout<<string(*buf, size):
46 This demonstrated the way to visit the buffer, "*buf"
47 as well as "buf.ptr()" would return the foremost
48 pointer of buffer. "size" indicates the length of
49 effective data in buffer.
50 ImportFileList(buf, argv[1]):
51 "argv[1]" should be a filename that contains filenames,
52 one row each filename.
53 InsertFile(buf, argv[1]):
54 "argv[1]" should be a single filename pending to read.
55 Please notice that, for multiple files, the would be no
56 EOF at the end of each file, thus, the data of next file
57 (if available) would append tightly at the very end of
58 previous file.
59 nSync(buf):
60 Let's rock.
64 #if defined(__CYGWIN__)
65 # define _WIN32_WINNT 0x0501
66 # define __USE_W32_SOCKETS
67 # undef BOOST_POSIX_API
68 # define BOOST_WINDOWS_API
69 #endif
70 #include <boost/asio.hpp>
71 #define BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE
72 #include <boost/process.hpp>
73 #include <boost/bind.hpp>
74 #include <unistd.h>
75 #include <fstream>
76 #include <boost/thread/mutex.hpp>
77 #include <exception>
78 #include <vector>
80 using namespace std;
81 using namespace boost;
82 namespace bp = ::boost::process;
83 namespace ba = ::boost::asio;
85 typedef char buf_t;
86 typedef buf_t * nSyncBufAry_t;
88 class nSyncBuf_s;
89 typedef void(*_ConsumeBufferRoutine_f)(nSyncBuf_s &, size_t);
91 class nSyncBuf_s
93 public:
95 explicit nSyncBuf_s(_ConsumeBufferRoutine_f, bool);
96 ~nSyncBuf_s();
98 void lock() __attribute__((always_inline));
99 void unlock() __attribute__((always_inline));
100 void release();
101 nSyncBufAry_t ptr();
102 nSyncBufAry_t operator *();
104 private:
105 friend int ImportFileList(nSyncBuf_s &, char *);
106 friend int InsertFile(nSyncBuf_s &, char *);
107 friend void nSync(nSyncBuf_s &);
108 friend bp::child StartChild(nSyncBuf_s &);
109 friend void nSyncRead(nSyncBuf_s &);
110 friend void endRead(nSyncBuf_s &, const boost::system::error_code &, size_t);
112 protected:
113 vector<string> fnList;
114 nSyncBufAry_t buffer;
115 bool atomEnabler;
116 mutex atom;
117 _ConsumeBufferRoutine_f Routine;
119 public:
120 //ASIO stream connectors
121 ba::io_service io_service;
122 #if defined(BOOST_POSIX_API)
123 ba::posix::stream_descriptor in;
124 #elif defined(BOOST_WINDOWS_API)
125 ba::windows::stream_handle in;
126 #else
127 #error Unsupported platform.
128 #endif
131 int ImportFileList(nSyncBuf_s &, char *) __attribute__((warn_unused_result));
132 int InsertFile(nSyncBuf_s &, char *) __attribute__((warn_unused_result));
133 void nSync(nSyncBuf_s &);
135 #endif /* _NSYNC_H_AQUA */