//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   SWMRG.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus - Modified and adapted from Jeffery Richter's ("Advanced Windows")
//                         non object oriented version.
//
//  Purpose:    Single-writer, multiple-reader guard synchronization class.
//
//  Notes:      1)  The lpszName to the constructor names this object. Specify NULL if
//                  if you don't want to share the object across process boundaries.
//
//              2)  Only destroy this object when when no writer or reader threads in the calling
//                  are not waiting.

#ifndef SWMRG_H_DEFINED
#define SWMRG_H_DEFINED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class EXPORT_DLL CSWMRG
{
public:

    // Construction and destruction.

                CSWMRG                      (   LPCTSTR lpszName    = NULL );           // Use NULL if you don't want to use this object across process boundaries.
    virtual    ~CSWMRG                      (   void );

    // 

    bool        IsInitialized               (   void ) const;                           // Check for construction success.

    // Write access.

    DWORD       WaitToWrite                 (   DWORD   dwTimeout   = INFINITE );
    void        DoneWriting                 (   void );

    // Read access.

    DWORD       WaitToRead                  (   DWORD   dwTimeout   = INFINITE );
    void        DoneReading                 (   void );

private:

    // Attributes.

    bool    m_bInitialized;                                                             // Construction success indicator flag.

    HANDLE  m_hMutexNoWriter;                                                           // Guard for internal state and indicatew whether writers are waiting.
    HANDLE  m_hEventNoReaders;                                                          // Manual reset event to singnal when no reader is reading.
    HANDLE  m_hSemNumReaders;                                                           // Multiprocess thread counter... not used for syncronization itself.


    // Default constructor and assignment operator not implemented.

                CSWMRG                      (   const CSWMRG   &rRhs );                 // Not implemented.
    CSWMRG &    operator =                  (   const CSWMRG   &rRhs );                 // Not implemented.

    LPCTSTR     ConstructObjName            (   LPCTSTR                             lpszPrefix,
                                                LPCTSTR                             lpszSuffix,
                                                LPTSTR                              lpszFullName,
                                                size_t                              cbFullName,
                                                bool                               *bOk )           const;

};

#endif // end SWMRG_H_DEFINED
