• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_pthread_test.h"
32 
33 static int g_number = 0;
34 static int g_okStatus = 777; // 777, a special number indicate the status is ok.
35 static pthread_once_t g_onceCtrl = PTHREAD_ONCE_INIT;
36 
InitRoutine(void)37 static void InitRoutine(void)
38 {
39     g_number++;
40 }
41 
Threadfunc(void * parm)42 static void *Threadfunc(void *parm)
43 {
44     int err;
45     err = pthread_once(&g_onceCtrl, InitRoutine);
46     ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
47     return reinterpret_cast<void *>(g_okStatus);
48 EXIT:
49     return NULL;
50 }
51 
ThreadFuncTest(void * arg)52 static void *ThreadFuncTest(void *arg)
53 {
54     pthread_t thread[3];
55     int rc = 0;
56     int i = 3;
57     void *status;
58     const int threadsNum = 3;
59 
60     g_number = 0;
61 
62     for (i = 0; i < threadsNum; ++i) {
63         rc = pthread_create(&thread[i], NULL, Threadfunc, NULL);
64         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
65     }
66 
67     for (i = 0; i < threadsNum; ++i) {
68         rc = pthread_join(thread[i], &status);
69         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
70         ICUNIT_GOTO_EQUAL((unsigned int)status, (unsigned int)g_okStatus, status, EXIT);
71     }
72 
73     ICUNIT_GOTO_EQUAL(g_number, 1, g_number, EXIT);
74 
75 EXIT:
76     return NULL;
77 }
78 
Testcase(void)79 static int Testcase(void)
80 {
81     int ret;
82     pthread_t newPthread;
83     int curThreadPri, curThreadPolicy;
84     pthread_attr_t a = { 0 };
85     struct sched_param param = { 0 };
86 
87     g_onceCtrl = PTHREAD_ONCE_INIT;
88 
89     ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
90     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
91 
92     curThreadPri = param.sched_priority;
93 
94     ret = pthread_attr_init(&a);
95     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
96     param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
97     pthread_attr_setschedparam(&a, &param);
98     ret = pthread_create(&newPthread, &a, ThreadFuncTest, 0);
99     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
100 
101     ret = pthread_join(newPthread, NULL);
102     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
103 
104     return 0;
105 }
106 
ItTestPthreadOnce001(void)107 void ItTestPthreadOnce001(void)
108 {
109     TEST_ADD_CASE("IT_PTHREAD_ONCE_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
110 }
111