修复潜在问题

This commit is contained in:
zhangzheng
2025-02-13 20:45:46 +08:00
parent fe4e4a377d
commit 72cbd3d0cd
5 changed files with 74 additions and 81 deletions

View File

@@ -1,13 +1,13 @@
#include "u_factory.h"
#include "u_sema.h"
#include "u_hd_man.h"
#include "u_sema.h"
#include "u_task.h"
#include <u_sleep.h>
#include <CuTest.h>
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <CuTest.h>
#include <u_sleep.h>
#include <unistd.h>
int u_sema_test(void)
@@ -34,14 +34,12 @@ static obj_handler_t sema_hd2;
static void *thread_th1(void *arg)
{
int j = 0;
while (1)
{
while (1) {
printf("sema_up start\n");
u_sema_up(sema_hd2);
u_sleep_ms(100);
printf("sema_up end\n");
if (j == TEST_CN * 2 + 2)
{
if (j == TEST_CN * 2 + 2) {
break;
}
j++;
@@ -52,14 +50,12 @@ static void *thread_th1(void *arg)
static void *thread_th2(void *arg)
{
int j = 0;
while (1)
{
while (1) {
printf("sema_down start\n");
u_sema_down(sema_hd2, 0, NULL);
u_sleep_ms(50);
printf("sema_down end\n");
if (j == TEST_CN)
{
if (j == TEST_CN) {
break;
}
j++;
@@ -70,14 +66,12 @@ static void *thread_th2(void *arg)
static void *thread_th3(void *arg)
{
int j = 0;
while (1)
{
while (1) {
printf("sema_down2 start\n");
u_sema_down(sema_hd2, 0, NULL);
u_sleep_ms(50);
printf("sema_down2 end\n");
if (j == TEST_CN)
{
if (j == TEST_CN) {
break;
}
j++;
@@ -97,25 +91,39 @@ static void u_sema_test2(CuTest *tc)
CuAssert(tc, "pthread_create fail.\n", pthread_create(&pth1, NULL, thread_th1, NULL) == 0);
CuAssert(tc, "pthread_create fail.\n", pthread_create(&pth2, NULL, thread_th2, NULL) == 0);
CuAssert(tc, "pthread_create fail.\n", pthread_create(&pth3, NULL, thread_th3, NULL) == 0);
if (pth1 != PTHREAD_NULL)
{
if (pth1 != PTHREAD_NULL) {
CuAssert(tc, "pthread_join fail.\n", pthread_join(pth1, NULL) == 0);
}
if (pth2 != PTHREAD_NULL)
{
if (pth2 != PTHREAD_NULL) {
CuAssert(tc, "pthread_join fail.\n", pthread_join(pth2, NULL) == 0);
}
if (pth3 != PTHREAD_NULL)
{
if (pth3 != PTHREAD_NULL) {
CuAssert(tc, "pthread_join fail.\n", pthread_join(pth3, NULL) == 0);
}
}
static void u_sema_test3(CuTest *tc)
{
msg_tag_t tag;
obj_handler_t sema_hd2;
umword_t reamin_times;
tag = facotry_create_sema(FACTORY_PROT,
vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, sema_hd2), 0, 1);
CuAssert(tc, "hd alloc fail.\n", msg_tag_get_val(tag) >= 0);
for (int i = 0; i < 5; i++) {
tag = u_sema_down(sema_hd2, 100, &reamin_times);
CuAssert(tc, "sema down fail.\n", msg_tag_get_val(tag) >= 0);
CuAssert(tc, "sema down fail.\n", reamin_times == 0);
}
handler_free_umap(sema_hd2);
}
static CuSuite suite;
CuSuite *sema_test_suite(void)
{
CuSuiteInit(&suite);
SUITE_ADD_TEST(&suite, u_sema_test2);
SUITE_ADD_TEST(&suite, u_sema_test3);
return &suite;
}