Having issue with using Mutexes

Discussions and support about ChibiOS/RT, the free embedded RTOS.
cipher
Posts: 24
Joined: Tue May 31, 2016 10:05 pm
Has thanked: 2 times
Been thanked: 2 times

Having issue with using Mutexes

Postby cipher » Sun Jun 05, 2016 6:27 pm

Hi Everyone
Another questions to the gurus!
I'm trying to use chMtxLock/chMtxUnlock api for basic mutex usage, however the code gets stuck in the infinite while loop (see picture attached)

The scenario is very simple.

A processing thread gets spawned, calls an API function from mbedtls library, which requires taking a mutex, doing some processing and releasing the mutex.
Mutex is being initialized, i did check that, and there is no problem taking the mutex. There is only one thread for now, so there are no other threads that I'm aware of that are trying to take the mutex at the same time yet. However, when the processing function tries to release the mutex it took, i get this error.
The interesting part is that i testing the same code but using the cmsis_os api, where the binary semaphores are being used, and it works without any issues.

Can anyone shed some light on this? What should i look for? I stepped through the code and it doesn't look like stack or a mutex object is being corrupted.
Any help is very appreciated.

The reason for trying muteness is that i might need them for other thinks as well as the recursive option on them, however recursive option doesn't make any difference in this problem behavior, it only works with semaphores.
Attachments
Mutex.JPG

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Having issue with using Mutexes

Postby Giovanni » Sun Jun 05, 2016 6:35 pm

Is state checker enabled? assertions?

Looping there can only happen misusing some function.

Giovanni

cipher
Posts: 24
Joined: Tue May 31, 2016 10:05 pm
Has thanked: 2 times
Been thanked: 2 times

Re: Having issue with using Mutexes

Postby cipher » Sun Jun 05, 2016 6:49 pm

This is what i have in my chconf.h

Code: Select all

/*===========================================================================*/
/**
 * @name Debug options
 * @{
 */
/*===========================================================================*/

/**
 * @brief   Debug option, kernel statistics.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_STATISTICS                   FALSE

/**
 * @brief   Debug option, system state check.
 * @details If enabled the correct call protocol for system APIs is checked
 *          at runtime.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_SYSTEM_STATE_CHECK           TRUE

/**
 * @brief   Debug option, parameters checks.
 * @details If enabled then the checks on the API functions input
 *          parameters are activated.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_ENABLE_CHECKS                TRUE

/**
 * @brief   Debug option, consistency checks.
 * @details If enabled then all the assertions in the kernel code are
 *          activated. This includes consistency checks inside the kernel,
 *          runtime anomalies and port-defined checks.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_ENABLE_ASSERTS               TRUE

/**
 * @brief   Debug option, trace buffer.
 * @details If enabled then the context switch circular trace buffer is
 *          activated.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_ENABLE_TRACE                 FALSE

/**
 * @brief   Debug option, stack checks.
 * @details If enabled then a runtime stack check is performed.
 *
 * @note    The default is @p FALSE.
 * @note    The stack check is performed in a architecture/port dependent way.
 *          It may not be implemented or some ports.
 * @note    The default failure mode is to halt the system with the global
 *          @p panic_msg variable set to @p NULL.
 */
#define CH_DBG_ENABLE_STACK_CHECK           TRUE

/**
 * @brief   Debug option, stacks initialization.
 * @details If enabled then the threads working area is filled with a byte
 *          value when a thread is created. This can be useful for the
 *          runtime measurement of the used stack.
 *
 * @note    The default is @p FALSE.
 */
#define CH_DBG_FILL_THREADS                 TRUE

/**
 * @brief   Debug option, threads profiling.
 * @details If enabled then a field is added to the @p thread_t structure that
 *          counts the system ticks occurred while executing the thread.
 *
 * @note    The default is @p FALSE.
 * @note    This debug option is not currently compatible with the
 *          tickless mode.
 */
#define CH_DBG_THREADS_PROFILING            FALSE

/** @} */



Also, this is the only mutex which is giving me trouble, about 5-6 other muteness are working always as expected, some from the same thread as well!!

cipher
Posts: 24
Joined: Tue May 31, 2016 10:05 pm
Has thanked: 2 times
Been thanked: 2 times

Re: Having issue with using Mutexes

Postby cipher » Sun Jun 05, 2016 6:52 pm

Just to make sure i didn't miss anything , here is my mbedtls threading layer for chibi, those are the api wrappers for mutex operations:

Header:

Code: Select all

#ifndef MBEDTLS_THREADING_ALT_H
#define MBEDTLS_THREADING_ALT_H

#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if (defined MBEDTLS_THREADING_CMSIS_OS)
#include "cmsis_os.h"
typedef struct
{
    osMutexId mutex;
    unsigned long is_valid;
} mbedtls_threading_mutex_t;


void threading_mutex_init_cmsis_os( mbedtls_threading_mutex_t *mutex );
void threading_mutex_free_cmsis_os( mbedtls_threading_mutex_t *mutex );
int threading_mutex_lock_cmsis_os( mbedtls_threading_mutex_t *mutex );
int threading_mutex_unlock_cmsis_os( mbedtls_threading_mutex_t *mutex );

#elif (defined MBEDTLS_THREADING_CHIBIOS)
#include <stdio.h>
#include <string.h>
#include "ch.h"

typedef struct
{
    mutex_t mutex;
    unsigned long is_valid;
} mbedtls_threading_mutex_t;


void threading_mutex_init_chibios( mbedtls_threading_mutex_t *mutex );
void threading_mutex_free_chibios( mbedtls_threading_mutex_t *mutex );
int threading_mutex_lock_chibios( mbedtls_threading_mutex_t *mutex );
int threading_mutex_unlock_chibios( mbedtls_threading_mutex_t *mutex );
#endif

#endif //MBEDTLS_THREADING_ALT_H


Code:

Code: Select all

/*
 *  Threading abstraction layer
 *
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
 *  SPDX-License-Identifier: Apache-2.0
 *
 *  Licensed 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.
 *
 *  This file is part of mbed TLS (https://tls.mbed.org)
 */

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if defined(MBEDTLS_THREADING_C)

#include "mbedtls/threading.h"

#if defined(MBEDTLS_THREADING_ALT)


#if defined(MBEDTLS_THREADING_CMSIS_OS)
void threading_mutex_init_cmsis_os( mbedtls_threading_mutex_t *mutex )
{
    if(mutex == NULL)
      return;
   
    osMutexDef(someMutex);
    mutex->mutex = osMutexCreate (osMutex(someMutex));
    if(NULL == mutex->mutex) mutex->is_valid = 0;
    else mutex->is_valid = 1;
}

void threading_mutex_free_cmsis_os( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL )
        return;

    (void) osMutexDelete( mutex->mutex );
}

int threading_mutex_lock_cmsis_os( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL || ! mutex->is_valid )
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );

    if( osMutexWait( mutex->mutex, osWaitForever ) != osOK )
        return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );

    return( 0 );
}

int threading_mutex_unlock_cmsis_os( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL || ! mutex->is_valid )
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );

    if( osMutexRelease( mutex->mutex ) != osOK )
        return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );

    return( 0 );
}

#elif defined(MBEDTLS_THREADING_CHIBIOS)

void threading_mutex_init_chibios( mbedtls_threading_mutex_t *mutex )
{
    if(mutex == NULL)
      return;
   
    chMtxObjectInit(&mutex->mutex);
    mutex->is_valid = 1;
}

void threading_mutex_free_chibios( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL )
        return;

    (void) memset( mutex,0,sizeof(mbedtls_threading_mutex_t) );
}

int threading_mutex_lock_chibios( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL || ! mutex->is_valid )
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );

    chMtxLock( &mutex->mutex );
    return( 0 );
}

int threading_mutex_unlock_chibios( mbedtls_threading_mutex_t *mutex )
{
    if( mutex == NULL || ! mutex->is_valid )
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );

    chMtxUnlock( &mutex->mutex );
    return( 0 );
}
#endif /* MBEDTLS_THREADING_CHIBIOS */


#endif /* MBEDTLS_THREADING_ALT */

#endif /* MBEDTLS_THREADING_C */

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Having issue with using Mutexes

Postby Giovanni » Sun Jun 05, 2016 8:38 pm

So you are doing an abstraction layer on top of an abstraction layer :)

I see nothing obvious there.

Giovanni

cipher
Posts: 24
Joined: Tue May 31, 2016 10:05 pm
Has thanked: 2 times
Been thanked: 2 times

Re: Having issue with using Mutexes

Postby cipher » Sun Jun 05, 2016 9:15 pm

Found the Issue!
Obviously not with ChibiOs. Everything works as expected, bad code was overwriting the mutex struct, was tricky to find where though :)

Actually, I'm just following the porting guide from mbedtls :)


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 5 guests