Home | History | Annotate | Download | only in Common
      1 // StreamBinder.h
      2 
      3 #ifndef __STREAM_BINDER_H
      4 #define __STREAM_BINDER_H
      5 
      6 #include "../../Windows/Synchronization.h"
      7 
      8 #include "../IStream.h"
      9 
     10 /*
     11 We don't use probably UNSAFE version:
     12 reader thread:
     13      _canWrite_Event.Set();
     14      _readingWasClosed = true
     15      _canWrite_Event.Set();
     16 writer thread:
     17      _canWrite_Event.Wait()
     18       if (_readingWasClosed)
     19 Can second call of _canWrite_Event.Set() be executed without memory barrier, if event is already set?
     20 */
     21 
     22 class CStreamBinder
     23 {
     24   NWindows::NSynchronization::CAutoResetEvent _canWrite_Event;
     25   NWindows::NSynchronization::CManualResetEvent _canRead_Event;
     26   NWindows::NSynchronization::CManualResetEvent _readingWasClosed_Event;
     27 
     28   // bool _readingWasClosed;
     29   bool _readingWasClosed2;
     30   // bool WritingWasCut;
     31   bool _waitWrite;
     32   UInt32 _bufSize;
     33   const void *_buf;
     34 public:
     35   UInt64 ProcessedSize;
     36 
     37   WRes CreateEvents();
     38   void CreateStreams(ISequentialInStream **inStream, ISequentialOutStream **outStream);
     39 
     40   void ReInit();
     41 
     42   HRESULT Read(void *data, UInt32 size, UInt32 *processedSize);
     43   HRESULT Write(const void *data, UInt32 size, UInt32 *processedSize);
     44 
     45   void CloseRead()
     46   {
     47     _readingWasClosed_Event.Set();
     48     // _readingWasClosed = true;
     49     // _canWrite_Event.Set();
     50   }
     51 
     52   void CloseWrite()
     53   {
     54     _buf = NULL;
     55     _bufSize = 0;
     56     _canRead_Event.Set();
     57   }
     58 };
     59 
     60 #endif
     61