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


#include"kernel.h" 
 
#define READY_HEAP_EXCHANGE(s_h,d_h)			\ 
{							\ 
	int s_t,d_t;					\ 
	s_t=os->thread_heap.ready_heap[s_h].thread;	\ 
	d_t=os->thread_heap.ready_heap[d_h].thread;	\ 
	os->thread_heap.ready_heap[s_h].thread=d_t;	\ 
	os->thread_heap.ready_heap[d_h].thread=s_t;	\ 
	os->thread[s_t].heap=(d_h);			\ 
	os->thread[d_t].heap=s_h;			\ 
} 
 
void ready_heap_up_deal(int thread_id) 
{ 
	int t,h,p,t_p,h_p,p_p; 
 
	t=thread_id; 
	h=os->thread[t].heap; 
	p=os->thread[t].priority; 
 
	for(;;){ 
		if(h<=0) 
			return; 
		h_p=(h-1)/2; 
		t_p=os->thread_heap.ready_heap[h_p].thread; 
		p_p=os->thread[t_p].priority; 
 
		if(p>=p_p) 
			return; 
		READY_HEAP_EXCHANGE(h,h_p); 
		h=h_p; 
	} 
} 
 
void ready_heap_down_deal(int thread_id) 
{ 
	int h1,h2,t1,t2,p1,p2; 
		 
	t1=thread_id; 
	h1=os->thread[t1].heap; 
	p1=os->thread[t1].priority; 
 
	for(;;){ 
		int h_l,h_r; 
		h_l=2*h1+1;h_r=2*h1+2; 
		if(h_l>=os->thread_heap.ready_thread_number) 
			if(h_r>=os->thread_heap.ready_thread_number) 
				break; 
			else  
				h2=h_r; 
		else 
			if(h_r>=os->thread_heap.ready_thread_number) 
				h2=h_l; 
			else{ 
				int t_l,t_r,p_l,p_r; 
				t_l=os->thread_heap.ready_heap[h_l].thread; 
				p_l=os->thread[t_l].priority; 
				t_r=os->thread_heap.ready_heap[h_r].thread; 
				p_r=os->thread[t_r].priority; 
				if(p_lthread_heap.ready_heap[h2].thread; 
		p2=os->thread[t2].priority; 
		if(p1<=p2) 
			break; 
		READY_HEAP_EXCHANGE(h1,h2); 
		h1=h2; 
	} 
	return; 
} 
 
void remove_from_ready_heap(thread_id) 
{ 
	int h1,h2,t1,t2,p1,p2; 
 
	h1=os->thread[thread_id].heap; 
	h2=os->thread_heap.ready_thread_number-1; 
	READY_HEAP_EXCHANGE(h1,h2); 
 
	t1=os->thread_heap.ready_heap[h1].thread; 
	p1=os->thread[t1].priority; 
	t2=os->thread_heap.ready_heap[h2].thread; 
	p2=os->thread[t2].priority; 
 
	os->thread_heap.ready_thread_number--; 
	 
 
	if(p1==p2) 
		return; 
	if(p1thread_heap.ready_thread_number; 
 
	os->thread[t].heap=h; 
	os->thread_heap.ready_heap[h].thread=t; 
 
	os->thread_heap.ready_thread_number++; 
	ready_heap_up_deal(t); 
	return; 
}