view TaskManager/kernel/ppe/MailManager.cc @ 1186:4c209dd223cd draft

fix MailManager and Semaphore
author Daichi TOMA
date Mon, 04 Jul 2011 14:11:26 +0900
parents 2e20d2bb55c6
children d4311f83377a
line wrap: on
line source

#include <stdlib.h>
#include "MailManager.h"

void
MailManager::calc_mask(unsigned int qsize) 
{
    mask = 1;
    while(qsize>mask) {
	mask <<= 1;
    }
    size = mask;
    mask--;
}

MailManager::MailManager(unsigned int qsize) {
    read = write = 0;
    calc_mask(qsize);
    queue = Newq(memaddr,size);

    queue_remain  = new Sem(size-1);			//queue内にあと入る数
    queue_count	= new Sem(0);				//queue内に現在入っている数

}

MailManager::~MailManager()
{
	free(queue);
	delete queue_remain;
	delete queue_count;
}

int 
MailManager::count()
{
    return queue_count->count();
}

void 
MailManager::send(memaddr data)
{
	queue_remain->sem_p();	//資源-1

    queue[write++] = data;
    //maskの範囲を超えた場合、0に戻す
    write &= mask;

    queue_count->sem_v();		//資源+1
}

memaddr 
MailManager::recv()
{
	queue_count->sem_p();		//資源-1

    memaddr data;
	data = queue[read++];
	read &= mask;

	queue_remain->sem_v();	//資源+1

    return data;
}

/* end */