diff --git a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp index a779ba3..e9ea056 100644 --- a/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp +++ b/kernel_liteos_a-master/testsuites/unittest/process/basic/pthread/smoke/pthread_once_test_001.cpp @@ -28,83 +28,96 @@ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "it_pthread_test.h" - -static int g_number = 0; -static int g_okStatus = 777; // 777, a special number indicate the status is ok. -static pthread_once_t g_onceCtrl = PTHREAD_ONCE_INIT; - +#include "it_pthread_test.h" // 包含测试框架的头文件 + +// 全局变量定义 +static int g_number = 0; // 用于记录 InitRoutine 被调用的次数 +static int g_okStatus = 777; // 定义一个特殊的状态值,表示状态正常 +static pthread_once_t g_onceCtrl = PTHREAD_ONCE_INIT; // pthread_once 的控制变量,初始化为未执行状态 + +// 初始化函数,每次调用都会增加 g_number 的值 static void InitRoutine(void) { g_number++; } - + +// 线程函数,测试 pthread_once 的行为 static void *Threadfunc(void *parm) { int err; + // 确保 InitRoutine 只被调用一次 err = pthread_once(&g_onceCtrl, InitRoutine); - ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); - return reinterpret_cast(g_okStatus); + ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); // 使用测试框架的宏检查 pthread_once 的返回值是否为 0 + return reinterpret_cast(g_okStatus); // 返回状态值 EXIT: - return NULL; + return NULL; // 出错时返回 NULL } - + +// 测试函数,创建多个线程并检查 pthread_once 的行为 static void *ThreadFuncTest(void *arg) { - pthread_t thread[3]; - int rc = 0; - int i = 3; - void *status; - const int threadsNum = 3; - - g_number = 0; - + pthread_t thread[3]; // 定义线程数组,用于存储线程 ID + int rc = 0; // 用于存储线程创建和加入的返回值 + int i = 3; // 循环变量,但立即被重新赋值,这里可能是代码风格问题 + void *status; // 用于存储线程函数的返回值 + const int threadsNum = 3; // 定义线程数量 + + g_number = 0; // 重置全局计数器 + + // 创建多个线程 for (i = 0; i < threadsNum; ++i) { rc = pthread_create(&thread[i], NULL, Threadfunc, NULL); - ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 检查 pthread_create 的返回值 } - + + // 等待所有线程结束 for (i = 0; i < threadsNum; ++i) { rc = pthread_join(thread[i], &status); - ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); - ICUNIT_GOTO_EQUAL((unsigned int)status, (unsigned int)g_okStatus, status, EXIT); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 检查 pthread_join 的返回值 + ICUNIT_GOTO_EQUAL((unsigned int)status, (unsigned int)g_okStatus, status, EXIT); // 检查线程函数的返回值 } - + + // 检查 InitRoutine 是否只被调用了一次 ICUNIT_GOTO_EQUAL(g_number, 1, g_number, EXIT); - + EXIT: - return NULL; + return NULL; // 出错时返回 NULL } - + +// 测试用例函数,设置线程属性并启动测试线程 static int Testcase(void) { - int ret; - pthread_t newPthread; - int curThreadPri, curThreadPolicy; - pthread_attr_t a = { 0 }; - struct sched_param param = { 0 }; - - g_onceCtrl = PTHREAD_ONCE_INIT; - + int ret; // 用于存储函数调用的返回值 + pthread_t newPthread; // 定义新线程的 ID + int curThreadPri, curThreadPolicy; // 用于存储当前线程的调度策略和优先级 + pthread_attr_t a = { 0 }; // 定义线程属性对象并初始化 + struct sched_param param = { 0 }; // 定义调度参数对象并初始化 + + g_onceCtrl = PTHREAD_ONCE_INIT; // 重置 pthread_once 的控制变量 + + // 获取当前线程的调度策略和优先级 ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m); - ICUNIT_ASSERT_EQUAL(ret, 0, -ret); - - curThreadPri = param.sched_priority; - - ret = pthread_attr_init(&a); - pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); - param.sched_priority = curThreadPri + 2; // 2, adjust the priority. - pthread_attr_setschedparam(&a, ¶m); - ret = pthread_create(&newPthread, &a, ThreadFuncTest, 0); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - + ICUNIT_ASSERT_EQUAL(ret, 0, -ret); // 检查 pthread_getschedparam 的返回值 + + curThreadPri = param.sched_priority; // 获取当前线程的优先级 + + // 设置新线程的属性 + ret = pthread_attr_init(&a); // 初始化线程属性 + pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED); // 设置线程继承调度策略为显式 + param.sched_priority = curThreadPri + 2; // 设置新线程的优先级为当前线程优先级加 2 + pthread_attr_setschedparam(&a, ¶m); // 设置线程调度参数 + ret = pthread_create(&newPthread, &a, ThreadFuncTest, 0); // 创建新线程并运行测试函数 + ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查 pthread_create 的返回值 + + // 等待新线程结束 ret = pthread_join(newPthread, NULL); - ICUNIT_ASSERT_EQUAL(ret, 0, ret); - - return 0; + ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查 pthread_join 的返回值 + + return 0; // 测试成功结束 } - + +// 注册测试用例 void ItTestPthreadOnce001(void) { - TEST_ADD_CASE("IT_PTHREAD_ONCE_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); + TEST_ADD_CASE("IT_PTHREAD_ONCE_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); // 使用测试框架的宏注册测试用例 }