/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
  QC_AutoReadLock.qpp
 
  Qore Programming Language
 
  Copyright (C) 2003 - 2023 David Nichols
 
  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.

  Note that the Qore library is released under a choice of three open-source
  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
  information.
*/

#include <qore/Qore.h>
#include "qore/intern/QC_AutoReadLock.h"
#include "qore/intern/QC_RWLock.h"

//! A helper class for the @ref Qore::Thread::RWLock "RWLock" class for exception-safe read lock handling
/** AutoReadLock objects, when used along with a @ref Qore::Thread::RWLock "RWLock" object, allow %Qore programmers to safely acquire and release a read lock, even if exceptions are thrown or return statements are executed in the block where the AutoReadLock object is created.

    AutoReadLock objects are helper objects that acquire a read lock for the lifetime of the AutoReadLock object. For this reason, it is only appropriate to assign an AutoReadLock object to a local variable, so when the local variable goes out of scope, the AutoReadLock object will be deleted and the read lock will be automatically released.

    For example:
    @code{.py}
our RWLock rwl();

sub check_error(error) {
    # note that the read lock is acquired in the AutoReadLock constructor, and
    # the read lock will be released as soon as the block is exited below.
    # (with either the throw statement or the return statement)
    AutoReadLock arl(rwl);
    if (error)
        throw "ERROR", "sorry, an error happened";

    return "OK";
}
    @endcode

    @note This class is not available with the @ref PO_NO_THREAD_CLASSES parse option
 */
qclass AutoReadLock [dom=THREAD_CLASS; arg=QoreAutoReadLock* arwl; ns=Qore::Thread];

//! Creates the AutoReadLock object based on the RWLock argument passed and immediately calls RWLock::readLock()
/** Creates the AutoReadLock object based on the RWLock argument passed. The AutoReadLock object immediately calls RWLock::readLock() on the RWLock object passed, and saves it so it can be released when the AutoReadLock object is destroyed.

    @par Example:
    @code{.py}
AutoReadLock arl(rwlock);
    @endcode

    @throw THREAD-DEADLOCK A deadlock was detected while trying to acquire the lock
    @throw LOCK-ERROR RWLock::readLock() called while already holding the write lock, object deleted in another thread, etc.
 */
AutoReadLock::constructor(RWLock[RWLock] rwl) {
   ReferenceHolder<QoreAutoReadLock> arwl(new QoreAutoReadLock(rwl, xsink), xsink);
   if (!*xsink)
      self->setPrivate(CID_AUTOREADLOCK, arwl.release());
}

//! Calls RWLock::readUnlock() on the saved RWLock and destroys the AutoReadLock object
/** 
    @par Example:
    @code{.py}
delete arl;
    @endcode

    @throw LOCK-ERROR RWLock::readUnlock() called while not holding the read lock, RWLock object deleted in another thread, etc
 */
AutoReadLock::destructor() {
   arwl->destructor(xsink);
   arwl->deref(xsink);
}

//! Throws an exception; objects of this class cannot be copied
/** 
    @throw AUTOREADLOCK-COPY-ERROR objects of this class cannot be copied
 */
AutoReadLock::copy() {
   xsink->raiseException("AUTOREADLOCK-COPY-ERROR", "objects of this class cannot be copied");
}
