Mutex
The mxos Mutex API can be implemented in the system. → the initialization, acquisition, release, and destruction of mutex locks.
Mutexes are used to ensure that multiple threads are mutually exclusive.
For example, if a thread occupies a certain resource, then other threads cannot access it.
Until the thread leaves, other threads can start to use this resource to ensure a period of time.
Only one thread is accessing the same resource.
⊕ mxos-demos SDK를 참조한다.
1. Mutex API
Mutex API | Description |
mxos_rtos_init_mutex | Initialize a mutex |
mxos_rtos_lock_mutex | Get a mutex |
mxos_rtos_unlock_mutex | Release a mutex |
mxos_rtos_deinit_mutex | Destroy a mutex |
2. os_mutex.c code
#include "mxos.h"
#define os_mutex_log(M, ...) custom_log("OS", M, ##__VA_ARGS__)
//#define USE_MUTEX //Uncommon this macro to see the different result
#ifdef USE_MUTEX
#define MUTEX_LOCK() mxos_rtos_lock_mutex(&mutex)
#define MUTEX_UNLOCK() mxos_rtos_unlock_mutex(&mutex)
#else
#define MUTEX_LOCK()
#define MUTEX_UNLOCK()
#endif
int g_tickets = 100; //remain tickets
mxos_mutex_t mutex = NULL;
char *p_name1 = "t1";
char *p_name2 = "t2";
char *p_name3 = "t3";
char *p_name4 = "t4";
void run( mxos_thread_arg_t arg )
{
char *name = (char *) arg;
char debug[20];
while ( 1 )
{
MUTEX_LOCK();
if ( g_tickets <= 0 )
{
MUTEX_UNLOCK();
goto exit;
}
g_tickets--;
sprintf( debug, "Thread %s, %drn", name, g_tickets );
mxosUartSend( STDIO_UART, debug, strlen( debug ) );
MUTEX_UNLOCK();
}
exit:
os_mutex_log( "thread: %s exit now", name );
mxos_rtos_delete_thread( NULL );
}
int application_start( void )
{
OSStatus err = kNoErr;
/* Create a mutex*/
err = mxos_rtos_init_mutex( &mutex );
require_noerr( err, exit );
/* Create threads */
err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY + 1, "t1", run, 0x800, (mxos_thread_arg_t) p_name1 );
require_noerr( err, exit );
err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY + 1, "t2", run, 0x800, (mxos_thread_arg_t) p_name2 );
require_noerr( err, exit );
err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY + 1, "t3", run, 0x800, (mxos_thread_arg_t) p_name3 );
require_noerr( err, exit );
err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY + 1, "t4", run, 0x800, (mxos_thread_arg_t) p_name4 );
require_noerr( err, exit );
exit:
if ( err != kNoErr )
os_mutex_log( "Thread exit with err: %d", err );
mxos_rtos_delete_thread( NULL );
return err;
}
♦ Achieve
◊ When a mutex is not used, when four threads print a ticket number of 0-100, the order is scrambled and the ticket number is
confusing.
◊ When a mutex is used, the four threads print the ticket numbers 0 – 100 in order.
♦ Main function code annotations:
1) Define the mutex control API macro definition:
#define MUTEX_LOCK() mxos_rtos_lock_mutex(&mutex)
#define MUTEX_UNLOCK() mxos_rtos_unlock_mutex(&mutex)
2) Call the mxos API to create a mutex: err = mxos_rtos_init_mutex( &mutex ); /* Create a mutex*/
3) Call the mxos API to get the mutex: mxos_rtos_lock_mutex(&mutex);
.
4) Call the mxos API to release the mutex: mxos_rtos_unlock_mutex(&mutex);
이상 끝 ~~~