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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| #include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define ALLOW_OTHER_PROCESS 0
class Semaphore {
public:
static const int WAIT_FOREVER = -1;
static int num;
protected:
sem_t id;
char name[20];
Semaphore() {
sprintf(name,"sem_%d",num);
num++;
if (num >= SEM_VALUE_MAX) {
printf("attention nombre maximal de semaphores a
tteint\n");
}
}
Semaphore(sem_t id) : id(id) { strcpy(name,"\0"); }
public:
virtual bool take(int to) { return (sem_wait(&id) == 0); }
void take() { take(WAIT_FOREVER); }
virtual void give() {
if (sem_post(&id) != 0)
perror("Semaphore::give() failure in sem_post\n"
);
}
virtual void flush() {
int tmp;
do {
sem_getvalue(&id,&tmp);
} while (tmp > 0);
}
virtual ~Semaphore() { sem_destroy(&id); }
};
int Semaphore::num = 1;
class Mutex : public Semaphore {
public:
Mutex() : Semaphore() {
if (sem_init(&id, ALLOW_OTHER_PROCESS, 1) < 0) {
perror("sem_init failure in Mutex()\n");
}
}
};
class CountSema : public Semaphore {
protected:
int count;
Mutex countMtx;
public:
CountSema(int init=0) : Semaphore() {
if (sem_init(&id, ALLOW_OTHER_PROCESS, init) < 0) {
perror("sem_init failure in CountSema()\n");
}
count = init;
}
virtual ~CountSema() { }
virtual bool take(int to) {
countMtx.take();
count--;
if (count < 0) {
countMtx.give();
if (sem_wait(&id) == 0) {
countMtx.take();
count++;
countMtx.give();
return false;
}
} else {
countMtx.give();
}
return true;
}
virtual void give() {
countMtx.take();
if (count < 0)
if (sem_post(&id) != 0) {
perror("sem_post failure in CountSema::g
ive()\n");
}
count++;
countMtx.give();
}
virtual void flush() {
countMtx.take();
while (count > 0) {
if (sem_post(&id) != 0)
perror("CountSema::flush() failure in se
m_post\n");
count--;
}
count = 0;
countMtx.give();
}
virtual int getCount() {
return count;
}
};
class BinSema : public Semaphore {
public:
BinSema(bool init=false) : Semaphore() {
if (sem_init(&id, ALLOW_OTHER_PROCESS, ((init) ? 1 : 0))
< 0) {
perror("sem_init failure in BinSema()\n");
}
;
}
};
static void* _c_run(void* pt);
class Thread {
public:
static int num;
protected:
const int stackSize;
int prio;
virtual void run() = 0;
pthread_t* id;
BinSema joinSem;
Mutex idMtx;
char name[20];
public:
Thread(int _prio, int _stackSize) : stackSize(_stackSize), prio(
_prio), id(NULL) {
sprintf(name,"thread_%d",num);
}
virtual ~Thread() {
if (id != NULL) {
pthread_cancel(*id);
free(id);
}
joinSem.flush();
}
void start() {
idMtx.take();
if (id == NULL) {
id = (pthread_t*) malloc(sizeof(pthread_t));
if (pthread_create(id, NULL, _c_run, (void*) thi
s) != 0) {
perror("Thread::start() failure in pthre
ad_create\n");
free(id);
}
idMtx.give();
} else {
idMtx.give();
throw "Thread non demarre";
}
}
void join() {
idMtx.take();
if (id != 0) {
idMtx.give();
joinSem.take();
} else
idMtx.give();
}
friend void* _c_run(void* p);
};
int Thread::num = 1;
static void* _c_run(void* arg) {
Thread* pt = (Thread*) arg;
pt->run();
pt->joinSem.flush();
free(pt->id);
} |
Partager