nuttx-apps/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
wangzhi16 094b0f235a test/mutex: fix mutex testcases.
Multiple threads should use the same lock for resource protection.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2026-01-22 19:53:39 +08:00

110 lines
3.5 KiB
C

/****************************************************************************
* apps/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "SchedTest.h"
/****************************************************************************
* Private Type Declarations
****************************************************************************/
struct resource_s
{
pthread_mutex_t mutex;
int run_flag;
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: schedpthread07threadroutine
****************************************************************************/
static void *schedpthread07threadroutine(void *arg)
{
struct resource_s *resource = (struct resource_s *)arg;
int i;
for (i = 0; i < 100; i++)
{
pthread_mutex_lock(&resource->mutex);
resource->run_flag++;
pthread_mutex_unlock(&resource->mutex);
}
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: test_nuttx_sched_pthread07
****************************************************************************/
void test_nuttx_sched_pthread07(FAR void **state)
{
int res;
pthread_t pt_1, pt_2, pt_3;
struct resource_s resource =
{
.mutex = PTHREAD_MUTEX_INITIALIZER,
.run_flag = 0
};
res = pthread_create(&pt_1, NULL, (void *)schedpthread07threadroutine,
&resource);
assert_int_equal(res, OK);
res = pthread_create(&pt_2, NULL, (void *)schedpthread07threadroutine,
&resource);
assert_int_equal(res, OK);
res = pthread_create(&pt_3, NULL, (void *)schedpthread07threadroutine,
&resource);
assert_int_equal(res, OK);
pthread_join(pt_1, NULL);
pthread_join(pt_2, NULL);
pthread_join(pt_3, NULL);
sleep(5);
assert_int_equal(resource.run_flag, 300);
}