<commit_before>#include <types.h>
#include <task.h>
#include <kernel.h>
#include <bwio.h>
#include <usertrap.h>
#include <syscall.h>

void tlistInitial(TaskList *tlist, Task **heads, Task **tails) {
	tlist->curtask = NULL;
	tlist->head = NULL;
	tlist->priority_heads = heads;
	tlist->priority_tails = tails;

	int i;

	// TODO: Add magic number to the end of each stack, so can detect stack overflow

	for (i = 0; i < TASK_MAX; i++) {
		heads[i] = NULL;
		tails[i] = NULL;
	}

}

void tarrayInitial(Task *task_array, char *stacks) {
	int i;
	for (i = 0; i < TASK_MAX; i++) {
		task_array[i].tid = i - TASK_MAX;
		task_array[i].init_sp = &(stacks[(i+1) * TASK_STACK_SIZE - 4]);
	}
}

void flistInitial(FreeList *flist, Task *task_array) {
	int i;
	for (i = 0; i < TASK_MAX - 1; i++) {
		task_array[i].next = &(task_array[i+1]);
		task_array[i].state = Empty;
	}
	task_array[TASK_MAX - 1].next = NULL;
	task_array[TASK_MAX - 1].state = Empty;

	flist->head = &task_array[0];
	flist->tail = &task_array[TASK_MAX - 1];
}

int insertTask(TaskList *tlist, Task *new_task) {
	//assert(tlist->list_counter < tlist->list_size, "Exceed tlist size!");
	//assert(priority >= 0 && priority <= TASK_PRIORITY_MAX, "Invalid priority value!");
	int i;
	int priority = new_task->priority;

	// Find and fill in new task
	// If it is the first task
	if (tlist->head == NULL) {
		tlist->head = new_task;
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;
	}
	// If it is the new highest priority
	else if (tlist->head->priority > priority) {
		tlist->head = new_task;
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;

		// Link tail
		for (i = priority + 1; i < TASK_PRIORITY_MAX; i++) {
			if (tlist->priority_heads[i] != NULL) {
				new_task->next = tlist->priority_heads[i];
				break;
			}
		}
	}
	// If it is not new highest, but new priority
	else if (tlist->priority_tails[priority] == NULL) {
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;
		// Link higher priority task next to new_task
		for (i = priority - 1; i >= 0; i--) {
			if (tlist->priority_tails[i] != NULL) {
				new_task->next = tlist->priority_tails[i]->next;
				tlist->priority_tails[i]->next = new_task;
				break;
			}
		}
		// assert(i >= 0, "TaskList: Failed to find higher priority task");
	}
	// If not new priority
	else {
		new_task->next = tlist->priority_tails[priority]->next;
		tlist->priority_tails[priority]->next = new_task;
		tlist->priority_tails[priority] = new_task;
	}

	DEBUG(DB_TASK, "Task(tid: %d) is created, stack at %x\n", new_task->tid, new_task->init_sp);
	return 1;
}

Task* removeCurrentTask(TaskList *tlist, FreeList *flist) {
	// assert(tlist->head != NULL, "TaskList: list is empty");
	int top_priority = tlist->head->priority;
	Task *ret = NULL;

	// Adjust top_priority head and tails
	if (tlist->priority_heads[top_priority] == tlist->priority_tails[top_priority]) {
		tlist->priority_heads[top_priority] = NULL;
		tlist->priority_tails[top_priority] = NULL;
	} else {
		tlist->priority_heads[top_priority] = tlist->priority_heads[top_priority]->next;
	}
	
	ret = tlist->head;
	
	if (tlist->head == tlist->curtask) {
		tlist->head = tlist->head->next;
	}

	if (flist->head == NULL) {
		flist->head = ret;
		flist->tail = ret;
		ret->next = NULL;
		ret->state = Empty;
	} else {
		flist->tail->next = ret;
		flist->tail = ret;
		flist->tail->next = NULL;
		flist->tail->state = Empty;
	}

	return ret;
}

Task *createTask(FreeList *flist, int priority, void * context()) {
	//assert(flist->head != NULL, "Task array is already full!");
	Task *ret;
	ret = flist->head;
	//assert(ret->state == Empty, "Invalid task space to use!");
	ret->tid = ret->tid + TASK_MAX;
	ret->generation += 1;
	ret->state = Ready;
	ret->priority = priority;
	ret->current_sp = ret->init_sp;

	flist->head = flist->head->next;

	// Link to next ready task and parent tid should be set when pushing to task list
	ret->next = NULL;
	ret->parent_tid = -1;

	ret->resume_point = context;
	initTrap(ret->init_sp, DATA_REGION_BASE + Exit);<commit_msg>Use initTrap returned new sp as current_sp<commit_after>#include <types.h>
#include <task.h>
#include <kernel.h>
#include <bwio.h>
#include <usertrap.h>
#include <syscall.h>

void tlistInitial(TaskList *tlist, Task **heads, Task **tails) {
	tlist->curtask = NULL;
	tlist->head = NULL;
	tlist->priority_heads = heads;
	tlist->priority_tails = tails;

	int i;

	// TODO: Add magic number to the end of each stack, so can detect stack overflow

	for (i = 0; i < TASK_MAX; i++) {
		heads[i] = NULL;
		tails[i] = NULL;
	}

}

void tarrayInitial(Task *task_array, char *stacks) {
	int i;
	for (i = 0; i < TASK_MAX; i++) {
		task_array[i].tid = i - TASK_MAX;
		task_array[i].init_sp = &(stacks[(i+1) * TASK_STACK_SIZE - 4]);
	}
}

void flistInitial(FreeList *flist, Task *task_array) {
	int i;
	for (i = 0; i < TASK_MAX - 1; i++) {
		task_array[i].next = &(task_array[i+1]);
		task_array[i].state = Empty;
	}
	task_array[TASK_MAX - 1].next = NULL;
	task_array[TASK_MAX - 1].state = Empty;

	flist->head = &task_array[0];
	flist->tail = &task_array[TASK_MAX - 1];
}

int insertTask(TaskList *tlist, Task *new_task) {
	//assert(tlist->list_counter < tlist->list_size, "Exceed tlist size!");
	//assert(priority >= 0 && priority <= TASK_PRIORITY_MAX, "Invalid priority value!");
	int i;
	int priority = new_task->priority;

	// Find and fill in new task
	// If it is the first task
	if (tlist->head == NULL) {
		tlist->head = new_task;
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;
	}
	// If it is the new highest priority
	else if (tlist->head->priority > priority) {
		tlist->head = new_task;
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;

		// Link tail
		for (i = priority + 1; i < TASK_PRIORITY_MAX; i++) {
			if (tlist->priority_heads[i] != NULL) {
				new_task->next = tlist->priority_heads[i];
				break;
			}
		}
	}
	// If it is not new highest, but new priority
	else if (tlist->priority_tails[priority] == NULL) {
		tlist->priority_heads[priority] = new_task;
		tlist->priority_tails[priority] = new_task;
		// Link higher priority task next to new_task
		for (i = priority - 1; i >= 0; i--) {
			if (tlist->priority_tails[i] != NULL) {
				new_task->next = tlist->priority_tails[i]->next;
				tlist->priority_tails[i]->next = new_task;
				break;
			}
		}
		// assert(i >= 0, "TaskList: Failed to find higher priority task");
	}
	// If not new priority
	else {
		new_task->next = tlist->priority_tails[priority]->next;
		tlist->priority_tails[priority]->next = new_task;
		tlist->priority_tails[priority] = new_task;
	}

	DEBUG(DB_TASK, "Task(tid: %d) is created, stack at %x\n", new_task->tid, new_task->init_sp);
	return 1;
}

Task* removeCurrentTask(TaskList *tlist, FreeList *flist) {
	// assert(tlist->head != NULL, "TaskList: list is empty");
	int top_priority = tlist->head->priority;
	Task *ret = NULL;

	// Adjust top_priority head and tails
	if (tlist->priority_heads[top_priority] == tlist->priority_tails[top_priority]) {
		tlist->priority_heads[top_priority] = NULL;
		tlist->priority_tails[top_priority] = NULL;
	} else {
		tlist->priority_heads[top_priority] = tlist->priority_heads[top_priority]->next;
	}
	
	ret = tlist->head;
	
	if (tlist->head == tlist->curtask) {
		tlist->head = tlist->head->next;
	}

	if (flist->head == NULL) {
		flist->head = ret;
		flist->tail = ret;
		ret->next = NULL;
		ret->state = Empty;
	} else {
		flist->tail->next = ret;
		flist->tail = ret;
		flist->tail->next = NULL;
		flist->tail->state = Empty;
	}

	return ret;
}

Task *createTask(FreeList *flist, int priority, void * context()) {
	//assert(flist->head != NULL, "Task array is already full!");
	Task *ret;
	ret = flist->head;
	//assert(ret->state == Empty, "Invalid task space to use!");
	ret->tid = ret->tid + TASK_MAX;
	ret->generation += 1;
	ret->state = Ready;
	ret->priority = priority;

	flist->head = flist->head->next;

	// Link to next ready task and parent tid should be set when pushing to task list
	ret->next = NULL;
	ret->parent_tid = -1;

	ret->resume_point = context;
	ret->current_sp = initTrap(ret->init_sp, DATA_REGION_BASE + Exit);
{'fraction_non_alphanumeric': 0.16224366706875754, 'fraction_numerical': 0.004583835946924005, 'mean_word_length': 3.8599062133645954, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 188, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1721423', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3112, 'n_words': 750}