Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions examples/pipe/interlock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ static void *null_writer(pthread_addr_t pvarg)

int interlock_test(void)
{
pthread_t readerid;
pthread_t writerid;
pthread_t tid;
void *value;
char data[16];
ssize_t nbytes;
Expand All @@ -171,7 +170,7 @@ int interlock_test(void)
/* Start the null_writer thread */

printf("interlock_test: Starting null_writer thread\n");
ret = pthread_create(&writerid, NULL, null_writer, NULL);
ret = pthread_create(&tid, NULL, null_writer, NULL);
if (ret != 0)
{
fprintf(stderr, \
Expand All @@ -194,7 +193,7 @@ int interlock_test(void)
"errno=%d\n",
FIFO_PATH2, errno);
ret = 3;
goto errout_with_null_writer_thread;
goto errout_with_null_thread;
}

/* Attempt to read one byte from the FIFO. This should return end-of-file
Expand All @@ -208,15 +207,15 @@ int interlock_test(void)
fprintf(stderr, \
"interlock_test: read failed, errno=%d\n", errno);
ret = 4;
goto errout_with_null_writer_thread;
goto errout_with_file;
}
else if (ret != 0)
else if (nbytes != 0)
{
fprintf(stderr, \
"interlock_test: Read %ld bytes of data -- aborting: %d\n",
(long)nbytes, errno);
ret = 5;
goto errout_with_null_writer_thread;
goto errout_with_file;
}

printf("interlock_test: read returned\n");
Expand All @@ -234,7 +233,7 @@ int interlock_test(void)
/* Wait for null_writer thread to complete */

printf("interlock_test: Waiting for null_writer thread\n");
ret = pthread_join(writerid, &value);
ret = pthread_join(tid, &value);
if (ret != 0)
{
fprintf(stderr, \
Expand All @@ -255,7 +254,7 @@ int interlock_test(void)
/* Start the null_reader thread */

printf("interlock_test: Starting null_reader thread\n");
ret = pthread_create(&readerid, NULL, null_reader, NULL);
ret = pthread_create(&tid, NULL, null_reader, NULL);
if (ret != 0)
{
fprintf(stderr, \
Expand All @@ -278,29 +277,26 @@ int interlock_test(void)
"errno=%d\n",
FIFO_PATH2, errno);
ret = 9;
goto errout_with_null_reader_thread;
goto errout_with_null_thread;
}

/* Attempt to write one byte from the FIFO. This should return 0 bytes
* written because the null_reader closes the FIFO.
/* Attempt to write one byte from the FIFO. This should return n bytes
* written since the null_reader has opened the FIFO.
*/

printf("interlock_test: Writing to %s\n", FIFO_PATH2);
nbytes = write(fd, data, 16);
if (nbytes < 0)
if (nbytes <= 0)
{
fprintf(stderr, \
"interlock_test: write failed, errno=%d\n", errno);
ret = 10;
goto errout_with_null_reader_thread;
goto errout_with_file;
}
else if (ret != 0)
else
{
fprintf(stderr, \
"interlock_test: Wrote %ld bytes of data -- aborting: %d\n",
(long)nbytes, errno);
ret = 11;
goto errout_with_null_reader_thread;
"interlock_test: Wrote %ld bytes of data\n", (long)nbytes);
}

printf("interlock_test: write returned\n");
Expand All @@ -318,7 +314,7 @@ int interlock_test(void)
/* Wait for null_reader thread to complete */

printf("interlock_test: Waiting for null_reader thread\n");
ret = pthread_join(readerid, &value);
ret = pthread_join(tid, &value);
if (ret != 0)
{
fprintf(stderr, \
Expand All @@ -339,20 +335,19 @@ int interlock_test(void)
ret = 0;
goto errout_with_fifo;

errout_with_null_reader_thread:
pthread_detach(readerid);
pthread_cancel(readerid);
errout_with_file:
if (close(fd) != 0)
{
fprintf(stderr, "interlock_test: close failed: %d\n", errno);
}

errout_with_null_writer_thread:
pthread_detach(writerid);
pthread_cancel(writerid);
errout_with_null_thread:
pthread_detach(tid);
pthread_cancel(tid);

errout_with_fifo:

if (fd != -1 && close(fd) != 0)
{
fprintf(stderr, "interlock_test: close failed: %d\n", errno);
}
/* Close the file */

ret = remove(FIFO_PATH2);
if (ret != 0)
Expand Down
23 changes: 23 additions & 0 deletions testing/fs/memfd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ##############################################################################
# apps/testing/fs/memfd/CMakeLists.txt
#
# 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.
#
# ##############################################################################

if(CONFIG_TESTING_MEMFD)
nuttx_add_application(NAME memfd SRCS memfd_test.c)
endif()
11 changes: 11 additions & 0 deletions testing/fs/memfd/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config TESTING_MEMFD
bool "Testing the stability of memfd"
depends on !LIBC_MEMFD_ERROR && MM_KASAN
default n
---help---
Enable to test the stability of memfd
23 changes: 23 additions & 0 deletions testing/fs/memfd/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/testing/fs/memfd/Make.defs
#
# 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.
#
############################################################################

ifneq ($(CONFIG_TESTING_MEMFD),)
CONFIGURED_APPS += $(APPDIR)/testing/fs/memfd
endif
29 changes: 29 additions & 0 deletions testing/fs/memfd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
############################################################################
# apps/testing/fs/memfd/Makefile
#
# 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.
#
############################################################################

include $(APPDIR)/Make.defs

PROGNAME = memfd_test
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)

MAINSRC = memfd_test.c

include $(APPDIR)/Application.mk
87 changes: 87 additions & 0 deletions testing/fs/memfd/memfd_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/****************************************************************************
* apps/testing/fs/memfd/memfd_test.c
*
* 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 <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define BUFFER_SIZE 8
#define THREAD_NUM 50
#define LOOP_NUM 1000

/****************************************************************************
* Private Functions
****************************************************************************/

static void *func(FAR void *arg)
{
void *buf;
int size = BUFFER_SIZE;
int i;
int memfd;

for (i = 0; i < LOOP_NUM; i++)
{
memfd = memfd_create("optee", O_CREAT | O_CLOEXEC);
ftruncate(memfd, size);
buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
close(memfd);

*((int *)buf) = 0xdeadbeef;
usleep(10);
*((int *)buf) = 0xdeadbeef;

munmap(buf, size);
}

printf("thread %d test pass!\n", pthread_self());
return NULL;
}

/****************************************************************************
* Public Functions
****************************************************************************/

int main(int argc, FAR char *argv[])
{
pid_t pid[THREAD_NUM];
int i;

for (i = 0; i < THREAD_NUM; i++)
{
pthread_create(&pid[i], NULL, func, NULL);
}

for (i = 0; i < THREAD_NUM; i++)
{
pthread_join(pid[i], NULL);
}

return 0;
}
33 changes: 24 additions & 9 deletions testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
#include <cmocka.h>
#include "SchedTest.h"

/****************************************************************************
* Private Type Declarations
****************************************************************************/

struct resource_s
{
pthread_mutex_t mutex;
int run_flag;
};

/****************************************************************************
* Private Functions
****************************************************************************/
Expand All @@ -50,13 +60,14 @@

static void *schedpthread07threadroutine(void *arg)
{
struct resource_s *resource = (struct resource_s *)arg;
int i;
pthread_mutex_t schedpthreadtest07_mutex = PTHREAD_MUTEX_INITIALIZER;

for (i = 0; i < 100; i++)
{
pthread_mutex_lock(&schedpthreadtest07_mutex);
(*((int *)arg))++;
pthread_mutex_unlock(&schedpthreadtest07_mutex);
pthread_mutex_lock(&resource->mutex);
resource->run_flag++;
pthread_mutex_unlock(&resource->mutex);
}

return NULL;
Expand All @@ -74,22 +85,26 @@ void test_nuttx_sched_pthread07(FAR void **state)
{
int res;
pthread_t pt_1, pt_2, pt_3;
int run_flag = 0;
struct resource_s resource =
{
.mutex = PTHREAD_MUTEX_INITIALIZER,
.run_flag = 0
};

res = pthread_create(&pt_1, NULL, (void *)schedpthread07threadroutine,
&run_flag);
&resource);
assert_int_equal(res, OK);
res = pthread_create(&pt_2, NULL, (void *)schedpthread07threadroutine,
&run_flag);
&resource);
assert_int_equal(res, OK);
res = pthread_create(&pt_3, NULL, (void *)schedpthread07threadroutine,
&run_flag);
&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(run_flag, 300);
assert_int_equal(resource.run_flag, 300);
}
Loading