/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * 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; // 用于记录 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); // 使用测试框架的宏检查 pthread_once 的返回值是否为 0
    return reinterpret_cast<void *>(g_okStatus); // 返回状态值
EXIT:
    return NULL; // 出错时返回 NULL
}
 
// 测试函数，创建多个线程并检查 pthread_once 的行为
static void *ThreadFuncTest(void *arg)
{
    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); // 检查 pthread_create 的返回值
    }
 
    // 等待所有线程结束
    for (i = 0; i < threadsNum; ++i) {
        rc = pthread_join(thread[i], &status);
        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; // 出错时返回 NULL
}
 
// 测试用例函数，设置线程属性并启动测试线程
static int Testcase(void)
{
    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, &param);
    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, &param); // 设置线程调度参数
    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); // 检查 pthread_join 的返回值
 
    return 0; // 测试成功结束
}
 
// 注册测试用例
void ItTestPthreadOnce001(void)
{
    TEST_ADD_CASE("IT_PTHREAD_ONCE_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION); // 使用测试框架的宏注册测试用例
}
