mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-22 03:09:16 +08:00
convert end of line
This commit is contained in:
+56
-56
@@ -1,56 +1,56 @@
|
||||
/*
|
||||
* File : listdir.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
static char fullpath[256];
|
||||
void list_dir(const char* path)
|
||||
{
|
||||
DIR *dir;
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir != RT_NULL)
|
||||
{
|
||||
struct dirent* dirent;
|
||||
struct stat s;
|
||||
|
||||
do
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
rt_memset(&s, 0, sizeof(struct stat));
|
||||
|
||||
/* build full path for each file */
|
||||
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
|
||||
|
||||
stat(fullpath, &s);
|
||||
if ( s.st_mode & DFS_S_IFDIR )
|
||||
{
|
||||
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
|
||||
}
|
||||
} while (dirent != RT_NULL);
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
else rt_kprintf("open %s directory failed\n", path);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(list_dir, list directory);
|
||||
#endif
|
||||
/*
|
||||
* File : listdir.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
static char fullpath[256];
|
||||
void list_dir(const char* path)
|
||||
{
|
||||
DIR *dir;
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir != RT_NULL)
|
||||
{
|
||||
struct dirent* dirent;
|
||||
struct stat s;
|
||||
|
||||
do
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
rt_memset(&s, 0, sizeof(struct stat));
|
||||
|
||||
/* build full path for each file */
|
||||
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
|
||||
|
||||
stat(fullpath, &s);
|
||||
if ( s.st_mode & DFS_S_IFDIR )
|
||||
{
|
||||
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
|
||||
}
|
||||
} while (dirent != RT_NULL);
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
else rt_kprintf("open %s directory failed\n", path);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(list_dir, list directory);
|
||||
#endif
|
||||
|
||||
+64
-64
@@ -1,64 +1,64 @@
|
||||
/*
|
||||
* File : readspeed.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void readspeed(const char* filename, int block_size)
|
||||
{
|
||||
int fd;
|
||||
char *buff_ptr;
|
||||
rt_size_t total_length;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, 0, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
tick = rt_tick_get();
|
||||
total_length = 0;
|
||||
while (1)
|
||||
{
|
||||
int length;
|
||||
length = read(fd, buff_ptr, block_size);
|
||||
|
||||
if (length <= 0) break;
|
||||
total_length += length;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate read speed */
|
||||
rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
|
||||
#endif
|
||||
/*
|
||||
* File : readspeed.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void readspeed(const char* filename, int block_size)
|
||||
{
|
||||
int fd;
|
||||
char *buff_ptr;
|
||||
rt_size_t total_length;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, 0, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
tick = rt_tick_get();
|
||||
total_length = 0;
|
||||
while (1)
|
||||
{
|
||||
int length;
|
||||
length = read(fd, buff_ptr, block_size);
|
||||
|
||||
if (length <= 0) break;
|
||||
total_length += length;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate read speed */
|
||||
rt_kprintf("File read speed: %d byte/s\n", total_length /tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
|
||||
#endif
|
||||
|
||||
+123
-123
@@ -1,123 +1,123 @@
|
||||
/*
|
||||
* 代码清单:文件读写例子
|
||||
*
|
||||
* 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
|
||||
|
||||
#define TEST_FN "/test.dat"
|
||||
|
||||
/* 测试用的数据和缓冲 */
|
||||
static char test_data[120], buffer[120];
|
||||
|
||||
/* 文件读写测试 */
|
||||
void readwrite(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
int index, length;
|
||||
|
||||
/* 只写 & 创建 打开 */
|
||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 准备写入数据 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
test_data[index] = index + 27;
|
||||
}
|
||||
|
||||
/* 写入数据 */
|
||||
length = write(fd, test_data, sizeof(test_data));
|
||||
if (length != sizeof(test_data))
|
||||
{
|
||||
rt_kprintf("write data failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 关闭文件 */
|
||||
close(fd);
|
||||
|
||||
/* 只写并在末尾添加打开 */
|
||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for append write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
length = write(fd, test_data, sizeof(test_data));
|
||||
if (length != sizeof(test_data))
|
||||
{
|
||||
rt_kprintf("append write data failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
/* 关闭文件 */
|
||||
close(fd);
|
||||
|
||||
/* 只读打开进行数据校验 */
|
||||
fd = open(TEST_FN, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("check: open file for read failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 读取数据(应该为第一次写入的数据) */
|
||||
length = read(fd, buffer, sizeof(buffer));
|
||||
if (length != sizeof(buffer))
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 检查数据是否正确 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 读取数据(应该为第二次写入的数据) */
|
||||
length = read(fd, buffer, sizeof(buffer));
|
||||
if (length != sizeof(buffer))
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 检查数据是否正确 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 检查数据完毕,关闭文件 */
|
||||
close(fd);
|
||||
/* 打印结果 */
|
||||
rt_kprintf("read/write done.\n");
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
/* 输出函数到finsh shell命令行中 */
|
||||
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
||||
#endif
|
||||
/*
|
||||
* 代码清单:文件读写例子
|
||||
*
|
||||
* 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
|
||||
|
||||
#define TEST_FN "/test.dat"
|
||||
|
||||
/* 测试用的数据和缓冲 */
|
||||
static char test_data[120], buffer[120];
|
||||
|
||||
/* 文件读写测试 */
|
||||
void readwrite(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
int index, length;
|
||||
|
||||
/* 只写 & 创建 打开 */
|
||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 准备写入数据 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
test_data[index] = index + 27;
|
||||
}
|
||||
|
||||
/* 写入数据 */
|
||||
length = write(fd, test_data, sizeof(test_data));
|
||||
if (length != sizeof(test_data))
|
||||
{
|
||||
rt_kprintf("write data failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 关闭文件 */
|
||||
close(fd);
|
||||
|
||||
/* 只写并在末尾添加打开 */
|
||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for append write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
length = write(fd, test_data, sizeof(test_data));
|
||||
if (length != sizeof(test_data))
|
||||
{
|
||||
rt_kprintf("append write data failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
/* 关闭文件 */
|
||||
close(fd);
|
||||
|
||||
/* 只读打开进行数据校验 */
|
||||
fd = open(TEST_FN, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("check: open file for read failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 读取数据(应该为第一次写入的数据) */
|
||||
length = read(fd, buffer, sizeof(buffer));
|
||||
if (length != sizeof(buffer))
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 检查数据是否正确 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 读取数据(应该为第二次写入的数据) */
|
||||
length = read(fd, buffer, sizeof(buffer));
|
||||
if (length != sizeof(buffer))
|
||||
{
|
||||
rt_kprintf("check: read file failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 检查数据是否正确 */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
if (test_data[index] != buffer[index])
|
||||
{
|
||||
rt_kprintf("check: check data failed at %d\n", index);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 检查数据完毕,关闭文件 */
|
||||
close(fd);
|
||||
/* 打印结果 */
|
||||
rt_kprintf("read/write done.\n");
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
/* 输出函数到finsh shell命令行中 */
|
||||
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
||||
#endif
|
||||
|
||||
+50
-50
@@ -1,50 +1,50 @@
|
||||
/*
|
||||
* File : seekdir.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-06-02 Bernard first version
|
||||
*/
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void seekdir_test(void)
|
||||
{
|
||||
DIR * dirp;
|
||||
long save3 = 0;
|
||||
long cur;
|
||||
int i = 0;
|
||||
struct dirent *dp;
|
||||
|
||||
dirp = opendir ("/");
|
||||
save3 = telldir(dirp);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
|
||||
/* 保存第三个目录项的目录指针 */
|
||||
if (i++ == 3)
|
||||
{
|
||||
save3 = telldir(dirp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 回到刚才保存的第三个目录项的目录指针 */
|
||||
seekdir (dirp, save3);
|
||||
rt_kprintf("seek dientry to: %d\n", save3);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
}
|
||||
|
||||
/* 关闭目录 */
|
||||
closedir (dirp);
|
||||
}
|
||||
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
|
||||
/*
|
||||
* File : seekdir.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-06-02 Bernard first version
|
||||
*/
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void seekdir_test(void)
|
||||
{
|
||||
DIR * dirp;
|
||||
long save3 = 0;
|
||||
long cur;
|
||||
int i = 0;
|
||||
struct dirent *dp;
|
||||
|
||||
dirp = opendir ("/");
|
||||
save3 = telldir(dirp);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
|
||||
/* 保存第三个目录项的目录指针 */
|
||||
if (i++ == 3)
|
||||
{
|
||||
save3 = telldir(dirp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 回到刚才保存的第三个目录项的目录指针 */
|
||||
seekdir (dirp, save3);
|
||||
rt_kprintf("seek dientry to: %d\n", save3);
|
||||
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
|
||||
{
|
||||
rt_kprintf("direntry: %s\n", dp->d_name);
|
||||
}
|
||||
|
||||
/* 关闭目录 */
|
||||
closedir (dirp);
|
||||
}
|
||||
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
|
||||
|
||||
+72
-72
@@ -1,72 +1,72 @@
|
||||
/*
|
||||
* File : writespeed.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void writespeed(const char* filename, int total_length, int block_size)
|
||||
{
|
||||
int fd, index, length;
|
||||
char *buff_ptr;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* prepare write data */
|
||||
for (index = 0; index < block_size; index++)
|
||||
{
|
||||
buff_ptr[index] = index;
|
||||
}
|
||||
index = 0;
|
||||
|
||||
/* get the beginning tick */
|
||||
tick = rt_tick_get();
|
||||
while (index < total_length / block_size)
|
||||
{
|
||||
length = write(fd, buff_ptr, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("write failed\n");
|
||||
break;
|
||||
}
|
||||
|
||||
index ++;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate write speed */
|
||||
rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
||||
#endif
|
||||
/*
|
||||
* File : writespeed.c
|
||||
* This file is part of RT-TestCase in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-02-10 Bernard first version
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <dfs_posix.h>
|
||||
|
||||
void writespeed(const char* filename, int total_length, int block_size)
|
||||
{
|
||||
int fd, index, length;
|
||||
char *buff_ptr;
|
||||
rt_tick_t tick;
|
||||
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file:%s failed\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
buff_ptr = rt_malloc(block_size);
|
||||
if (buff_ptr == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory\n");
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* prepare write data */
|
||||
for (index = 0; index < block_size; index++)
|
||||
{
|
||||
buff_ptr[index] = index;
|
||||
}
|
||||
index = 0;
|
||||
|
||||
/* get the beginning tick */
|
||||
tick = rt_tick_get();
|
||||
while (index < total_length / block_size)
|
||||
{
|
||||
length = write(fd, buff_ptr, block_size);
|
||||
if (length != block_size)
|
||||
{
|
||||
rt_kprintf("write failed\n");
|
||||
break;
|
||||
}
|
||||
|
||||
index ++;
|
||||
}
|
||||
tick = rt_tick_get() - tick;
|
||||
|
||||
/* close file and release memory */
|
||||
close(fd);
|
||||
rt_free(buff_ptr);
|
||||
|
||||
/* calculate write speed */
|
||||
rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
|
||||
#endif
|
||||
|
||||
+39
-39
@@ -1,39 +1,39 @@
|
||||
from building import *
|
||||
|
||||
src = Split("""
|
||||
tc_comm.c
|
||||
thread_static.c
|
||||
thread_dynamic.c
|
||||
thread_priority.c
|
||||
thread_same_priority.c
|
||||
thread_static_simple.c
|
||||
thread_dynamic_simple.c
|
||||
thread_delete.c
|
||||
thread_detach.c
|
||||
thread_yield.c
|
||||
thread_suspend.c
|
||||
thread_resume.c
|
||||
semaphore_static.c
|
||||
semaphore_dynamic.c
|
||||
semaphore_priority.c
|
||||
semaphore_buffer_worker.c
|
||||
semaphore_producer_consumer.c
|
||||
mutex_simple.c
|
||||
event_simple.c
|
||||
mbox_simple.c
|
||||
mbox_send_wait.c
|
||||
messageq_simple.c
|
||||
timer_static.c
|
||||
timer_dynamic.c
|
||||
timer_stop_self.c
|
||||
timer_control.c
|
||||
timer_timeout.c
|
||||
heap_malloc.c
|
||||
heap_realloc.c
|
||||
memp_simple.c
|
||||
tc_sample.c
|
||||
""")
|
||||
|
||||
group = DefineGroup('examples', src, depend = ['RT_USING_TC'])
|
||||
|
||||
Return('group')
|
||||
from building import *
|
||||
|
||||
src = Split("""
|
||||
tc_comm.c
|
||||
thread_static.c
|
||||
thread_dynamic.c
|
||||
thread_priority.c
|
||||
thread_same_priority.c
|
||||
thread_static_simple.c
|
||||
thread_dynamic_simple.c
|
||||
thread_delete.c
|
||||
thread_detach.c
|
||||
thread_yield.c
|
||||
thread_suspend.c
|
||||
thread_resume.c
|
||||
semaphore_static.c
|
||||
semaphore_dynamic.c
|
||||
semaphore_priority.c
|
||||
semaphore_buffer_worker.c
|
||||
semaphore_producer_consumer.c
|
||||
mutex_simple.c
|
||||
event_simple.c
|
||||
mbox_simple.c
|
||||
mbox_send_wait.c
|
||||
messageq_simple.c
|
||||
timer_static.c
|
||||
timer_dynamic.c
|
||||
timer_stop_self.c
|
||||
timer_control.c
|
||||
timer_timeout.c
|
||||
heap_malloc.c
|
||||
heap_realloc.c
|
||||
memp_simple.c
|
||||
tc_sample.c
|
||||
""")
|
||||
|
||||
group = DefineGroup('examples', src, depend = ['RT_USING_TC'])
|
||||
|
||||
Return('group')
|
||||
|
||||
@@ -1,72 +1,72 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for heap malloc
|
||||
*/
|
||||
|
||||
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
|
||||
{
|
||||
while (len)
|
||||
{
|
||||
if (*ptr != value) return RT_FALSE;
|
||||
|
||||
ptr ++;
|
||||
len --;
|
||||
}
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
static void heap_malloc_init()
|
||||
{
|
||||
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
|
||||
|
||||
ptr1 = rt_malloc(1);
|
||||
ptr2 = rt_malloc(13);
|
||||
ptr3 = rt_malloc(31);
|
||||
ptr4 = rt_malloc(127);
|
||||
ptr5 = rt_malloc(0);
|
||||
|
||||
memset(ptr1, 1, 1);
|
||||
memset(ptr2, 2, 13);
|
||||
memset(ptr3, 3, 31);
|
||||
memset(ptr4, 4, 127);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
|
||||
|
||||
rt_free(ptr4);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr1);
|
||||
|
||||
if (ptr5 != RT_NULL)
|
||||
{
|
||||
rt_free(ptr5);
|
||||
}
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
_failed:
|
||||
tc_done(TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_heap_malloc()
|
||||
{
|
||||
heap_malloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
heap_malloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for heap malloc
|
||||
*/
|
||||
|
||||
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
|
||||
{
|
||||
while (len)
|
||||
{
|
||||
if (*ptr != value) return RT_FALSE;
|
||||
|
||||
ptr ++;
|
||||
len --;
|
||||
}
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
static void heap_malloc_init()
|
||||
{
|
||||
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
|
||||
|
||||
ptr1 = rt_malloc(1);
|
||||
ptr2 = rt_malloc(13);
|
||||
ptr3 = rt_malloc(31);
|
||||
ptr4 = rt_malloc(127);
|
||||
ptr5 = rt_malloc(0);
|
||||
|
||||
memset(ptr1, 1, 1);
|
||||
memset(ptr2, 2, 13);
|
||||
memset(ptr3, 3, 31);
|
||||
memset(ptr4, 4, 127);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
|
||||
|
||||
rt_free(ptr4);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr1);
|
||||
|
||||
if (ptr5 != RT_NULL)
|
||||
{
|
||||
rt_free(ptr5);
|
||||
}
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
_failed:
|
||||
tc_done(TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_heap_malloc()
|
||||
{
|
||||
heap_malloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
heap_malloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,83 +1,83 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for heap malloc
|
||||
*/
|
||||
|
||||
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
|
||||
{
|
||||
while (len)
|
||||
{
|
||||
if (*ptr != value) return RT_FALSE;
|
||||
|
||||
ptr ++;
|
||||
len --;
|
||||
}
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
static void heap_realloc_init()
|
||||
{
|
||||
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
|
||||
|
||||
ptr1 = rt_malloc(1);
|
||||
ptr2 = rt_malloc(13);
|
||||
ptr3 = rt_malloc(31);
|
||||
ptr4 = rt_malloc(127);
|
||||
ptr5 = rt_malloc(0);
|
||||
|
||||
memset(ptr1, 1, 1);
|
||||
memset(ptr2, 2, 13);
|
||||
memset(ptr3, 3, 31);
|
||||
memset(ptr4, 4, 127);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
|
||||
|
||||
ptr1 = rt_realloc(ptr1, 13);
|
||||
ptr2 = rt_realloc(ptr2, 31);
|
||||
ptr3 = rt_realloc(ptr3, 127);
|
||||
ptr4 = rt_realloc(ptr4, 1);
|
||||
ptr5 = rt_realloc(ptr5, 0);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 1) != RT_FALSE) goto _failed;
|
||||
|
||||
rt_free(ptr4);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr1);
|
||||
|
||||
if (ptr5 != RT_NULL)
|
||||
{
|
||||
rt_free(ptr5);
|
||||
}
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
_failed:
|
||||
tc_done(TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_heap_realloc()
|
||||
{
|
||||
heap_realloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
heap_realloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for heap malloc
|
||||
*/
|
||||
|
||||
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
|
||||
{
|
||||
while (len)
|
||||
{
|
||||
if (*ptr != value) return RT_FALSE;
|
||||
|
||||
ptr ++;
|
||||
len --;
|
||||
}
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
static void heap_realloc_init()
|
||||
{
|
||||
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
|
||||
|
||||
ptr1 = rt_malloc(1);
|
||||
ptr2 = rt_malloc(13);
|
||||
ptr3 = rt_malloc(31);
|
||||
ptr4 = rt_malloc(127);
|
||||
ptr5 = rt_malloc(0);
|
||||
|
||||
memset(ptr1, 1, 1);
|
||||
memset(ptr2, 2, 13);
|
||||
memset(ptr3, 3, 31);
|
||||
memset(ptr4, 4, 127);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
|
||||
|
||||
ptr1 = rt_realloc(ptr1, 13);
|
||||
ptr2 = rt_realloc(ptr2, 31);
|
||||
ptr3 = rt_realloc(ptr3, 127);
|
||||
ptr4 = rt_realloc(ptr4, 1);
|
||||
ptr5 = rt_realloc(ptr5, 0);
|
||||
|
||||
if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
|
||||
if (mem_check(ptr4, 4, 1) != RT_FALSE) goto _failed;
|
||||
|
||||
rt_free(ptr4);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr3);
|
||||
rt_free(ptr1);
|
||||
|
||||
if (ptr5 != RT_NULL)
|
||||
{
|
||||
rt_free(ptr5);
|
||||
}
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
_failed:
|
||||
tc_done(TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_heap_realloc()
|
||||
{
|
||||
heap_realloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
heap_realloc_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+163
-163
@@ -1,163 +1,163 @@
|
||||
/*
|
||||
* 程序清单:
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
static rt_thread_t tid3 = RT_NULL;
|
||||
static rt_mutex_t mutex = RT_NULL;
|
||||
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
/* 先让低优先级线程运行 */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 此时thread3持有mutex,并且thread2等待持有mutex */
|
||||
|
||||
/* 检查thread2与thread3的优先级情况 */
|
||||
if (tid2->current_priority != tid3->current_priority)
|
||||
{
|
||||
/* 优先级不相同,测试失败 */
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* 先让低优先级线程运行 */
|
||||
rt_thread_delay(5);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/*
|
||||
* 试图持有互斥锁,此时thread3持有,应把thread3的优先级提升到thread2相同
|
||||
* 的优先级
|
||||
*/
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* 释放互斥锁 */
|
||||
rt_mutex_release(mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程3入口 */
|
||||
static void thread3_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
/* 做一个长时间的循环,总共50个OS Tick */
|
||||
tick = rt_tick_get();
|
||||
while (rt_tick_get() - tick < 50) ;
|
||||
|
||||
rt_mutex_release(mutex);
|
||||
rt_mutex_release(mutex);
|
||||
}
|
||||
}
|
||||
|
||||
int mutex_simple_init()
|
||||
{
|
||||
/* 创建互斥锁 */
|
||||
mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
|
||||
if (mutex == RT_NULL)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 创建线程1 */
|
||||
tid1 = rt_thread_create("t1",
|
||||
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, 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, THREAD_TIMESLICE);
|
||||
if (tid2 != RT_NULL)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
/* 创建线程3 */
|
||||
tid3 = rt_thread_create("t3",
|
||||
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
if (tid3 != RT_NULL)
|
||||
rt_thread_startup(tid3);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid3);
|
||||
|
||||
if (mutex != RT_NULL)
|
||||
{
|
||||
rt_mutex_delete(mutex);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_mutex_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
mutex_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
mutex_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
static rt_thread_t tid3 = RT_NULL;
|
||||
static rt_mutex_t mutex = RT_NULL;
|
||||
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
/* 先让低优先级线程运行 */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 此时thread3持有mutex,并且thread2等待持有mutex */
|
||||
|
||||
/* 检查thread2与thread3的优先级情况 */
|
||||
if (tid2->current_priority != tid3->current_priority)
|
||||
{
|
||||
/* 优先级不相同,测试失败 */
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* 先让低优先级线程运行 */
|
||||
rt_thread_delay(5);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/*
|
||||
* 试图持有互斥锁,此时thread3持有,应把thread3的优先级提升到thread2相同
|
||||
* 的优先级
|
||||
*/
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* 释放互斥锁 */
|
||||
rt_mutex_release(mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程3入口 */
|
||||
static void thread3_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
}
|
||||
|
||||
/* 做一个长时间的循环,总共50个OS Tick */
|
||||
tick = rt_tick_get();
|
||||
while (rt_tick_get() - tick < 50) ;
|
||||
|
||||
rt_mutex_release(mutex);
|
||||
rt_mutex_release(mutex);
|
||||
}
|
||||
}
|
||||
|
||||
int mutex_simple_init()
|
||||
{
|
||||
/* 创建互斥锁 */
|
||||
mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
|
||||
if (mutex == RT_NULL)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 创建线程1 */
|
||||
tid1 = rt_thread_create("t1",
|
||||
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, 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, THREAD_TIMESLICE);
|
||||
if (tid2 != RT_NULL)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
/* 创建线程3 */
|
||||
tid3 = rt_thread_create("t3",
|
||||
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
if (tid3 != RT_NULL)
|
||||
rt_thread_startup(tid3);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid3);
|
||||
|
||||
if (mutex != RT_NULL)
|
||||
{
|
||||
rt_mutex_delete(mutex);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_mutex_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
mutex_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
mutex_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+128
-128
@@ -1,128 +1,128 @@
|
||||
/*
|
||||
* 程序清单:动态信号量
|
||||
*
|
||||
* 这个例子中将创建一个动态信号量(初始值为0 )及一个动态线程,在这个动态线程中
|
||||
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
||||
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid = RT_NULL;
|
||||
/* 指向信号量的指针 */
|
||||
static rt_sem_t sem = RT_NULL;
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_tick_t tick;
|
||||
|
||||
/* 获得当前的OS Tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
/* 试图持有一个信号量,如果10个OS Tick依然没拿到,则超时返回 */
|
||||
result = rt_sem_take(sem, 10);
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
/* 判断是否刚好过去10个OS Tick */
|
||||
if (rt_tick_get() - tick != 10)
|
||||
{
|
||||
/* 如果失败,则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
rt_kprintf("take semaphore timeout\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 因为并没释放信号量,应该是超时返回,否则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 释放一次信号量 */
|
||||
rt_sem_release(sem);
|
||||
|
||||
/* 继续持有信号量,并永远等待直到持有到信号量 */
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* 返回不正确,测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试成功 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
/* 删除信号量 */
|
||||
rt_sem_delete(sem);
|
||||
}
|
||||
|
||||
int semaphore_dynamic_init()
|
||||
{
|
||||
/* 创建一个信号量,初始值是0 */
|
||||
sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO);
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 创建线程 */
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
||||
{
|
||||
rt_thread_delete(tid);
|
||||
|
||||
/* 删除信号量 */
|
||||
rt_sem_delete(sem);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_dynamic()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_dynamic_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_dynamic, a dynamic semaphore example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_dynamic_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:动态信号量
|
||||
*
|
||||
* 这个例子中将创建一个动态信号量(初始值为0 )及一个动态线程,在这个动态线程中
|
||||
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
||||
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid = RT_NULL;
|
||||
/* 指向信号量的指针 */
|
||||
static rt_sem_t sem = RT_NULL;
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_tick_t tick;
|
||||
|
||||
/* 获得当前的OS Tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
/* 试图持有一个信号量,如果10个OS Tick依然没拿到,则超时返回 */
|
||||
result = rt_sem_take(sem, 10);
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
/* 判断是否刚好过去10个OS Tick */
|
||||
if (rt_tick_get() - tick != 10)
|
||||
{
|
||||
/* 如果失败,则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
rt_kprintf("take semaphore timeout\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 因为并没释放信号量,应该是超时返回,否则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 释放一次信号量 */
|
||||
rt_sem_release(sem);
|
||||
|
||||
/* 继续持有信号量,并永远等待直到持有到信号量 */
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* 返回不正确,测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_delete(sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试成功 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
/* 删除信号量 */
|
||||
rt_sem_delete(sem);
|
||||
}
|
||||
|
||||
int semaphore_dynamic_init()
|
||||
{
|
||||
/* 创建一个信号量,初始值是0 */
|
||||
sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO);
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 创建线程 */
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
||||
{
|
||||
rt_thread_delete(tid);
|
||||
|
||||
/* 删除信号量 */
|
||||
rt_sem_delete(sem);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_dynamic()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_dynamic_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_dynamic, a dynamic semaphore example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_dynamic_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static rt_sem_t sem;
|
||||
static rt_uint8_t t1_count, t2_count;
|
||||
static rt_thread_t t1, t2, worker;
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
t1_count ++;
|
||||
rt_kprintf("thread1: got semaphore, count: %d\n", t1_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
t2_count ++;
|
||||
rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void worker_thread_entry(void* parameter)
|
||||
{
|
||||
rt_thread_delay(10);
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_sem_release(sem);
|
||||
rt_thread_delay(5);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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_done(TC_STAT_FAILED);
|
||||
else
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
int _tc_semaphore_priority()
|
||||
{
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_priority_init();
|
||||
|
||||
return 50;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static rt_sem_t sem;
|
||||
static rt_uint8_t t1_count, t2_count;
|
||||
static rt_thread_t t1, t2, worker;
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
t1_count ++;
|
||||
rt_kprintf("thread1: got semaphore, count: %d\n", t1_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
while (1)
|
||||
{
|
||||
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
t2_count ++;
|
||||
rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void worker_thread_entry(void* parameter)
|
||||
{
|
||||
rt_thread_delay(10);
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_sem_release(sem);
|
||||
rt_thread_delay(5);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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_done(TC_STAT_FAILED);
|
||||
else
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
}
|
||||
|
||||
int _tc_semaphore_priority()
|
||||
{
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_priority_init();
|
||||
|
||||
return 50;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,154 +1,154 @@
|
||||
/*
|
||||
* 程序清单:生产者消费者例子
|
||||
*
|
||||
* 这个例子中将创建两个线程用于实现生产者消费者问题
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定义最大5个元素能够被产生 */
|
||||
#define MAXSEM 5
|
||||
|
||||
/* 用于放置生产的整数数组 */
|
||||
rt_uint32_t array[MAXSEM];
|
||||
/* 指向生产者、消费者在array数组中的读写位置 */
|
||||
static rt_uint32_t set, get;
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t producer_tid = RT_NULL;
|
||||
static rt_thread_t consumer_tid = RT_NULL;
|
||||
|
||||
struct rt_semaphore sem_lock;
|
||||
struct rt_semaphore sem_empty, sem_full;
|
||||
|
||||
/* 生成者线程入口 */
|
||||
void producer_thread_entry(void* parameter)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
/* 运行100次 */
|
||||
while( cnt < 100)
|
||||
{
|
||||
/* 获取一个空位 */
|
||||
rt_sem_take(&sem_empty, RT_WAITING_FOREVER);
|
||||
|
||||
/* 修改array内容,上锁 */
|
||||
rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
|
||||
array[set%MAXSEM] = cnt + 1;
|
||||
rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]);
|
||||
set++;
|
||||
rt_sem_release(&sem_lock);
|
||||
|
||||
/* 发布一个满位 */
|
||||
rt_sem_release(&sem_full);
|
||||
cnt++;
|
||||
|
||||
/* 暂停一段时间 */
|
||||
rt_thread_delay(50);
|
||||
}
|
||||
|
||||
rt_kprintf("the producer exit!\n");
|
||||
}
|
||||
|
||||
/* 消费者线程入口 */
|
||||
void consumer_thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t no;
|
||||
rt_uint32_t sum;
|
||||
|
||||
/* 第n个线程,由入口参数传进来 */
|
||||
no = (rt_uint32_t)parameter;
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* 获取一个满位 */
|
||||
rt_sem_take(&sem_full, RT_WAITING_FOREVER);
|
||||
|
||||
/* 临界区,上锁进行操作 */
|
||||
rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
|
||||
sum += array[get%MAXSEM];
|
||||
rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] );
|
||||
get++;
|
||||
rt_sem_release(&sem_lock);
|
||||
|
||||
/* 释放一个空位 */
|
||||
rt_sem_release(&sem_empty);
|
||||
|
||||
/* 生产者生产到100个数目,停止,消费者线程相应停止 */
|
||||
if (get == 100) break;
|
||||
|
||||
/* 暂停一小会时间 */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
|
||||
rt_kprintf("the consumer[%d] sum is %d \n ", no, sum);
|
||||
rt_kprintf("the consumer[%d] exit!\n");
|
||||
}
|
||||
|
||||
int semaphore_producer_consumer_init()
|
||||
{
|
||||
/* 初始化3个信号量 */
|
||||
rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO);
|
||||
rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO);
|
||||
rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* 创建线程1 */
|
||||
producer_tid = rt_thread_create("producer",
|
||||
producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
if (producer_tid != RT_NULL)
|
||||
rt_thread_startup(producer_tid);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
/* 创建线程2 */
|
||||
consumer_tid = rt_thread_create("consumer",
|
||||
consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
if (consumer_tid != RT_NULL)
|
||||
rt_thread_startup(consumer_tid);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(producer_tid);
|
||||
if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(consumer_tid);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_producer_consumer()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_producer_consumer_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_producer_consumer_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:生产者消费者例子
|
||||
*
|
||||
* 这个例子中将创建两个线程用于实现生产者消费者问题
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定义最大5个元素能够被产生 */
|
||||
#define MAXSEM 5
|
||||
|
||||
/* 用于放置生产的整数数组 */
|
||||
rt_uint32_t array[MAXSEM];
|
||||
/* 指向生产者、消费者在array数组中的读写位置 */
|
||||
static rt_uint32_t set, get;
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t producer_tid = RT_NULL;
|
||||
static rt_thread_t consumer_tid = RT_NULL;
|
||||
|
||||
struct rt_semaphore sem_lock;
|
||||
struct rt_semaphore sem_empty, sem_full;
|
||||
|
||||
/* 生成者线程入口 */
|
||||
void producer_thread_entry(void* parameter)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
/* 运行100次 */
|
||||
while( cnt < 100)
|
||||
{
|
||||
/* 获取一个空位 */
|
||||
rt_sem_take(&sem_empty, RT_WAITING_FOREVER);
|
||||
|
||||
/* 修改array内容,上锁 */
|
||||
rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
|
||||
array[set%MAXSEM] = cnt + 1;
|
||||
rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]);
|
||||
set++;
|
||||
rt_sem_release(&sem_lock);
|
||||
|
||||
/* 发布一个满位 */
|
||||
rt_sem_release(&sem_full);
|
||||
cnt++;
|
||||
|
||||
/* 暂停一段时间 */
|
||||
rt_thread_delay(50);
|
||||
}
|
||||
|
||||
rt_kprintf("the producer exit!\n");
|
||||
}
|
||||
|
||||
/* 消费者线程入口 */
|
||||
void consumer_thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t no;
|
||||
rt_uint32_t sum;
|
||||
|
||||
/* 第n个线程,由入口参数传进来 */
|
||||
no = (rt_uint32_t)parameter;
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* 获取一个满位 */
|
||||
rt_sem_take(&sem_full, RT_WAITING_FOREVER);
|
||||
|
||||
/* 临界区,上锁进行操作 */
|
||||
rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
|
||||
sum += array[get%MAXSEM];
|
||||
rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] );
|
||||
get++;
|
||||
rt_sem_release(&sem_lock);
|
||||
|
||||
/* 释放一个空位 */
|
||||
rt_sem_release(&sem_empty);
|
||||
|
||||
/* 生产者生产到100个数目,停止,消费者线程相应停止 */
|
||||
if (get == 100) break;
|
||||
|
||||
/* 暂停一小会时间 */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
|
||||
rt_kprintf("the consumer[%d] sum is %d \n ", no, sum);
|
||||
rt_kprintf("the consumer[%d] exit!\n");
|
||||
}
|
||||
|
||||
int semaphore_producer_consumer_init()
|
||||
{
|
||||
/* 初始化3个信号量 */
|
||||
rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO);
|
||||
rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO);
|
||||
rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* 创建线程1 */
|
||||
producer_tid = rt_thread_create("producer",
|
||||
producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
if (producer_tid != RT_NULL)
|
||||
rt_thread_startup(producer_tid);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
/* 创建线程2 */
|
||||
consumer_tid = rt_thread_create("consumer",
|
||||
consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */
|
||||
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
if (consumer_tid != RT_NULL)
|
||||
rt_thread_startup(consumer_tid);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(producer_tid);
|
||||
if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(consumer_tid);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_producer_consumer()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_producer_consumer_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
semaphore_producer_consumer_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+132
-132
@@ -1,132 +1,132 @@
|
||||
/*
|
||||
* 程序清单:静态信号量
|
||||
*
|
||||
* 这个例子中将创建一个静态信号量(初始值为0 )及一个静态线程,在这个静态线程中
|
||||
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
||||
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程控制块及栈 */
|
||||
static struct rt_thread thread;
|
||||
static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
|
||||
/* 信号量控制块 */
|
||||
static struct rt_semaphore sem;
|
||||
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_tick_t tick;
|
||||
|
||||
/* 获得当前的OS Tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
/* 试图持有信号量,最大等待10个OS Tick后返回 */
|
||||
result = rt_sem_take(&sem, 10);
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
/* 超时后判断是否刚好是10个OS Tick */
|
||||
if (rt_tick_get() - tick != 10)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
rt_kprintf("take semaphore timeout\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 因为没有其他地方是否信号量,所以不应该成功持有信号量,否则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 释放一次信号量 */
|
||||
rt_sem_release(&sem);
|
||||
|
||||
/* 永久等待方式持有信号量 */
|
||||
result = rt_sem_take(&sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* 不成功则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试通过 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
/* 脱离信号量对象 */
|
||||
rt_sem_detach(&sem);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread.stat != RT_THREAD_CLOSE)
|
||||
{
|
||||
rt_thread_detach(&thread);
|
||||
|
||||
/* 执行信号量对象脱离 */
|
||||
rt_sem_detach(&sem);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_static()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_static_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:静态信号量
|
||||
*
|
||||
* 这个例子中将创建一个静态信号量(初始值为0 )及一个静态线程,在这个静态线程中
|
||||
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
||||
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程控制块及栈 */
|
||||
static struct rt_thread thread;
|
||||
static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
|
||||
/* 信号量控制块 */
|
||||
static struct rt_semaphore sem;
|
||||
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_err_t result;
|
||||
rt_tick_t tick;
|
||||
|
||||
/* 获得当前的OS Tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
/* 试图持有信号量,最大等待10个OS Tick后返回 */
|
||||
result = rt_sem_take(&sem, 10);
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
/* 超时后判断是否刚好是10个OS Tick */
|
||||
if (rt_tick_get() - tick != 10)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
rt_kprintf("take semaphore timeout\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 因为没有其他地方是否信号量,所以不应该成功持有信号量,否则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 释放一次信号量 */
|
||||
rt_sem_release(&sem);
|
||||
|
||||
/* 永久等待方式持有信号量 */
|
||||
result = rt_sem_take(&sem, RT_WAITING_FOREVER);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* 不成功则测试失败 */
|
||||
tc_done(TC_STAT_FAILED);
|
||||
rt_sem_detach(&sem);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 测试通过 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
/* 脱离信号量对象 */
|
||||
rt_sem_detach(&sem);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread.stat != RT_THREAD_CLOSE)
|
||||
{
|
||||
rt_thread_detach(&thread);
|
||||
|
||||
/* 执行信号量对象脱离 */
|
||||
rt_sem_detach(&sem);
|
||||
}
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_semaphore_static()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
semaphore_static_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+176
-176
@@ -1,176 +1,176 @@
|
||||
#include "tc_comm.h"
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
#define TC_PRIORITY 25
|
||||
#define TC_STACK_SIZE 0x400
|
||||
|
||||
static rt_uint8_t _tc_stat;
|
||||
static struct rt_semaphore _tc_sem;
|
||||
static struct rt_thread _tc_thread;
|
||||
static rt_uint8_t _tc_stack[TC_STACK_SIZE];
|
||||
static char _tc_prefix[64];
|
||||
static const char* _tc_current;
|
||||
static void (*_tc_cleanup)(void) = RT_NULL;
|
||||
|
||||
static rt_uint32_t _tc_scale = 1;
|
||||
FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)
|
||||
|
||||
void tc_thread_entry(void* parameter)
|
||||
{
|
||||
struct finsh_syscall* index;
|
||||
|
||||
/* create tc semaphore */
|
||||
rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
while (_tc_stat & TC_STAT_RUNNING)
|
||||
{
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* search testcase */
|
||||
if (rt_strstr(index->name, _tc_prefix) == index->name)
|
||||
{
|
||||
long tick;
|
||||
|
||||
_tc_current = index->name + 4;
|
||||
rt_kprintf("Run TestCase: %s\n", _tc_current);
|
||||
_tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
|
||||
tick = index->func();
|
||||
if (tick > 0)
|
||||
{
|
||||
rt_sem_take(&_tc_sem, tick * _tc_scale);
|
||||
|
||||
if (_tc_cleanup != RT_NULL)
|
||||
{
|
||||
/* perform testcase cleanup */
|
||||
_tc_cleanup();
|
||||
_tc_cleanup = RT_NULL;
|
||||
}
|
||||
|
||||
rt_sem_trytake(&_tc_sem);/* by nl1031 */
|
||||
|
||||
if (_tc_stat & TC_STAT_FAILED)
|
||||
rt_kprintf("TestCase[%s] failed\n", _tc_current);
|
||||
else
|
||||
rt_kprintf("TestCase[%s] passed\n", _tc_current);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_tc_cleanup != RT_NULL)
|
||||
{
|
||||
/* perform testcase cleanup */
|
||||
_tc_cleanup();
|
||||
_tc_cleanup = RT_NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rt_kprintf("RT-Thread TestCase Running Done!\n");
|
||||
/* detach tc semaphore */
|
||||
rt_sem_detach(&_tc_sem);
|
||||
}
|
||||
|
||||
void tc_stop()
|
||||
{
|
||||
_tc_stat &= ~TC_STAT_RUNNING;
|
||||
|
||||
rt_thread_delay(RT_TICK_PER_SECOND/2);
|
||||
if (_tc_thread.stat != RT_THREAD_INIT)
|
||||
{
|
||||
/* lock scheduler */
|
||||
rt_enter_critical();
|
||||
|
||||
/* detach old tc thread */
|
||||
rt_thread_detach(&_tc_thread);
|
||||
rt_sem_detach(&_tc_sem);
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
}
|
||||
rt_thread_delay(RT_TICK_PER_SECOND/2);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);
|
||||
|
||||
void tc_done(rt_uint8_t stat)
|
||||
{
|
||||
_tc_stat |= stat;
|
||||
_tc_stat &= ~TC_STAT_RUNNING;
|
||||
|
||||
/* release semaphore */
|
||||
rt_sem_release(&_tc_sem);
|
||||
}
|
||||
|
||||
void tc_stat(rt_uint8_t stat)
|
||||
{
|
||||
if (stat & TC_STAT_FAILED)
|
||||
{
|
||||
rt_kprintf("TestCases[%s] failed\n", _tc_current);
|
||||
}
|
||||
_tc_stat |= stat;
|
||||
}
|
||||
|
||||
void tc_cleanup(void (*cleanup)())
|
||||
{
|
||||
_tc_cleanup = cleanup;
|
||||
}
|
||||
|
||||
void tc_start(const char* tc_prefix)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* tesecase prefix is null */
|
||||
if (tc_prefix == RT_NULL)
|
||||
{
|
||||
rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
|
||||
rt_kprintf("list_tc() can list all testcases.\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
/* init tc thread */
|
||||
if (_tc_stat & TC_STAT_RUNNING)
|
||||
{
|
||||
/* stop old tc thread */
|
||||
tc_stop();
|
||||
}
|
||||
|
||||
rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
|
||||
rt_snprintf(_tc_prefix, sizeof(_tc_prefix), "_tc_%s", tc_prefix);
|
||||
|
||||
result = rt_thread_init(&_tc_thread, "tc",
|
||||
tc_thread_entry, RT_NULL,
|
||||
&_tc_stack[0], sizeof(_tc_stack),
|
||||
TC_PRIORITY - 3, 5);
|
||||
|
||||
/* set tc stat */
|
||||
_tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&_tc_thread);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);
|
||||
|
||||
void list_tc()
|
||||
{
|
||||
struct finsh_syscall* index;
|
||||
|
||||
rt_kprintf("TestCases List:\n");
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* search testcase */
|
||||
if (rt_strstr(index->name, "_tc_") == index->name)
|
||||
{
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name + 4);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
|
||||
#endif
|
||||
|
||||
#include "tc_comm.h"
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
#define TC_PRIORITY 25
|
||||
#define TC_STACK_SIZE 0x400
|
||||
|
||||
static rt_uint8_t _tc_stat;
|
||||
static struct rt_semaphore _tc_sem;
|
||||
static struct rt_thread _tc_thread;
|
||||
static rt_uint8_t _tc_stack[TC_STACK_SIZE];
|
||||
static char _tc_prefix[64];
|
||||
static const char* _tc_current;
|
||||
static void (*_tc_cleanup)(void) = RT_NULL;
|
||||
|
||||
static rt_uint32_t _tc_scale = 1;
|
||||
FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)
|
||||
|
||||
void tc_thread_entry(void* parameter)
|
||||
{
|
||||
struct finsh_syscall* index;
|
||||
|
||||
/* create tc semaphore */
|
||||
rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
while (_tc_stat & TC_STAT_RUNNING)
|
||||
{
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* search testcase */
|
||||
if (rt_strstr(index->name, _tc_prefix) == index->name)
|
||||
{
|
||||
long tick;
|
||||
|
||||
_tc_current = index->name + 4;
|
||||
rt_kprintf("Run TestCase: %s\n", _tc_current);
|
||||
_tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
|
||||
tick = index->func();
|
||||
if (tick > 0)
|
||||
{
|
||||
rt_sem_take(&_tc_sem, tick * _tc_scale);
|
||||
|
||||
if (_tc_cleanup != RT_NULL)
|
||||
{
|
||||
/* perform testcase cleanup */
|
||||
_tc_cleanup();
|
||||
_tc_cleanup = RT_NULL;
|
||||
}
|
||||
|
||||
rt_sem_trytake(&_tc_sem);/* by nl1031 */
|
||||
|
||||
if (_tc_stat & TC_STAT_FAILED)
|
||||
rt_kprintf("TestCase[%s] failed\n", _tc_current);
|
||||
else
|
||||
rt_kprintf("TestCase[%s] passed\n", _tc_current);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_tc_cleanup != RT_NULL)
|
||||
{
|
||||
/* perform testcase cleanup */
|
||||
_tc_cleanup();
|
||||
_tc_cleanup = RT_NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rt_kprintf("RT-Thread TestCase Running Done!\n");
|
||||
/* detach tc semaphore */
|
||||
rt_sem_detach(&_tc_sem);
|
||||
}
|
||||
|
||||
void tc_stop()
|
||||
{
|
||||
_tc_stat &= ~TC_STAT_RUNNING;
|
||||
|
||||
rt_thread_delay(RT_TICK_PER_SECOND/2);
|
||||
if (_tc_thread.stat != RT_THREAD_INIT)
|
||||
{
|
||||
/* lock scheduler */
|
||||
rt_enter_critical();
|
||||
|
||||
/* detach old tc thread */
|
||||
rt_thread_detach(&_tc_thread);
|
||||
rt_sem_detach(&_tc_sem);
|
||||
|
||||
/* unlock scheduler */
|
||||
rt_exit_critical();
|
||||
}
|
||||
rt_thread_delay(RT_TICK_PER_SECOND/2);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);
|
||||
|
||||
void tc_done(rt_uint8_t stat)
|
||||
{
|
||||
_tc_stat |= stat;
|
||||
_tc_stat &= ~TC_STAT_RUNNING;
|
||||
|
||||
/* release semaphore */
|
||||
rt_sem_release(&_tc_sem);
|
||||
}
|
||||
|
||||
void tc_stat(rt_uint8_t stat)
|
||||
{
|
||||
if (stat & TC_STAT_FAILED)
|
||||
{
|
||||
rt_kprintf("TestCases[%s] failed\n", _tc_current);
|
||||
}
|
||||
_tc_stat |= stat;
|
||||
}
|
||||
|
||||
void tc_cleanup(void (*cleanup)())
|
||||
{
|
||||
_tc_cleanup = cleanup;
|
||||
}
|
||||
|
||||
void tc_start(const char* tc_prefix)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* tesecase prefix is null */
|
||||
if (tc_prefix == RT_NULL)
|
||||
{
|
||||
rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
|
||||
rt_kprintf("list_tc() can list all testcases.\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
/* init tc thread */
|
||||
if (_tc_stat & TC_STAT_RUNNING)
|
||||
{
|
||||
/* stop old tc thread */
|
||||
tc_stop();
|
||||
}
|
||||
|
||||
rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
|
||||
rt_snprintf(_tc_prefix, sizeof(_tc_prefix), "_tc_%s", tc_prefix);
|
||||
|
||||
result = rt_thread_init(&_tc_thread, "tc",
|
||||
tc_thread_entry, RT_NULL,
|
||||
&_tc_stack[0], sizeof(_tc_stack),
|
||||
TC_PRIORITY - 3, 5);
|
||||
|
||||
/* set tc stat */
|
||||
_tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&_tc_thread);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);
|
||||
|
||||
void list_tc()
|
||||
{
|
||||
struct finsh_syscall* index;
|
||||
|
||||
rt_kprintf("TestCases List:\n");
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* search testcase */
|
||||
if (rt_strstr(index->name, "_tc_") == index->name)
|
||||
{
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
|
||||
#else
|
||||
rt_kprintf("%s\n", index->name + 4);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
|
||||
#endif
|
||||
|
||||
|
||||
+43
-43
@@ -1,43 +1,43 @@
|
||||
#ifndef __TC_COMM_H__
|
||||
#define __TC_COMM_H__
|
||||
|
||||
/*
|
||||
* RT-Thread TestCase
|
||||
*
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
#endif
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX == 8
|
||||
#define THREAD_PRIORITY 6
|
||||
#elif RT_THREAD_PRIORITY_MAX == 32
|
||||
#define THREAD_PRIORITY 25
|
||||
#elif RT_THREAD_PRIORITY_MAX == 256
|
||||
#define THREAD_PRIORITY 200
|
||||
#endif
|
||||
#define THREAD_STACK_SIZE 512
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
#define TC_STAT_END 0x00
|
||||
#define TC_STAT_RUNNING 0x01
|
||||
#define TC_STAT_FAILED 0x10
|
||||
#define TC_STAT_PASSED 0x00
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
void tc_start(const char* tc_prefix);
|
||||
void tc_stop(void);
|
||||
void tc_done(rt_uint8_t state);
|
||||
void tc_stat(rt_uint8_t state);
|
||||
void tc_cleanup(void (*cleanup)(void));
|
||||
#else
|
||||
#define tc_start(x)
|
||||
#define tc_stop()
|
||||
#define tc_done(s)
|
||||
#define tc_stat(s)
|
||||
#define tc_cleanup(c)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef __TC_COMM_H__
|
||||
#define __TC_COMM_H__
|
||||
|
||||
/*
|
||||
* RT-Thread TestCase
|
||||
*
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
#endif
|
||||
|
||||
#if RT_THREAD_PRIORITY_MAX == 8
|
||||
#define THREAD_PRIORITY 6
|
||||
#elif RT_THREAD_PRIORITY_MAX == 32
|
||||
#define THREAD_PRIORITY 25
|
||||
#elif RT_THREAD_PRIORITY_MAX == 256
|
||||
#define THREAD_PRIORITY 200
|
||||
#endif
|
||||
#define THREAD_STACK_SIZE 512
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
#define TC_STAT_END 0x00
|
||||
#define TC_STAT_RUNNING 0x01
|
||||
#define TC_STAT_FAILED 0x10
|
||||
#define TC_STAT_PASSED 0x00
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
void tc_start(const char* tc_prefix);
|
||||
void tc_stop(void);
|
||||
void tc_done(rt_uint8_t state);
|
||||
void tc_stat(rt_uint8_t state);
|
||||
void tc_cleanup(void (*cleanup)(void));
|
||||
#else
|
||||
#define tc_start(x)
|
||||
#define tc_stop()
|
||||
#define tc_done(s)
|
||||
#define tc_stat(s)
|
||||
#define tc_cleanup(c)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for delay thread
|
||||
*/
|
||||
static struct rt_thread thread;
|
||||
static char thread_stack[THREAD_STACK_SIZE];
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
rt_kprintf("thread inited ok\n");
|
||||
|
||||
rt_kprintf("thread delay 10 tick\n");
|
||||
tick = rt_tick_get();
|
||||
rt_thread_delay(10);
|
||||
if (rt_tick_get() - tick > 10)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("thread delay 15 tick\n");
|
||||
tick = rt_tick_get();
|
||||
rt_thread_delay(15);
|
||||
if (rt_tick_get() - tick > 15)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_delay()
|
||||
{
|
||||
thread_delay_init();
|
||||
|
||||
return 30;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_delay, a thread delay test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_delay_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for delay thread
|
||||
*/
|
||||
static struct rt_thread thread;
|
||||
static char thread_stack[THREAD_STACK_SIZE];
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
rt_kprintf("thread inited ok\n");
|
||||
|
||||
rt_kprintf("thread delay 10 tick\n");
|
||||
tick = rt_tick_get();
|
||||
rt_thread_delay(10);
|
||||
if (rt_tick_get() - tick > 10)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("thread delay 15 tick\n");
|
||||
tick = rt_tick_get();
|
||||
rt_thread_delay(15);
|
||||
if (rt_tick_get() - tick > 15)
|
||||
{
|
||||
tc_done(TC_STAT_FAILED);
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_delay()
|
||||
{
|
||||
thread_delay_init();
|
||||
|
||||
return 30;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_delay, a thread delay test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_delay_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+144
-144
@@ -1,144 +1,144 @@
|
||||
/*
|
||||
* 程序清单:删除线程
|
||||
*
|
||||
* 这个例子会创建两个线程,在一个线程中删除另外一个线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* 线程删除(rt_thread_delete)函数仅适合于动态线程,为了在一个线程
|
||||
* 中访问另一个线程的控制块,所以把线程块指针声明成全局类型以供全
|
||||
* 局访问
|
||||
*/
|
||||
static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
|
||||
/* 线程1的入口函数 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
// rt_kprintf("thread count: %d\n", count ++);
|
||||
count ++;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/* 线程2的入口函数 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
|
||||
/* 线程2启动后先睡眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2唤醒后直接删除线程1,删除线程1后,线程1自动脱离就绪线程
|
||||
* 队列
|
||||
*/
|
||||
rt_thread_delete(tid1);
|
||||
|
||||
/*
|
||||
* 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程
|
||||
* idle线程将执行真正的线程1控制块和线程栈的删除
|
||||
*/
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* 线程删除示例的初始化 */
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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 _tc_thread_delete()
|
||||
{
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_delete_init();
|
||||
|
||||
return 27;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_delete, a thread delete example);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_delete_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:删除线程
|
||||
*
|
||||
* 这个例子会创建两个线程,在一个线程中删除另外一个线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* 线程删除(rt_thread_delete)函数仅适合于动态线程,为了在一个线程
|
||||
* 中访问另一个线程的控制块,所以把线程块指针声明成全局类型以供全
|
||||
* 局访问
|
||||
*/
|
||||
static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
|
||||
/* 线程1的入口函数 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
// rt_kprintf("thread count: %d\n", count ++);
|
||||
count ++;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/* 线程2的入口函数 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
|
||||
/* 线程2启动后先睡眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2唤醒后直接删除线程1,删除线程1后,线程1自动脱离就绪线程
|
||||
* 队列
|
||||
*/
|
||||
rt_thread_delete(tid1);
|
||||
|
||||
/*
|
||||
* 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程
|
||||
* idle线程将执行真正的线程1控制块和线程栈的删除
|
||||
*/
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* 线程删除示例的初始化 */
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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 _tc_thread_delete()
|
||||
{
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_delete_init();
|
||||
|
||||
return 27;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_delete, a thread delete example);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_delete_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+118
-118
@@ -1,118 +1,118 @@
|
||||
/*
|
||||
* 程序清单:线程脱离
|
||||
*
|
||||
* 这个例子会创建两个线程,在其中一个线程中执行对另一个线程的脱离。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程1控制块 */
|
||||
static struct rt_thread thread1;
|
||||
/* 线程1栈 */
|
||||
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
|
||||
/* 线程2控制块 */
|
||||
static struct rt_thread thread2;
|
||||
/* 线程2栈 */
|
||||
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
rt_kprintf("thread count: %d\n", count ++);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
|
||||
/* 线程2启动后先睡眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除
|
||||
*/
|
||||
rt_thread_detach(&thread1);
|
||||
|
||||
/*
|
||||
* 线程2继续休眠10个OS Tick然后退出
|
||||
*/
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列
|
||||
*/
|
||||
}
|
||||
|
||||
int thread_detach_init()
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* 初始化线程1 */
|
||||
result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */
|
||||
thread1_entry, RT_NULL, /* 线程的入口是thread1_entry,入口参数是RT_NULL*/
|
||||
&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 */
|
||||
thread2_entry, RT_NULL, /* 线程的入口是thread2_entry,入口参数是RT_NULL*/
|
||||
&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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread1.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread1);
|
||||
if (thread2.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_detach()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_detach_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 25;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_detach_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:线程脱离
|
||||
*
|
||||
* 这个例子会创建两个线程,在其中一个线程中执行对另一个线程的脱离。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程1控制块 */
|
||||
static struct rt_thread thread1;
|
||||
/* 线程1栈 */
|
||||
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
|
||||
/* 线程2控制块 */
|
||||
static struct rt_thread thread2;
|
||||
/* 线程2栈 */
|
||||
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
rt_kprintf("thread count: %d\n", count ++);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
|
||||
/* 线程2启动后先睡眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除
|
||||
*/
|
||||
rt_thread_detach(&thread1);
|
||||
|
||||
/*
|
||||
* 线程2继续休眠10个OS Tick然后退出
|
||||
*/
|
||||
rt_thread_delay(10);
|
||||
|
||||
/*
|
||||
* 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列
|
||||
*/
|
||||
}
|
||||
|
||||
int thread_detach_init()
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
/* 初始化线程1 */
|
||||
result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */
|
||||
thread1_entry, RT_NULL, /* 线程的入口是thread1_entry,入口参数是RT_NULL*/
|
||||
&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 */
|
||||
thread2_entry, RT_NULL, /* 线程的入口是thread2_entry,入口参数是RT_NULL*/
|
||||
&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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread1.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread1);
|
||||
if (thread2.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_detach()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_detach_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 25;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_detach_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_kprintf("thread dynamicly created ok\n");
|
||||
rt_thread_delay(10);
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_dynamic()
|
||||
{
|
||||
thread_dynamic_init();
|
||||
|
||||
return 20;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_dynamic, a dynamic thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_dynamic_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_kprintf("thread dynamicly created ok\n");
|
||||
rt_thread_delay(10);
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_dynamic()
|
||||
{
|
||||
thread_dynamic_init();
|
||||
|
||||
return 20;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_dynamic, a dynamic thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_dynamic_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,89 +1,89 @@
|
||||
/*
|
||||
* 程序清单:动态线程
|
||||
*
|
||||
* 这个程序会初始化2个动态线程,它们拥有共同的入口函数,但参数不相同
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程计数值输出 */
|
||||
rt_kprintf("thread%d count: %d\n", no, count ++);
|
||||
|
||||
/* 休眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_dynamic_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_dynamic_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_dynamic_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:动态线程
|
||||
*
|
||||
* 这个程序会初始化2个动态线程,它们拥有共同的入口函数,但参数不相同
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程计数值输出 */
|
||||
rt_kprintf("thread%d count: %d\n", no, count ++);
|
||||
|
||||
/* 休眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_dynamic_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_dynamic_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_dynamic_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+105
-105
@@ -1,105 +1,105 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
struct rt_thread thread1;
|
||||
struct rt_thread thread2;
|
||||
static char thread1_stack[THREAD_STACK_SIZE];
|
||||
static char thread2_stack[THREAD_STACK_SIZE];
|
||||
static rt_uint32_t count = 0;
|
||||
|
||||
/*
|
||||
* the priority of thread1 > the priority of thread2
|
||||
*/
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
count ++;
|
||||
rt_kprintf("count = %d\n", count);
|
||||
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
|
||||
tick = rt_tick_get();
|
||||
while (1)
|
||||
{
|
||||
if (rt_tick_get() - tick >= 50)
|
||||
{
|
||||
if (count == 0)
|
||||
tc_done(TC_STAT_FAILED);
|
||||
else
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int thread_priority_init()
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
result = rt_thread_init(&thread1,
|
||||
"t1",
|
||||
thread1_entry, RT_NULL,
|
||||
&thread1_stack[0], sizeof(thread1_stack),
|
||||
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&thread1);
|
||||
else
|
||||
tc_stat(TC_STAT_FAILED);
|
||||
|
||||
rt_thread_init(&thread2,
|
||||
"t2",
|
||||
thread2_entry, RT_NULL,
|
||||
&thread2_stack[0], sizeof(thread2_stack),
|
||||
THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&thread2);
|
||||
else
|
||||
tc_stat(TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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();
|
||||
}
|
||||
int _tc_thread_priority()
|
||||
{
|
||||
count = 0;
|
||||
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_priority_init();
|
||||
|
||||
return RT_TICK_PER_SECOND;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_priority, a priority thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
struct rt_thread thread1;
|
||||
struct rt_thread thread2;
|
||||
static char thread1_stack[THREAD_STACK_SIZE];
|
||||
static char thread2_stack[THREAD_STACK_SIZE];
|
||||
static rt_uint32_t count = 0;
|
||||
|
||||
/*
|
||||
* the priority of thread1 > the priority of thread2
|
||||
*/
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
count ++;
|
||||
rt_kprintf("count = %d\n", count);
|
||||
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
|
||||
tick = rt_tick_get();
|
||||
while (1)
|
||||
{
|
||||
if (rt_tick_get() - tick >= 50)
|
||||
{
|
||||
if (count == 0)
|
||||
tc_done(TC_STAT_FAILED);
|
||||
else
|
||||
tc_done(TC_STAT_PASSED);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int thread_priority_init()
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
result = rt_thread_init(&thread1,
|
||||
"t1",
|
||||
thread1_entry, RT_NULL,
|
||||
&thread1_stack[0], sizeof(thread1_stack),
|
||||
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&thread1);
|
||||
else
|
||||
tc_stat(TC_STAT_FAILED);
|
||||
|
||||
rt_thread_init(&thread2,
|
||||
"t2",
|
||||
thread2_entry, RT_NULL,
|
||||
&thread2_stack[0], sizeof(thread2_stack),
|
||||
THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
||||
|
||||
if (result == RT_EOK)
|
||||
rt_thread_startup(&thread2);
|
||||
else
|
||||
tc_stat(TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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();
|
||||
}
|
||||
int _tc_thread_priority()
|
||||
{
|
||||
count = 0;
|
||||
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_priority_init();
|
||||
|
||||
return RT_TICK_PER_SECOND;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_priority, a priority thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
+123
-123
@@ -1,123 +1,123 @@
|
||||
/*
|
||||
* 程序清单:唤醒线程
|
||||
*
|
||||
* 这个例子中将创建两个动态线程,低优先级线程将挂起自身,然后
|
||||
* 高优先级线程将在一定时刻后唤醒低优先级线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
/* 低优先级线程1开始运行 */
|
||||
rt_kprintf("thread1 startup%d\n");
|
||||
|
||||
/* 挂起自身 */
|
||||
rt_kprintf("suspend thread self\n");
|
||||
rt_thread_suspend(tid1);
|
||||
/* 主动执行线程调度 */
|
||||
rt_schedule();
|
||||
|
||||
/* 当线程1被唤醒时 */
|
||||
rt_kprintf("thread1 resumed\n");
|
||||
}
|
||||
static void thread_cleanup(rt_thread_t tid)
|
||||
{
|
||||
if (tid == tid1)
|
||||
{
|
||||
tid1 = RT_NULL;
|
||||
}
|
||||
if (tid == tid2)
|
||||
{
|
||||
tid = RT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 唤醒线程1 */
|
||||
rt_thread_resume(tid1);
|
||||
rt_kprintf("thread2: to resume thread1\n");
|
||||
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 线程2自动退出 */
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_resume()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_resume_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 25;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_resume_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:唤醒线程
|
||||
*
|
||||
* 这个例子中将创建两个动态线程,低优先级线程将挂起自身,然后
|
||||
* 高优先级线程将在一定时刻后唤醒低优先级线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
/* 低优先级线程1开始运行 */
|
||||
rt_kprintf("thread1 startup%d\n");
|
||||
|
||||
/* 挂起自身 */
|
||||
rt_kprintf("suspend thread self\n");
|
||||
rt_thread_suspend(tid1);
|
||||
/* 主动执行线程调度 */
|
||||
rt_schedule();
|
||||
|
||||
/* 当线程1被唤醒时 */
|
||||
rt_kprintf("thread1 resumed\n");
|
||||
}
|
||||
static void thread_cleanup(rt_thread_t tid)
|
||||
{
|
||||
if (tid == tid1)
|
||||
{
|
||||
tid1 = RT_NULL;
|
||||
}
|
||||
if (tid == tid2)
|
||||
{
|
||||
tid = RT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 唤醒线程1 */
|
||||
rt_thread_resume(tid1);
|
||||
rt_kprintf("thread2: to resume thread1\n");
|
||||
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 线程2自动退出 */
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_resume()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_resume_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 25;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_resume_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,96 +1,96 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static struct rt_thread thread1;
|
||||
static struct rt_thread thread2;
|
||||
static char thread1_stack[THREAD_STACK_SIZE];
|
||||
static char thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
volatile static rt_uint32_t t1_count = 0;
|
||||
volatile static rt_uint32_t t2_count = 0;
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
t1_count ++;
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
t2_count ++;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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);
|
||||
}
|
||||
|
||||
int _tc_thread_same_priority()
|
||||
{
|
||||
t1_count = 0;
|
||||
t2_count = 0;
|
||||
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
thread_same_priority_init();
|
||||
|
||||
return 100;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_same_priority, a same priority thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_same_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
static struct rt_thread thread1;
|
||||
static struct rt_thread thread2;
|
||||
static char thread1_stack[THREAD_STACK_SIZE];
|
||||
static char thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
volatile static rt_uint32_t t1_count = 0;
|
||||
volatile static rt_uint32_t t2_count = 0;
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
t1_count ++;
|
||||
}
|
||||
}
|
||||
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
t2_count ++;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
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);
|
||||
}
|
||||
|
||||
int _tc_thread_same_priority()
|
||||
{
|
||||
t1_count = 0;
|
||||
t2_count = 0;
|
||||
|
||||
/* set tc cleanup */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
thread_same_priority_init();
|
||||
|
||||
return 100;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_same_priority, a same priority thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_same_priority_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,52 +1,52 @@
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for static thread
|
||||
*/
|
||||
static struct rt_thread thread;
|
||||
static char thread_stack[THREAD_STACK_SIZE];
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_kprintf("thread staticly inited ok\n");
|
||||
rt_thread_delay(10);
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
rt_err_t thread_static_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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_static()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 20;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_static, a static thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/*
|
||||
* This is an example for static thread
|
||||
*/
|
||||
static struct rt_thread thread;
|
||||
static char thread_stack[THREAD_STACK_SIZE];
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_kprintf("thread staticly inited ok\n");
|
||||
rt_thread_delay(10);
|
||||
rt_kprintf("thread exit\n");
|
||||
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
rt_err_t thread_static_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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
int _tc_thread_static()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 20;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_static, a static thread test);
|
||||
#else
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
/*
|
||||
* 程序清单:静态线程
|
||||
*
|
||||
* 这个程序会初始化2个静态线程,它们拥有共同的入口函数,但参数不相同
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程1控制块 */
|
||||
static struct rt_thread thread1;
|
||||
/* 线程1栈 */
|
||||
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
|
||||
/* 线程2控制块 */
|
||||
static struct rt_thread thread2;
|
||||
/* 线程2栈 */
|
||||
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程计数值输出 */
|
||||
rt_kprintf("thread%d count: %d\n", no, count ++);
|
||||
|
||||
/* 休眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread1.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread1);
|
||||
if (thread2.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_static_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_static_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:静态线程
|
||||
*
|
||||
* 这个程序会初始化2个静态线程,它们拥有共同的入口函数,但参数不相同
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 线程1控制块 */
|
||||
static struct rt_thread thread1;
|
||||
/* 线程1栈 */
|
||||
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
|
||||
/* 线程2控制块 */
|
||||
static struct rt_thread thread2;
|
||||
/* 线程2栈 */
|
||||
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
|
||||
|
||||
/* 线程入口 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程计数值输出 */
|
||||
rt_kprintf("thread%d count: %d\n", no, count ++);
|
||||
|
||||
/* 休眠10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 执行线程脱离 */
|
||||
if (thread1.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread1);
|
||||
if (thread2.stat != RT_THREAD_CLOSE)
|
||||
rt_thread_detach(&thread2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_static_simple()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_static_simple_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_static_simple_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+101
-101
@@ -1,101 +1,101 @@
|
||||
/*
|
||||
* 程序清单:挂起线程
|
||||
*
|
||||
* 这个例子中将创建两个动态线程,高优先级线程将在一定时刻后挂起低优先级线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
rt_kprintf("thread count: %d\n", count ++);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 挂起线程1 */
|
||||
rt_thread_suspend(tid1);
|
||||
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 线程2自动退出 */
|
||||
tid2 = RT_NULL;
|
||||
}
|
||||
|
||||
int thread_suspend_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)
|
||||
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)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_suspend()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_suspend_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_suspend_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:挂起线程
|
||||
*
|
||||
* 这个例子中将创建两个动态线程,高优先级线程将在一定时刻后挂起低优先级线程。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
rt_kprintf("thread count: %d\n", count ++);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 挂起线程1 */
|
||||
rt_thread_suspend(tid1);
|
||||
|
||||
/* 延时10个OS Tick */
|
||||
rt_thread_delay(10);
|
||||
|
||||
/* 线程2自动退出 */
|
||||
tid2 = RT_NULL;
|
||||
}
|
||||
|
||||
int thread_suspend_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)
|
||||
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)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_suspend()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_suspend_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_suspend_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+101
-101
@@ -1,101 +1,101 @@
|
||||
/*
|
||||
* 程序清单:
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程1的输出 */
|
||||
rt_kprintf("thread1: count = %d\n", count ++);
|
||||
|
||||
/* 执行yield后应该切换到thread2执行 */
|
||||
rt_thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程2的输出 */
|
||||
rt_kprintf("thread2: count = %d\n", count ++);
|
||||
|
||||
/* 执行yield后应该切换到thread1执行 */
|
||||
rt_thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
int thread_yield_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)
|
||||
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, THREAD_TIMESLICE);
|
||||
if (tid2 != RT_NULL)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_yield()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_yield_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 30;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_yield_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
/* 线程1入口 */
|
||||
static void thread1_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程1的输出 */
|
||||
rt_kprintf("thread1: count = %d\n", count ++);
|
||||
|
||||
/* 执行yield后应该切换到thread2执行 */
|
||||
rt_thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void* parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 打印线程2的输出 */
|
||||
rt_kprintf("thread2: count = %d\n", count ++);
|
||||
|
||||
/* 执行yield后应该切换到thread1执行 */
|
||||
rt_thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
int thread_yield_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)
|
||||
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, THREAD_TIMESLICE);
|
||||
if (tid2 != RT_NULL)
|
||||
rt_thread_startup(tid2);
|
||||
else
|
||||
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid1);
|
||||
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_thread_yield()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
thread_yield_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 30;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
thread_yield_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建1个动态周期型定时器对象,然后控制它进行定时时间长度的更改。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_uint8_t count;
|
||||
|
||||
/* 定时器超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_tick_t timeout = 50;
|
||||
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
|
||||
count ++;
|
||||
/* 停止定时器自身 */
|
||||
if (count >= 8)
|
||||
{
|
||||
/* 控制定时器然后更改超时时间长度 */
|
||||
rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_control_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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
timer1 = RT_NULL;
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_control()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
count = 0;
|
||||
timer_control_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_control, a timer control example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_control_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建1个动态周期型定时器对象,然后控制它进行定时时间长度的更改。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_uint8_t count;
|
||||
|
||||
/* 定时器超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_tick_t timeout = 50;
|
||||
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
|
||||
count ++;
|
||||
/* 停止定时器自身 */
|
||||
if (count >= 8)
|
||||
{
|
||||
/* 控制定时器然后更改超时时间长度 */
|
||||
rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_control_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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
timer1 = RT_NULL;
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_control()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
count = 0;
|
||||
timer_control_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_control, a timer control example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_control_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,91 +1,91 @@
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建两个动态定时器对象,一个是单次定时,一个是周期性的定时
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_timer_t timer2;
|
||||
|
||||
/* 定时器1超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
}
|
||||
|
||||
/* 定时器2超时函数 */
|
||||
static void timeout2(void* parameter)
|
||||
{
|
||||
rt_kprintf("one shot timer is timeout\n");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
rt_timer_delete(timer2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_create()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
timer_create_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_create, a dynamic timer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_create_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建两个动态定时器对象,一个是单次定时,一个是周期性的定时
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_timer_t timer2;
|
||||
|
||||
/* 定时器1超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
}
|
||||
|
||||
/* 定时器2超时函数 */
|
||||
static void timeout2(void* parameter)
|
||||
{
|
||||
rt_kprintf("one shot timer is timeout\n");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
rt_timer_delete(timer2);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_create()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
timer_create_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_create, a dynamic timer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_create_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,82 +1,82 @@
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建1个动态周期型定时器对象
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_uint8_t count;
|
||||
|
||||
/* 定时器超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
|
||||
count ++;
|
||||
/* 停止定时器自身 */
|
||||
if (count >= 8)
|
||||
{
|
||||
/* 停止定时器 */
|
||||
rt_timer_stop(timer1);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_stop_self_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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
timer1 = RT_NULL;
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_stop_self()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
count = 0;
|
||||
timer_stop_self_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_stop_self, a dynamic timer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_stop_self_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:动态定时器例程
|
||||
*
|
||||
* 这个例程会创建1个动态周期型定时器对象
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static rt_timer_t timer1;
|
||||
static rt_uint8_t count;
|
||||
|
||||
/* 定时器超时函数 */
|
||||
static void timeout1(void* parameter)
|
||||
{
|
||||
rt_kprintf("periodic timer is timeout\n");
|
||||
|
||||
count ++;
|
||||
/* 停止定时器自身 */
|
||||
if (count >= 8)
|
||||
{
|
||||
/* 停止定时器 */
|
||||
rt_timer_stop(timer1);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void timer_stop_self_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);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除定时器对象 */
|
||||
rt_timer_delete(timer1);
|
||||
timer1 = RT_NULL;
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_stop_self()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
|
||||
/* 执行定时器例程 */
|
||||
count = 0;
|
||||
timer_stop_self_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_stop_self, a dynamic timer example);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_stop_self_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
+122
-122
@@ -1,122 +1,122 @@
|
||||
/*
|
||||
* 程序清单:消息队列例程
|
||||
*
|
||||
* 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
||||
* 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid = RT_NULL;
|
||||
|
||||
/* 消息队列控制块 */
|
||||
static struct rt_messagequeue mq;
|
||||
/* 消息队列中用到的放置消息的内存池 */
|
||||
static char msg_pool[2048];
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static struct rt_timer timer;
|
||||
static rt_uint16_t no = 0;
|
||||
static void timer_timeout(void* parameter)
|
||||
{
|
||||
char buf[32];
|
||||
rt_uint32_t length;
|
||||
|
||||
length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
|
||||
rt_mq_send(&mq, &buf[0], length);
|
||||
}
|
||||
|
||||
/* 线程入口函数 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
char buf[64];
|
||||
rt_err_t result;
|
||||
|
||||
/* 初始化定时器 */
|
||||
rt_timer_init(&timer, "timer", /* 定时器名字是 timer1 */
|
||||
timer_timeout, /* 超时时回调的处理函数 */
|
||||
RT_NULL, /* 超时函数的入口参数 */
|
||||
1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
|
||||
RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_memset(&buf[0], 0, sizeof(buf));
|
||||
|
||||
/* 从消息队列中接收消息 */
|
||||
result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
rt_kprintf("recv msg: %s\n", buf);
|
||||
}
|
||||
else if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
rt_kprintf("recv msg timeout\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int timer_timeout_init()
|
||||
{
|
||||
/* 初始化消息队列 */
|
||||
rt_mq_init(&mq, "mqt",
|
||||
&msg_pool[0], /* 内存池指向msg_pool */
|
||||
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
|
||||
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
|
||||
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
||||
|
||||
/* 创建线程 */
|
||||
tid = rt_thread_create("t",
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid);
|
||||
|
||||
/* 执行消息队列对象脱离 */
|
||||
rt_mq_detach(&mq);
|
||||
/* 执行定时器脱离 */
|
||||
rt_timer_detach(&timer);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_timeout()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
timer_timeout_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_timeout_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* 程序清单:消息队列例程
|
||||
*
|
||||
* 这个程序会创建3个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
||||
* 息队列发送消息;一个线程会定时给消息队列发送紧急消息。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include "tc_comm.h"
|
||||
|
||||
/* 指向线程控制块的指针 */
|
||||
static rt_thread_t tid = RT_NULL;
|
||||
|
||||
/* 消息队列控制块 */
|
||||
static struct rt_messagequeue mq;
|
||||
/* 消息队列中用到的放置消息的内存池 */
|
||||
static char msg_pool[2048];
|
||||
|
||||
/* 定时器的控制块 */
|
||||
static struct rt_timer timer;
|
||||
static rt_uint16_t no = 0;
|
||||
static void timer_timeout(void* parameter)
|
||||
{
|
||||
char buf[32];
|
||||
rt_uint32_t length;
|
||||
|
||||
length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
|
||||
rt_mq_send(&mq, &buf[0], length);
|
||||
}
|
||||
|
||||
/* 线程入口函数 */
|
||||
static void thread_entry(void* parameter)
|
||||
{
|
||||
char buf[64];
|
||||
rt_err_t result;
|
||||
|
||||
/* 初始化定时器 */
|
||||
rt_timer_init(&timer, "timer", /* 定时器名字是 timer1 */
|
||||
timer_timeout, /* 超时时回调的处理函数 */
|
||||
RT_NULL, /* 超时函数的入口参数 */
|
||||
1, /* 定时长度,以OS Tick为单位,即1个OS Tick */
|
||||
RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_memset(&buf[0], 0, sizeof(buf));
|
||||
|
||||
/* 从消息队列中接收消息 */
|
||||
result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
rt_kprintf("recv msg: %s\n", buf);
|
||||
}
|
||||
else if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
rt_kprintf("recv msg timeout\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int timer_timeout_init()
|
||||
{
|
||||
/* 初始化消息队列 */
|
||||
rt_mq_init(&mq, "mqt",
|
||||
&msg_pool[0], /* 内存池指向msg_pool */
|
||||
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
|
||||
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
|
||||
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
||||
|
||||
/* 创建线程 */
|
||||
tid = rt_thread_create("t",
|
||||
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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TC
|
||||
static void _tc_cleanup()
|
||||
{
|
||||
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
||||
rt_enter_critical();
|
||||
|
||||
/* 删除线程 */
|
||||
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
||||
rt_thread_delete(tid);
|
||||
|
||||
/* 执行消息队列对象脱离 */
|
||||
rt_mq_detach(&mq);
|
||||
/* 执行定时器脱离 */
|
||||
rt_timer_detach(&timer);
|
||||
|
||||
/* 调度器解锁 */
|
||||
rt_exit_critical();
|
||||
|
||||
/* 设置TestCase状态 */
|
||||
tc_done(TC_STAT_PASSED);
|
||||
}
|
||||
|
||||
int _tc_timer_timeout()
|
||||
{
|
||||
/* 设置TestCase清理回调函数 */
|
||||
tc_cleanup(_tc_cleanup);
|
||||
timer_timeout_init();
|
||||
|
||||
/* 返回TestCase运行的最长时间 */
|
||||
return 100;
|
||||
}
|
||||
/* 输出函数命令到finsh shell中 */
|
||||
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
|
||||
#else
|
||||
/* 用户应用入口 */
|
||||
int rt_application_init()
|
||||
{
|
||||
timer_timeout_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('libc_test', src, depend = ['RT_USING_NEWLIB', 'RT_USING_PTHREADS'])
|
||||
|
||||
Return('group')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('libc_test', src, depend = ['RT_USING_NEWLIB', 'RT_USING_PTHREADS'])
|
||||
|
||||
Return('group')
|
||||
|
||||
+56
-56
@@ -1,56 +1,56 @@
|
||||
/*
|
||||
* dirent.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
#include <dirent.h>
|
||||
int libc_dirent()
|
||||
{
|
||||
DIR * dirp;
|
||||
long int save3 = 0;
|
||||
long int cur;
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
struct dirent *dp;
|
||||
|
||||
dirp = opendir("/");
|
||||
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
|
||||
{
|
||||
/* save position 3 (after fourth entry) */
|
||||
if (i++ == 3)
|
||||
save3 = telldir(dirp);
|
||||
|
||||
printf("%s\n", dp->d_name);
|
||||
|
||||
/* stop at 400 (just to make sure dirp->__offset and dirp->__size are
|
||||
scrambled */
|
||||
if (i == 400)
|
||||
break;
|
||||
}
|
||||
|
||||
printf("going back past 4-th entry...\n");
|
||||
|
||||
/* go back to saved entry */
|
||||
seekdir(dirp, save3);
|
||||
|
||||
/* Check whether telldir equals to save3 now. */
|
||||
cur = telldir(dirp);
|
||||
if (cur != save3)
|
||||
{
|
||||
printf("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
|
||||
result = 1;
|
||||
}
|
||||
|
||||
/* print remaining files (3-last) */
|
||||
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
|
||||
printf("%s\n", dp->d_name);
|
||||
|
||||
closedir(dirp);
|
||||
return result;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_dirent, dirent test for libc);
|
||||
/*
|
||||
* dirent.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
#include <dirent.h>
|
||||
int libc_dirent()
|
||||
{
|
||||
DIR * dirp;
|
||||
long int save3 = 0;
|
||||
long int cur;
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
struct dirent *dp;
|
||||
|
||||
dirp = opendir("/");
|
||||
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
|
||||
{
|
||||
/* save position 3 (after fourth entry) */
|
||||
if (i++ == 3)
|
||||
save3 = telldir(dirp);
|
||||
|
||||
printf("%s\n", dp->d_name);
|
||||
|
||||
/* stop at 400 (just to make sure dirp->__offset and dirp->__size are
|
||||
scrambled */
|
||||
if (i == 400)
|
||||
break;
|
||||
}
|
||||
|
||||
printf("going back past 4-th entry...\n");
|
||||
|
||||
/* go back to saved entry */
|
||||
seekdir(dirp, save3);
|
||||
|
||||
/* Check whether telldir equals to save3 now. */
|
||||
cur = telldir(dirp);
|
||||
if (cur != save3)
|
||||
{
|
||||
printf("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
|
||||
result = 1;
|
||||
}
|
||||
|
||||
/* print remaining files (3-last) */
|
||||
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
|
||||
printf("%s\n", dp->d_name);
|
||||
|
||||
closedir(dirp);
|
||||
return result;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_dirent, dirent test for libc);
|
||||
|
||||
+18
-18
@@ -1,18 +1,18 @@
|
||||
/*
|
||||
* env.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int libc_env()
|
||||
{
|
||||
printf("PATH=%s\n", getenv("PATH"));
|
||||
putenv("foo=bar");
|
||||
printf("foo=%s\n", getenv("foo"));
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_env, get/set_env test);
|
||||
/*
|
||||
* env.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int libc_env()
|
||||
{
|
||||
printf("PATH=%s\n", getenv("PATH"));
|
||||
putenv("foo=bar");
|
||||
printf("foo=%s\n", getenv("foo"));
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_env, get/set_env test);
|
||||
|
||||
+516
-516
File diff suppressed because it is too large
Load Diff
+56
-56
@@ -1,56 +1,56 @@
|
||||
/*
|
||||
* memory.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
#include <errno.h>
|
||||
|
||||
static int errors = 0;
|
||||
static void merror(const char *msg)
|
||||
{
|
||||
++errors;
|
||||
printf("Error: %s\n", msg);
|
||||
}
|
||||
|
||||
int libc_mem(void)
|
||||
{
|
||||
void *p;
|
||||
int save;
|
||||
|
||||
errno = 0;
|
||||
|
||||
p = malloc(-1);
|
||||
save = errno;
|
||||
|
||||
if (p != NULL)
|
||||
merror("malloc (-1) succeeded.");
|
||||
|
||||
if (p == NULL && save != ENOMEM)
|
||||
merror("errno is not set correctly");
|
||||
|
||||
p = malloc(10);
|
||||
if (p == NULL)
|
||||
merror("malloc (10) failed.");
|
||||
|
||||
/* realloc (p, 0) == free (p). */
|
||||
p = realloc(p, 0);
|
||||
if (p != NULL)
|
||||
merror("realloc (p, 0) failed.");
|
||||
|
||||
p = malloc(0);
|
||||
if (p == NULL)
|
||||
{
|
||||
printf("malloc(0) returns NULL\n");
|
||||
}
|
||||
|
||||
p = realloc(p, 0);
|
||||
if (p != NULL)
|
||||
merror("realloc (p, 0) failed.");
|
||||
|
||||
return errors != 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc);
|
||||
/*
|
||||
* memory.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
#include <errno.h>
|
||||
|
||||
static int errors = 0;
|
||||
static void merror(const char *msg)
|
||||
{
|
||||
++errors;
|
||||
printf("Error: %s\n", msg);
|
||||
}
|
||||
|
||||
int libc_mem(void)
|
||||
{
|
||||
void *p;
|
||||
int save;
|
||||
|
||||
errno = 0;
|
||||
|
||||
p = malloc(-1);
|
||||
save = errno;
|
||||
|
||||
if (p != NULL)
|
||||
merror("malloc (-1) succeeded.");
|
||||
|
||||
if (p == NULL && save != ENOMEM)
|
||||
merror("errno is not set correctly");
|
||||
|
||||
p = malloc(10);
|
||||
if (p == NULL)
|
||||
merror("malloc (10) failed.");
|
||||
|
||||
/* realloc (p, 0) == free (p). */
|
||||
p = realloc(p, 0);
|
||||
if (p != NULL)
|
||||
merror("realloc (p, 0) failed.");
|
||||
|
||||
p = malloc(0);
|
||||
if (p == NULL)
|
||||
{
|
||||
printf("malloc(0) returns NULL\n");
|
||||
}
|
||||
|
||||
p = realloc(p, 0);
|
||||
if (p != NULL)
|
||||
merror("realloc (p, 0) failed.");
|
||||
|
||||
return errors != 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc);
|
||||
|
||||
+119
-119
@@ -1,119 +1,119 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
#define MQ_NAME_1 "testmsg1"
|
||||
#define MQ_NAME_2 "testmsg2"
|
||||
#define MSG_SIZE 128
|
||||
#define MAX_MSG 3
|
||||
|
||||
const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
|
||||
char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
|
||||
char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
|
||||
pthread_t send1, send2, rev1, rev2;
|
||||
|
||||
int * send_1(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq1 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into send_1 \n");
|
||||
for (i = 0; i < MAX_MSG; i++ ) {
|
||||
if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
|
||||
perror("mq_send doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
|
||||
}
|
||||
|
||||
int * send_2(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq2 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into send_2 \n");
|
||||
for (i = 0; i < MAX_MSG; i++ ) {
|
||||
if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
|
||||
perror("mq_send doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
|
||||
int * receive_1(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq1 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into receive_1 \n");
|
||||
for (i = 0; i< MAX_MSG; i++) {
|
||||
if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
|
||||
perror("mq_receive doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
int * receive_2(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq2 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into receive_2 \n");
|
||||
for (i = 0; i< MAX_MSG; i++) {
|
||||
if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
|
||||
perror("mq_receive doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
|
||||
int libc_mq()
|
||||
{
|
||||
mqd_t mq1 = 0, mq2 = 0;
|
||||
struct mq_attr mqstat;
|
||||
int oflag = O_CREAT|O_RDWR;
|
||||
|
||||
memset(&mqstat, 0, sizeof(mqstat));
|
||||
mqstat.mq_maxmsg = MAX_MSG;
|
||||
mqstat.mq_msgsize = MSG_SIZE;
|
||||
mqstat.mq_flags = 0;
|
||||
|
||||
if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
|
||||
printf("mq_open doesn't return success \n");
|
||||
return -1;
|
||||
}
|
||||
if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
|
||||
printf("mq_open doesn't return success \n");
|
||||
return -1;
|
||||
}
|
||||
pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
|
||||
pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
|
||||
pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
|
||||
pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
|
||||
pthread_join(send1, NULL);
|
||||
pthread_join(send2, NULL);
|
||||
pthread_join(rev1, NULL);
|
||||
pthread_join(rev2, NULL);
|
||||
|
||||
mq_close(mq1);
|
||||
mq_close(mq2);
|
||||
mq_unlink(MQ_NAME_1);
|
||||
mq_unlink(MQ_NAME_2);
|
||||
|
||||
printf("PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <mqueue.h>
|
||||
|
||||
#define MQ_NAME_1 "testmsg1"
|
||||
#define MQ_NAME_2 "testmsg2"
|
||||
#define MSG_SIZE 128
|
||||
#define MAX_MSG 3
|
||||
|
||||
const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
|
||||
char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
|
||||
char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
|
||||
pthread_t send1, send2, rev1, rev2;
|
||||
|
||||
int * send_1(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq1 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into send_1 \n");
|
||||
for (i = 0; i < MAX_MSG; i++ ) {
|
||||
if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
|
||||
perror("mq_send doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
|
||||
}
|
||||
|
||||
int * send_2(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq2 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into send_2 \n");
|
||||
for (i = 0; i < MAX_MSG; i++ ) {
|
||||
if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
|
||||
perror("mq_send doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
|
||||
int * receive_1(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq1 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into receive_1 \n");
|
||||
for (i = 0; i< MAX_MSG; i++) {
|
||||
if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
|
||||
perror("mq_receive doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
int * receive_2(void * mq)
|
||||
{
|
||||
int i;
|
||||
mqd_t mq2 = *(mqd_t *)mq;
|
||||
|
||||
printf("Enter into receive_2 \n");
|
||||
for (i = 0; i< MAX_MSG; i++) {
|
||||
if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
|
||||
perror("mq_receive doesn't return success \n");
|
||||
pthread_exit((void *)1);
|
||||
}
|
||||
printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
|
||||
}
|
||||
pthread_exit((void *)0);
|
||||
}
|
||||
|
||||
int libc_mq()
|
||||
{
|
||||
mqd_t mq1 = 0, mq2 = 0;
|
||||
struct mq_attr mqstat;
|
||||
int oflag = O_CREAT|O_RDWR;
|
||||
|
||||
memset(&mqstat, 0, sizeof(mqstat));
|
||||
mqstat.mq_maxmsg = MAX_MSG;
|
||||
mqstat.mq_msgsize = MSG_SIZE;
|
||||
mqstat.mq_flags = 0;
|
||||
|
||||
if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
|
||||
printf("mq_open doesn't return success \n");
|
||||
return -1;
|
||||
}
|
||||
if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
|
||||
printf("mq_open doesn't return success \n");
|
||||
return -1;
|
||||
}
|
||||
pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
|
||||
pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
|
||||
pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
|
||||
pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
|
||||
pthread_join(send1, NULL);
|
||||
pthread_join(send2, NULL);
|
||||
pthread_join(rev1, NULL);
|
||||
pthread_join(rev2, NULL);
|
||||
|
||||
mq_close(mq1);
|
||||
mq_close(mq2);
|
||||
mq_unlink(MQ_NAME_1);
|
||||
mq_unlink(MQ_NAME_2);
|
||||
|
||||
printf("PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);
|
||||
|
||||
+200
-200
@@ -1,200 +1,200 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/fcntl.h>
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
char * format[] = {
|
||||
"%",
|
||||
"%0.",
|
||||
"%.0",
|
||||
"%+0.",
|
||||
"%+.0",
|
||||
"%.5",
|
||||
"%+.5",
|
||||
"%2.5",
|
||||
"%22.5",
|
||||
"%022.5",
|
||||
"%#022.5",
|
||||
"%-#022.5",
|
||||
"%+#022.5",
|
||||
"%-22.5",
|
||||
"%+22.5",
|
||||
"%--22.5",
|
||||
"%++22.5",
|
||||
"%+-22.5",
|
||||
"%-+22.5",
|
||||
"%-#022.5",
|
||||
"%-#22.5",
|
||||
"%-2.22",
|
||||
"%+2.22",
|
||||
"%-#02.22",
|
||||
"%-#2.22",
|
||||
"%-1.5",
|
||||
"%1.5",
|
||||
"%-#01.5",
|
||||
"%-#1.5",
|
||||
"%-#.5",
|
||||
"%-#1.",
|
||||
"%-#.",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
intchk (const char *fmt)
|
||||
{
|
||||
(void) printf("%15s :, \"", fmt);
|
||||
(void) printf(fmt, 0);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, 123);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, -18);
|
||||
(void) printf("\"\n");
|
||||
}
|
||||
|
||||
static void
|
||||
fltchk (const char *fmt)
|
||||
{
|
||||
(void) printf("%15s :, \"", fmt);
|
||||
(void) printf(fmt, 0.0);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, 123.0001);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, -18.0002301);
|
||||
(void) printf("\"\n");
|
||||
}
|
||||
|
||||
|
||||
int printf_test()
|
||||
{
|
||||
char buf[256];
|
||||
int i;
|
||||
|
||||
printf("%s\n\n", "# vim:syntax=off:");
|
||||
|
||||
/* integers */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "d");
|
||||
intchk(buf);
|
||||
}
|
||||
|
||||
/* floats */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "f");
|
||||
fltchk(buf);
|
||||
}
|
||||
/* hexa */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "x");
|
||||
intchk(buf);
|
||||
}
|
||||
|
||||
printf("#%.4x %4x#\n", 4, 88);
|
||||
printf("#%4x#\n",4);
|
||||
printf("#%#22.8x#\n",1234567);
|
||||
|
||||
printf("#%+2i#\n",18);
|
||||
printf("#%i#\n",18);
|
||||
printf("#%llu#\n",4294967297ULL);
|
||||
printf("#%#x#\n",44444);
|
||||
printf("#%-8i#\n",33);
|
||||
printf("#%i#\n",18);
|
||||
printf("#%d#\n",18);
|
||||
printf("#%u#\n",18);
|
||||
printf("#%lu#\n",18);
|
||||
printf("#%li#\n",18);
|
||||
printf("#%-+#06d#\n", -123);
|
||||
printf("#%-+#6d#\n", -123);
|
||||
printf("#%+#06d#\n", -123);
|
||||
printf("#%06d#\n", -123);
|
||||
printf("#%+15s#\n","ABCDEF");
|
||||
/* from ncurses make_keys */
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
|
||||
printf("%*.*f\n", 0, 16, 0.0);
|
||||
printf("%*.*f\n", 16, 16, 0.0);
|
||||
printf("%*.*f\n", 2, 2, -0.0);
|
||||
printf("%*.*f\n", 20, 0, -123.123);
|
||||
printf("%*.*f\n", 10, 0, +123.123);
|
||||
|
||||
|
||||
i = printf("\"%s\"\n","A");
|
||||
printf("%i\n", i);
|
||||
/* from glibc's tst-printf.c */
|
||||
|
||||
{
|
||||
char buf[20];
|
||||
char buf2[512];
|
||||
int i;
|
||||
|
||||
printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
|
||||
snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
|
||||
buf);
|
||||
memset(buf2,0,sizeof(buf));
|
||||
i=snprintf(buf2, 256, "%.9999u", 10);
|
||||
printf("%i %i\n",i,strlen(buf2));
|
||||
|
||||
printf ("snprintf (\"%%.999999u\", 10) == %d\n",
|
||||
snprintf(buf2, sizeof(buf2), "%.999999u", 10));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void libc_printf()
|
||||
{
|
||||
printf("stdout test!!\n");
|
||||
fprintf(stdout, "fprintf test!!\n");
|
||||
fprintf(stderr, "fprintf test!!\n");
|
||||
puts("puts test!!\n");
|
||||
|
||||
putc('1', stderr);
|
||||
putc('2', stderr);
|
||||
putc('\n', stderr);
|
||||
|
||||
printf_test();
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_printf, printf test in libc);
|
||||
|
||||
|
||||
void libc_dprintf()
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open("/dev/console", O_WRONLY, 0);
|
||||
if (fd >0)
|
||||
{
|
||||
dprintf(fd, "fd:%d printf test!!\n", fd);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_dprintf, dprintf test);
|
||||
|
||||
|
||||
void libc_fdopen()
|
||||
{
|
||||
int fd;
|
||||
FILE* fp;
|
||||
|
||||
fd = open("/dev/console", O_WRONLY, 0);
|
||||
if (fd >0)
|
||||
{
|
||||
fp = fdopen(fd, "w");
|
||||
fprintf(fp, "fdopen test, fd %d!!\n", fileno(fp));
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_fdopen, fdopen test);
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/fcntl.h>
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
char * format[] = {
|
||||
"%",
|
||||
"%0.",
|
||||
"%.0",
|
||||
"%+0.",
|
||||
"%+.0",
|
||||
"%.5",
|
||||
"%+.5",
|
||||
"%2.5",
|
||||
"%22.5",
|
||||
"%022.5",
|
||||
"%#022.5",
|
||||
"%-#022.5",
|
||||
"%+#022.5",
|
||||
"%-22.5",
|
||||
"%+22.5",
|
||||
"%--22.5",
|
||||
"%++22.5",
|
||||
"%+-22.5",
|
||||
"%-+22.5",
|
||||
"%-#022.5",
|
||||
"%-#22.5",
|
||||
"%-2.22",
|
||||
"%+2.22",
|
||||
"%-#02.22",
|
||||
"%-#2.22",
|
||||
"%-1.5",
|
||||
"%1.5",
|
||||
"%-#01.5",
|
||||
"%-#1.5",
|
||||
"%-#.5",
|
||||
"%-#1.",
|
||||
"%-#.",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
intchk (const char *fmt)
|
||||
{
|
||||
(void) printf("%15s :, \"", fmt);
|
||||
(void) printf(fmt, 0);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, 123);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, -18);
|
||||
(void) printf("\"\n");
|
||||
}
|
||||
|
||||
static void
|
||||
fltchk (const char *fmt)
|
||||
{
|
||||
(void) printf("%15s :, \"", fmt);
|
||||
(void) printf(fmt, 0.0);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, 123.0001);
|
||||
(void) printf("\", \"");
|
||||
(void) printf(fmt, -18.0002301);
|
||||
(void) printf("\"\n");
|
||||
}
|
||||
|
||||
|
||||
int printf_test()
|
||||
{
|
||||
char buf[256];
|
||||
int i;
|
||||
|
||||
printf("%s\n\n", "# vim:syntax=off:");
|
||||
|
||||
/* integers */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "d");
|
||||
intchk(buf);
|
||||
}
|
||||
|
||||
/* floats */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "f");
|
||||
fltchk(buf);
|
||||
}
|
||||
/* hexa */
|
||||
for(i=0;format[i];i++) {
|
||||
strcpy(buf, format[i]);
|
||||
strcat(buf, "x");
|
||||
intchk(buf);
|
||||
}
|
||||
|
||||
printf("#%.4x %4x#\n", 4, 88);
|
||||
printf("#%4x#\n",4);
|
||||
printf("#%#22.8x#\n",1234567);
|
||||
|
||||
printf("#%+2i#\n",18);
|
||||
printf("#%i#\n",18);
|
||||
printf("#%llu#\n",4294967297ULL);
|
||||
printf("#%#x#\n",44444);
|
||||
printf("#%-8i#\n",33);
|
||||
printf("#%i#\n",18);
|
||||
printf("#%d#\n",18);
|
||||
printf("#%u#\n",18);
|
||||
printf("#%lu#\n",18);
|
||||
printf("#%li#\n",18);
|
||||
printf("#%-+#06d#\n", -123);
|
||||
printf("#%-+#6d#\n", -123);
|
||||
printf("#%+#06d#\n", -123);
|
||||
printf("#%06d#\n", -123);
|
||||
printf("#%+15s#\n","ABCDEF");
|
||||
/* from ncurses make_keys */
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
|
||||
printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
|
||||
printf("%*.*f\n", 0, 16, 0.0);
|
||||
printf("%*.*f\n", 16, 16, 0.0);
|
||||
printf("%*.*f\n", 2, 2, -0.0);
|
||||
printf("%*.*f\n", 20, 0, -123.123);
|
||||
printf("%*.*f\n", 10, 0, +123.123);
|
||||
|
||||
|
||||
i = printf("\"%s\"\n","A");
|
||||
printf("%i\n", i);
|
||||
/* from glibc's tst-printf.c */
|
||||
|
||||
{
|
||||
char buf[20];
|
||||
char buf2[512];
|
||||
int i;
|
||||
|
||||
printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
|
||||
snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
|
||||
buf);
|
||||
memset(buf2,0,sizeof(buf));
|
||||
i=snprintf(buf2, 256, "%.9999u", 10);
|
||||
printf("%i %i\n",i,strlen(buf2));
|
||||
|
||||
printf ("snprintf (\"%%.999999u\", 10) == %d\n",
|
||||
snprintf(buf2, sizeof(buf2), "%.999999u", 10));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void libc_printf()
|
||||
{
|
||||
printf("stdout test!!\n");
|
||||
fprintf(stdout, "fprintf test!!\n");
|
||||
fprintf(stderr, "fprintf test!!\n");
|
||||
puts("puts test!!\n");
|
||||
|
||||
putc('1', stderr);
|
||||
putc('2', stderr);
|
||||
putc('\n', stderr);
|
||||
|
||||
printf_test();
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_printf, printf test in libc);
|
||||
|
||||
|
||||
void libc_dprintf()
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open("/dev/console", O_WRONLY, 0);
|
||||
if (fd >0)
|
||||
{
|
||||
dprintf(fd, "fd:%d printf test!!\n", fd);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_dprintf, dprintf test);
|
||||
|
||||
|
||||
void libc_fdopen()
|
||||
{
|
||||
int fd;
|
||||
FILE* fp;
|
||||
|
||||
fd = open("/dev/console", O_WRONLY, 0);
|
||||
if (fd >0)
|
||||
{
|
||||
fp = fdopen(fd, "w");
|
||||
fprintf(fp, "fdopen test, fd %d!!\n", fileno(fp));
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_fdopen, fdopen test);
|
||||
|
||||
+43
-43
@@ -1,43 +1,43 @@
|
||||
/*
|
||||
* rand.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int libc_rand(void)
|
||||
{
|
||||
int i1, i2;
|
||||
int j1, j2;
|
||||
|
||||
/* The C standard says that "If rand is called before any calls to
|
||||
srand have been made, the same sequence shall be generated as
|
||||
when srand is first called with a seed value of 1." */
|
||||
i1 = rand();
|
||||
i2 = rand();
|
||||
srand(1);
|
||||
j1 = rand();
|
||||
j2 = rand();
|
||||
if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0)
|
||||
{
|
||||
puts("Test FAILED!");
|
||||
}
|
||||
if (j1 == i1 && j2 == i2)
|
||||
{
|
||||
puts("Test succeeded.");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j1 != i1)
|
||||
printf("%d != %d\n", j1, i1);
|
||||
if (j2 != i2)
|
||||
printf("%d != %d\n", j2, i2);
|
||||
puts("Test FAILED!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_rand, rand test for libc);
|
||||
/*
|
||||
* rand.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int libc_rand(void)
|
||||
{
|
||||
int i1, i2;
|
||||
int j1, j2;
|
||||
|
||||
/* The C standard says that "If rand is called before any calls to
|
||||
srand have been made, the same sequence shall be generated as
|
||||
when srand is first called with a seed value of 1." */
|
||||
i1 = rand();
|
||||
i2 = rand();
|
||||
srand(1);
|
||||
j1 = rand();
|
||||
j2 = rand();
|
||||
if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0)
|
||||
{
|
||||
puts("Test FAILED!");
|
||||
}
|
||||
if (j1 == i1 && j2 == i2)
|
||||
{
|
||||
puts("Test succeeded.");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j1 != i1)
|
||||
printf("%d != %d\n", j1, i1);
|
||||
if (j2 != i2)
|
||||
printf("%d != %d\n", j2, i2);
|
||||
puts("Test FAILED!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_rand, rand test for libc);
|
||||
|
||||
+65
-65
@@ -1,65 +1,65 @@
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static sem_t sema;
|
||||
static void* other_thread()
|
||||
{
|
||||
printf("other_thread here!\n");
|
||||
|
||||
sleep(1);
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("other_thread: sem_post...\n");
|
||||
if(sem_post(&sema) == -1)
|
||||
printf("sem_post failed\n");
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
printf("other_thread dies!\n");
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
static void test_thread(void* parameter)
|
||||
{
|
||||
pthread_t tid;
|
||||
|
||||
printf("main thread here!\n");
|
||||
printf("sleep 5 seconds...");
|
||||
sleep(5);
|
||||
printf("done\n");
|
||||
|
||||
sem_init(&sema, 0, 0);
|
||||
|
||||
/* create the "other" thread */
|
||||
if(pthread_create(&tid, 0, &other_thread, 0)!=0)
|
||||
/* error */
|
||||
printf("pthread_create OtherThread failed.\n");
|
||||
else
|
||||
printf("created OtherThread=%x\n", tid);
|
||||
|
||||
/* let the other thread run */
|
||||
while (1)
|
||||
{
|
||||
printf("Main: sem_wait...\n");
|
||||
if(sem_wait(&sema) == -1)
|
||||
printf("sem_wait failed\n");
|
||||
printf("Main back.\n\n");
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
#include <finsh.h>
|
||||
void libc_sem()
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("semtest", test_thread, RT_NULL,
|
||||
2048, 20, 5);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_sem, posix semaphore test);
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static sem_t sema;
|
||||
static void* other_thread()
|
||||
{
|
||||
printf("other_thread here!\n");
|
||||
|
||||
sleep(1);
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("other_thread: sem_post...\n");
|
||||
if(sem_post(&sema) == -1)
|
||||
printf("sem_post failed\n");
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
printf("other_thread dies!\n");
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
static void test_thread(void* parameter)
|
||||
{
|
||||
pthread_t tid;
|
||||
|
||||
printf("main thread here!\n");
|
||||
printf("sleep 5 seconds...");
|
||||
sleep(5);
|
||||
printf("done\n");
|
||||
|
||||
sem_init(&sema, 0, 0);
|
||||
|
||||
/* create the "other" thread */
|
||||
if(pthread_create(&tid, 0, &other_thread, 0)!=0)
|
||||
/* error */
|
||||
printf("pthread_create OtherThread failed.\n");
|
||||
else
|
||||
printf("created OtherThread=%x\n", tid);
|
||||
|
||||
/* let the other thread run */
|
||||
while (1)
|
||||
{
|
||||
printf("Main: sem_wait...\n");
|
||||
if(sem_wait(&sema) == -1)
|
||||
printf("sem_wait failed\n");
|
||||
printf("Main back.\n\n");
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
#include <finsh.h>
|
||||
void libc_sem()
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("semtest", test_thread, RT_NULL,
|
||||
2048, 20, 5);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(libc_sem, posix semaphore test);
|
||||
|
||||
+24
-24
@@ -1,24 +1,24 @@
|
||||
/*
|
||||
* time.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int speed()
|
||||
{
|
||||
int i;
|
||||
time_t t;
|
||||
|
||||
printf("%d\n", time(0));
|
||||
for (i = 0; i < 10000000; ++i)
|
||||
t = time(0);
|
||||
|
||||
printf("%d\n", time(0));
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(speed, speed test);
|
||||
/*
|
||||
* time.c
|
||||
*
|
||||
* Created on: 2010-11-17
|
||||
* Author: bernard
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <finsh.h>
|
||||
|
||||
int speed()
|
||||
{
|
||||
int i;
|
||||
time_t t;
|
||||
|
||||
printf("%d\n", time(0));
|
||||
for (i = 0; i < 10000000; ++i)
|
||||
t = time(0);
|
||||
|
||||
printf("%d\n", time(0));
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(speed, speed test);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
example:
|
||||
1.edit rtconfig.py to config toolchain and bsp
|
||||
2.scons --app=basicapp
|
||||
3.copy basicapp/build/$bsp/basicapp.so to filesystem
|
||||
example:
|
||||
1.edit rtconfig.py to config toolchain and bsp
|
||||
2.scons --app=basicapp
|
||||
3.copy basicapp/build/$bsp/basicapp.so to filesystem
|
||||
|
||||
+73
-73
@@ -1,73 +1,73 @@
|
||||
import os
|
||||
import sys
|
||||
import SCons.cpp
|
||||
import rtconfig
|
||||
|
||||
if os.getenv('RTT_ROOT'):
|
||||
RTT_ROOT = os.getenv('RTT_ROOT')
|
||||
else:
|
||||
RTT_ROOT = os.path.normpath(os.getcwd() + '/../..')
|
||||
|
||||
sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
|
||||
from building import *
|
||||
|
||||
Export('RTT_ROOT')
|
||||
|
||||
# add target option
|
||||
AddOption('--app',
|
||||
dest='app',
|
||||
nargs=1, type='string',
|
||||
action='store',
|
||||
metavar='DIR',
|
||||
help='installation prefix')
|
||||
|
||||
# add target option
|
||||
AddOption('--type',
|
||||
dest='type',
|
||||
nargs=1, type='string',
|
||||
action='store',
|
||||
metavar='DIR',
|
||||
help='installation prefix')
|
||||
|
||||
app = GetOption('app')
|
||||
|
||||
if GetOption('type') == 'ext':
|
||||
linkflags = rtconfig.LFLAGS + ' -e 0'
|
||||
else:
|
||||
linkflags = rtconfig.LFLAGS + ' -e main'
|
||||
|
||||
env = Environment(tools = ['mingw'],
|
||||
AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
|
||||
CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS,
|
||||
CXX = rtconfig.CXX,
|
||||
AR = rtconfig.AR, ARFLAGS = '-rc',
|
||||
LINK = rtconfig.LINK, LINKFLAGS = linkflags,
|
||||
CPPPATH = [
|
||||
RTT_ROOT + '/include',
|
||||
RTT_ROOT + '/bsp/' + rtconfig.BSP,
|
||||
RTT_ROOT + '/components/finsh',
|
||||
RTT_ROOT + '/components/rtgui/include',
|
||||
RTT_ROOT + '/components/rgtui/common',
|
||||
RTT_ROOT + '/components/rtgui/server',
|
||||
RTT_ROOT + '/components/rtgui/widgets',
|
||||
RTT_ROOT + '/components/libdl',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src/os/rt-thread',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src/demos',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/apps/common',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src',
|
||||
RTT_ROOT + '/components/dfs',
|
||||
RTT_ROOT + '/components/dfs/include',
|
||||
RTT_ROOT + '/components/libc/newlib',
|
||||
RTT_ROOT + '/components/external/cairo/cairo-1.10.2/src',
|
||||
RTT_ROOT + '/components/external/cairo/'
|
||||
])
|
||||
env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
|
||||
|
||||
PrepareModuleBuilding(env, RTT_ROOT)
|
||||
|
||||
dir = app + '/build/' + rtconfig.BSP
|
||||
objs = SConscript(app + '/Sconscript', variant_dir=dir, duplicate=0)
|
||||
TARGET = dir + '/' + app + '.' + rtconfig.TARGET_EXT
|
||||
|
||||
# build program
|
||||
env.Program(TARGET, objs)
|
||||
import os
|
||||
import sys
|
||||
import SCons.cpp
|
||||
import rtconfig
|
||||
|
||||
if os.getenv('RTT_ROOT'):
|
||||
RTT_ROOT = os.getenv('RTT_ROOT')
|
||||
else:
|
||||
RTT_ROOT = os.path.normpath(os.getcwd() + '/../..')
|
||||
|
||||
sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
|
||||
from building import *
|
||||
|
||||
Export('RTT_ROOT')
|
||||
|
||||
# add target option
|
||||
AddOption('--app',
|
||||
dest='app',
|
||||
nargs=1, type='string',
|
||||
action='store',
|
||||
metavar='DIR',
|
||||
help='installation prefix')
|
||||
|
||||
# add target option
|
||||
AddOption('--type',
|
||||
dest='type',
|
||||
nargs=1, type='string',
|
||||
action='store',
|
||||
metavar='DIR',
|
||||
help='installation prefix')
|
||||
|
||||
app = GetOption('app')
|
||||
|
||||
if GetOption('type') == 'ext':
|
||||
linkflags = rtconfig.LFLAGS + ' -e 0'
|
||||
else:
|
||||
linkflags = rtconfig.LFLAGS + ' -e main'
|
||||
|
||||
env = Environment(tools = ['mingw'],
|
||||
AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
|
||||
CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS,
|
||||
CXX = rtconfig.CXX,
|
||||
AR = rtconfig.AR, ARFLAGS = '-rc',
|
||||
LINK = rtconfig.LINK, LINKFLAGS = linkflags,
|
||||
CPPPATH = [
|
||||
RTT_ROOT + '/include',
|
||||
RTT_ROOT + '/bsp/' + rtconfig.BSP,
|
||||
RTT_ROOT + '/components/finsh',
|
||||
RTT_ROOT + '/components/rtgui/include',
|
||||
RTT_ROOT + '/components/rgtui/common',
|
||||
RTT_ROOT + '/components/rtgui/server',
|
||||
RTT_ROOT + '/components/rtgui/widgets',
|
||||
RTT_ROOT + '/components/libdl',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src/os/rt-thread',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src/demos',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/apps/common',
|
||||
RTT_ROOT + '/components/external/ftk/ftk/src',
|
||||
RTT_ROOT + '/components/dfs',
|
||||
RTT_ROOT + '/components/dfs/include',
|
||||
RTT_ROOT + '/components/libc/newlib',
|
||||
RTT_ROOT + '/components/external/cairo/cairo-1.10.2/src',
|
||||
RTT_ROOT + '/components/external/cairo/'
|
||||
])
|
||||
env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
|
||||
|
||||
PrepareModuleBuilding(env, RTT_ROOT)
|
||||
|
||||
dir = app + '/build/' + rtconfig.BSP
|
||||
objs = SConscript(app + '/Sconscript', variant_dir=dir, duplicate=0)
|
||||
TARGET = dir + '/' + app + '.' + rtconfig.TARGET_EXT
|
||||
|
||||
# build program
|
||||
env.Program(TARGET, objs)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
Return('group')
|
||||
@@ -1,7 +1,7 @@
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
Return('group')
|
||||
+23
-23
@@ -1,23 +1,23 @@
|
||||
# bsp name
|
||||
BSP = 'mini2440'
|
||||
|
||||
# toolchains
|
||||
EXEC_PATH = 'C:/Program Files/CodeSourcery/Sourcery G++ Lite/bin'
|
||||
PREFIX = 'arm-none-eabi-'
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
AR = PREFIX + 'ar'
|
||||
LINK = PREFIX + 'gcc'
|
||||
TARGET_EXT = 'so'
|
||||
SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
DEVICE = ' -mcpu=arm920t'
|
||||
CFLAGS = DEVICE + ' -O0 -fPIC -DFTK_AS_PLUGIN -DRT_THREAD '
|
||||
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp'
|
||||
LFLAGS = DEVICE + ' -Wl,-z,max-page-size=0x4 -shared -fPIC -nostdlib -s'
|
||||
|
||||
CPATH = ''
|
||||
LPATH = ''
|
||||
# bsp name
|
||||
BSP = 'mini2440'
|
||||
|
||||
# toolchains
|
||||
EXEC_PATH = 'C:/Program Files/CodeSourcery/Sourcery G++ Lite/bin'
|
||||
PREFIX = 'arm-none-eabi-'
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
AR = PREFIX + 'ar'
|
||||
LINK = PREFIX + 'gcc'
|
||||
TARGET_EXT = 'so'
|
||||
SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
DEVICE = ' -mcpu=arm920t'
|
||||
CFLAGS = DEVICE + ' -O0 -fPIC -DFTK_AS_PLUGIN -DRT_THREAD '
|
||||
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp'
|
||||
LFLAGS = DEVICE + ' -Wl,-z,max-page-size=0x4 -shared -fPIC -nostdlib -s'
|
||||
|
||||
CPATH = ''
|
||||
LPATH = ''
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
# bsp name
|
||||
BSP = 'lm3s8962'
|
||||
|
||||
# toolchains
|
||||
EXEC_PATH = 'C:/Program Files/CodeSourcery/Sourcery G++ Lite/bin'
|
||||
PREFIX = 'arm-none-eabi-'
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
AR = PREFIX + 'ar'
|
||||
LINK = PREFIX + 'gcc'
|
||||
TARGET_EXT = 'so'
|
||||
SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
DEVICE = ' -mcpu=cortex-m3'
|
||||
CFLAGS = DEVICE + ' -mthumb -mlong-calls -Dsourcerygxx -O0 -fPIC'
|
||||
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp'
|
||||
LFLAGS = DEVICE + ' -mthumb -Wl,-z,max-page-size=0x4 -shared -fPIC -e main -nostdlib'
|
||||
|
||||
CPATH = ''
|
||||
LPATH = ''
|
||||
# bsp name
|
||||
BSP = 'lm3s8962'
|
||||
|
||||
# toolchains
|
||||
EXEC_PATH = 'C:/Program Files/CodeSourcery/Sourcery G++ Lite/bin'
|
||||
PREFIX = 'arm-none-eabi-'
|
||||
CC = PREFIX + 'gcc'
|
||||
CXX = PREFIX + 'g++'
|
||||
AS = PREFIX + 'gcc'
|
||||
AR = PREFIX + 'ar'
|
||||
LINK = PREFIX + 'gcc'
|
||||
TARGET_EXT = 'so'
|
||||
SIZE = PREFIX + 'size'
|
||||
OBJDUMP = PREFIX + 'objdump'
|
||||
OBJCPY = PREFIX + 'objcopy'
|
||||
|
||||
DEVICE = ' -mcpu=cortex-m3'
|
||||
CFLAGS = DEVICE + ' -mthumb -mlong-calls -Dsourcerygxx -O0 -fPIC'
|
||||
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp'
|
||||
LFLAGS = DEVICE + ' -mthumb -Wl,-z,max-page-size=0x4 -shared -fPIC -e main -nostdlib'
|
||||
|
||||
CPATH = ''
|
||||
LPATH = ''
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
import rtconfig
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
src = Glob('*.c')
|
||||
group = DefineGroup('', src, depend = [''])
|
||||
Return('group')
|
||||
@@ -1,61 +1,61 @@
|
||||
/*
|
||||
* File : tetris_modal.c
|
||||
* This file is part of RTGUI in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-08-14 Yi.Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
struct rt_tetris;
|
||||
typedef struct rt_tetris rt_tetris_t;
|
||||
|
||||
struct rt_tetris_view;
|
||||
typedef struct rt_tetris_view rt_tetris_view_t;
|
||||
|
||||
typedef rt_err_t (*on_update)(rt_tetris_view_t* thiz, rt_tetris_t* tetris);
|
||||
|
||||
struct rt_tetris_view
|
||||
{
|
||||
rt_uint32_t width;
|
||||
rt_uint32_t height;
|
||||
|
||||
on_update update;
|
||||
on_update update_next_brick;
|
||||
on_update update_level;
|
||||
on_update update_score_and_lines;
|
||||
void *private;
|
||||
};
|
||||
|
||||
rt_tetris_t* rt_tetris_create(rt_uint32_t width, rt_uint32_t height);
|
||||
rt_err_t rt_tetris_destory(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_start(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_pause(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_width(rt_tetris_t* thiz);
|
||||
rt_uint32_t* rt_tetris_next_brick(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_level(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_lines(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_score(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_height(rt_tetris_t* thiz);
|
||||
rt_bool_t rt_tetris_status(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_down(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_left(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_right(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_drop(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_rotate(rt_tetris_t* thiz, rt_bool_t direction);
|
||||
rt_err_t rt_tetris_add_view(rt_tetris_t* thiz, rt_tetris_view_t* view);
|
||||
rt_err_t rt_tetris_delete_view(rt_tetris_t* thiz, rt_tetris_view_t* view);
|
||||
rt_err_t rt_tetris_check_collision(rt_tetris_t* thiz, rt_uint32_t block);
|
||||
|
||||
rt_tetris_view_t* rt_tetris_view_create(void* private);
|
||||
rt_err_t rt_tetris_view_destroy(rt_tetris_view_t* thiz);
|
||||
|
||||
void tetris_ui_entry(void* parameter);
|
||||
|
||||
/*
|
||||
* File : tetris_modal.c
|
||||
* This file is part of RTGUI in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-08-14 Yi.Qiu first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
struct rt_tetris;
|
||||
typedef struct rt_tetris rt_tetris_t;
|
||||
|
||||
struct rt_tetris_view;
|
||||
typedef struct rt_tetris_view rt_tetris_view_t;
|
||||
|
||||
typedef rt_err_t (*on_update)(rt_tetris_view_t* thiz, rt_tetris_t* tetris);
|
||||
|
||||
struct rt_tetris_view
|
||||
{
|
||||
rt_uint32_t width;
|
||||
rt_uint32_t height;
|
||||
|
||||
on_update update;
|
||||
on_update update_next_brick;
|
||||
on_update update_level;
|
||||
on_update update_score_and_lines;
|
||||
void *private;
|
||||
};
|
||||
|
||||
rt_tetris_t* rt_tetris_create(rt_uint32_t width, rt_uint32_t height);
|
||||
rt_err_t rt_tetris_destory(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_start(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_pause(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_width(rt_tetris_t* thiz);
|
||||
rt_uint32_t* rt_tetris_next_brick(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_level(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_lines(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_score(rt_tetris_t* thiz);
|
||||
rt_uint32_t rt_tetris_height(rt_tetris_t* thiz);
|
||||
rt_bool_t rt_tetris_status(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_down(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_left(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_right(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_drop(rt_tetris_t* thiz);
|
||||
rt_err_t rt_tetris_rotate(rt_tetris_t* thiz, rt_bool_t direction);
|
||||
rt_err_t rt_tetris_add_view(rt_tetris_t* thiz, rt_tetris_view_t* view);
|
||||
rt_err_t rt_tetris_delete_view(rt_tetris_t* thiz, rt_tetris_view_t* view);
|
||||
rt_err_t rt_tetris_check_collision(rt_tetris_t* thiz, rt_uint32_t block);
|
||||
|
||||
rt_tetris_view_t* rt_tetris_view_create(void* private);
|
||||
rt_err_t rt_tetris_view_destroy(rt_tetris_view_t* thiz);
|
||||
|
||||
void tetris_ui_entry(void* parameter);
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user