1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#ifndef MT_SEMAPHORE_H
#define MT_SEMAPHORE_H
#include "base.h"
#include <iostream>
#include <semaphore.h>
namespace mt
{
class semaphore
{
MT_PREVENT_COPY(semaphore)
public:
typedef semaphore this_type;
typedef void base_type;
semaphore(void)
{
sem_init(&(this->s), 0, 0);
}
semaphore(int value)
{
sem_init(&(this->s), 0, value);
}
~semaphore(void)
{
sem_destroy(&(this->s));
}
void post(void)
{
sem_post(&(this->s));
}
/*
void post(int n)
{
sem_post_multiple(&(this->s), n);
}
*/
void wait(void)
{
sem_wait(&(this->s));
}
bool trywait(void)
{
return (sem_trywait(&(this->s)) == 0);
}
//methods added for conforming to the QT implementation
//jnoguera 14-12-2011
void release(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.release() not supported\n";
sem_post(&(this->s));
}
void acquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
sem_wait(&(this->s));
}
bool tryAcquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
return (sem_trywait(&(this->s)) == 0);
}
int available()
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
private:
sem_t s;
};
}
#endif // MT_SEMAPHORE_H
|