Exemplo n.º 1
0
/*!
 * Close a message queue
 * \param mqdes Queue descriptor address (user level descriptor)
 * \return 0 if successful, -1 otherwise and appropriate error number is set
 */
int sys__mq_close ( void *p )
{
	mqd_t *mqdes;

	kmq_queue_t *kq_queue;
	kobject_t *kobj;
	kmq_msg_t *kmq_msg;
	kthread_t *kthread;

	mqdes = *( (mqd_t **) p );

	ASSERT_ERRNO_AND_EXIT ( mqdes, EBADF );

	kobj = mqdes->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj, EBADF );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj->list ),
				EBADF );

	kq_queue = kobj->kobject;
	kq_queue = list_find_and_remove ( &kmq_queue, &kq_queue->list );

	if ( !kq_queue || kq_queue->id != mqdes->id )
		EXIT2 ( EBADF, EXIT_FAILURE );

	kq_queue->ref_cnt--;

	if ( !kq_queue->ref_cnt )
	{
		/* remove messages */
		while( (kmq_msg = list_remove(&kq_queue->msg_list,FIRST,NULL)) )
			kfree (kmq_msg);

		/* remove blocked threads */
		while ( (kthread = kthreadq_remove (&kq_queue->send_q, NULL)) )
		{
			kthread_move_to_ready ( kthread, LAST );
			kthread_set_errno ( kthread, EBADF );
			kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
		}
		while ( (kthread = kthreadq_remove (&kq_queue->recv_q, NULL)) )
		{
			kthread_move_to_ready ( kthread, LAST );
			kthread_set_errno ( kthread, EBADF );
			kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
		}

		list_remove ( &kmq_queue, 0, &kq_queue->list );
		k_free_id ( kq_queue->id );
		kfree ( kq_queue->name );
		kfree ( kq_queue );
	}

	/* remove kernel object descriptor */
	kfree_kobject ( kobj );

	EXIT2 ( EXIT_SUCCESS, EXIT_SUCCESS );
}
Exemplo n.º 2
0
/*! Cancel sleep
 *  - handle return values and errno;
 *  - thread must be handled elsewhere - with source of interrupt (signal?)
 */
static void kclock_interrupt_sleep ( kthread_t *kthread, void *param )
{
	ktimer_t *ktimer;
	timespec_t *remain;
	itimerspec_t irem;

	ASSERT ( kthread && param );
	ASSERT ( kthread_check_kthread ( kthread ) ); /* is this valid thread */
	ASSERT ( kthread_is_suspended ( kthread, NULL, NULL ) );

	ktimer = param;
	remain = ktimer->param;

	if ( remain )
	{
		/* save remaining time */
		timespec_t now;
		kclock_gettime ( CLOCK_REALTIME, &now );
		ktimer_gettime ( ktimer, &irem );
		*remain = irem.it_value;
		time_sub ( remain, &now );
	}

	ktimer_delete (ktimer);

	kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
	kthread_set_errno ( kthread, EINTR );
}
Exemplo n.º 3
0
/*!
 * Decrement (lock) semaphore value by 1 (if not 0 when thread is blocked)
 * \param sem Semaphore descriptor (user level descriptor)
 * \return 0 if successful, -1 otherwise and appropriate error number is set
 */
int sys__sem_wait ( sem_t *sem )
{
	ksem_t *ksem;
	kobject_t *kobj;
	kthread_t *kthread;

	SYS_ENTRY();

	ASSERT_ERRNO_AND_EXIT ( sem, EINVAL );

	kthread = kthread_get_active ();

	kobj = sem->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj->list ),
				EINVAL );
	ksem = kobj->kobject;
	ASSERT_ERRNO_AND_EXIT ( ksem && ksem->id == sem->id, EINVAL );

	kthread_set_errno ( kthread, EXIT_SUCCESS );
	kthread_set_syscall_retval ( kthread, EXIT_SUCCESS );

	if ( ksem->sem_value > 0 )
	{
		ksem->sem_value--;
		ksem->last_lock = kthread;
	}
	else {
		kthread_enqueue ( kthread, &ksem->queue );
		kthreads_schedule ();
	}

	SYS_EXIT ( kthread_get_errno(NULL), kthread_get_syscall_retval(NULL) );
}
Exemplo n.º 4
0
/*!
 * Wait on conditional variable
 * \param cond conditional variable descriptor (user level descriptor)
 * \param mutex Mutex descriptor (user level descriptor)
 * \return 0 if successful, -1 otherwise and appropriate error number is set
 */
int sys__pthread_cond_wait ( pthread_cond_t *cond, pthread_mutex_t *mutex )
{
	kpthread_cond_t *kcond;
	kpthread_mutex_t *kmutex;
	kobject_t *kobj_cond, *kobj_mutex;

	SYS_ENTRY();

	ASSERT_ERRNO_AND_EXIT ( cond && mutex, EINVAL );

	kobj_cond = cond->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj_cond, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj_cond->list ),
				EINVAL );
	kcond = kobj_cond->kobject;
	ASSERT_ERRNO_AND_EXIT ( kcond && kcond->id == cond->id, EINVAL );

	kobj_mutex = mutex->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj_mutex, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj_mutex->list),
				EINVAL );
	kmutex = kobj_mutex->kobject;
	ASSERT_ERRNO_AND_EXIT ( kmutex && kmutex->id == mutex->id, EINVAL );

	ASSERT_ERRNO_AND_EXIT ( kmutex->owner == kthread_get_active(), EPERM );

	kthread_set_errno ( NULL, EXIT_SUCCESS );
	kthread_set_syscall_retval ( NULL, EXIT_SUCCESS );

	/* move thread in conditional variable queue */
	kthread_enqueue ( NULL, &kcond->queue );

	/* save reference to mutex object */
	kthread_set_private_param ( NULL, kobj_mutex );

	/* release mutex */
	kmutex->owner = kthreadq_get ( &kmutex->queue );
	if ( kmutex->owner )
		kthreadq_release ( &kmutex->queue );

	kthreads_schedule ();

	SYS_EXIT ( kthread_get_errno(NULL), kthread_get_syscall_retval(NULL) );
}
Exemplo n.º 5
0
/*!
 * Increment (lock) semaphore value by 1 (or unblock one thread that is blocked)
 * \param sem Semaphore descriptor (user level descriptor)
 * \return 0 if successful, -1 otherwise and appropriate error number is set
 */
int sys__sem_post ( sem_t *sem )
{
	ksem_t *ksem;
	kobject_t *kobj;
	kthread_t *kthread, *released;

	SYS_ENTRY();

	ASSERT_ERRNO_AND_EXIT ( sem, EINVAL );

	kthread = kthread_get_active ();

	kobj = sem->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj->list ),
				EINVAL );
	ksem = kobj->kobject;
	ASSERT_ERRNO_AND_EXIT ( ksem && ksem->id == sem->id, EINVAL );

	kthread_set_errno ( kthread, EXIT_SUCCESS );
	kthread_set_syscall_retval ( kthread, EXIT_SUCCESS );

	released = kthreadq_get ( &ksem->queue ); /* first to release */

	if ( !released || ksem->sem_value < 0 )
	{
		/* if initial semaphore value (set by sem_init) was negative,
		 * semaphore will not release threads until its value
		 * reaches zero (small extension of POSIX semaphore) */
		ksem->sem_value++;
	}
	else {
		kthreadq_release ( &ksem->queue );
		kthreads_schedule ();
	}

	SYS_EXIT ( kthread_get_errno(NULL), kthread_get_syscall_retval(NULL) );
}
Exemplo n.º 6
0
/*!
 * Wait for thread termination
 * \param thread Thread descriptor (user level descriptor)
 * \param retval Where to store exit status of joined thread
 * \return 0 if thread already gone; -1 if not finished and 'wait' not set;
 *         'thread exit status' otherwise
 */
int sys__pthread_join ( pthread_t *thread, void **retval )
{
	kthread_t *kthread;

	SYS_ENTRY();

	ASSERT_ERRNO_AND_EXIT ( thread, ESRCH );

	kthread = thread->ptr;

	if ( kthread_get_id (kthread) != thread->id )
	{
		/* at 'kthread' is now something else */
		SYS_EXIT ( ESRCH, EXIT_FAILURE );
	}
	else if ( kthread_is_alive (kthread) )
	{
		kthread_set_errno ( NULL, EXIT_SUCCESS );
		kthread_set_syscall_retval ( NULL, EXIT_SUCCESS );

		kthread_set_private_param ( kthread_get_active(), retval );

		kthread_wait_thread ( NULL, kthread );

		kthreads_schedule ();

		SYS_EXIT ( kthread_get_errno(NULL),
			   kthread_get_syscall_retval(NULL) );
	}
	else {
		/* target thread is passive, collect status and free descr. */
		kthread_collect_status ( kthread, retval );

		SYS_EXIT ( EXIT_SUCCESS, EXIT_SUCCESS );
	}
}
Exemplo n.º 7
0
static int kmq_send ( void *p, kthread_t *sender )
{
	mqd_t *mqdes;
	char *msg_ptr;
	size_t msg_len;
	uint msg_prio;

	kmq_queue_t *kq_queue;
	kobject_t *kobj;
	kmq_msg_t *kmq_msg;
	kthread_t *kthread;
	int retval;

	mqdes =		*( (mqd_t **) p );	p += sizeof (mqd_t *);
	msg_ptr = 	*( (char **) p );	p += sizeof (char *);
	msg_len = 	*( (size_t *) p );	p += sizeof (size_t);
	msg_prio =	*( (uint *) p );

	ASSERT_ERRNO_AND_EXIT ( mqdes && msg_ptr, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( msg_prio <= MQ_PRIO_MAX, EINVAL );

	kobj = mqdes->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj, EBADF );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj->list ),
				EBADF );

	kq_queue = kobj->kobject;
	ASSERT_ERRNO_AND_EXIT ( kq_queue->id == mqdes->id, EBADF );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kmq_queue, &kq_queue->list ),
				EBADF );

	if ( kq_queue->attr.mq_curmsgs >= kq_queue->attr.mq_maxmsg )
	{
		if ( (kobj->flags & O_NONBLOCK) )
			return EAGAIN;

		/* block thread */
		kthread_enqueue ( sender, &kq_queue->send_q, 1, NULL, NULL );
		kthreads_schedule ();

		return EAGAIN;
	}

	if ( msg_len > kq_queue->attr.mq_msgsize )
		return EMSGSIZE;

	kmq_msg = kmalloc ( sizeof (kmq_msg_t) + msg_len );
	ASSERT_ERRNO_AND_EXIT ( kmq_msg, ENOMEM );

	/* create message */
	kmq_msg->msg_size = msg_len;
	kmq_msg->msg_prio = msg_prio;
	memcpy ( &kmq_msg->msg_data[0], msg_ptr, msg_len );

	list_sort_add ( &kq_queue->msg_list, kmq_msg, &kmq_msg->list,
			( int (*)(void *, void *) ) cmp_mq_msg );

	kq_queue->attr.mq_curmsgs++;

	/* is there a blocked receiver? */
	if ( (kthread = kthreadq_remove ( &kq_queue->recv_q, NULL )) )
	{
		/* "save" sender thread */
		kthread_move_to_ready ( sender, FIRST );
		kthread_set_errno ( sender, EXIT_SUCCESS );
		kthread_set_syscall_retval ( sender, EXIT_SUCCESS );

		/* unblock receiver */
		kthread_set_active ( kthread ); /* temporary */
		p = arch_syscall_get_params ( kthread_get_context (kthread) );

		retval = kmq_receive ( p, kthread );

		if ( retval >= 0 )
		{
			kthread_set_errno ( kthread, EXIT_SUCCESS );
			kthread_set_syscall_retval ( kthread, retval );
		}
		else {
			kthread_set_errno ( kthread, -retval );
			kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
		}

		kthreads_schedule ();
	}

	return EXIT_SUCCESS;
}
Exemplo n.º 8
0
static int kmq_receive ( void *p, kthread_t *receiver )
{
	mqd_t *mqdes;
	char *msg_ptr;
	size_t msg_len;
	uint *msg_prio;

	kmq_queue_t *kq_queue;
	kobject_t *kobj;
	kmq_msg_t *kmq_msg;
	kthread_t *kthread;
	int retval;

	mqdes =		*( (mqd_t **) p );	p += sizeof (mqd_t *);
	msg_ptr = 	*( (char **) p );	p += sizeof (char *);
	msg_len = 	*( (size_t *) p );	p += sizeof (size_t);
	msg_prio =	*( (uint **) p );

	ASSERT_ERRNO_AND_EXIT ( mqdes && msg_ptr, -EINVAL );

	kobj = mqdes->ptr;
	ASSERT_ERRNO_AND_EXIT ( kobj, -EBADF );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kobjects, &kobj->list ),
				-EBADF );

	kq_queue = kobj->kobject;
	ASSERT_ERRNO_AND_EXIT ( kq_queue->id == mqdes->id, -EBADF );
	ASSERT_ERRNO_AND_EXIT ( list_find ( &kmq_queue, &kq_queue->list ),
				-EBADF );

	if ( kq_queue->attr.mq_curmsgs == 0 )
	{
		if ( (kobj->flags & O_NONBLOCK) )
			return -EAGAIN;

		/* block thread */
		kthread_enqueue ( receiver, &kq_queue->recv_q, 1, NULL, NULL );
		kthreads_schedule ();

		return -EAGAIN;
	}

	if ( msg_len < kq_queue->attr.mq_msgsize )
		return -EMSGSIZE;

	kmq_msg = list_remove ( &kq_queue->msg_list, FIRST, NULL );

	memcpy ( msg_ptr, &kmq_msg->msg_data[0], kmq_msg->msg_size );
	msg_len = kmq_msg->msg_size;
	if ( msg_prio )
		*msg_prio = kmq_msg->msg_prio;

	kfree (kmq_msg);

	kq_queue->attr.mq_curmsgs--;

	/* is there a blocked sender? */
	if ( (kthread = kthreadq_remove ( &kq_queue->send_q, NULL )) )
	{
		/* "save" receiver thread */
		kthread_move_to_ready ( receiver, FIRST );
		kthread_set_errno ( receiver, EXIT_SUCCESS );
		kthread_set_syscall_retval ( receiver, msg_len );

		/* unblock sender */
		kthread_set_active ( kthread ); /* temporary */
		p = arch_syscall_get_params ( kthread_get_context (kthread) );

		retval = kmq_send ( p, kthread );

		if ( retval == EXIT_SUCCESS )
		{
			kthread_set_errno ( kthread, EXIT_SUCCESS );
			kthread_set_syscall_retval ( kthread, retval );
		}
		else {
			kthread_set_errno ( kthread, retval );
			kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
		}

		kthreads_schedule ();
	}

	return msg_len;
}
Exemplo n.º 9
0
/*! Send signal to target thread */
int ksignal_queue ( kthread_t *kthread, siginfo_t *sig )
{
	int enqueue = FALSE;
	int retval = EXIT_SUCCESS;
	int schedule = FALSE;
	ksignal_handling_t *sh;
	sigaction_t *act;
	void (*func) (kthread_t *, void *), *param;
	kprocess_t *proc;
	siginfo_t *us;
	param_t param1, param2, param3;

	ASSERT ( kthread );
	ASSERT ( kthread_check_kthread ( kthread ) );
	ASSERT ( sig->si_signo > 0 && sig->si_signo <= SIGMAX );

	if ( !kthread_is_alive ( kthread ) )
		return ESRCH;

	sh = kthread_get_sigparams ( kthread );

	/* is thread suspended and waits for this signal? */
	if (
		kthread_is_suspended ( kthread, (void **) &func, &param ) &&
		((void *) func) == ((void *) ksignal_received_signal)
	)
	{
		/* thread is waiting for signal */
		if ( !ksignal_received_signal ( kthread, sig ) )
		{
			/* waited for this signal */

			/* do not process this signal further */
			return EXIT_SUCCESS;
		}
	}

	/*
	 * If thread is in interruptable state and signal is not masked:
	 * - process signal
	 * - otherwise queue it
	 */
	if (
		kthread_get_interruptable ( kthread ) &&
		!sigtestset ( sh->mask, sig->si_signo )
	)
	{
		act = &sh->act[sig->si_signo];

		if ( act->sa_flags != SA_SIGINFO )
			return ENOTSUP; /* not supported without SA_SIGINFO! */

		if ( 	act->sa_sigaction == SIG_ERR ||
			act->sa_sigaction == SIG_DFL ||
			act->sa_sigaction == SIG_IGN ||
			act->sa_sigaction == SIG_HOLD	)
		{
			return ENOTSUP; /* not yet supported */
		}

		if ( !kthread_is_ready ( kthread ) )
		{
			/*
			 * thread is suspended/blocked on something else
			 *
			 * 1. handle interruption:
			 *    a) we break suspend/wait state
			 *       (call cancel function)
			 *    b) set errno = EINTR
			 *    c) set return value = FAILURE
			 * 2. create new thread state
			 *    a) process signal in this state
			 */
			void (*func) (kthread_t *, void *), *param;
			kthread_is_suspended (kthread, (void **) &func, &param);

			if ( func )
				func ( kthread, param );

			kthread_move_to_ready ( kthread, LAST );
			kthread_set_errno ( kthread, EINTR );
			kthread_set_syscall_retval ( kthread, EXIT_FAILURE );

			/* thread is unsuspended, but signal
			 * handler will be added first */
			schedule = TRUE;
		}

		/* copy sig to user space */
		proc = kthread_get_process ( kthread );
		us = kprocess_stack_alloc ( proc );
		ASSERT (us);
		/*if ( !us )
			return ENOMEM;*/

		*us = *sig;

		kthread_create_new_state ( kthread, act->sa_sigaction,
					   K2U_GET_ADR ( us, proc ), NULL,
					   HANDLER_STACK_SIZE, TRUE );

		param1.p_ptr = proc;
		param2.p_ptr = us;
		param3.p_ptr = NULL;
		kthread_add_cleanup ( kthread, kprocess_stack_free,
				      param1, param2, param3 );

		/* mask signal in thread mask */
		sigaddset ( sh->mask, sig->si_signo );
		/* mask additional signals in thread mask */
		sigaddsets ( sh->mask, &act->sa_mask );
	}
	else {
		enqueue = TRUE;
	}

	if ( enqueue )
	{
		ksignal_add_to_pending ( sh, sig );

		retval = EAGAIN;
	}

	if ( schedule )
		kthreads_schedule ();

	return retval;
}
Exemplo n.º 10
0
/*! Callback function called when a signal is delivered to suspended thread */
static int ksignal_received_signal ( kthread_t *kthread, void *param )
{
	siginfo_t *sig;
	context_t *context;
	uint sysid;
	void *p;
	sigset_t *set;
	siginfo_t *info;
	int retval = EXIT_SUCCESS;

	ASSERT ( kthread );

	/* thread waked by signal or other event? */
	if ( param == NULL )
	{
		kthread_set_errno ( kthread, EINTR );
		kthread_set_syscall_retval ( kthread, EXIT_FAILURE );

		return EXIT_FAILURE; /* other event interrupted thread */
	}

	/* signal interrupted, but did thread waited for this signal? */
	sig = param;

	/* get syscall which caused thread to be suspend */
	context = kthread_get_context ( kthread );
	sysid = arch_syscall_get_id ( context );

	switch ( sysid )
	{
	case SIGWAITINFO: /* sigwaitinfo */
		p = arch_syscall_get_params ( context );

		set =   *( (sigset_t **) p );	p += sizeof (sigset_t *);
		info =  *( (siginfo_t **) p );	p += sizeof (siginfo_t *);

		ASSERT ( set );
		set = U2K_GET_ADR ( set, kthread_get_process (NULL) );
		ASSERT ( set );

		if ( info )
			info = U2K_GET_ADR ( info, kthread_get_process (NULL) );

		retval = EXIT_FAILURE;

		if ( sigtestset ( set, sig->si_signo ) )
		{
			retval = sig->si_signo;
			kthread_set_syscall_retval ( kthread, retval );

			if ( info )
				*info = *sig;

			kthread_set_errno ( kthread, EXIT_SUCCESS );

			/* resume with thread */
			kthread_move_to_ready ( kthread, LAST );

			kthreads_schedule ();

			return EXIT_SUCCESS;
		}
		else {
			/* not waiting for this signal */
			return EXIT_FAILURE;
		}
	default:
		return EXIT_FAILURE;
	}
}
Exemplo n.º 11
0
static void edf_deadline_timer ( void *p )
{
	alarm_t alarm;
	kthread_t *kthread = p, *test;
	kthread_sched_data_t *tsched  = kthread_get_sched_param ( kthread );


	ASSERT ( kthread );

	test = kthreadq_remove ( &ksched_edf.params.edf.wait, kthread );

	LOG( DEBUG, "%x %x [Deadline alarm]", kthread, test );

	if( test == kthread )
	{
		if ( edf_check_deadline ( kthread ) )
		{
			LOG( DEBUG, "%x [Waked, but too late]", kthread );

			kthread_set_syscall_retval ( kthread, -1 );
			kthread_move_to_ready ( kthread, LAST );

			if ( tsched->params.edf.flags & EDF_TERMINATE )
			{
				LOG( DEBUG, "%x [EDF_TERMINATE]", kthread );
				tsched = kthread_get_sched_param ( kthread );
				k_alarm_remove ( tsched->params.edf.edf_period_alarm );
				k_alarm_remove ( tsched->params.edf.edf_deadline_alarm );
				tsched->params.edf.edf_period_alarm = NULL;
				kthread_cancel ( kthread, -E_DEADLINE );
			}

			kthreads_schedule ();
		}
	}
	else {
		/*
		 * thread is not in edf.wait queue, but might be running or its
		 * blocked - it is probable (sure?) it missed deadline
		 */
		LOG( DEBUG, "%x [Not in edf.wait. Missed deadline?]", kthread );

		if ( edf_check_deadline ( kthread ) )
		{
			/* what to do if its missed? kill thread? */
			tsched = kthread_get_sched_param ( kthread );
			if ( tsched->params.edf.flags & EDF_TERMINATE )
			{
				LOG( DEBUG, "%x [EDF_TERMINATE]", kthread );
				k_alarm_remove ( tsched->params.edf.edf_period_alarm );
				k_alarm_remove ( tsched->params.edf.edf_deadline_alarm );
				tsched->params.edf.edf_period_alarm = NULL;
				kthread_cancel ( kthread, -E_DEADLINE );
			}
			else if ( tsched->params.edf.flags & EDF_CONTINUE )
			{
				/* continue as deadline is not missed */
				LOG( DEBUG, "%x [EDF_CONTINUE]", kthread );
			}
			else if ( tsched->params.edf.flags & EDF_SKIP )
			{
				/* skip deadline */
				/* set times for next period */
				LOG( DEBUG, "%x [EDF_SKIP]", kthread );

				time_add ( &tsched->params.edf.next_run,
					   &tsched->params.edf.period );
			
				tsched->params.edf.active_deadline = tsched->params.edf.next_run;
				time_add ( &tsched->params.edf.active_deadline,
					   &tsched->params.edf.relative_deadline );
			
				if ( kthread == ksched_edf.params.edf.active )
					ksched_edf.params.edf.active = NULL;

				alarm.action = edf_deadline_timer;
				alarm.param = kthread;
				alarm.flags = 0;
				alarm.period.sec = alarm.period.nsec = 0;
				alarm.exp_time = tsched->params.edf.active_deadline;

				k_alarm_set ( tsched->params.edf.edf_deadline_alarm, &alarm );

				alarm.action = edf_period_timer;
				alarm.param = kthread;
				alarm.flags = ALARM_PERIODIC;
				alarm.period = tsched->params.edf.period;
				alarm.exp_time = tsched->params.edf.next_run;

				k_alarm_set ( tsched->params.edf.edf_period_alarm, &alarm );

				kthread_enqueue ( kthread, &ksched_edf.params.edf.ready );
				kthreads_schedule (); /* will call edf_schedule() */
			}
		}
	}
}
Exemplo n.º 12
0
/*! Send signal to target thread */
int ksignal_queue ( kthread_t *kthread, siginfo_t *sig )
{
	int enqueue = FALSE;
	int retval = EXIT_SUCCESS;
	int schedule = FALSE;
	ksignal_handling_t *sh;
	sigaction_t *act;
	void (*func) (kthread_t *, void *), *param;
	kprocess_t *proc;
	siginfo_t *us;
	param_t param1, param2, param3;
	ksiginfo_t *ksig;

	ASSERT ( kthread );
	ASSERT ( kthread_check_kthread ( kthread ) );
	ASSERT ( sig->si_signo > 0 && sig->si_signo <= SIGMAX );

	if ( !kthread_is_alive ( kthread ) )
		return ESRCH;

	sh = kthread_get_sigparams ( kthread );

	/* is thread suspended and waits for this signal? */
	if ( kthread_is_suspended ( kthread, (void **) &func, &param ) )
	{
		if ( ((void *) func) == ((void *) ksignal_received_signal) )
		{
			/* thread is waiting for signal */
			if ( !ksignal_received_signal ( kthread, sig ) )
			{
				/* waited for this signal */
				/* should continue with signal handler also? */

				/* do not process this signal further */
				return EXIT_SUCCESS;
			}

		}
		/* else {
		 *	thread is waiting for something else;
		 * 	deal with this later (in next "if")
		 * } */
	}

	/* if signal is not masked in thread signal mask, deliver signal */
	if ( !sigtestset ( sh->mask, sig->si_signo ) )
	{
		act = &sh->act[sig->si_signo];

		if ( act->sa_flags != SA_SIGINFO )
			return ENOTSUP; /* not supported without SA_SIGINFO! */

		if ( 	act->sa_sigaction == SIG_ERR ||
			act->sa_sigaction == SIG_DFL ||
			act->sa_sigaction == SIG_IGN ||
			act->sa_sigaction == SIG_HOLD	)
		{
			return ENOTSUP; /* not yet supported */
		}

		/* current implementation
			* - if thread is active or ready
			* -- save old context in list
			* -- create new context
			* - else
			* -- enqueue signal or cancel wait state (todo)
			*
			* on sys__exit check if its handler or thread!!!
			*/

		if ( !kthread_is_ready ( kthread ) )
		{
			void (*func) (kthread_t *, void *), *param;
			if ( kthread_is_suspended ( kthread,
						    (void **) &func, &param ) )
			{
				/*
					* thread is suspended on something
					* else; interrupt it or not?
					*
					* -handle interruption (kernel part)
					* -interrupt it (resume)
					* -process signal
					*
					* to do above just move thread to
					* ready, set errno & retval
					*/
				if ( func )
					func ( kthread, param );

				kthread_move_to_ready ( kthread, LAST );
				kthread_set_errno ( kthread, EINTR );
				kthread_set_syscall_retval(kthread,EXIT_FAILURE);

				/* thread is unsuspended, but signal
				 * handler will be added first */
				schedule = TRUE;
			}
			else {
				/* what else? this is error */
				ASSERT ( FALSE );
			}
		}

		/* copy sig to user space */
		proc = kthread_get_process ( kthread );
		us = ffs_alloc(proc->stack_pool, sizeof (siginfo_t));
		ASSERT (us);
		/*if ( !us )
			return ENOMEM;*/

		*us = *sig;

		kthread_create_new_state ( kthread, act->sa_sigaction,
					   K2U_GET_ADR ( us, proc ), NULL,
					   HANDLER_STACK_SIZE, TRUE );

		param1.p_ptr = proc->stack_pool;
		param2.p_ptr = us;
		param3.p_ptr = NULL;
		kthread_add_cleanup ( kthread, kthread_param_free,
				      param1, param2, param3 );

		/* mask signal in thread mask */
		sigaddset ( sh->mask, sig->si_signo );
		/* mask additional signals in thread mask */
		sigaddsets ( sh->mask, &act->sa_mask );
	}
	else {
		enqueue = TRUE;
	}

	if ( enqueue )
	{
		/* mask signal in thread mask */
		sigaddset ( sh->mask, sig->si_signo );

		/* add signal to list of pending signals */
		ksig = kmalloc ( sizeof (ksiginfo_t) );
		ksig->siginfo = *sig;

		list_append ( &sh->pending_signals, ksig, &ksig->list );
		/* list_sort_add ( &sh->pending_signals, ksig, &ksig->list,
				ksignal_compare ); */
		retval = EAGAIN;
	}

	if ( schedule )
		kthreads_schedule ();

	return retval;
}
Exemplo n.º 13
0
static void edf_deadline_alarm ( sigval_t sigev_value )
{
	kthread_t *kthread = sigev_value.sival_ptr, *test;
	kthread_sched2_t *tsched;
	ksched_t *ksched;
	itimerspec_t alarm;

	ASSERT ( kthread );

	ksched = ksched2_get ( kthread_get_sched_policy (kthread) );
	tsched = kthread_get_sched2_param ( kthread );

	test = kthreadq_remove ( &ksched->params.edf.wait, kthread );

	EDF_LOG ( "%x %x [Deadline alarm]", kthread, test );

	if( test == kthread )
	{
		EDF_LOG ( "%x [Waked, but too late]", kthread );

		kthread_set_syscall_retval ( kthread, EXIT_FAILURE );
		kthread_move_to_ready ( kthread, LAST );

		if ( tsched->params.edf.flags & EDF_TERMINATE )
		{
			EDF_LOG ( "%x [EDF_TERMINATE]", kthread );
			ktimer_delete ( tsched->params.edf.period_alarm );
			tsched->params.edf.period_alarm = NULL;
			ktimer_delete ( tsched->params.edf.deadline_alarm );
			tsched->params.edf.deadline_alarm = NULL;
			kthread_set_errno ( kthread, ETIMEDOUT );
			kthread_exit ( kthread, NULL, TRUE );
		}
		else {
			edf_schedule (ksched);
		}
	}
	else {
	/*
	 * thread is not in edf.wait queue, but might be running or its
	 * blocked - it is probable (almost certain) that it missed deadline
	 */
	EDF_LOG ( "%x [Not in edf.wait. Missed deadline?]", kthread );

	if ( edf_check_deadline ( kthread ) )
	{
		/* what to do if its missed? kill thread? */
		if ( tsched->params.edf.flags & EDF_TERMINATE )
		{
			EDF_LOG ( "%x [EDF_TERMINATE]", kthread );
			ktimer_delete (tsched->params.edf.period_alarm);
			tsched->params.edf.period_alarm = NULL;
			ktimer_delete ( tsched->params.edf.deadline_alarm );
			tsched->params.edf.deadline_alarm = NULL;
			kthread_set_errno ( kthread, ETIMEDOUT );
			kthread_exit ( kthread, NULL, TRUE );
		}
		else if ( tsched->params.edf.flags & EDF_CONTINUE )
		{
			/* continue as deadline is not missed */
			EDF_LOG ( "%x [EDF_CONTINUE]", kthread );
		}
		else if ( tsched->params.edf.flags & EDF_SKIP )
		{
			/* skip deadline */
			/* set times for next period */
			EDF_LOG ( "%x [EDF_SKIP]", kthread );

			time_add ( &tsched->params.edf.next_run,
				   &tsched->params.edf.period );

			tsched->params.edf.active_deadline =
					tsched->params.edf.next_run;
			time_add ( &tsched->params.edf.active_deadline,
					&tsched->params.edf.relative_deadline );

			if ( kthread == ksched->params.edf.active )
				ksched->params.edf.active = NULL;

			TIME_RESET ( &alarm.it_interval );
			alarm.it_value = tsched->params.edf.active_deadline;
			ktimer_settime ( tsched->params.edf.deadline_alarm,
					 TIMER_ABSTIME, &alarm, NULL );

			alarm.it_interval = tsched->params.edf.period;
			alarm.it_value = tsched->params.edf.next_run;
			ktimer_settime ( tsched->params.edf.period_alarm,
					 TIMER_ABSTIME, &alarm, NULL );

			kthread_enqueue (kthread, &ksched->params.edf.ready);
			edf_schedule (ksched);
		}
	} /* moved 1 tab left for readability */
	}
}