페이지 선택
Generic selectors
Exact matches only
Search in title
Search in content
Search in pages

Semaphor

 

Semaphores, sometimes referred to as semaphores, are a facility used in a multi-threaded environment

to ensure that two or more critical code segments are not called concurrently.

 

Before entering a critical code segment, the thread must acquire a semaphore; once the critical code segment is completed,

the thread must release the semaphore. Other threads that want to enter this critical code segment must wait

until the first thread releases the semaphore.

 

To complete this process, you need to create a semaphore VI, then place the Acquire Semaphore VI,

and the Release Semaphore VI at the beginning of each key snippet,

confirming that these semaphore VIs refer to the semaphore that was originally created.

The mxos RTOS semaphore API can be implemented in the system.→ semaphore initialization, setup, acquisition and destruction.

 

 

§ MXCHIP MXOS API Site 참조한다.

⊕ mxos-demos SDK를 참조한다.

 

 

 

1. Semaphore API

 

 

Semaphre API Descripion
mxos_rtos_init_semaphore Initialize a semaphore and provide a base
mxos_rtos_set_semaphore Set the semaphore (increase or send)
mxos_rtos_get_semaphore Get (reduce, receive) semaphores and provide a timeout mechanism
mxos_rtos_deinit_semaphore Destroy a semaphore

 

 

 

2. os_sem.c code

 

#include "mxos.h"
#define os_sem_log(M, ...) custom_log("OS", M, ##__VA_ARGS__)

static mxos_semaphore_t os_sem = NULL;

void release_thread( mxos_thread_arg_t arg )
{
    UNUSED_PARAMETER( arg );

    while ( 1 )
    {
        os_sem_log( "release semaphore" );
        mxos_rtos_set_semaphore( &os_sem );
        mxos_thread_sleep( 3 );
    }

    mxos_rtos_delete_thread( NULL );
}

int application_start( void )
{
    OSStatus err = kNoErr;
    int semphr_fd = -1;
    os_sem_log( "test binary semaphore" );

    err = mxos_rtos_init_semaphore( &os_sem, 1 ); //0/1 binary semaphore || 0/N semaphore
    require_noerr( err, exit );

    err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY, "release sem", release_thread, 0x500, 0 );
    require_noerr( err, exit );

    semphr_fd = mxos_create_event_fd( os_sem );
    fd_set readfds;
    while ( 1 )
    {
        FD_ZERO( &readfds );
        FD_SET( semphr_fd, &readfds );
        select( 24, &readfds, NULL, NULL, NULL );

        if ( FD_ISSET( semphr_fd, &readfds ) )
        {
            mxos_rtos_get_semaphore( &os_sem, mxos_WAIT_FOREVER ); //wait until get semaphore
            os_sem_log( "get semaphore" );
        }
    }

    exit:
    if ( err != kNoErr )
        os_sem_log( "Thread exit with err: %d", err );

    if ( os_sem != NULL )
    {
        mxos_rtos_deinit_semaphore( &os_sem );
    }

    if( semphr_fd != -1 )
    {
        mxos_rtos_deinit_event_fd( semphr_fd );
    }

    mxos_rtos_delete_thread( NULL );
    return err;
}

 

 

♦ Achieve

◊ First create a new thread in the body thread and define a semaphore;

◊ The main thread and the new thread acquire execution rights through semaphores;

◊ Where, the new thread releases the semaphore after printing the log, at which point the main thread can execute and print the log,

and the new thread sleeps for 3 seconds.

 

 

♦ Main function code annotations

1) Define a semaphore structure: static mxos_semaphore_t os_sem = NULL;

2) Call the mxos API to create a semaphore:

  err = mxos_rtos_init_semaphore( &os_sem, 1 ); //0/1 binary semaphore || 0/N semaphore

3) Call the mxos API to release the semaphore: mxos_rtos_set_semaphore( &os_sem );

4) Call the mxos API to get the semaphore:

  mxos_rtos_get_semaphore( &os_sem, mxos_WAIT_FOREVER ); //wait until get semaphore

◊ Destroy the semaphore: mxos_rtos_deinit_semaphore( &os_sem );

 

 

 

이상 끝 ~~~

 

 

 

Viewed Page List