exec-stream-helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (C) 2004 Artem Khodush
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. 3. The name of the author may not be used to endorse or promote products
  11. derived from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  13. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  18. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  19. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  20. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  21. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. class os_error_t : public exec_stream_t::error_t {
  24. public:
  25. os_error_t( std::string const & msg );
  26. os_error_t( std::string const & msg, exec_stream_t::error_code_t code );
  27. private:
  28. void compose( std::string const & msg, exec_stream_t::error_code_t code );
  29. };
  30. class pipe_t {
  31. public:
  32. pipe_t();
  33. ~pipe_t();
  34. HANDLE r() const;
  35. HANDLE w() const;
  36. void close_r();
  37. void close_w();
  38. void close();
  39. void open();
  40. private:
  41. enum direction_t{ closed, read, write, both };
  42. direction_t m_direction;
  43. HANDLE m_r;
  44. HANDLE m_w;
  45. };
  46. class set_stdhandle_t {
  47. public:
  48. set_stdhandle_t( DWORD kind, HANDLE handle );
  49. ~set_stdhandle_t();
  50. private:
  51. DWORD m_kind;
  52. HANDLE m_save_handle;
  53. };
  54. class wait_result_t {
  55. public:
  56. wait_result_t();
  57. wait_result_t( DWORD wait_result, int objects_count, HANDLE const * objects );
  58. bool ok();
  59. bool is_signaled( class event_t & event );
  60. bool timed_out();
  61. DWORD error_code();
  62. char const * error_message();
  63. private:
  64. HANDLE m_signaled_object;
  65. bool m_timed_out;
  66. DWORD m_error_code;
  67. char const * m_error_message;
  68. };
  69. class event_t {
  70. public:
  71. event_t();
  72. ~event_t();
  73. bool set();
  74. bool reset();
  75. private:
  76. HANDLE m_handle;
  77. friend wait_result_t wait( event_t & e, DWORD timeout );
  78. friend wait_result_t wait( event_t & e1, event_t & e2, DWORD timeout );
  79. friend class wait_result_t;
  80. };
  81. wait_result_t wait( HANDLE e, DWORD timeout );
  82. wait_result_t wait( event_t & e, DWORD timeout );
  83. wait_result_t wait( event_t & e1, event_t & e2, DWORD timeout ); // waits for any one of e1, e2
  84. class mutex_t {
  85. public:
  86. mutex_t();
  87. ~mutex_t();
  88. private:
  89. HANDLE m_handle;
  90. friend class grab_mutex_t;
  91. };
  92. class grab_mutex_t {
  93. public:
  94. grab_mutex_t( mutex_t & mutex, DWORD timeout );
  95. ~grab_mutex_t();
  96. bool ok();
  97. DWORD error_code();
  98. char const * error_message();
  99. private:
  100. HANDLE m_mutex;
  101. wait_result_t m_wait_result;
  102. };
  103. class thread_buffer_t {
  104. public:
  105. thread_buffer_t();
  106. ~thread_buffer_t();
  107. // those three may be called only before the thread is started
  108. void set_wait_timeout( DWORD milliseconds );
  109. void set_thread_termination_timeout( DWORD milliseconds );
  110. void set_buffer_limit( std::size_t limit );
  111. void set_read_buffer_size( std::size_t size );
  112. void set_binary_mode();
  113. void set_text_mode();
  114. void start_reader_thread( HANDLE pipe );
  115. void start_writer_thread( HANDLE pipe);
  116. void get( exec_stream_t::stream_kind_t kind, char * dst, std::size_t & size, bool & no_more ); // may be called only after start_reader_thread
  117. void put( char * const src, std::size_t & size, bool & no_more );// may be called only after start_writer_thread
  118. bool stop_thread();
  119. bool abort_thread();
  120. private:
  121. enum direction_t { dir_none, dir_read, dir_write };
  122. direction_t m_direction; // set by start_thread
  123. buffer_list_t m_buffer_list;
  124. mutex_t m_mutex; // protecting m_buffer_list
  125. char const * m_message_prefix; // error occured in the thread, if any
  126. DWORD m_error_code; // they are examined only after the thread has terminated
  127. char const * m_error_message; // so setting them anywhere in the thread is safe
  128. DWORD m_wait_timeout; // parameters used in thread
  129. std::size_t m_buffer_limit; // they are set before the thread is started,
  130. std::size_t m_read_buffer_size; // so accessing them anywhere in the thread is safe
  131. HANDLE m_thread;
  132. event_t m_want_data; // for synchronisation between get and reader_thread
  133. event_t m_got_data; // or between put and writer_thread
  134. event_t m_stop_thread;
  135. HANDLE m_pipe;
  136. DWORD m_thread_termination_timeout;
  137. bool m_translate_crlf;
  138. void start_thread( HANDLE pipe, direction_t direction );
  139. static DWORD WINAPI reader_thread( LPVOID param );
  140. static DWORD WINAPI writer_thread( LPVOID param );
  141. void check_error( std::string const & message_prefix, DWORD error_code, std::string const & error_message );
  142. void note_thread_error( char const * message_prefix, DWORD error_code, char const * error_message );
  143. bool check_thread_stopped();
  144. };