view TaskManager/Cell/SpeThreads.cc @ 76:5a1a5f4c28fd

*** empty log message ***
author gongo
date Mon, 18 Feb 2008 11:40:11 +0900
parents 1034077dd217
children 028ffc9c0375
line wrap: on
line source

#include "types.h"
#include "SpeThreads.h"

SpeThreads::SpeThreads(int num) : spe_num(num) {}

SpeThreads::~SpeThreads(void)
{
    unsigned int mail = MY_SPE_COMMAND_EXIT;
    int ret;

    for (int i = 0; i < spe_num; i++) {
	send_mail(i, &mail);
    }

    printf("******  SpeThreads 1 ********\n"); 

    for (int i = 0; i < spe_num; i++) {
	pthread_join(threads[i], NULL);
	ret = spe_context_destroy(spe_ctx[i]);
	if (ret) {
	    perror("[~SpeThreads] spe_context_destroy");
	}
    }

    printf("******  SpeThreads 2 ********\n"); 

    spe_image_close(spe_handle);

    delete [] spe_ctx;
    delete [] threads;
    delete [] args;


    printf("******  SpeThreads 3 ********\n"); 
}

void*
SpeThreads::spe_thread_run(void *arg)
{
    unsigned int entry = SPE_DEFAULT_ENTRY;
    spe_context_ptr_t ctx = (spe_context_ptr_t)arg;
    
    spe_context_run(ctx, &entry, 0, NULL, NULL, NULL);

    pthread_exit(NULL);
}

void*
SpeThreads::frontend_thread_run(void *arg)
{
    pthread_t thread;
    thread_arg_t *arg_t = (thread_arg_t *)arg;

    pthread_create(&thread, NULL, &spe_thread_run, (void*)arg_t->ctx);

    // mail read の blocking ができれば
    // ここで呼んだ方が早い。

    pthread_exit(NULL);
}

void
SpeThreads::init(void)
{
    int i;

    spe_handle = spe_image_open(SPE_ELF);

    spe_ctx = new spe_context_ptr_t[spe_num];
    threads = new pthread_t[spe_num];
    args    = new thread_arg_t[spe_num];

    for (i = 0; i < spe_num; i++) {
	args[i].speid = i;
	spe_ctx[i] = spe_context_create(0, NULL);
	spe_program_load(spe_ctx[i], spe_handle);
	args[i].ctx = spe_ctx[i];
    }

    for (i = 0; i < spe_num; i++) {
	pthread_create(&threads[i], NULL,
		       &frontend_thread_run, (void*)&args[i]);
    }
}

int
SpeThreads::get_mail(int speid)
{
    unsigned int ret;

    if (spe_out_mbox_read(spe_ctx[speid], &ret, 1) > 0) {
	return (int)ret;
    } else {
	return -1;
    }
}

void
SpeThreads::send_mail(int speid, unsigned int *data)
{
    spe_in_mbox_write(spe_ctx[speid], data, 1, SPE_MBOX_ANY_NONBLOCKING);
}