Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: posix: common: do not overspecify pthread_attr_t #67094

Merged
Show file tree
Hide file tree
Changes from 5 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
171 changes: 35 additions & 136 deletions tests/posix/common/src/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,102 +10,54 @@
#include <zephyr/ztest.h>

#define N_THR 2
#define N_KEY 2
#define STACKSZ (MAX(1024, PTHREAD_STACK_MIN) + CONFIG_TEST_EXTRA_STACK_SIZE)
#define N_KEY 2
#define BUFFSZ 48

K_THREAD_STACK_ARRAY_DEFINE(stackp, N_THR, STACKSZ);

pthread_key_t key, keys[N_KEY];
static pthread_key_t key;
static pthread_key_t keys[N_KEY];
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
static pthread_once_t keys_once = PTHREAD_ONCE_INIT;

void *thread_top(void *p1)
static void *thread_top(void *p1)
{
int ret = -1;

void *value;
void *getval;
char *buffer[BUFFSZ];

value = k_malloc(sizeof(buffer));

zassert_true((int) POINTER_TO_INT(value),
"thread could not allocate storage");

ret = pthread_setspecific(key, value);

/* TESTPOINT: Check if thread's value is associated with key */
zassert_false(ret, "pthread_setspecific failed");

getval = 0;

getval = pthread_getspecific(key);

/* TESTPOINT: Check if pthread_getspecific returns the same value
* set by pthread_setspecific
*/
zassert_equal(value, getval,
"set and retrieved values are different");

printk("set value = %d and retrieved value = %d\n",
(int) POINTER_TO_INT(value), (int) POINTER_TO_INT(getval));
zassert_not_null(value, "thread could not allocate storage");
zassert_ok(pthread_setspecific(key, value), "pthread_setspecific failed");
zassert_equal(pthread_getspecific(key), value, "set and retrieved values are different");
k_free(value);

return NULL;
}

void *thread_func(void *p1)
static void *thread_func(void *p1)
{
int i, ret = -1;

void *value;
void *getval;
char *buffer[BUFFSZ];

value = k_malloc(sizeof(buffer));

zassert_true((int) POINTER_TO_INT(value),
"thread could not allocate storage");

for (i = 0; i < N_KEY; i++) {
ret = pthread_setspecific(keys[i], value);

/* TESTPOINT: Check if thread's value is associated with keys */
zassert_false(ret, "pthread_setspecific failed");
}

for (i = 0; i < N_KEY; i++) {
getval = 0;
getval = pthread_getspecific(keys[i]);

/* TESTPOINT: Check if pthread_getspecific returns the same
* value set by pthread_setspecific for each of the keys
*/
zassert_equal(value, getval,
"set and retrieved values are different");

printk("key %d: set value = %d and retrieved value = %d\n",
i, (int) POINTER_TO_INT(value),
(int) POINTER_TO_INT(getval));
zassert_not_null(value, "thread could not allocate storage");
for (int i = 0; i < N_KEY; i++) {
zassert_ok(pthread_setspecific(keys[i], value), "pthread_setspecific failed");
zassert_equal(pthread_getspecific(keys[i]), value,
"set and retrieved values are different");
}
k_free(value);
return NULL;
}

static void make_key(void)
{
int ret = 0;

ret = pthread_key_create(&key, NULL);
zassert_false(ret, "insufficient memory to create key");
zassert_ok(pthread_key_create(&key, NULL), "insufficient memory to create key");
}

static void make_keys(void)
{
int i, ret = 0;

for (i = 0; i < N_KEY; i++) {
ret = pthread_key_create(&keys[i], NULL);
zassert_false(ret, "insufficient memory to create keys");
for (int i = 0; i < N_KEY; i++) {
zassert_ok(pthread_key_create(&keys[i], NULL),
"insufficient memory to create keys");
}
}

Expand All @@ -124,94 +76,41 @@ static void make_keys(void)

ZTEST(posix_apis, test_key_1toN_thread)
{
int i, ret = -1;

pthread_attr_t attr[N_THR];
struct sched_param schedparam;
pthread_t newthread[N_THR];
void *retval;
pthread_t newthread[N_THR];

ret = pthread_once(&key_once, make_key);

/* TESTPOINT: Check if key is created */
zassert_false(ret, "attempt to create key failed");

printk("\nDifferent threads set different values to same key:\n");

/* Creating threads with lowest application priority */
for (i = 0; i < N_THR; i++) {
ret = pthread_attr_init(&attr[i]);
if (ret != 0) {
zassert_false(pthread_attr_destroy(&attr[i]),
"Unable to destroy pthread object attr");
zassert_false(pthread_attr_init(&attr[i]),
"Unable to create pthread object attr");
}

schedparam.sched_priority = 2;
pthread_attr_setschedparam(&attr[i], &schedparam);
pthread_attr_setstack(&attr[i], &stackp[i][0], STACKSZ);
zassert_ok(pthread_once(&key_once, make_key), "attempt to create key failed");

ret = pthread_create(&newthread[i], &attr[i], thread_top,
INT_TO_POINTER(i));
/* Different threads set different values to same key */

/* TESTPOINT: Check if threads are created successfully */
zassert_false(ret, "attempt to create threads failed");
for (int i = 0; i < N_THR; i++) {
zassert_ok(pthread_create(&newthread[i], NULL, thread_top, NULL),
"attempt to create thread %d failed", i);
}

for (i = 0; i < N_THR; i++) {
printk("thread %d: ", i);
pthread_join(newthread[i], &retval);
for (int i = 0; i < N_THR; i++) {
zassert_ok(pthread_join(newthread[i], &retval), "failed to join thread %d", i);
}

ret = pthread_key_delete(key);

/* TESTPOINT: Check if key is deleted */
zassert_false(ret, "attempt to delete key failed");
printk("\n");
zassert_ok(pthread_key_delete(key), "attempt to delete key failed");
}

ZTEST(posix_apis, test_key_Nto1_thread)
{
int i, ret = -1;

pthread_attr_t attr;
struct sched_param schedparam;
pthread_t newthread;

ret = pthread_once(&keys_once, make_keys);

/* TESTPOINT: Check if keys are created successfully */
zassert_false(ret, "attempt to create keys failed");

printk("\nSingle thread associates its value with different keys:\n");
ret = pthread_attr_init(&attr);
if (ret != 0) {
zassert_false(pthread_attr_destroy(&attr),
"Unable to destroy pthread object attr");
zassert_false(pthread_attr_init(&attr),
"Unable to create pthread object attr");
}

schedparam.sched_priority = 2;
pthread_attr_setschedparam(&attr, &schedparam);
pthread_attr_setstack(&attr, &stackp[0][0], STACKSZ);

ret = pthread_create(&newthread, &attr, thread_func,
(void *)0);
zassert_ok(pthread_once(&keys_once, make_keys), "attempt to create keys failed");

/*TESTPOINT: Check if thread is created successfully */
zassert_false(ret, "attempt to create thread failed");
/* Single thread associates its value with different keys */

pthread_join(newthread, NULL);
zassert_ok(pthread_create(&newthread, NULL, thread_func, NULL),
"attempt to create thread failed");

for (i = 0; i < N_KEY; i++) {
ret = pthread_key_delete(keys[i]);
zassert_ok(pthread_join(newthread, NULL), "failed to join thread");

/* TESTPOINT: Check if keys are deleted */
zassert_false(ret, "attempt to delete keys failed");
for (int i = 0; i < N_KEY; i++) {
zassert_ok(pthread_key_delete(keys[i]), "attempt to delete keys[%d] failed", i);
}
printk("\n");
}

ZTEST(posix_apis, test_key_resource_leak)
Expand Down
33 changes: 9 additions & 24 deletions tests/posix/common/src/mqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
#include <zephyr/sys/util.h>
#include <zephyr/ztest.h>

#define N_THR 2
#define STACKSZ (MAX(1024, PTHREAD_STACK_MIN) + CONFIG_TEST_EXTRA_STACK_SIZE)
#define N_THR 2
#define SENDER_THREAD 0
#define RECEIVER_THREAD 1
#define MESSAGE_SIZE 16
#define MESG_COUNT_PERMQ 4

K_THREAD_STACK_ARRAY_DEFINE(stacks, N_THR, STACKSZ);

char queue[16] = "server";

char send_data[MESSAGE_SIZE] = "timed data send";
Expand Down Expand Up @@ -60,7 +57,8 @@ void *receiver_thread(void *p1)
clock_gettime(CLOCK_MONOTONIC, &curtime);
curtime.tv_sec += 1;
mq_timedreceive(mqd, rec_data, MESSAGE_SIZE, 0, &curtime);
zassert_false(strcmp(rec_data, send_data), "Error in data reception");
zassert_false(strcmp(rec_data, send_data), "Error in data reception. exp: %s act: %s",
send_data, rec_data);
usleep(USEC_PER_MSEC);
zassert_false(mq_close(mqd),
"unable to close message queue descriptor.");
Expand All @@ -72,38 +70,25 @@ ZTEST(posix_apis, test_mqueue)
{
mqd_t mqd;
struct mq_attr attrs;
int32_t mode = 0777, flags = O_RDWR | O_CREAT, ret, i;
int32_t mode = 0777;
int flags = O_RDWR | O_CREAT;
void *retval;
pthread_attr_t attr[N_THR];
pthread_t newthread[N_THR];

attrs.mq_msgsize = MESSAGE_SIZE;
attrs.mq_maxmsg = MESG_COUNT_PERMQ;

mqd = mq_open(queue, flags, mode, &attrs);

for (i = 0; i < N_THR; i++) {
for (int i = 0; i < N_THR; i++) {
/* Creating threads */
zassert_ok(pthread_attr_init(&attr[i]));
pthread_attr_setstack(&attr[i], &stacks[i][0], STACKSZ);

if (i % 2) {
ret = pthread_create(&newthread[i], &attr[i],
sender_thread,
INT_TO_POINTER(i));
} else {
ret = pthread_create(&newthread[i], &attr[i],
receiver_thread,
INT_TO_POINTER(i));
}

zassert_false(ret, "Not enough space to create new thread");
zassert_equal(pthread_attr_destroy(&attr[i]), 0);
zassert_ok(pthread_create(&newthread[i], NULL,
(i % 2 == 0) ? receiver_thread : sender_thread, NULL));
}

usleep(USEC_PER_MSEC * 10U);

for (i = 0; i < N_THR; i++) {
for (int i = 0; i < N_THR; i++) {
pthread_join(newthread[i], &retval);
}

Expand Down
Loading
Loading