1189
|
1 #include <stdlib.h>
|
|
2 #include "SemMailManager.h"
|
|
3
|
|
4 void
|
|
5 SemMailManager::calc_mask(unsigned int qsize)
|
|
6 {
|
|
7 mask = 1;
|
|
8 while(qsize>mask) {
|
|
9 mask <<= 1;
|
|
10 }
|
|
11 size = mask;
|
|
12 mask--;
|
|
13 }
|
|
14
|
|
15 SemMailManager::SemMailManager(unsigned int qsize) {
|
|
16 read = write = 0;
|
|
17 calc_mask(qsize);
|
|
18 queue = Newq(memaddr,size);
|
|
19
|
|
20 queue_remain = new Sem(size-1); //queue内に入る残りの数
|
|
21 queue_count = new Sem(0); //queue内に現在入っている数
|
|
22
|
|
23 }
|
|
24
|
|
25 SemMailManager::~SemMailManager()
|
|
26 {
|
|
27 free(queue);
|
|
28 delete queue_remain;
|
|
29 delete queue_count;
|
|
30 }
|
|
31
|
|
32 int
|
|
33 SemMailManager::count()
|
|
34 {
|
|
35 return queue_count->count();
|
|
36 }
|
|
37
|
|
38 void
|
|
39 SemMailManager::send(memaddr data)
|
|
40 {
|
|
41 queue_remain->sem_p(); //資源-1
|
|
42
|
|
43 queue[write++] = data;
|
|
44 //maskの範囲を超えた場合、0に戻す
|
|
45 write &= mask;
|
|
46
|
|
47 queue_count->sem_v(); //資源+1
|
|
48 }
|
|
49
|
|
50 memaddr
|
|
51 SemMailManager::recv()
|
|
52 {
|
|
53 queue_count->sem_p(); //資源-1
|
|
54
|
|
55 memaddr data;
|
|
56 data = queue[read++];
|
|
57 read &= mask;
|
|
58
|
|
59 queue_remain->sem_v(); //資源+1
|
|
60
|
|
61 return data;
|
|
62 }
|
|
63
|
|
64 /* end */
|