changeset 1180:2e20d2bb55c6 draft

change mailmanager to mail on semaphore
author Daichi TOMA
date Sat, 25 Jun 2011 09:39:45 +0900
parents 5393bebe0956
children fa380908f801
files TaskManager/Makefile TaskManager/kernel/ppe/MailManager.cc TaskManager/kernel/ppe/MailManager.h TaskManager/kernel/ppe/Sem.cc TaskManager/kernel/ppe/Sem.h TaskManager/test/Makefile
diffstat 6 files changed, 164 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Makefile	Sat Jun 25 09:39:45 2011 +0900
@@ -0,0 +1,56 @@
+include ./Makefile.def
+
+TAGS       = gtags
+TAGSOPTION = 
+TAGSFILE   = GPATH GRTAGS GSYMS GTAGS
+
+default: fifo
+
+fifo: FORCE
+	@$(MAKE) -f Makefile.fifo
+
+fifo64: FORCE
+	@$(MAKE) -f Makefile.fifo ABIBIT=64
+
+depend: FORCE
+	@$(MAKE) -f Makefile.fifo depend
+
+cell: FORCE
+	@$(MAKE) -f Makefile.cell
+
+parallel: FORCE
+	@$(MAKE) -f Makefile.parallel
+
+FORCE:
+	-mkdir -p ../include/TaskManager
+	rsync `find . -name Test -prune -or -name '*.h' -print` ../include/TaskManager
+
+distclean: clean
+	rm -f $(TAGSFILE)
+	$(MAKE) -f Makefile.cell celldistclean
+	$(MAKE) -f Makefile.fifo fifodistclean
+
+clean:
+	rm -f *~ \#*
+	rm -f $(TARGET)
+	cd $(KERN_DIR);         rm -f *~ \#*
+	cd $(KERN_PPE_DIR);     rm -f *~ \#*
+	cd $(KERN_SPE_DIR);     rm -f *~ \#*
+	cd $(KERN_SCHED_DIR);   rm -f *~ \#*
+	cd $(KERN_MEM_DIR);   rm -f *~ \#*
+	cd $(KERN_SYSTASK_DIR); rm -f *~ \#*
+	cd $(KERN_MEM_DIR); rm -f *~ \#*
+	cd $(IMPL_FIFO_DIR);    rm -f *~ \#*
+	cd $(IMPL_CELL_DIR);    rm -f *~ \#*
+	rm -f $(KERN_MAIN_OBJS) $(KERN_PPE_OBJS) $(KERN_SPE_OBJS) \
+	$(KERN_SCHED_OBJS) $(KERN_SYSTASK_OBJS) $(KERN_MEM_OBJS)
+	rm -f $(IMPL_FIFO_OBJS) $(IMPL_CELL_OBJS)
+	$(MAKE) -f Makefile.cell cellclean
+	$(MAKE) -f Makefile.fifo fifoclean
+	rm -rf *.a ../include	
+	cd $(IMPL_FIFO_DIR);    rm -f {ShowTime.cc,StartProfile.cc}	
+
+tags:
+	$(TAGS) $(TAGSOPTION)
+
+-include ./Makefile.dep
--- a/TaskManager/kernel/ppe/MailManager.cc	Tue Jun 21 19:50:53 2011 +0900
+++ b/TaskManager/kernel/ppe/MailManager.cc	Sat Jun 25 09:39:45 2011 +0900
@@ -16,9 +16,27 @@
     read = write = 0;
     calc_mask(qsize);
     queue = Newq(memaddr,size);
+
+    mutex_s = new pthread_mutex_t;	//sendのロック用
+    mutex_r = new pthread_mutex_t;	//recvのロック用
+    pthread_mutex_init(mutex_s, NULL);
+    pthread_mutex_init(mutex_r, NULL);
+
+    remain  = new Sem(0);			//queue内のアイテム数を管理
+
 }
 
-MailManager::~MailManager() { free(queue); }
+MailManager::~MailManager()
+{
+	free(queue);
+
+	pthread_mutex_destroy(mutex_s);
+	pthread_mutex_destroy(mutex_r);
+
+	delete mutex_s;
+	delete mutex_r;
+	delete remain;
+}
 
 int 
 MailManager::count()
@@ -44,16 +62,24 @@
 void 
 MailManager::send(memaddr data)
 {
+	pthread_mutex_lock(mutex_s);
+
     queue[write++] = data;
     write &= mask;
     if (write==read) {
 	extend();
     }
+
+    pthread_mutex_unlock(mutex_s);
+    remain->sem_v();		//資源を解放
 }
 
 memaddr 
 MailManager::recv()
 {
+	pthread_mutex_lock(mutex_r);
+
+	remain->sem_p();		//資源の確保
     memaddr data;
     if (count()>0) {
 	data = queue[read++];
@@ -61,6 +87,8 @@
     } else {
 	data = 0;
     }
+    pthread_mutex_unlock(mutex_r);
+
     return data;
 }
 
--- a/TaskManager/kernel/ppe/MailManager.h	Tue Jun 21 19:50:53 2011 +0900
+++ b/TaskManager/kernel/ppe/MailManager.h	Sat Jun 25 09:39:45 2011 +0900
@@ -1,7 +1,9 @@
 #ifndef INCLUDED_MAIL_MANAGER
 #define INCLUDED_MAIL_MANAGER
 
+#include <pthread.h>
 #include "types.h"
+#include "Sem.h"
 
 class MailManager {
 public:
@@ -18,6 +20,9 @@
 private:
     /* variables */
     memaddr *queue;
+    pthread_mutex_t *mutex_s;
+    pthread_mutex_t *mutex_r;
+    SemPtr remain;
     unsigned int size;
     unsigned int read;
     unsigned int write;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/kernel/ppe/Sem.cc	Sat Jun 25 09:39:45 2011 +0900
@@ -0,0 +1,46 @@
+#include "Sem.h"
+#include <pthread.h>
+
+
+Sem::Sem(int value)
+{
+	sem = new sem_t;
+	pthread_mutex_init(&sem->mutex, NULL);
+	pthread_cond_init(&sem->cond, NULL);
+	sem->value = value;
+}
+
+Sem::~Sem()
+{
+	pthread_mutex_destroy(&sem->mutex);
+	pthread_cond_destroy(&sem->cond);
+
+	delete sem;
+}
+
+//P命令
+//資源を確保する
+void
+Sem::sem_p()
+{
+	pthread_mutex_lock(&sem->mutex);
+	//セマフォ変数が0の時、他のスレッドが資源を解放するのを待つ
+	while(sem->value == 0) {
+		pthread_cond_wait(&sem->cond, &sem->mutex);
+	}
+	sem->value--;	//資源の確保
+	pthread_mutex_unlock(&sem->mutex);
+}
+
+//V命令
+//資源を解放する
+void
+Sem::sem_v()
+{
+	pthread_mutex_lock(&sem->mutex);
+	sem->value++;	//資源の解放
+	pthread_mutex_unlock(&sem->mutex);
+
+	//資源の解放を知らせる
+	pthread_cond_signal(&sem->cond);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/kernel/ppe/Sem.h	Sat Jun 25 09:39:45 2011 +0900
@@ -0,0 +1,26 @@
+#ifndef INCLUDED_SEM
+#define INCLUDED_SEM
+
+#include <pthread.h>
+
+typedef struct sem_t {
+	volatile int value;		//セマフォ変数
+	pthread_mutex_t mutex;	//セマフォ操作用のロック
+	pthread_cond_t cond;	//待ち合わせ用の条件変数
+} sem_t, *sem_ptr;
+
+class Sem {
+public:
+	/* constructor */
+	Sem(int value);
+	~Sem();
+	void sem_p();
+	void sem_v();
+	/* variables */
+private:
+	sem_t *sem;
+};
+
+typedef Sem *SemPtr;
+
+#endif /* INCLUDED_SEM */
--- a/TaskManager/test/Makefile	Tue Jun 21 19:50:53 2011 +0900
+++ b/TaskManager/test/Makefile	Sat Jun 25 09:39:45 2011 +0900
@@ -6,7 +6,8 @@
 
 $(TARGET) :
 
-LIBS += ../libFifoManager.a
+#LIBS += ../libFifoManager.a
+LIBS += ../libParallelManager.a
 
 MailManagerTest : MailManagerTest.o
 	$(CC) $(CFLAGS) -o $@ $? $(LIBS)