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


#include"kernel.h" 
 
static void  semaphore_go_up(int semaphore_id,struct kernel_time *new_time) 
{ 
	int s_id,h_id; 
	int s_parent,h_parent; 
	struct kernel_time *current,*parent; 
 
	s_id=semaphore_id;h_id=os->semaphore[s_id].heap; 
	current=&(os->semaphore[s_id].first); 
	COPY_TIME((*new_time),(*current)); 
	for(;;){ 
		if(h_id<=0) 
			break; 
		h_parent=((h_id-1)/2); 
		s_parent=os->semaphore_heap[h_parent].semaphore_id; 
		parent=&(os->semaphore[s_parent].first); 
		if(TIME_GREAT_AND_EQUAL((*current),(*parent))) 
			break; 
		os->semaphore_heap[h_parent].semaphore_id=s_id; 
		os->semaphore_heap[h_id].semaphore_id=s_parent; 
		os->semaphore[s_id].heap=h_parent; 
		os->semaphore[s_parent].heap=h_id; 
		h_id=h_parent; 
	} 
	return; 
} 
 
static void  semaphore_go_down(int semaphore_id,struct kernel_time *new_time) 
{ 
	int h_id,h_left,h_right,h_child; 
	int s_id,s_left,s_right,s_child; 
	struct kernel_time *current,*left,*right,*child; 
 
	s_id=semaphore_id; 
	h_id=os->semaphore[s_id].heap; 
	current=&(os->semaphore[s_id].first); 
	COPY_TIME((*new_time),(*current)); 
 
	for(;;){ 
		h_left =(h_id+h_id+1);h_right=(h_id+h_id+2); 
		if(h_left>=SEMAPHORE_NUMBER) 
			break; 
		if(h_right>=SEMAPHORE_NUMBER){ 
			h_child=h_left; 
			s_child=os->semaphore_heap[h_left].semaphore_id; 
			child=&(os->semaphore[s_child].first); 
		}else{ 
			s_left=os->semaphore_heap[h_left].semaphore_id; 
			s_right=os->semaphore_heap[h_right].semaphore_id; 
			left=&(os->semaphore[s_left].first); 
			right=&(os->semaphore[s_right].first); 
			if(TIME_GREAT_AND_EQUAL((*left),(*right))){ 
				s_child=s_right;h_child=h_right; 
				child=right; 
			}else{ 
				s_child=s_left;h_child=h_left; 
				child=left; 
			} 
		} 
		if(TIME_GREAT_AND_EQUAL((*child),(*current))) 
			break; 
		os->semaphore_heap[h_child].semaphore_id=s_id; 
		os->semaphore_heap[h_id].semaphore_id=s_child; 
		os->semaphore[s_id].heap=h_child; 
		os->semaphore[s_child].heap=h_id; 
 
		h_id=h_child; 
	}; 
	return; 
} 
 
void set_semaphore_time(int semaphore_id,struct kernel_time *new_time) 
{ 
	struct kernel_time *current; 
 
	if((semaphore_id<=0)||(semaphore_id>=SEMAPHORE_NUMBER)) 
		return; 
	current=&(os->semaphore[semaphore_id].first); 
	if(TIME_LESS((*new_time),(*current))) 
		semaphore_go_up(semaphore_id,new_time); 
	else if(TIME_GREAT((*new_time),(*current))) 
		semaphore_go_down(semaphore_id,new_time); 
	return; 
}