Skip to content

Commit f585842

Browse files
committed
components:libc:cplusplus:os/utest:Some comments have been initially added.
Some comments have been initially added. 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<2739960959@qq.com>
1 parent 9e31279 commit f585842

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

components/libc/cplusplus/os/cxx_Semaphore.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,45 @@
1111

1212
using namespace rtthread;
1313

14+
/**
15+
* @brief Semaphore class implementation.
16+
*/
1417
Semaphore::Semaphore(const char *name, int32_t count)
1518
{
19+
// Initialize the semaphore with a specified count and FIFO order.
1620
rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
1721
}
1822

23+
/**
24+
* @brief Wait on the semaphore.
25+
* @param millisec Timeout in milliseconds.
26+
* @return Boolean indicating if the semaphore was successfully taken.
27+
*/
1928
bool Semaphore::wait(int32_t millisec)
2029
{
2130
rt_int32_t tick;
2231

32+
// Convert milliseconds to system ticks.
2333
if (millisec < 0)
2434
tick = -1;
2535
else
2636
tick = rt_tick_from_millisecond(millisec);
2737

38+
// Attempt to take the semaphore.
2839
return rt_sem_take(&mID, tick) == RT_EOK;
2940
}
3041

42+
/**
43+
* @brief Release the semaphore.
44+
*/
3145
void Semaphore::release(void)
3246
{
3347
rt_sem_release(&mID);
3448
}
3549

50+
/**
51+
* Detach the semaphore when the object is destroyed.
52+
*/
3653
Semaphore::~Semaphore()
3754
{
3855
rt_sem_detach(&mID);

components/libc/cplusplus/os/cxx_Thread.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
using namespace rtthread;
1313

14+
/**
15+
* @brief Thread class constructor with parameters for stack size, priority, tick, and name.
16+
*/
1417
Thread::Thread(rt_uint32_t stack_size,
1518
rt_uint8_t priority,
1619
rt_uint32_t tick,
@@ -27,6 +30,9 @@ Thread::Thread(rt_uint32_t stack_size,
2730
tick);
2831
}
2932

33+
/**
34+
* @brief Thread class constructor with entry function and parameters.
35+
*/
3036
Thread::Thread(void (*entry)(void *p),
3137
void *p,
3238
rt_uint32_t stack_size,
@@ -45,12 +51,19 @@ Thread::Thread(void (*entry)(void *p),
4551
tick);
4652
}
4753

54+
/**
55+
* @brief Detach the event and delete the thread when the object is destroyed.
56+
*/
4857
Thread::~Thread()
4958
{
5059
rt_event_detach(&_event);
5160
rt_thread_delete(_thread);
5261
}
5362

63+
/**
64+
* @brief Start the thread execution.
65+
* @return Boolean indicating if the thread was successfully started.
66+
*/
5467
bool Thread::start()
5568
{
5669
if (rt_thread_startup(_thread) == RT_EOK)
@@ -61,20 +74,30 @@ bool Thread::start()
6174
return started;
6275
}
6376

77+
/**
78+
* Make the thread sleep for a specified duration.
79+
* @param millisec Duration in milliseconds.
80+
*/
6481
void Thread::sleep(int32_t millisec)
6582
{
6683
rt_int32_t tick;
6784

85+
// Convert milliseconds to system ticks.
6886
if (millisec < 0)
6987
tick = 1;
7088
else
7189
tick = rt_tick_from_millisecond(millisec);
7290

91+
// Delay the thread for a specified number of ticks.
7392
rt_thread_delay(tick);
7493
}
7594

95+
/**
96+
* Static function to run the thread's entry function.
97+
*/
7698
void Thread::func(Thread *pThis)
7799
{
100+
// If an entry function is provided, execute it.
78101
if (pThis->_entry != RT_NULL)
79102
{
80103
pThis->_entry(pThis->_param);
@@ -84,30 +107,46 @@ void Thread::func(Thread *pThis)
84107
pThis->run(pThis->_param);
85108
}
86109

110+
// Send an event to signal thread completion.
87111
rt_event_send(&pThis->_event, 1);
88112
}
89113

114+
/**
115+
* Default run function that can be overridden by subclasses.
116+
*/
90117
void Thread::run(void *parameter)
91118
{
92119
/* please overload this method */
93120
}
94121

122+
/**
123+
* Wait for the thread to complete with a timeout.
124+
* @param millisec Timeout in milliseconds.
125+
* @return Status code indicating the execution status.
126+
*/
95127
rt_err_t Thread::wait(int32_t millisec)
96128
{
97129
return join(millisec);
98130
}
99131

132+
/**
133+
* Join the thread with a timeout.
134+
* @param millisec Timeout in milliseconds.
135+
* @return Status code indicating the execution status.
136+
*/
100137
rt_err_t Thread::join(int32_t millisec)
101138
{
102139
if (started)
103140
{
104141
rt_int32_t tick;
105142

143+
// Convert milliseconds to system ticks.
106144
if (millisec < 0)
107145
tick = -1;
108146
else
109147
tick = rt_tick_from_millisecond(millisec);
110148

149+
// Wait for the thread's completion event.
111150
return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL);
112151
}
113152
else

components/libc/cplusplus/utest/tc_atomic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,13 @@ static void test_compare_exchange_weak(void)
238238
}
239239
uassert_int_equal(val.load(), 2);
240240
}
241-
241+
// Test case initialization function.
242242
static rt_err_t utest_tc_init(void)
243243
{
244244
return RT_EOK;
245245
}
246246

247+
// Test case cleanup function.
247248
static rt_err_t utest_tc_cleanup(void)
248249
{
249250
return RT_EOK;

components/libc/cplusplus/utest/tc_smartptr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ static void test_shared_ptr(void)
3535
uassert_int_equal(p1.use_count(), 2);
3636
}
3737

38+
/**
39+
* @brief Test case initialization function.
40+
*/
3841
static rt_err_t utest_tc_init(void)
3942
{
4043
return RT_EOK;
4144
}
45+
/**
46+
* @brief Test case cleanup function.
47+
*/
4248

4349
static rt_err_t utest_tc_cleanup(void)
4450
{

components/libc/cplusplus/utest/tc_thread.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,71 @@
1212
#include "utest.h"
1313
#include <thread>
1414

15+
16+
/**
17+
* @brief Function to test thread functionality.
18+
*/
1519
static void test_thread(void)
1620
{
17-
int count = 0;
21+
int count = 0; // Initialize a counter to zero.
22+
23+
// Lambda function to increment the count.
1824
auto func = [&]() mutable
1925
{
2026
for (int i = 0; i < 100; ++i)
2127
{
22-
++count;
28+
++count; // Increment the count 100 times.
2329
}
2430
};
2531

32+
// Create and run a thread executing the lambda function.
2633
std::thread t1(func);
27-
t1.join();
34+
t1.join(); // Wait for the thread to finish execution.
2835

36+
// Verify if the count is as expected after the first thread execution.
2937
if (count != 100)
3038
{
31-
uassert_false(1);
39+
uassert_false(1); // Assert failure if count is not 100.
3240
}
3341

42+
// Create and run another thread executing the same lambda function.
3443
std::thread t2(func);
35-
t2.join();
44+
t2.join(); // Wait for the second thread to finish execution.
3645

46+
// Verify if the count is as expected after the second thread execution.
3747
if (count != 200)
3848
{
39-
uassert_false(1);
49+
uassert_false(1); // Assert failure if count is not 200.
4050
}
4151

52+
// If both assertions passed, the test is successful.
4253
uassert_true(1);
4354
}
4455

56+
57+
/**
58+
* @brief Test case initialization function.
59+
*/
60+
4561
static rt_err_t utest_tc_init(void)
4662
{
47-
return RT_EOK;
63+
return RT_EOK;
4864
}
49-
65+
/**
66+
* @brief Test case cleanup function.
67+
*/
5068
static rt_err_t utest_tc_cleanup(void)
5169
{
52-
return RT_EOK;
70+
return RT_EOK;
5371
}
5472

73+
/**
74+
* @brief Main test case function that runs the test.
75+
*/
5576
static void testcase(void)
5677
{
5778
UTEST_UNIT_RUN(test_thread);
5879
}
80+
81+
// Export the test case with initialization and cleanup functions and a timeout of 10 ticks.
5982
UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)