www.pudn.com > LFYOS.zip > create.c


 
#include "kernel.h" 
 
#define  WRONG_THREAD_ID		(-2) 
#define  WRONG_PROCESS_NUMBER 		(-3) 
#define  WRONG_CAPABILITY 		(-4) 
#define  CREATE_TOO_MANY_THREAD 	(-5) 
#define  NO_THREAD_TO_CREATE 		(-6) 
#define  WRONG_STACK_TOP		(-7) 
#define  EMPTY_RETURN_STACK		(-8) 
#define  NOT_SEMAPHORE_FOR_CREATE	(-9) 
 
int create_thread( 
	struct user_file_information *file_info, 
	int process_id,int current_process,int priority, 
	struct thread_environment *thread_environment, 
	struct capability *process_capability) 
{ 
	int i,thread_id; 
	struct process *process; 
	struct thread *t; 
	struct return_stack *rt; 
 
	if((process_id<=0)||(process_id>=PROCESS_NUMBER)) 
		return WRONG_PROCESS_NUMBER; 
	process=&(os->process[process_id]); 
	if(!KERNEL_COMPARE_CAPABILITY( 
		process->capability,(*process_capability))) 
			return WRONG_CAPABILITY; 
	if(process->thread_number>=process->max_thread_number) 
		return CREATE_TOO_MANY_THREAD; 
 
	if((thread_id=os->process[0].thread_ring)<0) 
		return NO_THREAD_TO_CREATE; 
	process->thread_number++; 
	os->process[0].thread_number--; 
	t=&(os->thread[thread_id]); 
	t->process=process_id; 
	 
	if(t->pro_front==thread_id) 
		os->process[0].thread_ring=-1; 
	else{ 
		os->process[0].thread_ring=t->pro_back; 
		os->thread[t->pro_front].pro_back=t->pro_back; 
		os->thread[t->pro_back].pro_front=t->pro_front; 
	} 
 
	if(process->thread_ring<0){ 
			t->pro_front=thread_id; 
			t->pro_back=thread_id; 
			process->thread_ring=thread_id; 
		} 
	else{ 
		int front_thread,back_thread; 
		front_thread=process->thread_ring; 
		back_thread=os->thread[front_thread].pro_back; 
		os->thread[front_thread].pro_back=thread_id; 
		os->thread[back_thread].pro_front=thread_id; 
		os->thread[thread_id].pro_front=front_thread; 
		os->thread[thread_id].pro_back=back_thread; 
	} 
 
	t->return_stack_top=1; 
	rt=&(t->return_stack[0]); 
 
	COPY_THREAD_ENVIRONMENT((*thread_environment), 
		(rt->environment)); 
	COPY_RUN_POINT((thread_environment->point), 
		(rt->environment.point)); 
	RESET_THREAD_PARAMETER(rt->environment); 
	rt->current_process=current_process; 
	RESET_EXCEPTION_ITEM(rt->exception); 
	rt->physical_block.memory_id=-1; 
	rt->physical_block.stack_block_id=-1; 
	rt->physical_block.block_id=-1; 
	 
	t->priority=priority; 
	t->set_v_operation_result_flag=FALSE; 
	os->semaphore[0].value=(-THREAD_NUMBER); 
	v_thread(thread_id); 
	t->semaphore=allocate_semaphore(t->process,0, 
		&(os->process[t->process].capability), 
		&(os->process[rt->current_process].capability)); 
	if(t->semaphore<=0){ 
		thread_return(thread_id,FALSE); 
		return NOT_SEMAPHORE_FOR_CREATE; 
	} 
	os->semaphore[t->semaphore].thread_id=thread_id; 
 
	thread_environment->system_call=t->semaphore; 
	thread_environment->system_call_arg1=0; 
 
	rt->environment.system_call=t->semaphore; 
	rt->environment.system_call_arg1=1; 
	rt->process_p_flag=TRUE; 
 
	for(i=0;ifile[i])); 
	return t->semaphore; 
}