Ejemplo n.º 1
0
/* 线程删除示例的初始化 */
int thread_delete_init()
{
	/* 创建线程1 */
	tid1 = rt_thread_create("t1", /* 线程1的名称是t1 */
		thread1_entry, RT_NULL,   /* 入口是thread1_entry,参数是RT_NULL */
		THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
	if (tid1 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */
	{
		tid1->cleanup = thread1_cleanup;
		rt_thread_startup(tid1);
	}
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	/* 创建线程1 */
	tid2 = rt_thread_create("t2", /* 线程1的名称是t2 */
		thread2_entry, RT_NULL,   /* 入口是thread2_entry,参数是RT_NULL */
		THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
	if (tid2 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */
	{
		tid2->cleanup = thread2_cleanup;
		rt_thread_startup(tid2);
	}
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return 0;
}
int semaphore_buffer_worker_init()
{
    /* 初始化ring buffer */
    rb_init(&working_rb, working_buffer, BUFFER_SIZE);

    /* 创建信号量 */
    sem = rt_sem_create("sem", 1, RT_IPC_FLAG_FIFO);
    if (sem == RT_NULL) {
        tc_stat(TC_STAT_END | TC_STAT_FAILED);
        return 0;
    }

    /* 创建线程1 */
    tid = rt_thread_create("thread",
                           thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
                           THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    /* 创建线程2 */
    worker = rt_thread_create("worker",
                              worker_entry, RT_NULL, /* 线程入口是worker_entry, 入口参数是RT_NULL */
                              THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (worker != RT_NULL)
        rt_thread_startup(worker);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
}
Ejemplo n.º 3
0
void timer_create_init()
{
    /* 创建定时器1 */
    timer1 = rt_timer_create("timer1",  /* 定时器名字是 timer1 */
        timeout1, /* 超时时回调的处理函数 */
        RT_NULL, /* 超时函数的入口参数 */
        10, /* 定时长度,以OS Tick为单位,即10个OS Tick */
        RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
    /* 启动定时器 */
    if (timer1 != RT_NULL)
        rt_timer_start(timer1);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    /* 创建定时器2 */
    timer2 = rt_timer_create("timer2",   /* 定时器名字是 timer2 */
        timeout2, /* 超时时回调的处理函数 */
        RT_NULL, /* 超时函数的入口参数 */
        30, /* 定时长度为30个OS Tick */
        RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */

    /* 启动定时器 */
    if (timer2 != RT_NULL)
        rt_timer_start(timer2);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);
}
int thread_resume_init()
{
    /* 创建线程1 */
    tid1 = rt_thread_create("thread",
        thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
        THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid1 != RT_NULL)
    {
        tid1->cleanup = thread_cleanup;
        rt_thread_startup(tid1);
    }
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    /* 创建线程2 */
    tid2 = rt_thread_create("thread",
        thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
        THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    if (tid2 != RT_NULL)
    {
        tid2->cleanup = thread_cleanup;
        rt_thread_startup(tid2);
    }
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
}
int thread_static_simple_init()
{
	rt_err_t result;

	/* 初始化线程1 */
	result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */
							thread_entry, (void*)1, /* 线程的入口是thread_entry,入口参数是1 */
							&thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */
							THREAD_PRIORITY, 10);
	if (result == RT_EOK) /* 如果返回正确,启动线程1 */
		rt_thread_startup(&thread1);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	/* 初始化线程2 */
	result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */
							thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是2 */
							&thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */
							THREAD_PRIORITY + 1, 10);
	if (result == RT_EOK) /* 如果返回正确,启动线程2 */
		rt_thread_startup(&thread2);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return 0;
}
rt_err_t thread_same_priority_init()
{
	rt_err_t result;

	result = rt_thread_init(&thread1,
							"t1",
							thread1_entry, RT_NULL,
							&thread1_stack[0], sizeof(thread1_stack),
							THREAD_PRIORITY, 10);
	if (result == RT_EOK)
		rt_thread_startup(&thread1);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	result = rt_thread_init(&thread2,
							"t2",
							thread2_entry, RT_NULL,
							&thread2_stack[0], sizeof(thread2_stack),
							THREAD_PRIORITY, 5);
	if (result == RT_EOK)
		rt_thread_startup(&thread2);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return result;
}
int mempool_simple_init()
{
    int i;
    for (i = 0; i < 48; i ++) ptr[i] = RT_NULL;

    /* 初始化内存池对象 */
    rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);

    /* 创建线程1 */
    tid1 = rt_thread_create("t1",
                            thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
                            THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    /* 创建线程2 */
    tid2 = rt_thread_create("t2",
                            thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
                            THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
}
Ejemplo n.º 8
0
static void _tc_cleanup()
{
	/* lock scheduler */
	rt_enter_critical();

	/* delete thread */
	if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
		tc_stat(TC_STAT_FAILED);
	if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
		tc_stat(TC_STAT_FAILED);

	/* unlock scheduler */
	rt_exit_critical();
}
Ejemplo n.º 9
0
static void _tc_cleanup()
{
	/* lock scheduler */
	rt_enter_critical();

	/* delete t1, t2 and worker thread */
	rt_thread_delete(t1);
	rt_thread_delete(t2);
	rt_thread_delete(worker);

	if (t1_count > t2_count)
		tc_stat(TC_STAT_FAILED);
	else
		tc_stat(TC_STAT_PASSED);

	/* unlock scheduler */
	rt_exit_critical();
}
Ejemplo n.º 10
0
int _tc_test_spi_bus_open_close(void)
{
    tc_cleanup(_tc_cleanup);

    if(RT_EOK != test_spi_bus_open_close()) {
        tc_stat(TC_STAT_FAILED);
    }
    return 100;
}
Ejemplo n.º 11
0
int _tc_test_spi_bus_register(void)
{
    tc_cleanup(_tc_cleanup);

    if(RT_EOK != test_spi_bus_register()) {
        tc_stat(TC_STAT_FAILED);
    }
    return 100;
}
Ejemplo n.º 12
0
static void thread1_cleanup(struct rt_thread *tid)
{
	if (tid != tid1)
	{
		tc_stat(TC_STAT_END | TC_STAT_FAILED);
		return ;
	}
	rt_kprintf("thread1 end\n");
	tid1 = RT_NULL;
}
Ejemplo n.º 13
0
static void _tc_cleanup()
{
	/* lock scheduler */
	rt_enter_critical();

	/* delete thread */
	if (tid1 != RT_NULL)
	{
		rt_kprintf("tid1 is bad\n");
		tc_stat(TC_STAT_FAILED);
	}
	if (tid2 != RT_NULL)
	{
		rt_kprintf("tid2 is bad\n");
		tc_stat(TC_STAT_FAILED);
	}

	/* unlock scheduler */
	rt_exit_critical();
}
int thread_dynamic_simple_init()
{
    /* 创建线程1 */
    tid1 = rt_thread_create("t1",
        thread_entry, (void*)1, /* 线程入口是thread_entry, 入口参数是1 */
        THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    /* 创建线程2 */
    tid2 = rt_thread_create("t2",
        thread_entry, (void*)2, /* 线程入口是thread_entry, 入口参数是2 */
        THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
}
Ejemplo n.º 15
0
int thread_dynamic_init()
{
    rt_thread_t tid;

    tid = rt_thread_create("test",
        thread_entry, RT_NULL,
        THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);

    return 0;
}
Ejemplo n.º 16
0
int semaphore_static_init()
{
	rt_err_t result;

	/* 初始化信号量,初始值是0 */
	result = rt_sem_init(&sem, "sem", 0, RT_IPC_FLAG_FIFO);
	if (result != RT_EOK)
	{
		tc_stat(TC_STAT_END | TC_STAT_FAILED);
		return 0;
	}

	/* 初始化线程1 */
	result = rt_thread_init(&thread, "thread", /* 线程名:thread */
		thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是RT_NULL*/
		&thread_stack[0], sizeof(thread_stack), /* 线程栈是thread_stack */
		THREAD_PRIORITY, 10);
	if (result == RT_EOK) /* 如果返回正确,启动线程1 */
		rt_thread_startup(&thread);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return 0;
}
Ejemplo n.º 17
0
int semaphore_priority_init()
{
	sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO);
	if (sem == RT_NULL) {
		tc_stat(TC_STAT_END | TC_STAT_FAILED);
		return 0;
	}

	t1_count = t2_count = 0;

	t1 = rt_thread_create("t1",
						  thread1_entry, RT_NULL,
						  THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
	if (t1 != RT_NULL)
		rt_thread_startup(t1);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	t2 = rt_thread_create("t2",
						  thread2_entry, RT_NULL,
						  THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
	if (t2 != RT_NULL)
		rt_thread_startup(t2);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	worker = rt_thread_create("worker",
							  worker_thread_entry, RT_NULL,
							  THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
	if (worker != RT_NULL)
		rt_thread_startup(worker);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return 0;
}
static void _tc_cleanup()
{
	/* lock scheduler */
	rt_enter_critical();

	if (thread1.stat != RT_THREAD_CLOSE)
		rt_thread_detach(&thread1);
	if (thread2.stat != RT_THREAD_CLOSE)
		rt_thread_detach(&thread2);

	/* unlock scheduler */
	rt_exit_critical();

	if (t1_count / t2_count != 2)
		tc_stat(TC_STAT_END | TC_STAT_FAILED);
}
Ejemplo n.º 19
0
static void thread2_cleanup(struct rt_thread *tid)
{
	/*
	 * 线程2运行结束后也将自动被删除(线程控制块和线程栈在idle线
	 * 程中释放)
	 */

	if (tid != tid2)
	{
		tc_stat(TC_STAT_END | TC_STAT_FAILED);
		return ;
	}
	rt_kprintf("thread2 end\n");
	tid2 = RT_NULL;
	tc_done(TC_STAT_PASSED);
}
Ejemplo n.º 20
0
rt_err_t thread_delay_init()
{
	rt_err_t result;

	result = rt_thread_init(&thread,
		"test",
		thread_entry, RT_NULL,
		&thread_stack[0], sizeof(thread_stack),
		THREAD_PRIORITY, 10);

	if (result == RT_EOK)
		rt_thread_startup(&thread);
	else
		tc_stat(TC_STAT_END | TC_STAT_FAILED);

	return result;
}
static void _tc_cleanup()
{
    /* lock scheduler */
    rt_enter_critical();

    if (thread1.stat != RT_THREAD_CLOSE)
        rt_thread_detach(&thread1);
    if (thread2.stat != RT_THREAD_CLOSE)
        rt_thread_detach(&thread2);

    /* unlock scheduler */
    rt_exit_critical();

    rt_kprintf("t1_count=%d t2_count=%d\n",t1_count,t2_count);

    if (t1_count / t2_count != 2)
        tc_stat(TC_STAT_END | TC_STAT_FAILED);
    else
        tc_done(TC_STAT_PASSED);
}
Ejemplo n.º 22
0
TYPED_TEST_P(TcTest, RequestDoesNotFitIntoOneCompound)
{
	const int NFILES = 64; // 64 * 8 == 512
	const char *paths[NFILES];
	int flags[NFILES];
	struct tc_attrs attrs[NFILES];
	const char *new_paths[NFILES];
	struct tc_file_pair pairs[NFILES];
	const char *ROOTDIR = "DontFit";

	EXPECT_TRUE(tc_rm_recursive(ROOTDIR));
	for (int i = 0; i < NFILES; ++i) {
		paths[i] = new_auto_path("DontFit/a%03d/b/c/d/e/f/g/h/file", i);
		tc_ensure_parent_dir(paths[i]);
		flags[i] = O_WRONLY | O_CREAT;
		attrs[i].file = tc_file_from_path(paths[i]);
		new_paths[i] = new_auto_path("DontFit/file-%d", i);
		pairs[i].src_file = tc_file_from_path(paths[i]);
		pairs[i].dst_file = tc_file_from_path(new_paths[i]);
	}
	tc_file *files = tc_openv(paths, NFILES, flags, NULL);
	EXPECT_NOTNULL(files);
	EXPECT_OK(tc_closev(files, NFILES));
	EXPECT_OK(tc_getattrsv(attrs, NFILES, false));

	struct tc_attrs_masks listdir_mask = { .has_mode = true };
	std::set<std::string> objs;
	EXPECT_OK(tc_listdirv(&ROOTDIR, 1, listdir_mask, 0, true,
			      listdir_test_cb, &objs, false));
	std::set<std::string> expected;
	for (int i = 0; i < NFILES; ++i) {
		std::string p(paths[i]);
		size_t n = p.length();
		while (n != std::string::npos) {
			expected.emplace(p.data(), n);
			n = p.find_last_of('/', n - 1);
		}
	}
	expected.erase("DontFit");
	EXPECT_THAT(objs, testing::ContainerEq(expected));

	EXPECT_OK(tc_renamev(pairs, NFILES, false));
	EXPECT_OK(tc_unlinkv(new_paths, NFILES));
}

static bool is_same_stat(const struct stat *st1, const struct stat *st2)
{
	return st1->st_ino == st2->st_ino
	    && st1->st_mode == st2->st_mode
	    && st1->st_nlink == st2->st_nlink
	    && st1->st_uid == st2->st_uid
	    && st1->st_gid == st2->st_gid
	    && st1->st_rdev == st2->st_rdev
	    && st1->st_size == st2->st_size
	    && st1->st_mtime == st2->st_mtime
	    && st1->st_ctime == st2->st_ctime;
	    //&& st1->st_dev == st2->st_dev
	    //&& st1->st_blksize == st2->st_blksize
	    //&& st1->st_blocks == st2->st_blocks
}

TYPED_TEST_P(TcTest, TcStatBasics)
{
	const char *FPATH = "TcTest-TcStatFile.txt";
	const char *LPATH = "TcTest-TcStatLink.txt";

	tc_unlink(FPATH);
	tc_unlink(LPATH);
	tc_touch(FPATH, 4_KB);
	EXPECT_EQ(0, tc_symlink(FPATH, LPATH));

	struct stat st1;
	EXPECT_EQ(0, tc_stat(LPATH, &st1));

	struct stat st2;
	tc_file *tcf = tc_open(FPATH, O_RDONLY, 0);
	EXPECT_EQ(0, tc_fstat(tcf, &st2));
	EXPECT_TRUE(is_same_stat(&st1, &st2));
	tc_close(tcf);

	struct stat st3;
	EXPECT_EQ(0, tc_lstat(LPATH, &st3));
	EXPECT_TRUE(S_ISLNK(st3.st_mode));
	EXPECT_FALSE(is_same_stat(&st1, &st3));
}

TYPED_TEST_P(TcTest, TcRmBasic)
{
#define TCRM_PREFIX "/vfs0/tc_nfs4_test/TcRmBasic"
	EXPECT_OK(tc_ensure_dir(TCRM_PREFIX "/dir-a/subdir-a1", 0755, NULL));
	EXPECT_OK(tc_ensure_dir(TCRM_PREFIX "/dir-a/subdir-a2", 0755, NULL));
	EXPECT_OK(tc_ensure_dir(TCRM_PREFIX "/dir-b/subdir-b1", 0755, NULL));

	tc_touch(TCRM_PREFIX "/dir-a/subdir-a1/a1-file1", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-a/subdir-a1/a1-file2", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-a/subdir-a1/a1-file3", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-a/subdir-a2/a2-file1", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-a/subdir-a2/a2-file2", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-b/subdir-b1/b1-file1", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-b/subdir-b1/b1-file2", 4_KB);
	tc_touch(TCRM_PREFIX "/dir-b/subdir-b1/b1-file3", 4_KB);
	tc_touch(TCRM_PREFIX "/file1", 4_KB);
	tc_touch(TCRM_PREFIX "/file2", 4_KB);

	const char *objs[4] = {
		TCRM_PREFIX "/dir-a",
		TCRM_PREFIX "/dir-b",
		TCRM_PREFIX "/file1",
		TCRM_PREFIX "/file2",
	};

	EXPECT_OK(tc_rm(objs, 4, true));
#undef TCRM_PREFIX
}