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


#include"kernel.h" 
 
#define WRONG_THREAD_ID 	-1 
#define WRONG_CAPABILITY 	-2 
#define WRONG_STACK_TOP 	-3 
#define WRONG_INTERRUPT_NUMBER 	-4 
#define WRONG_FUNCTION_NUMBER	-5 
#define WRONG_PROCESS_NUMBER	-6 
#define WRONG_STACK_NUMBER	-7 
#define WRONG_THREAD_STATE	-8 
#define SET_RETURN_NUMBER_FAIL	-9 
 
int set_thread_attribute(int semaphore_id, 
	struct exception_item *exception, 
	int *priority,int *mandatory_exit_number, 
	struct capability *current_process_capability) 
{ 
	int i,thread_id,return_value; 
	struct thread *t; 
	struct return_stack *rt; 
	if((semaphore_id<=0)||(semaphore_id>=SEMAPHORE_NUMBER)) 
		return WRONG_THREAD_ID; 
	thread_id=os->semaphore[semaphore_id].thread_id; 
	if((thread_id<0)||(thread_id>=THREAD_NUMBER)) 
		return WRONG_THREAD_ID; 
	t=&(os->thread[thread_id]); 
	rt=&(t->return_stack[t->return_stack_top-1]); 
	if(!KERNEL_COMPARE_CAPABILITY( 
		(os->process[rt->current_process].capability), 
		(*current_process_capability))) 
			return WRONG_CAPABILITY; 
	if(!KERNEL_COMPARE_CAPABILITY((os->system_capability), 
		(*current_process_capability))) 
	if(*priorityprocess[rt->current_process].priority) 
		*priority=os->process[rt->current_process].priority; 
	if(!exception->flag) 
		COPY_EXCEPTION_ITEM((*exception),(rt->exception)); 
	*priority=t->priority; 
	modify_thread_priority(thread_id,*priority); 
 
	if((*mandatory_exit_number)<=0){ 
		*mandatory_exit_number=0; 
		return 0; 
	} 
	if(t->state==SLEEP) 
		v_thread(thread_id); 
	for(i=0;i<*mandatory_exit_number;i++){ 
		rt=&(t->return_stack[t->return_stack_top-1]); 
		if(!KERNEL_COMPARE_CAPABILITY( 
			(os->process[rt->current_process].capability), 
			(*current_process_capability))) 
				break; 
		if((return_value=thread_return(thread_id,FALSE))<0) 
			return return_value; 
		if(t->state==SLEEP) 
			break; 
	} 
	*mandatory_exit_number=i; 
	return i; 
} 
 
int get_thread_attribute(int semaphore_id, 
	struct exception_item *exception,int *priority, 
	int *return_stack_top,int *process,int *current_process, 
	struct capability *current_process_capability) 
{ 
	int thread_id; 
	struct thread *t; 
	struct return_stack *rt; 
	struct capability  *cap; 
 
	if((semaphore_id<=0)||(semaphore_id>=SEMAPHORE_NUMBER)) 
		return WRONG_THREAD_ID; 
	thread_id=os->semaphore[semaphore_id].thread_id; 
	if((thread_id<0)||(thread_id>=THREAD_NUMBER)) 
		return WRONG_THREAD_ID; 
	t=&(os->thread[thread_id]); 
	rt=&(t->return_stack[t->return_stack_top-1]); 
	cap=&(os->process[rt->current_process].capability); 
	if(!KERNEL_COMPARE_CAPABILITY( 
			(*cap),(*current_process_capability))) 
		return WRONG_CAPABILITY; 
	*priority=t->priority; 
 
	COPY_EXCEPTION_ITEM((rt->exception),(*exception)); 
	*return_stack_top=t->return_stack_top; 
	*process=t->process; 
	*current_process=rt->current_process; 
	return 0; 
}