annotate TaskManager/kernel/ppe/Sem.cc @ 1531:4443d59a8210 draft

SDL.h in main.cc
author yuhi@cr.ie.u-ryukyu.ac.jp
date Fri, 07 Dec 2012 15:46:27 +0900
parents d4311f83377a
children 039e6d5cf5b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1180
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
1 #include "Sem.h"
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
2 #include <pthread.h>
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
3
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
4
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
5 Sem::Sem(int value)
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
6 {
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
7 sem = new sem_t;
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
8 pthread_mutex_init(&sem->mutex, NULL);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
9 pthread_cond_init(&sem->cond, NULL);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
10 sem->value = value;
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
11 }
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
12
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
13 Sem::~Sem()
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
14 {
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
15 pthread_mutex_destroy(&sem->mutex);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
16 pthread_cond_destroy(&sem->cond);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
17
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
18 delete sem;
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
19 }
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
20
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
21 //P命令
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
22 //資源を確保する
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
23 void
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
24 Sem::sem_p()
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
25 {
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
26 pthread_mutex_lock(&sem->mutex);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
27 //セマフォ変数が0の時、他のスレッドが資源を解放するのを待つ
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
28 while(sem->value == 0) {
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
29 pthread_cond_wait(&sem->cond, &sem->mutex);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
30 }
1186
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
31 //atomic
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
32 //sem->value--; //資源の確保
1187
d4311f83377a Put in order Test directory
Daichi TOMA
parents: 1186
diff changeset
33 //count()のvalueを取得する際にアトミック操作が必要
1186
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
34 __sync_fetch_and_sub(&sem->value,1);
1180
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
35 pthread_mutex_unlock(&sem->mutex);
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
36 }
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
37
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
38 //V命令
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
39 //資源を解放する
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
40 void
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
41 Sem::sem_v()
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
42 {
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
43 pthread_mutex_lock(&sem->mutex);
1186
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
44 //atomic
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
45 //sem->value++; //資源の解放
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
46 __sync_fetch_and_add(&sem->value,1);
1180
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
47
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
48 //資源の解放を知らせる
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
49 pthread_cond_signal(&sem->cond);
1186
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
50 pthread_mutex_unlock(&sem->mutex);
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
51
1180
2e20d2bb55c6 change mailmanager to mail on semaphore
Daichi TOMA
parents:
diff changeset
52 }
1186
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
53
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
54 int
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
55 Sem::count()
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
56 {
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
57 //semの値を返せばよい。
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
58 //atomic
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
59 return sem->value;
4c209dd223cd fix MailManager and Semaphore
Daichi TOMA
parents: 1180
diff changeset
60 }