rem: thread_set_priority(t->id, THREAD_HIGH_PRIORITY);
add: thread_set_priority(t->id, B_FIRST_REAL_TIME_PRIORITY);
context: void thread_exit(int retcode){	int state;	struct thread *t = thread_get_current_thread();	struct team *p = t->team;	bool delete_team = false;	unsigned int death_stack;	sem_id cached_death_sem;	dprintf("thread 0x%x exiting w/return code 0x%x\n", t->id, retcode);	// boost our priority to get this over with	thread_set_priority(t->id, THREAD_HIGH_PRIORITY);	// delete the user stack region first	if(p->_aspace_id >= 0 && t->user_stack_region_id >= 0) {		region_id rid = t->user_stack_region_id;		t->user_stack_region_id = -1;		vm_delete_region(p->_aspace_id, rid);	}	if(p != team_get_kernel_team()) {		// remove this thread from the current team and add it to the kernel		// put the thread into the kernel team until it dies		state = disable_interrupts();		GRAB_TEAM_LOCK();		remove_thread_from_team(p, t);		insert_thread_into_team(team_get_kernel_team(), t);		if(p->main_thread == t) {			// this was main thread in this team			delete_team = true;			team_remove_team_from_hash(p);			p->state = TEAM_STATE_DEATH;		}		RELEASE_TEAM_LOCK();		// swap address spaces, to make sure we're running on the kernel's pgdir		vm_aspace_swap(team_get_kernel_team()->kaspace);		restore_interrupts(state);//		dprintf("thread_exit: thread 0x%x now a kernel thread!\n", t->id);	}		cached_death_sem = p->death_sem;	// delete the team	if(delete_team) {		if(p->num_threads > 0) {			// there are other threads still in this team,			// cycle through and signal kill on each of the threads			// XXX this can be optimized. There's got to be a better solution.			struct thread *temp_thread;			char death_sem_name[SYS_MAX_OS_NAME_LEN];			sprintf(death_sem_name, "team %d death sem", p->id);			p->death_sem = create_sem(0, death_sem_name);			if (p->death_sem < 0)				panic("thread_exit: cannot init death sem for team %d\n", p->id);						state = disable_interrupts();			GRAB_TEAM_LOCK();			// we can safely walk the list because of the lock. no new threads can be created			// because of the TEAM_STATE_DEATH flag on the team			temp_thread = p->thread_list;			while(temp_thread) {				struct thread *next = temp_thread->team_next;				thread_kill_thread_nowait(temp_thread->id);				temp_thread = next;			}			RELEASE_TEAM_LOCK();			restore_interrupts(state);#if 0			// Now wait for all of the threads to die			// XXX block on a semaphore			while((volatile int)p->num_threads > 0) {				snooze(10000); // 10 ms			}#endif			acquire_sem_etc(p->death_sem, p->num_threads, 0, 0);			delete_sem(p->death_sem);		}		cached_death_sem = -1;		vm_put_aspace(p->aspace);		vm_delete_aspace(p->_aspace_id);		delete_owned_ports(p->id);		sem_delete_owned_sems(p->id);		vfs_free_io_context(p->ioctx);		kfree(p);	}	// delete the sem that others will use to wait on us and get the retcode	{		sem_id s = t->return_code_sem;		t->return_code_sem = -1;		delete_sem_etc(s, retcode);	}	death_stack = get_death_stack();	{		struct thread_exit_args args;		args.t = t;		args.old_kernel_stack = t->kernel_stack_region_id;		args.death_stack = death_stack;		args.death_sem = cached_death_sem;		// disable the interrupts. Must remain disabled until the kernel stack pointer can be officially switched		args.int_state = disable_interrupts();		// set the new kernel stack officially to the death stack, wont be really switched until		// the next function is called. This bookkeeping must be done now before a context switch		// happens, or the processor will interrupt to the old stack		t->kernel_stack_region_id = death_stacks[death_stack].rid;		t->kernel_stack_base = death_stacks[death_stack].address;		// we will continue in thread_exit2(), on the new stack		arch_thread_switch_kstack_and_call(t, t->kernel_stack_base + KSTACK_SIZE, thread_exit2, &args);	}	panic("never can get here\n");}