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

CMSIS RTOS v1 Signals Implementation Issue #27279

Closed
anthonytex opened this issue Jul 31, 2020 · 3 comments
Closed

CMSIS RTOS v1 Signals Implementation Issue #27279

anthonytex opened this issue Jul 31, 2020 · 3 comments
Assignees
Labels
area: Portability Standard compliant code, toolchain abstraction bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale

Comments

@anthonytex
Copy link
Contributor

Hi Zephyr devs,

yesterday i've try zephyr for the first time.
My idea was to port a simple firmware that is written with CMSIS RTOS API v1 to Zephyr.
For this, i'm using zephyr's portability layer: CONFIG_CMSIS_RTOS_V1=y.

This firmware uses signals API to sync threads, but something is not working, here there is a simple program to test the issue:

#include <kernel.h>
#include <cmsis_os.h>

void print_thread(void const *args)
{
    printk("Started Thread 1, waiting for signals\n");

    while (true)
    {
        osSignalWait(0x1, osWaitForever);
        printk("Signal Received\n");
    }
}

osThreadDef(print_thread, osPriorityNormal, 1, 256);

int main(void)
{
    osThreadId tid = osThreadCreate(osThread(print_thread), NULL);
    //Send signals every second
    while (true)
    {
        printk("Waiting\n");
        osDelay(1000);
        osSignalSet(tid, 0x1);
    }
}

Digging in, i've found 2 problems:

  1. when the thread is created:
    k_thread_custom_data_set((void *)thread_def);

    the custom data that is used for signals handling in your CMSIS implementation is, for what i can see, set for the current running thread and not for the created new thread: so when you call
    osThreadDef_t *thread_def = k_thread_custom_data_get();
    you are pointing to other thread data.
  2. By assuming that the custom data was properly set, then when the thread is created by calling
    tid = k_thread_create(&cm_thread[thread_def->instances],
    the custom data is reset by this
    new_thread->custom_data = NULL;

So signals handling for me is not working, a simple hack fixes the problem (only for demostration, sorry but i've not a deep knowledge of zephyr so feel free to fix this in a better way of course):

cm_thread = thread_def->cm_thread;
	atomic_dec((atomic_t *)&thread_def->instances);
	stk_ptr = thread_def->stack_mem;
	prio = cmsis_to_zephyr_priority(thread_def->tpriority);
	//k_thread_custom_data_set((void *)thread_def);

	tid = k_thread_create(&cm_thread[thread_def->instances],
			stk_ptr[thread_def->instances], stacksz,
			(k_thread_entry_t)zephyr_thread_wrapper,
			(void *)arg, NULL, thread_def->pthread,
			prio, 0, K_NO_WAIT);
       tid->custom_data = (void *)thread_def;

Also your test suit does not cover this case, in fact the current thread is waiting to be signaled and not the newly created like in my example:

void test_signal_events_no_wait(void)
{
osThreadId id1;
osEvent evt;
id1 = osThreadCreate(osThread(Thread_1), osThreadGetId());
zassert_true(id1 != NULL, "Thread creation failed");
/* Let id1 run to trigger SIGNAL1 */
osDelay(10);
/* wait for SIGNAL1. It should return immediately as it is
* already triggered.
*/
evt = osSignalWait(SIGNAL1, 0);
zassert_equal(evt.status, osEventSignal, "");
zassert_equal((evt.value.signals & SIGNAL1), SIGNAL1, "");
osThreadTerminate(id1);
}

Thanks

@carlescufi carlescufi added area: Portability Standard compliant code, toolchain abstraction bug The issue is a bug, or the PR is fixing a bug labels Jul 31, 2020
@carlescufi
Copy link
Member

@anthonytex thanks for the extensive analysis. Perhaps you'd like to send a Pull Request fixing the behavior?

@anthonytex
Copy link
Contributor Author

Hi @carlescufi , i've just open a PR but for another issue. In these days i'm going to find some time to send a PR to fix this.
Thanks

@MaureenHelm MaureenHelm added the priority: low Low impact/importance bug label Aug 4, 2020
anthonytex added a commit to anthonytex/zephyr that referenced this issue Aug 13, 2020
This commit fixes zephyrproject-rtos#27279
The custom_data of a thread was wrongly assigned, by calling
osThreadCreate, to the thread who was spawning a new thread
and not to the new one.

Signed-off-by: Antonio Tessarolo <[email protected]>
@rljordan rljordan assigned ceolin and unassigned andrewboie Sep 16, 2020
@ceolin ceolin added the has-pr label Sep 23, 2020
@github-actions
Copy link

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Portability Standard compliant code, toolchain abstraction bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants