/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
  QC_Counter.cpp

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

//! Implements a class that can be used for blocking a thread until a counter reaches zero
/** Counter objects allow Qore threads to sleep until a counter reaches zero.

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

//! Creates the Counter object
/** @param c an argument is supplied here, then the Counter will be initialized with this value, otherwise the Counter is initialized with 0

    @par Example:
    @code{.py}
Counter counter();
    @endcode

    @throw COUNTER-ERROR a negative number was passed to initialize the Counter
 */
Counter::constructor(softint c = 0) {
   if (c < 0) {
      xsink->raiseException("COUNTER-ERROR", "Counter::constructor() called with initial value " QLLD ", however value must be non-negative", c);
      return;
   }
   self->setPrivate(CID_COUNTER, new Counter(c));
}

//! Destroys the Counter object
/** Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and also in each thread blocked on this object when it is deleted.

    @par Example:
    @code{.py}
delete counter;
    @endcode

    @throw COUNTER-ERROR Object deleted while other threads blocked on it
 */
Counter::destructor() {
   c->destructor(xsink);
   c->deref(xsink);
}

//! Creates a new Counter object with the same count as the original
/**
    @par Example:
    @code{.py}
Counter new_counter = counter.copy();
    @endcode
 */
Counter::copy() {
   self->setPrivate(CID_COUNTER, new Counter(c->getCount()));
}

//! Atomically increments the counter value
/**
    @par Example:
    @code{.py}
counter.inc();
    @endcode

    @return the current value after the increment is returned

    @since %Qore 0.9 this method returns the current value after the increment
 */
int Counter::inc() {
   return c->inc();
}

//! Atomically decrements the counter value
/** A \c COUNTER-ERROR exception can be thrown if the object is deleted in another thread while this call is in progress; this is a race condition caused by a user programming error and should not occur in practice with correct code.

    @par Example:
    @code{.py}
counter.dec();
    @endcode

    @return the current value after the decrement is returned

    @throw COUNTER-ERROR Counter has been deleted in another thread or Counter is already at 0

    @since %Qore 0.8.13 this method returns the current value after the decrement
 */
int Counter::dec() {
   return c->dec(xsink);
}

//! Blocks a thread until the counter reaches zero
/**
    @par Example:
    @code{.py}
counter.waitForZero();
    @endcode

    @throw COUNTER-ERROR Counter has been deleted in another thread
 */
nothing Counter::waitForZero() {
   c->waitForZero(xsink);
}

//! Blocks a thread until the counter reaches zero
/** @param timeout_ms a @ref timeout_type "timeout" value to wait for the Counter to reach zero; integers are interpreted as milliseconds; @ref relative_dates "relative date/time values" are interpreted literally (with a resolution of milliseconds)

    @return 0 on sucess, or non-zero if a timeout occurred

    @par Example:
    @code{.py}
if (counter.waitForZero(1500))
    throw "TIMEOUT", "counter did not reach 0 in 1.5s";
    @endcode

    @throw COUNTER-ERROR Counter has been deleted in another thread
 */
int Counter::waitForZero(timeout timeout_ms) {
   return c->waitForZero(xsink, timeout_ms);
}

//! Returns the current counter value
/** @return the current counter value

    @par Example:
    @code{.py}
int c = counter.getCount();
    @endcode
 */
int Counter::getCount() [flags=CONSTANT] {
   return c->getCount();
}

//! Returns the number of threads currently blocked on this object
/** @return the number of threads currently blocked on this object

    @par Example:
    @code{.py}
int c = counter.getWaiting();
    @endcode
 */
int Counter::getWaiting() [flags=CONSTANT] {
   return c->getWaiting();
}
