/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef DDTHREADEVENT_H
#define DDTHREADEVENT_H

#include <pthread.h>
#include <ddthreadmutex.h>

/// ddthreadevent: NICHT BENUTZT! FIX!
/**

*/
 
class ddthreadevent
{

public:

	enum ddthreadeventType
	{
		DM1_EVENT_SINGLE = 1,
		DM1_EVENT_BROADCAST = 2
	};

  /**
	 * Creates an event. The Default is create an ddthreadevent of type Single.
	 */

	explicit ddthreadevent( ddthreadeventType type = DM1_EVENT_SINGLE );

	/**
	 * Destroys the ddthreadevent.
  */

	~ddthreadevent();

	/**
	 * Sets the ddthreadevent to Non-Signalled state.
	 */

	void reset();

	/**
	 * Unconditional wait. Waits for the ddthreadevent to become Signalled.
	 * Wait returns immediately if ddthreadevent is already in Signalled state;
	 * the ddthreadevent is automatically reset to Not-Signalled state.
	 */

	void wait();

	/**
	 * Conditional wait. Waits for upto “secs” seconds for the ddthreadevent
	 * to be signalled. Wait returns immediately if ddthreadevent is already
	 * in Signalled state; the ddthreadevent is automatically reset to
	 * Non-Signalled state.
	 */

	int  wait(unsigned secs);

	/**
	 * Sets an ddthreadevent to Signalled state, and wakes up one waiting
	 * thread in case of an ddthreadevent of type Single, or all waiting threads
	 * in case of an ddthreadevent of type Broadcast.
	 */

	void notify();

	static void setDebug(int value) { debug = value; }


private:

	ddthreadeventType type;
	pthread_cond_t cond;
	ddthreadmutex mutex;
	bool signaled;
	unsigned waitersCount;

	static int debug;

private:

	ddthreadevent(const ddthreadevent& other);
	ddthreadevent& operator=(const ddthreadevent& other);

};

#endif
