⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stos.c

📁 stos Linux 源码示范程序。 可以移植到其他平台
💻 C
📖 第 1 页 / 共 3 页
字号:
Limitations :
Returns     : ST_NO_ERROR
*******************************************************************************/
ST_ErrorCode_t  STOS_TaskExit ( void * Param )
{
    Param = Param;   /* To avoid warnings */

#if defined (ST_OS20) || defined (ST_OS21)
    /* Nothing to do */

    return ST_NO_ERROR;

#elif defined (ST_OSLINUX)

#ifdef MODULE
    while (!kthread_should_stop())
         schedule_timeout(10); /* 10 jiffies */

    return ST_NO_ERROR;
#else
    return ST_NO_ERROR;
#endif  /* MODULE */

#else
    #error STOS_TaskExit() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreCreateFifo
Description : OS-independent implementation of semaphore_create_fifo().
              Allocates memory for semaphore structure and initializes semaphore.
              The STOS_SemaphoreCreateFifo() function call is supposed to be independent of the OS used but needs
              a partition_p argument. Depending on the OS used, a different partition must be used.
Parameters  : Partition where to create semaphore, initial value of the semaphore
              If Partition_p = NULL:
                 - For OS20, the STOS function recognizes that it needs to use system_partition.
                 - For OS21, the semaphore_create() function is used instead of semaphore_create_p().
                 - For Linux, the Partition_p parameter is not used.
Assumptions :
Limitations :
Returns     : Address of initialized semaphore or NULL if not enough memory
*******************************************************************************/
semaphore_t * STOS_SemaphoreCreateFifo(partition_t * Partition_p, const int InitialValue)
{
#if defined (ST_OS20)
    semaphore_t * Semaphore_p;

    if(Partition_p == NULL)
    {
        Semaphore_p = (semaphore_t *) memory_allocate(system_partition, sizeof(semaphore_t));
    }
    else
    {
        Semaphore_p = (semaphore_t *) memory_allocate(Partition_p, sizeof(semaphore_t));
    }

    if (Semaphore_p != NULL)
    {
        semaphore_init_fifo(Semaphore_p, InitialValue);
    }
    return(Semaphore_p);

#elif defined (ST_OS21)
    if(Partition_p == NULL)
    {
        return(semaphore_create_fifo(InitialValue));
    }
    else
    {
        return(semaphore_create_fifo_p(Partition_p, InitialValue));
    }

#elif defined (ST_OSLINUX)
    return(semaphore_create_fifo(InitialValue));

#else
    #error STOS_SemaphoreCreateFifo() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreCreateFifoTimeOut
Description : OS-independent implementation of semaphore_create_fifo_timeout()
              Allocates memory for semaphore structure and initializes semaphore
              The STOS_SemaphoreCreateFifo() function call is supposed to be independent of the OS used but needs
              a partition_p argument. Depending on the OS used, a different partition must be used.
Parameters  : Partition where to create semaphore, initial value of the semaphore
              If Partition_p = NULL:
                 - the STOS function recognizes that it needs to use system_partition for OS20.
                 - For OS21, the semaphore_create() function is used instead of semaphore_create_p().
Assumptions :
Limitations :
Returns     : Address of initialized semaphore or NULL if not enough memory
*******************************************************************************/
semaphore_t * STOS_SemaphoreCreateFifoTimeOut(partition_t * Partition_p, const int InitialValue)
{
#if defined (ST_OS20)
    semaphore_t * Semaphore_p;

    if(Partition_p == NULL)
    {
        Semaphore_p = (semaphore_t *) memory_allocate(system_partition, sizeof(semaphore_t));
    }
    else
    {
        Semaphore_p = (semaphore_t *) memory_allocate(Partition_p, sizeof(semaphore_t));
    }

    if (Semaphore_p != NULL)
    {
        semaphore_init_fifo_timeout(Semaphore_p, InitialValue);
    }
    return(Semaphore_p);

#elif defined (ST_OS21)
    /* OS21 does not have semaphore_create_fifo_timeout() function so calls to
       STOS_SemaphoreCreateFifo() and STOS_SemaphoreCreateFifoTimeOut() are equivalent */
    if(Partition_p == NULL)
    {
        return(semaphore_create_fifo(InitialValue));
    }
    else
    {
        return(semaphore_create_fifo_p(Partition_p, InitialValue));
    }

#elif defined (ST_OSLINUX)
    return(semaphore_create_fifo_timeout(InitialValue));

#else
    #error STOS_SemaphoreCreateFifoTimeOut() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreCreatePriority
Description : OS-independent implementation of semaphore_create_priority().
              Allocates memory for semaphore structure and initializes semaphore.
              The STOS_SemaphoreCreatePriority() function call is supposed to be independent of the OS used but needs
              a partition_p argument. Depending on the OS used, a different partition must be used.
Parameters  : Partition where to create semaphore, initial value of the semaphore
              If Partition_p = NULL:
                 - the STOS function recognizes that it needs to use system_partition for OS20.
                 - For OS21, the semaphore_create() function is used instead of semaphore_create_p().
Assumptions :
Limitations :
Returns     : Address of initialized semaphore or NULL if not enough memory
*******************************************************************************/
semaphore_t * STOS_SemaphoreCreatePriority(partition_t * Partition_p, const int InitialValue)
{
#if defined (ST_OS20)
    semaphore_t * Semaphore_p;

    if(Partition_p == NULL)
    {
        Semaphore_p = (semaphore_t *) memory_allocate(system_partition, sizeof(semaphore_t));
    }
    else
    {
        Semaphore_p = (semaphore_t *) memory_allocate(Partition_p, sizeof(semaphore_t));
    }

    if (Semaphore_p != NULL)
    {
        semaphore_init_priority(Semaphore_p, InitialValue);
    }
    return(Semaphore_p);

#elif defined (ST_OS21)
    if(Partition_p == NULL)
    {
        return(semaphore_create_priority(InitialValue));
    }
    else
    {
        return(semaphore_create_priority_p(Partition_p, InitialValue));
    }

#elif defined (ST_OSLINUX)
    return(semaphore_create_priority(InitialValue));

#else
    #error STOS_SemaphoreCreatePriority() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreCreatePriorityTimeOut
Description : OS-independent implementation of semaphore_create_priority_timeout().
              Allocates memory for semaphore structure and initializes semaphore.
              The STOS_SemaphoreCreatePriority() function call is supposed to be independent of the OS used but needs
              a partition_p argument. Depending on the OS used, a different partition must be used.
Parameters  : Partition where to create semaphore, initial value of the semaphore
              If Partition_p = NULL:
                 - the STOS function recognizes that it needs to use system_partition for OS20.
                 - For OS21, the semaphore_create() function is used instead of semaphore_create_p().
Assumptions :
Limitations :
Returns     : Address of initialized semaphore or NULL if not enough memory
*******************************************************************************/
semaphore_t * STOS_SemaphoreCreatePriorityTimeOut(partition_t * Partition_p, const int InitialValue)
{
#if defined (ST_OS20)
    semaphore_t * Semaphore_p;

    if(Partition_p == NULL)
    {
        Semaphore_p = (semaphore_t *) memory_allocate(system_partition, sizeof(semaphore_t));
    }
    else
    {
        Semaphore_p = (semaphore_t *) memory_allocate(Partition_p, sizeof(semaphore_t));
    }

    if (Semaphore_p != NULL)
    {
        semaphore_init_priority_timeout(Semaphore_p, InitialValue);
    }
    return(Semaphore_p);

#elif defined (ST_OS21)
    /* OS21 does not have semaphore_create_priority_timeout() function so calls to
       STOS_SemaphoreCreatePriority() and STOS_SemaphoreCreatePriorityTimeOut() are equivalent */
    if(Partition_p == NULL)
    {
        return(semaphore_create_priority(InitialValue));
    }
    else
    {
        return(semaphore_create_priority_p(Partition_p, InitialValue));
    }

#elif defined (ST_OSLINUX)
    return(semaphore_create_priority(InitialValue));

#else
    #error STOS_SemaphoreCreatePriorityTimeOut() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreDelete
Description : OS-independent implementation of semaphore_delete
Parameters  : Partition of the semaphore to delete, pointer on semaphore
              Partition is the same as used for STOS_SemaphoreCreate.
              If Partition_p = NULL, then we use the predefined system_partition for OS20.
              For OS21, this parameter is unused.
Assumptions : A call to STOS_SemaphoreCreate was made to create the semaphore to be deleted
Limitations : For OS20, don't use after call to semaphore_create because semaphore_delete()
              already deallocates memory in that case. Only use after STOS_SemaphoreCreate.
Returns     : STOS_SUCCESS or STOS_FAILURE
*******************************************************************************/
int STOS_SemaphoreDelete(partition_t * Partition_p, semaphore_t * Semaphore_p)
{
    assert(Semaphore_p != NULL);

#if defined (ST_OS20)
    semaphore_delete(Semaphore_p);

    if(Partition_p == NULL)
    {
        memory_deallocate(system_partition, (void *) Semaphore_p);
    }
    else
    {
        memory_deallocate(Partition_p, (void *) Semaphore_p);
    }
    return(STOS_SUCCESS);

#elif defined (ST_OS21)
    return(semaphore_delete(Semaphore_p));

#elif defined (ST_OSLINUX)
    return(semaphore_delete(Semaphore_p));

#else
    #error STOS_SemaphoreDelete() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreSignal
Description : OS-independent implementation of semaphore_signal
Parameters  : semaphore to signal to
Assumptions :
Limitations :
Returns     : Nothing
*******************************************************************************/
void STOS_SemaphoreSignal(semaphore_t * Semaphore_p)
{
    assert(Semaphore_p != NULL);

#if (defined (ST_OS20)) || (defined (ST_OS21)) || (defined (ST_OSLINUX))
    semaphore_signal(Semaphore_p);

#else
    #error STOS_SemaphoreSignal() can only be used with OS20, OS21 or LINUX!
#endif
}

/*******************************************************************************
Name        : STOS_SemaphoreWait
Description : OS-independent implementation of semaphore_wait
Parameters  : semaphore to wait for
Assumptions :
Limitations :
Returns     : Always returns STOS_SUCCESS
*******************************************************************************/
int STOS_SemaphoreWait(semaphore_t * Semaphore_p)
{
    assert(Semaphore_p != NULL);

#if (defined (ST_OS20)) || (defined (ST_OS21)) || (defined (ST_OSLINUX))
    semaphore_wait(Semaphore_p);
    return(STOS_SUCCESS);
#else
    #error STOS_SemaphoreWait() can only be used with OS20, OS21 or LINUX!
#endif
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -