Skip to content

Commit 170069b

Browse files
lct1001Rbb666
authored andcommitted
components:libc:cplusplus:os/utest:Some comments have been initially added.
Some comments have been initially added as a bignner task. components/libc/cplusplus/os/cxx_Semaphore.cpp components/libc/cplusplus/os/cxx_Thread.cpp components/libc/cplusplus/utest/tc_atomic.cpp components/libc/cplusplus/utest/tc_smartptr.cpp components/libc/cplusplus/utest/tc_thread.cpp Signed-off-by:Liu Chengtao<[email protected]>
1 parent b75828f commit 170069b

File tree

5 files changed

+132
-25
lines changed

5 files changed

+132
-25
lines changed

components/libc/cplusplus/os/cxx_Semaphore.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@
1111

1212
using namespace rtthread;
1313

14+
/**
15+
* @brief Semaphore class constructor.
16+
* @param name Semaphore name
17+
* @param count Initial semaphore count
18+
*/
1419
Semaphore::Semaphore(const char *name, int32_t count)
1520
{
1621
rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
1722
}
1823

24+
/**
25+
* @brief Wait on the semaphore.
26+
* @param millisec Timeout in milliseconds (-1 for infinite wait).
27+
* @return true if the semaphore was successfully taken, false on timeout.
28+
*/
1929
bool Semaphore::wait(int32_t millisec)
2030
{
2131
rt_int32_t tick;
@@ -28,11 +38,17 @@ bool Semaphore::wait(int32_t millisec)
2838
return rt_sem_take(&mID, tick) == RT_EOK;
2939
}
3040

41+
/**
42+
* @brief Release the semaphore.
43+
*/
3144
void Semaphore::release(void)
3245
{
3346
rt_sem_release(&mID);
3447
}
3548

49+
/**
50+
* @brief Detach the semaphore when the object is destroyed.
51+
*/
3652
Semaphore::~Semaphore()
3753
{
3854
rt_sem_detach(&mID);

components/libc/cplusplus/os/cxx_Thread.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
using namespace rtthread;
1313

14+
/**
15+
* @brief Thread class constructor with parameters for stack size, priority, tick, and name.
16+
* @param stack_size Stack size in bytes
17+
* @param priority Thread priority
18+
* @param tick Time slice in ticks
19+
* @param name Thread name
20+
*/
1421
Thread::Thread(rt_uint32_t stack_size,
1522
rt_uint8_t priority,
1623
rt_uint32_t tick,
@@ -27,6 +34,15 @@ Thread::Thread(rt_uint32_t stack_size,
2734
tick);
2835
}
2936

37+
/**
38+
* @brief Thread class constructor with entry function and parameters.
39+
* @param entry The entry function pointer for the thread.
40+
* @param p The parameter to pass to the entry function.
41+
* @param stack_size The size of the thread stack in bytes.
42+
* @param priority The priority of the thread.
43+
* @param tick The time slice (tick) for the thread.
44+
* @param name The name of the thread.
45+
*/
3046
Thread::Thread(void (*entry)(void *p),
3147
void *p,
3248
rt_uint32_t stack_size,
@@ -45,12 +61,19 @@ Thread::Thread(void (*entry)(void *p),
4561
tick);
4662
}
4763

64+
/**
65+
* @brief Detach the event and delete the thread when the object is destroyed.
66+
*/
4867
Thread::~Thread()
4968
{
5069
rt_event_detach(&_event);
5170
rt_thread_delete(_thread);
5271
}
5372

73+
/**
74+
* @brief Start the thread execution.
75+
* @return true if the thread was successfully started.
76+
*/
5477
bool Thread::start()
5578
{
5679
if (rt_thread_startup(_thread) == RT_EOK)
@@ -61,6 +84,10 @@ bool Thread::start()
6184
return started;
6285
}
6386

87+
/**
88+
* @brief Make the thread sleep for a specified duration.
89+
* @param millisec Duration in milliseconds.
90+
*/
6491
void Thread::sleep(int32_t millisec)
6592
{
6693
rt_int32_t tick;
@@ -73,6 +100,9 @@ void Thread::sleep(int32_t millisec)
73100
rt_thread_delay(tick);
74101
}
75102

103+
/**
104+
* @brief Function to run the thread's entry function.
105+
*/
76106
void Thread::func(Thread *pThis)
77107
{
78108
if (pThis->_entry != RT_NULL)
@@ -87,22 +117,34 @@ void Thread::func(Thread *pThis)
87117
rt_event_send(&pThis->_event, 1);
88118
}
89119

120+
/**
121+
* @brief Default run function that can be overridden by subclasses.
122+
*/
90123
void Thread::run(void *parameter)
91124
{
92125
/* please overload this method */
93126
}
94127

128+
/**
129+
* @brief Wait for the thread to complete with a timeout.
130+
* @param millisec Timeout in milliseconds.
131+
* @return RT_EOK if the thread completed within the timeout, error code otherwise.
132+
*/
95133
rt_err_t Thread::wait(int32_t millisec)
96134
{
97135
return join(millisec);
98136
}
99137

138+
/**
139+
* @brief Join the thread with a timeout.
140+
* @param millisec Timeout in milliseconds.
141+
* @return RT_EOK if the thread completed within the timeout, error code otherwise.
142+
*/
100143
rt_err_t Thread::join(int32_t millisec)
101144
{
102145
if (started)
103146
{
104147
rt_int32_t tick;
105-
106148
if (millisec < 0)
107149
tick = -1;
108150
else
@@ -114,4 +156,4 @@ rt_err_t Thread::join(int32_t millisec)
114156
{
115157
return -RT_ENOSYS;
116158
}
117-
}
159+
}

components/libc/cplusplus/utest/tc_atomic.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
static void test_atomic_load_store_int32(void)
2222
{
23-
constexpr int kRound = 10000000;
23+
constexpr int kRound = 10000000;
2424
std::atomic<int32_t> thread_count(0);
2525
std::atomic<int32_t> count(100);
2626
uassert_int_equal(count.load(), 100);
@@ -56,7 +56,7 @@ static void test_atomic_load_store_int32(void)
5656
*/
5757
static void test_atomic_load_store_int64(void)
5858
{
59-
constexpr int kRound = 10000000;
59+
constexpr int kRound = 10000000;
6060
std::atomic<int64_t> thread_count(0);
6161
std::atomic<int64_t> count(100);
6262
uassert_int_equal(count.load(), 100);
@@ -112,7 +112,7 @@ static void test_atomic_basic_int32(void)
112112
val.fetch_and(14);
113113
uassert_int_equal(val.load(), 10);
114114

115-
val.fetch_or(11); //1010
115+
val.fetch_or(11); /* 1010 */
116116
uassert_int_equal(val.load(), 11);
117117

118118
val.fetch_xor(4);
@@ -121,9 +121,9 @@ static void test_atomic_basic_int32(void)
121121
val.exchange(1);
122122
uassert_int_equal(val.load(), 1);
123123

124-
int32_t x = 2;
125-
int32_t y = 3;
126-
bool exchanged = val.compare_exchange_strong(x, y);
124+
int32_t x = 2;
125+
int32_t y = 3;
126+
bool exchanged = val.compare_exchange_strong(x, y);
127127
uassert_false(exchanged);
128128
uassert_int_equal(val.load(), 1);
129129
uassert_int_equal(x, 1);
@@ -159,7 +159,7 @@ static void test_atomic_basic_int64(void)
159159
val.fetch_and(14);
160160
uassert_int_equal(val.load(), 10);
161161

162-
val.fetch_or(11); //1010
162+
val.fetch_or(11); /* 1010 */
163163
uassert_int_equal(val.load(), 11);
164164

165165
val.fetch_xor(4);
@@ -168,9 +168,9 @@ static void test_atomic_basic_int64(void)
168168
val.exchange(1);
169169
uassert_int_equal(val.load(), 1);
170170

171-
int64_t x = 2;
172-
int64_t y = 3;
173-
bool exchanged = val.compare_exchange_strong(x, y);
171+
int64_t x = 2;
172+
int64_t y = 3;
173+
bool exchanged = val.compare_exchange_strong(x, y);
174174
uassert_false(exchanged);
175175
uassert_int_equal(val.load(), 1);
176176
uassert_int_equal(x, 1);
@@ -191,7 +191,7 @@ static void test_atomic_bool(void)
191191
flag.exchange(false);
192192
uassert_false(flag.load());
193193
bool expected = false;
194-
bool desired = true;
194+
bool desired = true;
195195
uassert_true(flag.compare_exchange_strong(expected, desired));
196196
uassert_true(flag.load());
197197
}
@@ -201,7 +201,7 @@ static void test_atomic_bool(void)
201201
*/
202202
static void test_atomic_pointer(void)
203203
{
204-
int a = 1, b = 2;
204+
int a = 1, b = 2;
205205
std::atomic<int *> ptr(&a);
206206
ptr.store(&b);
207207
uassert_int_equal(*ptr.load(), 2);
@@ -217,7 +217,7 @@ static void test_memory_order(void)
217217
{
218218
std::atomic<int> x(0);
219219
std::atomic<int> y(0);
220-
// Simple test for memory order
220+
/* Simple test for memory order */
221221
x.store(1, std::memory_order_release);
222222
y.store(2, std::memory_order_release);
223223
uassert_int_equal(x.load(std::memory_order_acquire), 1);
@@ -230,25 +230,36 @@ static void test_memory_order(void)
230230
static void test_compare_exchange_weak(void)
231231
{
232232
std::atomic<int> val(1);
233-
int expected = 1;
234-
int desired = 2;
233+
int expected = 1;
234+
int desired = 2;
235235
while (!val.compare_exchange_weak(expected, desired))
236236
{
237-
expected = 1; // reset
237+
/* Reset */
238+
expected = 1;
238239
}
239240
uassert_int_equal(val.load(), 2);
240241
}
241-
242+
/**
243+
* @brief Test case initialization function.
244+
* @return RT_EOK on success.
245+
*/
242246
static rt_err_t utest_tc_init(void)
243247
{
244248
return RT_EOK;
245249
}
246250

251+
/**
252+
* @brief Test case cleanup function.
253+
* @return RT_EOK on success.
254+
*/
247255
static rt_err_t utest_tc_cleanup(void)
248256
{
249257
return RT_EOK;
250258
}
251259

260+
/**
261+
* @brief Main test case function that runs the test.
262+
*/
252263
static void testcase(void)
253264
{
254265
/* Test load and store operations for int32_t atomic variables in multi-threaded environment */
@@ -268,4 +279,6 @@ static void testcase(void)
268279
/* Test compare_exchange_weak operation */
269280
UTEST_UNIT_RUN(test_compare_exchange_weak);
270281
}
271-
UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10);
282+
283+
/* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */
284+
UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10);

components/libc/cplusplus/utest/tc_smartptr.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,34 @@ static void test_shared_ptr(void)
3535
uassert_int_equal(p1.use_count(), 2);
3636
}
3737

38+
/**
39+
* @brief Test case initialization function.
40+
* @return RT_EOK on success.
41+
*/
3842
static rt_err_t utest_tc_init(void)
3943
{
4044
return RT_EOK;
4145
}
4246

47+
/**
48+
* @brief Test case cleanup function.
49+
* @return RT_EOK on success.
50+
*/
4351
static rt_err_t utest_tc_cleanup(void)
4452
{
4553
return RT_EOK;
4654
}
4755

56+
/**
57+
* @brief Main test case function that runs the test.
58+
*/
4859
static void testcase(void)
4960
{
5061
/* Test unique_ptr basic operations */
5162
UTEST_UNIT_RUN(test_unique_ptr);
5263
/* Test shared_ptr basic operations */
5364
UTEST_UNIT_RUN(test_shared_ptr);
5465
}
55-
UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);
66+
67+
/* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */
68+
UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)