/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
    QC_AbstractSmartLock.qpp

    Qore Programming Language

    Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.

    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_AbstractSmartLock.h"

//! The abstract base class for locks that support the internal API for use with the @ref Qore::Thread::Condition "Condition" class
/** This is an abstract class to be inherited by builtin classes that implement the internal %Qore API that allows them to be used by the @ref Qore::Thread::Condition "Condition" class. Currently the @ref Qore::Thread::RWLock "RWLock" and @ref Qore::Thread::Mutex "Mutex" classes inherit this class.

    This class cannot be instantiated directly and also cannot be directly inherited by user-defined classes.

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

//! Throws an exception if called directly; this class can only be instantiated by builtin subclasses
/** @throw ABSTRACTSMARTLOCK-CONSTRUCTOR-ERROR this exception is thrown if this class is constructed directly (also if directly inherited by user classes)
 */
AbstractSmartLock::constructor() {
    xsink->raiseException("ABSTRACTSMARTLOCK-CONSTRUCTOR-ERROR", "this class is an abstract class and cannot be " \
        "constructed directly or inherited directly by a user-defined class");
}

//! Returns the name of the threading class directly inheriting this class
/** @par Example:
    @code{.py}
string name = lock.getName();
    @endcode

    @return the name of the threading class directly inheriting this class
 */
string AbstractSmartLock::getName() [flags=CONSTANT] {
    return new QoreStringNode(asl->getName());
}

//! Returns @ref True if the calling thread owns the lock, @ref False if not
/** @par Example:
    @code{.py}
# only grab and release lock if we don't already own it
bool lck = !lock.lockOwner();
if (lck)
    lock.lock();
on_exit
    if (lck)
	lock.unlock();
    @endcode

    @return @ref True if the calling thread owns the lock, @ref False if not
 */
bool AbstractSmartLock::lockOwner() [flags=CONSTANT] {
    return asl->get_tid() == q_gettid();
}

//! Returns the TID of the thread owning the lock or -1 if the lock is currently not acquired
/** This method normally not useful in practice for anything except checking that the current thread owns the lock, in which case AbstractSmartLock::lockOwner() is better, because if the lock is not owned by the current thread the lock ownership can change at any time.

    @par Example:
    @code{.py}
int tid = lock.lockTID();
    @endcode

    @return the TID of the thread owning the lock or -1 if the lock is currently not acquired
 */
int AbstractSmartLock::lockTID() [flags=CONSTANT] {
    int tid = asl->get_tid();
    return !tid ? -1 : tid;
}
