⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idle.c

📁 pebble
💻 C
字号:
/* 
 * Copyright 1999, 2000, 2001, 2002 Lucent Technologies Inc.
 * All Rights Reserved.
 * Information Sciences Research Center, Bell Labs.
 *
 * LUCENT TECHNOLOGIES DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE 
 * OR THE SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The
 * software is provided "as is" without expressed or implied warranty 
 * of any kind.
 *
 * These notices must be retained in any copies of any part of this
 * software.
 *
 */

/*
 * Idle task.
 * Always run at the lowest priority.
 *
 * The idle task must never block. It must be always available
 * for the scheduler.
 */

#include "unistd.h"
#include "string.h"
#include "pebble.h"
#include "portals.h"
#include "time.h"

enum	{
	N = 1000,
	SCALE = 2,		/* scale factor to ensure correct conversion */
				/* from count to clock ticks */
	NYIELD = 10,		/* number of initial yields() */
	IDLE_BY_YIELD = 0,	/* do a yield in the idle loop */
	
};

static uvlong idle_count = 0;
static Time count2ticks;

/* the idle thread */
void
idle()
{
	int i;

	/* must not call regular printf, since it may block inside the */
	/* serial driver. */
	fdprintf(2, "idle thread starting");
	if (IDLE_BY_YIELD)
		fdprintf(2, " (using yields)\n");
	else
		fdprintf(2, " (using interruptable spin loop)\n");

	/* verify that we are running in user mode with interrupts enabled */
	if (!check_psw(1,1))
		panic("idle: invalid processor status");

	for (i = 0; i < NYIELD; i++)
		yield();

	while (1) {
		for (i = 0; i < N; i++)
			idle_count++;

		if (IDLE_BY_YIELD)
			yield();
	}

}


Time
sys_idle_time(void)
{
	return (idle_count * count2ticks) / SCALE;
}


/* main scheduler thread */
int main()
{
	int i;
	Time start, elapsed, hrtime_overhead;

	/* verify that we are running in user mode with interrupts enabled */
	if (!check_psw(1,1)) {
		printf("idle: invalid processor status: %08lx\n", get_psw());
		task_exit(1);
	}

	if (set_priority(get_asid(), PRI_IDLE) < 0)
		panic("set_priority of idle thread failed");

	/* compute the number of clock ticks per idle count */
	start = hrtime();
	for (i = 0; i < N; i++)
		hrtime();
	hrtime_overhead = (hrtime() - start) / (N+1);

	start = hrtime();
	for (i = 0; i < N; i++)
		idle_count++;
	elapsed = hrtime() - start - hrtime_overhead;
	count2ticks = (elapsed*SCALE) / N;
	printf("idle count to clock ticks=%d scale=%d\n",
		(int)count2ticks, SCALE);

	if (portal_create(SYS_IDLE_TIME, "smtiii", 0, (Func)sys_idle_time, 0)
	    < 0)
		panic("portal_create failed:");

	printf("spawning idle thread\n");
	if (thr_spawn(idle, 0) < 0)
		panic("spawn of idle thread failed");

	/*
	 * return to initialization code.
	 * cannot just "return", since the startup code (crt0.S) calls
	 * exit when main routine terminates.
	 */
	call_portal(SYS_RTN_RPC);
	return(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -