view TaskManager/Cell/SpeThreads.cc @ 73:811ffebd8deb

*** empty log message ***
author gongo
date Mon, 18 Feb 2008 03:34:35 +0900
parents 1034077dd217
children 5a1a5f4c28fd
line wrap: on
line source

#include "SpeThreads.h"

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

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);

    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);
}