#ifndef CONDVAR_H
#define CONDVAR_H

#include <pthread.h>
#include <stdio.h>
#include "mutex.h"

class CondVar
{
public:
	CondVar();
	~CondVar();

	int wait();        // this is what waiting thread does
	int signal();      // wake one of the waiting threads
	int broadcast();   // wake all of the waiting threads
	int wait_first_signal(); // if signal already sent it goes trough
                                 // if no signal has been ever sent it waits for first signal
                                 // this is very useful for waiting to start
                                 // and it is 101% safe (race conditions unpossible)

	int wait_one_signal(); // if signal was already sent it goes trough
                                 // if signal has not been sent since last call, it waits for signal
                                 // this allows us never to miss a signal
                                 // even if it happenes before waiting for it
				 // but two signals sent one after another can get merged (like two unlocks)
	int wait_one_signal_timeout(int timeoutsec, int timeoutnsec); // if signal was already sent it goes trough
                                 // if signal has not been sent since last call, it waits for signal
                                 // this allows us never to miss a signal
                                 // even if it happenes before waiting for it
				 // but two signals sent one after another can get merged (like two unlocks)
				// timeout is miliseconds
				
	// signalhistory contains some history about signal
	// bit0 = 1 if any signal has been sent since creation of object 
	// bit1 = 1 if any signal been since last call of wait_one_signal 
	int signalhistory;

	// sets the signalhistory
	int set_signalhistory(int number);
	
	// if we want to handle locking on waiting ourselves
	int set_autolock_onwait(int al);
	int autolock;

	
	// conditional variable and mutex to lock it
	Mutex cond_mutex;
	pthread_cond_t cond;
};


#endif
