view Scheduler/scheduler.c @ 33:3946f8d26710 draft default tip

add benchmarck/binary-trees
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 09 Apr 2013 16:41:30 +0900
parents e3b6e2eef223
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include "types.h"

#define MAX_USER_TASK 100
#define MAX_SYSTEM_TASK 2
#define MAX_TASK_OBJECT MAX_USER_TASK + MAX_SYSTEM_TASK
#define DEFAULT_ALIGNMENT 16
#define MAX_GLOBAL_AREA 32
#define MAX_MAINMEM_AREA 32


typedef int (*task_object_run)();
typedef struct task_object task_object;
typedef struct main_scheduler main_scheduler;
typedef struct task_manager_impl task_manager_impl;
typedef struct task_manager task_manager;

struct task_object 
{
	memaddr location;
	memaddr end;
	uint32 entry_offset;

	task_object_run run;
	void (*load)();
	void (*wait)();
};

task_object task_list[MAX_TASK_OBJECT];

struct main_scheduler
{
	int id;
	task_manager_impl *m_impl;
	main_scheduler *scheduler;
    void* global_list[MAX_GLOBAL_AREA];
	memaddr main_mem_list[MAX_MAINMEM_AREA];

	void (*init)();	// init_scheduler function.
	void (*set_manager)(); // set_manager function.
	void (*init_impl)(); // Reference to Scheduler::init_impl method.
	void (*main_mem_alloc)(); 
	void (*mail_write_from_host)();
	void (*mail_read_from_host)();	
	void (*has_mail_from_host)();
};

/* task_manager_impl is FifoTaskManagerImpl now. */
struct task_manager_impl 
{
	main_scheduler *scheduler;
	int machine_num;	
	void (*init)();	
	void* (*create_task)();
	void* (*allocate)();
};

struct task_manager 
{
	int machine_num;
	void (*init)();
	task_manager_impl *m_impl;
	void (*allocate)();
};

int null_run() 
{
	printf("null_run\n");
	return 0;
}

void null_load() 
{
	printf("null_load\n");
	return;
}
void init_scheduler(task_manager_impl *m) 
{
	main_scheduler *scheduler = m->scheduler;
	
	int i = 0;
	for (i=0; i<MAX_TASK_OBJECT; i++) 
	{
		task_list[i].run = null_run;
		task_list[i].load = null_load;
		task_list[i].wait = null_load;
	}

	scheduler->set_manager(m,scheduler);
	
	for (i=0; i<MAX_GLOBAL_AREA; i++) {
		scheduler->global_list[i] = NULL;
	}

	for (i=0; i<MAX_MAINMEM_AREA; i++) {
		scheduler->main_mem_list[i] = (memaddr)NULL;
	}


}

void set_manager(task_manager_impl *impl, main_scheduler *scheduler) 
{
	scheduler->m_impl = impl;
}

main_scheduler *create_main_scheduler() 
{
	main_scheduler *scheduler = malloc(sizeof(main_scheduler));
	scheduler->init = init_scheduler;
	scheduler->set_manager = set_manager;
	return scheduler;
}

/* Reference to FifoTaskManagerImpl::init */
void init_impl(task_manager_impl *m_impl) 
{
	m_impl->scheduler = create_main_scheduler();
	main_scheduler *scheduler = m_impl->scheduler;
	scheduler->init(m_impl);
	scheduler->id = 0;
//  m_impl->set_scheduler(main_scheduler);
}

void* create_task(task_manager *manager) 
{

}

/* Reference to TaskManagerImpl::allocate */
void* memory_allocate(int size, int alignment)
{
	void *buff = 0;
	if (size == 0) return 0;
	posix_memalign(&buff, DEFAULT_ALIGNMENT, size);
	if (buff == 0)
		printf("Can't allocate memory\n");
	return buff;
}

task_manager_impl* create_impl(int num) 
{
	task_manager_impl *impl = malloc(sizeof(task_manager_impl));
#ifdef __CERIUM_PARALLEL__
	if (num == 0) num = 1;
#else
	num = 0;
#endif
	impl->machine_num = num;
	impl->init = init_impl;
	impl->allocate = memory_allocate;
	impl->create_task = create_task;
	return impl;
}

void init_task_manager(task_manager *manager) 
{
	manager->m_impl = create_impl(manager->machine_num);
	task_manager_impl *m_impl = manager->m_impl;
	m_impl->init(m_impl);
//	m_impl->systask_init();
}

void allocate_task_manager(task_manager *manager, int size) 
{
	manager->m_impl->allocate(size);
}

task_manager* create_manager(int num) 
{
	task_manager* manager = malloc(sizeof(task_manager));
	manager->machine_num = num;
	manager->init = init_task_manager;
	manager->allocate = allocate_task_manager;
	return manager;
}

void free_manager(task_manager *manager) 
{
	free(manager);
}

int main(int argc, char* argv[]) 
{
	int machine_num = 1;
	task_manager *manager = create_manager(machine_num);
	manager->init(manager);


//	free_manager(manager);
	return 0;
}