/**
 * @file semaphore.h
 * @brief Header file for implementation of semaphores, file semaphore.c
 * 
 * Semaphores are implemented as a integer variable and queue of threads waiting on the semaphore.
 * The implementation don't solve this cases (&& we know it && we think it can cause troubles )
 * Is undefined what happens, if thread waiting in the quque is waked up by another thread and not
 * by semaphore. It is undefined, what happens, if thread "owning" semaphore is killed.
 * 
 * This file is based on Kalisto, Development Kernel copyrighted (c) to 
 * Distributed Systems Research Group MFF UK, Czech republic.
 */
 
#ifndef SEMAPHORE_H_
#define SEMAPHORE_H_

#include "malloc.h"
#include "tqueue.h"
#include "thread.h"

struct semaphore {
	/** value on semaphore */
	int value;           
	/** list of threads waiting on the semaphore*/
	thread_queue_t  waiting_threads;
	/** thread holding mutex.. for mutex purposes onlydd */
};

typedef struct semaphore semaphore_t;

/* dont move to the top of the file, see previous declaration */

void sem_init (struct semaphore * sem, const int value);
void sem_destroy (semaphore_t * sem);
int sem_get_value (semaphore_t * sem);
void sem_up (semaphore_t * sem);
void sem_down (semaphore_t * sem);
int sem_down_timeout (semaphore_t * sem, const unsigned int usec);

#endif /*SEMAPHORE_H_*/
