Ejemplo n.º 1
0
/*!
 * Create new timer
 * \param clockid	Clock used in timer
 * \param evp		Timer expiration action
 * \param timerid	Timer descriptor is returned in this variable
 * \return status	0 for success
 */
int sys__timer_create ( void *p )
{
	clockid_t clockid;
	sigevent_t *evp;
	timer_t *timerid;

	ktimer_t *ktimer;
	int retval = EXIT_SUCCESS;
	kobject_t *kobj;

	clockid =	*( (clockid_t *) p );	p += sizeof (clockid_t);
	evp =		*( (sigevent_t **) p );	p += sizeof (sigevent_t *);
	timerid =	*( (timer_t **) p );

	ASSERT_ERRNO_AND_EXIT (
	clockid == CLOCK_REALTIME || clockid == CLOCK_MONOTONIC, EINVAL );
	ASSERT_ERRNO_AND_EXIT ( evp && timerid, EINVAL );

	retval = ktimer_create ( clockid, evp, &ktimer, kthread_get_active() );
	if ( retval == EXIT_SUCCESS )
	{
		kobj = kmalloc_kobject ( 0 );
		kobj->kobject = ktimer;
		timerid->id = ktimer->id;
		timerid->ptr = kobj;
	}
	EXIT ( retval );
}
Ejemplo n.º 2
0
void ksched_rr_start_timer ()
{
	sigevent_t evp;
	itimerspec_t itimer;
	int retval = 0;

	if ( rr_ktimer )
		return; /* already set */

	evp.sigev_notify = SIGEV_THREAD;
	evp.sigev_value.sival_ptr = NULL;
	evp.sigev_notify_function = ksched_rr_tick;

	retval += ktimer_create ( CLOCK_REALTIME, &evp, &rr_ktimer, NULL );
	ASSERT ( retval == EXIT_SUCCESS );

	TIME_RESET ( &itimer.it_value );
	itimer.it_value.tv_sec = 0;
	itimer.it_value.tv_nsec = SCHED_RR_TICK;
	itimer.it_interval = itimer.it_value;

	retval += ktimer_settime ( rr_ktimer, 0, &itimer, NULL );
	ASSERT ( retval == EXIT_SUCCESS );

}
Ejemplo n.º 3
0
/*!
 * Suspend thread until given time elapses
 * \param clockid Clock to use
 * \param flags Flags (TIMER_ABSTIME)
 * \param request Suspend duration
 * \param remain Remainder time if interrupted during suspension
 * \return status
 */
int sys__clock_nanosleep ( void *p )
{
	clockid_t clockid;
	int flags;
	timespec_t *request;
	timespec_t *remain;

	int retval = EXIT_SUCCESS;
	kthread_t *kthread = kthread_get_active ();
	ktimer_t *ktimer;
	sigevent_t evp;
	itimerspec_t itimer;

	clockid =	*( (clockid_t *) p );	p += sizeof (clockid_t);
	flags =		*( (int *) p );		p += sizeof (int);
	request =	*( (timespec_t **) p );	p += sizeof (timespec_t *);
	remain =	*( (timespec_t **) p );

	ASSERT_ERRNO_AND_EXIT (
	    (clockid==CLOCK_REALTIME || clockid==CLOCK_MONOTONIC) &&
	    request && TIME_IS_SET(request),
	    EINVAL
	);

	/* Timers are used for "sleep" operations through steps 1-4 */

	/* 1. create timer, but not arm it yet */
	evp.sigev_notify = SIGEV_WAKE_THREAD;
	evp.sigev_value.sival_ptr = kthread;
	evp.sigev_notify_function = kclock_wake_thread;

	retval += ktimer_create ( clockid, &evp, &ktimer, kthread );
	ASSERT ( retval == EXIT_SUCCESS );

	/* save remainder location, if provided */
	ktimer->param = remain;

	/* 2. suspend thread */
	kthread_set_private_param ( kthread, ktimer );
	retval += kthread_suspend ( kthread, kclock_interrupt_sleep, ktimer );
	ASSERT ( retval == EXIT_SUCCESS );

	/* 3. arm timer */
	TIME_RESET ( &itimer.it_interval );
	itimer.it_value = *request;

	retval += ktimer_settime ( ktimer, flags, &itimer, NULL );
	ASSERT ( retval == EXIT_SUCCESS );

	/* 4. pick other thread as active */
	kthreads_schedule ();

	EXIT ( retval );
}
Ejemplo n.º 4
0
static int edf_create_alarm ( kthread_t *kthread, void *action, void **timer )
{
	sigevent_t evp;

	evp.sigev_notify = SIGEV_THREAD;
	evp.sigev_notify_function = action;
	evp.sigev_notify_attributes = NULL;
	evp.sigev_value.sival_ptr = kthread;

	return ktimer_create ( CLOCK_REALTIME, &evp, timer, NULL );
}
Ejemplo n.º 5
0
int main(int argc, char* argv[]) {
    int                i            = 0;
    kloop_t*            loop         = 0;
    ktimer_t*          timer        = 0;
    kchannel_ref_t*     connector    = 0;
    kthread_runner_t*   timer_thread = 0;
    static const char* helper_string =
        "-n    client count\n"
        "-ip   remote host IP\n"
        "-port remote host port\n";

    if (argc > 2) {
        for (i = 1; i < argc; i++) {
            if (!strcmp("-n", argv[i])) {
                client_n = atoi(argv[i+1]);
            } else if (!strcmp("-ip", argv[i])) {
                ip = argv[i+1];
            } else if (!strcmp("-port", argv[i])) {
                port = atoi(argv[i+1]);
            }
        }
    } else {
        printf(helper_string);
        exit(0);
    }

    loop       = knet_loop_create();
    timer_loop = ktimer_loop_create(1000);
    timer      = ktimer_create(timer_loop);

    ktimer_start(timer, timer_cb, 0, 1000);
    timer_thread = thread_runner_create(0, 0);
    thread_runner_start_timer_loop(timer_thread, timer_loop, 0);

    connector = knet_loop_create_channel(loop, 8, 121);
    knet_channel_ref_set_cb(connector, connector_cb);
    knet_channel_ref_set_timeout(connector, 1);
    if (error_ok != knet_channel_ref_connect(connector, ip, port, 20)) {
        return 0;
    }

    knet_loop_run(loop);
    thread_runner_destroy(timer_thread);
    knet_loop_destroy(loop);
    ktimer_loop_destroy(timer_loop);

    return 0;
}