// Semaphore.h V0.1pre Provides class Semaphore (Binary & Counting) definition
// © 2003 Elliot Nierman

#ifdef POSIX
//necessary SysV extensions for SysV semaphore
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

//If union senmu is not defined, it must be defined here
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
#else
    union semun {
                           int val;
                           struct semid_ds *buf;
                           unsigned short int *array;
                           struct seminfo *__buf;
                        };
#endif
#endif

class Semaphore
{
        public:
                    Semaphore();
                    Semaphore(long initial_count);
                    Semaphore(long intial_count, long max_count);
                  ~Semaphore();
             void Post(); //Increase semaphore count
             void Wait(); //if non-zero, sem val decremented

        private:
                      int count;

#ifdef WIN
                     HANDLE sem;
#endif

#ifdef POSIX
            int p_sem;
            union semun options;
            struct sembuf action;
#endif
};