/****************************************************************************
** $Id:  qt/qwaitcondition_unix.cpp   3.0.3   edited Oct 12 12:18 $
**
** QWaitCondition class for Unix
**
** Created : 20010725
**
** Copyright (C) 1992-2001 Trolltech AS.  All rights reserved.
**
** This file is part of the kernel module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#if defined(QT_THREAD_SUPPORT)

#include "qplatformdefs.h"

#ifdef    Q_OS_SOLARIS
// Solaris
typedef mutex_t Q_MUTEX_T;
typedef cond_t  Q_COND_T;

// helpers
#define Q_COND_INIT(a)            cond_init((a), NULL, NULL)
#define Q_COND_DESTROY(a)         cond_destroy((a));
#define Q_COND_BROADCAST(a)       cond_broadcast((a));
#define Q_COND_SIGNAL(a)          cond_signal((a));
#define Q_COND_WAIT(a, b)         cond_wait((a), (b));
#define Q_COND_TIMEDWAIT(a, b, c) cond_timedwait((a), (b), (c));
#else // !Q_OS_SOLARIS
// Pthreads
typedef pthread_mutex_t Q_MUTEX_T;
typedef pthread_cond_t  Q_COND_T;

// helpers
#define Q_COND_INIT(a)            pthread_cond_init((a), NULL)
#define Q_COND_DESTROY(a)         pthread_cond_destroy((a));
#define Q_COND_BROADCAST(a)       pthread_cond_broadcast((a));
#define Q_COND_SIGNAL(a)          pthread_cond_signal((a));
#define Q_COND_WAIT(a, b)         pthread_cond_wait((a), (b));
#define Q_COND_TIMEDWAIT(a, b, c) pthread_cond_timedwait((a), (b), (c));
#endif // Q_OS_SOLARIS

#include "qwaitcondition.h"
#include "qmutex.h"
#include "qmutex_p.h"

#include <errno.h>
#include <string.h>


struct QWaitConditionPrivate {
    Q_COND_T cond;
    QMutex mutex;
};


/*!
  \class QWaitCondition qthread.h
  \brief The QWaitCondition class allows waiting/waking for conditions between threads.

  \ingroup thread
  \ingroup environment

  QWaitConditions allow a thread to tell other threads that some sort of
  condition has been met; one or many threads can block waiting for a
  QWaitCondition to set a condition with wakeOne() or wakeAll().  Use
  wakeOne() to wake one randomly selected event or wakeAll() to wake them
  all. For example, say we have three tasks that should be performed every
  time the user presses a key; each task could be split into a thread, each
  of which would have a run() body like this:

  \code
  QWaitCondition key_pressed;

  for (;;) {
     key_pressed.wait(); // This is a QWaitCondition global variable
     // Key was pressed, do something interesting
     do_something();
  }
  \endcode

  A fourth thread would read key presses and wake the other three threads
  up every time it receives one, like this:

  \code
  QWaitCondition key_pressed;

  for (;;) {
     getchar();
     // Causes any thread in key_pressed.wait() to return from
     // that method and continue processing
     key_pressed.wakeAll();
  }
  \endcode

  Note that the order the three threads are woken up in is undefined,
  and that if some or all of the threads are still in do_something()
  when the key is pressed, they won't be woken up (since they're not
  waiting on the condition variable) and so the task will not be performed
  for that key press.  This can be avoided by, for example, doing something
  like this:

  \code
  QMutex mymutex;
  QWaitCondition key_pressed;
  int mycount=0;

  // Worker thread code
  for (;;) {
     key_pressed.wait(); // This is a QWaitCondition global variable
     mymutex.lock();
     mycount++;
     mymutex.unlock();
     do_something();
     mymutex.lock();
     mycount--;
     mymutex.unlock();
  }

  // Key reading thread code
  for (;;) {
     getchar();
     mymutex.lock();
     // Sleep until there are no busy worker threads
     while( count > 0 ) {
       mymutex.unlock();
       sleep( 1 );
       mymutex.lock();
     }
     mymutex.unlock();
     key_pressed.wakeAll();
  }
  \endcode

  The mutexes are necessary because the results of two threads
  attempting to change the value of the same variable simultaneously
  are unpredictable.

*/

/*!
  Constructs a new event signalling, i.e. wait condition, object.
*/
QWaitCondition::QWaitCondition()
{
    d = new QWaitConditionPrivate;

    int ret = Q_COND_INIT(&d->cond);

#ifdef QT_CHECK_RANGE
    if (ret)
	qWarning( "Wait condition init failure: %s", strerror( ret ) );
#endif
}


/*!
  Deletes the event signalling, i.e. wait condition, object.
*/
QWaitCondition::~QWaitCondition()
{
    int ret = Q_COND_DESTROY(&d->cond);

    if (ret) {
#ifdef QT_CHECK_RANGE
	qWarning( "Wait condition destroy failure: %s", strerror( ret ) );
#endif

	// seems we have threads waiting on us, lets wake them up
	Q_COND_BROADCAST(&d->cond);
    }

    delete d;
}

/*!
  This wakes one thread waiting on the QWaitCondition.  The thread that
  is woken up depends on the operating system's scheduling policies, and
  cannot be controlled or predicted.

  \sa wakeAll()
*/
void QWaitCondition::wakeOne()
{
    d->mutex.lock();

    int ret = Q_COND_SIGNAL(&d->cond);

#ifdef QT_CHECK_RANGE
    if (ret)
	qWarning("Wait condition wakeOne failure: %s", strerror(ret));
#endif

    d->mutex.unlock();
}

/*!
  This wakes all threads waiting on the QWaitCondition.  The order in
  which the threads are woken up depends on the operating system's
  scheduling policies, and cannot be controlled or predicted.

  \sa wakeOne()
*/
void QWaitCondition::wakeAll()
{
    d->mutex.lock();

    int ret = Q_COND_BROADCAST(&d->cond);

#ifdef QT_CHECK_RANGE
    if (ret)
	qWarning("Wait condition wakeAll failure: %s", strerror(ret));
#endif

    d->mutex.unlock();
}

/*!
  Wait on the thread event object. The thread calling this will block
  until either of these conditions is met:
  \list
  \i Another thread signals it using wakeOne() or wakeAll(). This
       function will return TRUE in this case.
  \i \a time milliseconds has elapsed.  If \a time is ULONG_MAX (the default),
       then the wait will never timeout (the event must
       be signalled).  This function will return FALSE if the
       wait timed out.
  \endlist

  \sa wakeOne(), wakeAll()
*/
bool QWaitCondition::wait(unsigned long time)
{
    d->mutex.lock();

    int ret;
    if (time != ULONG_MAX) {
	struct timeval tv;
	gettimeofday(&tv, 0);

	timespec ti;
	ti.tv_sec = tv.tv_sec + (time / 1000);
	ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;

	ret = Q_COND_TIMEDWAIT(&d->cond, &d->mutex.d->handle, &ti);
    } else
	ret = Q_COND_WAIT(&d->cond, &d->mutex.d->handle);

    d->mutex.unlock();

#ifdef QT_CHECK_RANGE
    if (ret && ret != ETIMEDOUT)
	qWarning("Wait condition wait failure: %s",strerror(ret));
#endif

    return (ret == 0);
}

/*!
  \overload
  Release the locked \a mutex and wait on the thread event object.  The
  \a mutex must be initially locked by the calling thread.  If \a mutex
  is not in a locked state, this function returns immediately.  If \a
  mutex is a recursive mutex, this function returns immediately.  The
  \a mutex will be unlocked, and the calling thread will block until
  either of these conditions is met:
  \list
  \i Another thread signals it using wakeOne() or wakeAll(). This
       function will return TRUE in this case.
  \i \a time milliseconds has elapsed.  If \a time is ULONG_MAX (the default),
       then the wait will never timeout (the event must be
       signalled).  This function will return FALSE if the
       wait timed out.
  \endlist

  The mutex will be returned to the same locked state.  This function is
  provided to allow the atomic transition from the locked state to the
  wait state.

  \sa wakeOne(), wakeAll()
*/
bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
{
    if (! mutex)
	return FALSE;

    if (mutex->d->type() == Q_MUTEX_RECURSIVE) {
#ifdef QT_CHECK_RANGE
	qWarning("Wait condition warning: using recursive mutexes with\n"
		 "                        wait conditions is undefined!");
#endif
	return FALSE;
    }

    int ret;
    if (time != ULONG_MAX) {
	struct timeval tv;
	gettimeofday(&tv, 0);

	timespec ti;
	ti.tv_sec = tv.tv_sec + (time / 1000);
	ti.tv_nsec = (tv.tv_usec * 1000) + (time % 1000) * 1000000;

	ret = Q_COND_TIMEDWAIT(&d->cond, &mutex->d->handle, &ti);
    } else
	ret = Q_COND_WAIT(&d->cond, &mutex->d->handle);

#ifdef QT_CHECK_RANGE
    if (ret && ret != ETIMEDOUT)
	qWarning("Wait condition wait failure: %s",strerror(ret));
#endif

    return (ret == 0);
}

#endif // QT_THREAD_SUPPORT
