freertos: release the generic version source code
freertos runs on the second core (small one) of the CPU
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX.h
|
||||
* @brief FreeRTOS+POSIX header.
|
||||
*
|
||||
* This file must be included before all other FreeRTOS+POSIX includes.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_H_
|
||||
#define _FREERTOS_POSIX_H_
|
||||
|
||||
/* FreeRTOS+POSIX platform-specific configuration headers. */
|
||||
#include "FreeRTOS_POSIX_portable.h"
|
||||
#include "FreeRTOS_POSIX_portable_default.h"
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "event_groups.h"
|
||||
#include "semphr.h"
|
||||
#include "task.h"
|
||||
|
||||
/* FreeRTOS+POSIX data types and internal structs. */
|
||||
#include "FreeRTOS_POSIX/sys/types.h"
|
||||
#include "FreeRTOS_POSIX_internal.h"
|
||||
|
||||
#endif /* _FREERTOS_POSIX_H_ */
|
||||
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_INTERNAL_H_
|
||||
#define _FREERTOS_POSIX_INTERNAL_H_
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_internal.h
|
||||
* @brief Internal structs and initializers for FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
/* Amazon FreeRTOS includes. */
|
||||
#include "iot_doubly_linked_list.h"
|
||||
|
||||
/**
|
||||
* @brief Mutex attribute object.
|
||||
*/
|
||||
#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1
|
||||
typedef struct pthread_mutexattr_internal
|
||||
{
|
||||
int iType; /**< Mutex type. */
|
||||
} pthread_mutexattr_internal_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_MUTEX_T == 1
|
||||
|
||||
/**
|
||||
* @brief Mutex.
|
||||
*/
|
||||
typedef struct pthread_mutex_internal
|
||||
{
|
||||
BaseType_t xIsInitialized; /**< Set to pdTRUE if this mutex is initialized, pdFALSE otherwise. */
|
||||
StaticSemaphore_t xMutex; /**< FreeRTOS mutex. */
|
||||
TaskHandle_t xTaskOwner; /**< Owner; used for deadlock detection and permission checks. */
|
||||
pthread_mutexattr_internal_t xAttr; /**< Mutex attributes. */
|
||||
} pthread_mutex_internal_t;
|
||||
|
||||
/**
|
||||
* @brief Compile-time initializer of pthread_mutex_internal_t.
|
||||
*/
|
||||
#define FREERTOS_POSIX_MUTEX_INITIALIZER \
|
||||
( ( ( pthread_mutex_internal_t ) \
|
||||
{ \
|
||||
.xIsInitialized = pdFALSE, \
|
||||
.xMutex = { { 0 } }, \
|
||||
.xTaskOwner = NULL, \
|
||||
.xAttr = { .iType = 0 } \
|
||||
} \
|
||||
) \
|
||||
)
|
||||
#endif /* if posixconfigENABLE_PTHREAD_MUTEX_T == 1 */
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_COND_T == 1
|
||||
|
||||
/**
|
||||
* @brief Condition variable.
|
||||
*/
|
||||
typedef struct pthread_cond_internal
|
||||
{
|
||||
BaseType_t xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */
|
||||
StaticSemaphore_t xCondWaitSemaphore; /**< Threads block on this semaphore in pthread_cond_wait. */
|
||||
unsigned iWaitingThreads; /**< The number of threads currently waiting on this condition variable. */
|
||||
} pthread_cond_internal_t;
|
||||
|
||||
/**
|
||||
* @brief Compile-time initializer of pthread_cond_internal_t.
|
||||
*/
|
||||
|
||||
#define FREERTOS_POSIX_COND_INITIALIZER \
|
||||
( ( ( pthread_cond_internal_t ) \
|
||||
{ \
|
||||
.xIsInitialized = pdFALSE, \
|
||||
.xCondWaitSemaphore = { { 0 } }, \
|
||||
.iWaitingThreads = 0 \
|
||||
} \
|
||||
) \
|
||||
)
|
||||
|
||||
#endif /* if posixconfigENABLE_PTHREAD_COND_T == 1 */
|
||||
|
||||
#if posixconfigENABLE_SEM_T == 1
|
||||
|
||||
/**
|
||||
* @brief Semaphore type.
|
||||
*/
|
||||
typedef struct sem_internal
|
||||
{
|
||||
StaticSemaphore_t xSemaphore; /**< FreeRTOS semaphore. */
|
||||
int value; /**< POSIX semaphore count. */
|
||||
} sem_internal_t;
|
||||
#endif /* if posixconfigENABLE_SEM_T == 1 */
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_BARRIER_T == 1
|
||||
|
||||
/**
|
||||
* @brief Barrier object.
|
||||
*/
|
||||
typedef struct pthread_barrier_internal
|
||||
{
|
||||
unsigned uThreadCount; /**< Current number of threads that have entered barrier. */
|
||||
unsigned uThreshold; /**< The count argument of pthread_barrier_init. */
|
||||
StaticSemaphore_t xThreadCountSemaphore; /**< Prevents more than uThreshold threads from exiting pthread_barrier_wait at once. */
|
||||
StaticEventGroup_t xBarrierEventGroup; /**< FreeRTOS event group that blocks to wait on threads entering barrier. */
|
||||
} pthread_barrier_internal_t;
|
||||
#endif /* if posixconfigENABLE_PTHREAD_BARRIER_T == 1 */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_INTERNAL_H_ */
|
||||
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_INTERNAL_TYPES_H_
|
||||
#define _FREERTOS_POSIX_INTERNAL_TYPES_H_
|
||||
|
||||
#include "FreeRTOS_POSIX_internal.h"
|
||||
|
||||
/*
|
||||
* sys/types.h defines a POSIX type when posixconfigENABLE_PTHREAD_<TYPE>_T
|
||||
* is not defined AND when posixconfigENABLE_PTHREAD_<TYPE>_T is set to 1.
|
||||
* FreeRTOS_POSIX_internal.h defines internal type ONLY when
|
||||
* posixconfigENABLE_PTHREAD_<TYPE>_T is set to 1.
|
||||
* #else part below is to have a type defined, so the code compiles, when
|
||||
* posixconfigENABLE_PTHREAD_<TYPE>_T is not defined.
|
||||
*/
|
||||
#if posixconfigENABLE_PTHREAD_MUTEX_T == 1
|
||||
typedef pthread_mutex_internal_t PthreadMutexType_t;
|
||||
#else
|
||||
typedef void * PthreadMutexType_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_COND_T == 1
|
||||
typedef pthread_cond_internal_t PthreadCondType_t;
|
||||
#else
|
||||
typedef void * PthreadCondType_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_SEM_T == 1
|
||||
typedef sem_internal_t PosixSemType_t;
|
||||
#else
|
||||
typedef void * PosixSemType_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_MUTEXATTR_T == 1
|
||||
typedef struct pthread_mutexattr
|
||||
{
|
||||
uint32_t ulpthreadMutexAttrStorage;
|
||||
} PthreadMutexAttrType_t;
|
||||
#else
|
||||
typedef void * PthreadMutexAttrType_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_ATTR_T == 1
|
||||
typedef struct pthread_attr
|
||||
{
|
||||
uint32_t ulpthreadAttrStorage;
|
||||
} PthreadAttrType_t;
|
||||
#else
|
||||
typedef void * PthreadAttrType_t;
|
||||
#endif
|
||||
|
||||
#if posixconfigENABLE_PTHREAD_BARRIER_T == 1
|
||||
typedef pthread_barrier_internal_t PthreadBarrierType_t;
|
||||
#else
|
||||
typedef void * PthreadBarrierType_t;
|
||||
#endif
|
||||
|
||||
#endif /* _FREERTOS_POSIX_INTERNAL_TYPES_H_ */
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this
|
||||
* file is empty. */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable_default.h
|
||||
* @brief Defaults for port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_DEFAULT_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_DEFAULT_H_
|
||||
|
||||
/**
|
||||
* @name The FreeRTOS task name given to pthreads.
|
||||
*/
|
||||
/**@{ */
|
||||
#ifndef posixconfigPTHREAD_TASK_NAME
|
||||
#define posixconfigPTHREAD_TASK_NAME "pthread" /**< Task name. */
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
/**
|
||||
* @name the FreeRTOS timer name given to POSIX timers.
|
||||
*/
|
||||
/**@{ */
|
||||
#ifndef posixconfigTIMER_NAME
|
||||
#define posixconfigTIMER_NAME "timer" /**< Timer name. */
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
/**
|
||||
* @name Defaults for POSIX message queue implementation.
|
||||
*/
|
||||
/**@{ */
|
||||
#ifndef posixconfigMQ_MAX_MESSAGES
|
||||
#define posixconfigMQ_MAX_MESSAGES 10 /**< Maximum number of messages in an mq at one time. */
|
||||
#endif
|
||||
|
||||
#ifndef posixconfigMQ_MAX_SIZE
|
||||
#define posixconfigMQ_MAX_SIZE 128 /**< Maximum size (in bytes) of each message. */
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
/**
|
||||
* @name POSIX implementation-dependent constants usually defined in limits.h.
|
||||
*
|
||||
* They are defined here to provide portability between platforms.
|
||||
*/
|
||||
/**@{ */
|
||||
#ifndef PTHREAD_STACK_MIN
|
||||
#define PTHREAD_STACK_MIN configMINIMAL_STACK_SIZE * sizeof( StackType_t ) /**< Minimum size in bytes of thread stack storage. */
|
||||
#endif
|
||||
#ifndef NAME_MAX
|
||||
#define NAME_MAX 64 /**< Maximum number of bytes in a filename (not including terminating null). */
|
||||
#endif
|
||||
#ifndef SEM_VALUE_MAX
|
||||
#define SEM_VALUE_MAX 0x7FFFU /**< Maximum value of a sem_t. */
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
/**
|
||||
* @name Enable typedefs of POSIX types.
|
||||
*
|
||||
* Set these values to 1 or 0 to enable or disable the typedefs, respectively.
|
||||
* These typedefs should only be disabled if they conflict with system typedefs.
|
||||
*/
|
||||
/**@{ */
|
||||
#ifndef posixconfigENABLE_CLOCK_T
|
||||
#define posixconfigENABLE_CLOCK_T 1 /**< clock_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_CLOCKID_T
|
||||
#define posixconfigENABLE_CLOCKID_T 1 /**< clockid_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_MODE_T
|
||||
#define posixconfigENABLE_MODE_T 1 /**< mode_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PID_T
|
||||
#define posixconfigENABLE_PID_T 1 /**< pid_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_ATTR_T
|
||||
#define posixconfigENABLE_PTHREAD_ATTR_T 1 /**< pthread_attr_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_COND_T
|
||||
#define posixconfigENABLE_PTHREAD_COND_T 1 /**< pthread_cond_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_CONDATTR_T
|
||||
#define posixconfigENABLE_PTHREAD_CONDATTR_T 1 /**< pthread_condattr_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_MUTEX_T
|
||||
#define posixconfigENABLE_PTHREAD_MUTEX_T 1 /**< pthread_mutex_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_MUTEXATTR_T
|
||||
#define posixconfigENABLE_PTHREAD_MUTEXATTR_T 1 /**< pthread_mutexattr_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_T
|
||||
#define posixconfigENABLE_PTHREAD_T 1 /**< pthread_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_SSIZE_T
|
||||
#define posixconfigENABLE_SSIZE_T 1 /**< ssize_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_TIME_T
|
||||
#define posixconfigENABLE_TIME_T 1 /**< time_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_TIMER_T
|
||||
#define posixconfigENABLE_TIMER_T 1 /**< timer_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_USECONDS_T
|
||||
#define posixconfigENABLE_USECONDS_T 1 /**< useconds_t in sys/types.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_TIMESPEC
|
||||
#define posixconfigENABLE_TIMESPEC 1 /**< struct timespec in time.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_ITIMERSPEC
|
||||
#define posixconfigENABLE_ITIMERSPEC 1 /**< struct itimerspec in time.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_SEM_T
|
||||
#define posixconfigENABLE_SEM_T 1 /**< struct sem_t in semaphore.h */
|
||||
#endif
|
||||
#ifndef posixconfigENABLE_PTHREAD_BARRIER_T
|
||||
#define posixconfigENABLE_PTHREAD_BARRIER_T 1 /**< pthread_barrier_t in sys/types.h */
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
#endif /* ifndef _FREERTOS_POSIX_PORTABLE_DEFAULT_H_ */
|
||||
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* ESP-IDF already defines the following types. */
|
||||
#define posixconfigENABLE_CLOCK_T 0
|
||||
#define posixconfigENABLE_CLOCKID_T 0
|
||||
#define posixconfigENABLE_MODE_T 0
|
||||
#define posixconfigENABLE_PTHREAD_ATTR_T 0
|
||||
#define posixconfigENABLE_PTHREAD_COND_T 0
|
||||
#define posixconfigENABLE_PTHREAD_CONDATTR_T 0
|
||||
#define posixconfigENABLE_PTHREAD_MUTEX_T 0
|
||||
#define posixconfigENABLE_PTHREAD_MUTEXATTR_T 0
|
||||
#define posixconfigENABLE_PTHREAD_T 0
|
||||
#define posixconfigENABLE_TIME_T 0
|
||||
#define posixconfigENABLE_TIMESPEC 0
|
||||
#define posixconfigENABLE_ITIMERSPEC 0
|
||||
|
||||
/* ESP-IDF already provides the header sched.h. Exclude FreeRTOS+POSIX sched.h by
|
||||
* defining its double inclusion guard. */
|
||||
#define _FREERTOS_POSIX_SCHED_H_
|
||||
|
||||
/* Use the FreeRTOS+POSIX time.h header instead of the ESP-IDF time.h. Disable
|
||||
* ESP-IDF time.h by defining its double inclusion guard. */
|
||||
#define _TIME_H_
|
||||
|
||||
/* Disable the timer_t type defined by ESP-IDF. */
|
||||
#define __timer_t_defined
|
||||
#include <sys/types.h>
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* Microchip includes. */
|
||||
#include <sys/types.h>
|
||||
|
||||
/* Microchip already typedefs the following types. */
|
||||
#define posixconfigENABLE_MODE_T 0
|
||||
#define posixconfigENABLE_PID_T 0
|
||||
#define posixconfigENABLE_SSIZE_T 0
|
||||
#define posixconfigENABLE_USECONDS_T 0
|
||||
/* Microchip -mnewlib compiler option supports these types. */
|
||||
#define posixconfigENABLE_TIMESPEC 0
|
||||
#define posixconfigENABLE_ITIMERSPEC 0
|
||||
#define posixconfigENABLE_CLOCKID_T 0
|
||||
#define posixconfigENABLE_TIME_T 0
|
||||
#define posixconfigENABLE_TIMER_T 0
|
||||
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this
|
||||
* file is empty. */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this
|
||||
* file is empty. */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this
|
||||
* file is empty. */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Amazon FreeRTOS+POSIX V1.0.4
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_portable.h
|
||||
* @brief Port-specific configuration of FreeRTOS+POSIX.
|
||||
*/
|
||||
|
||||
#ifndef _FREERTOS_POSIX_PORTABLE_H_
|
||||
#define _FREERTOS_POSIX_PORTABLE_H_
|
||||
|
||||
/* This port uses the defaults in FreeRTOS_POSIX_portable_default.h, so this
|
||||
* file is empty. */
|
||||
|
||||
#endif /* _FREERTOS_POSIX_PORTABLE_H_ */
|
||||
@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_clock.c
|
||||
* @brief Implementation of clock functions in time.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/time.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
/* Declaration of snprintf. The header stdio.h is not included because it
|
||||
* includes conflicting symbols on some platforms. */
|
||||
extern int snprintf( char * s,
|
||||
size_t n,
|
||||
const char * format,
|
||||
... );
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
clock_t clock( void )
|
||||
{
|
||||
/* This function is currently unsupported. It will always return -1. */
|
||||
|
||||
return ( clock_t ) -1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int clock_getcpuclockid( pid_t pid,
|
||||
clockid_t * clock_id )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) pid;
|
||||
( void ) clock_id;
|
||||
|
||||
/* This function is currently unsupported. It will always return EPERM. */
|
||||
return EPERM;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int clock_getres( clockid_t clock_id,
|
||||
struct timespec * res )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) clock_id;
|
||||
|
||||
/* Convert FreeRTOS tick resolution as timespec. */
|
||||
if( res != NULL )
|
||||
{
|
||||
res->tv_sec = 0;
|
||||
res->tv_nsec = NANOSECONDS_PER_TICK;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int clock_gettime( clockid_t clock_id,
|
||||
struct timespec * tp )
|
||||
{
|
||||
TimeOut_t xCurrentTime = { 0 };
|
||||
|
||||
/* Intermediate variable used to convert TimeOut_t to struct timespec.
|
||||
* Also used to detect overflow issues. It must be unsigned because the
|
||||
* behavior of signed integer overflow is undefined. */
|
||||
uint64_t ullTickCount = 0ULL;
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) clock_id;
|
||||
|
||||
/* Get the current tick count and overflow count. vTaskSetTimeOutState()
|
||||
* is used to get these values because they are both static in tasks.c. */
|
||||
vTaskSetTimeOutState( &xCurrentTime );
|
||||
|
||||
/* Adjust the tick count for the number of times a TickType_t has overflowed.
|
||||
* portMAX_DELAY should be the maximum value of a TickType_t. */
|
||||
ullTickCount = ( uint64_t ) ( xCurrentTime.xOverflowCount ) << ( sizeof( TickType_t ) * 8 );
|
||||
|
||||
/* Add the current tick count. */
|
||||
ullTickCount += xCurrentTime.xTimeOnEntering;
|
||||
|
||||
/* Convert ullTickCount to timespec. */
|
||||
UTILS_NanosecondsToTimespec( ( int64_t ) ullTickCount * NANOSECONDS_PER_TICK, tp );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int clock_nanosleep( clockid_t clock_id,
|
||||
int flags,
|
||||
const struct timespec * rqtp,
|
||||
struct timespec * rmtp )
|
||||
{
|
||||
int iStatus = 0;
|
||||
TickType_t xSleepTime = 0;
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) clock_id;
|
||||
( void ) rmtp;
|
||||
( void ) flags; /* This is only ignored if INCLUDE_vTaskDelayUntil is 0. */
|
||||
|
||||
/* Check rqtp. */
|
||||
if( UTILS_ValidateTimespec( rqtp ) == false )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Get current time */
|
||||
if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Check for absolute time sleep. */
|
||||
if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME )
|
||||
{
|
||||
/* Get current time */
|
||||
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Get number of ticks until absolute time. */
|
||||
if( ( iStatus == 0 ) && ( UTILS_AbsoluteTimespecToDeltaTicks( rqtp, &xCurrentTime, &xSleepTime ) == 0 ) )
|
||||
{
|
||||
/* Delay until absolute time if vTaskDelayUntil is available. */
|
||||
#if ( INCLUDE_vTaskDelayUntil == 1 )
|
||||
|
||||
/* Get the current tick count. This variable isn't declared
|
||||
* at the top of the function because it's only used and needed
|
||||
* if vTaskDelayUntil is available. */
|
||||
TickType_t xCurrentTicks = xTaskGetTickCount();
|
||||
|
||||
/* Delay until absolute time. */
|
||||
vTaskDelayUntil( &xCurrentTicks, xSleepTime );
|
||||
#else
|
||||
|
||||
/* If vTaskDelayUntil isn't available, ignore the TIMER_ABSTIME flag
|
||||
* and sleep for a relative time. */
|
||||
vTaskDelay( xSleepTime );
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If TIMER_ABSTIME isn't specified, convert rqtp to ticks and
|
||||
* sleep for a relative time. */
|
||||
if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )
|
||||
{
|
||||
vTaskDelay( xSleepTime );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int clock_settime( clockid_t clock_id,
|
||||
const struct timespec * tp )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) clock_id;
|
||||
( void ) tp;
|
||||
|
||||
/* This function is currently unsupported. It will always return -1 and
|
||||
* set errno to EPERM. */
|
||||
errno = EPERM;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int nanosleep( const struct timespec * rqtp,
|
||||
struct timespec * rmtp )
|
||||
{
|
||||
int iStatus = 0;
|
||||
TickType_t xSleepTime = 0;
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) rmtp;
|
||||
|
||||
/* Check rqtp. */
|
||||
if( UTILS_ValidateTimespec( rqtp ) == false )
|
||||
{
|
||||
errno = EINVAL;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Convert rqtp to ticks and delay. */
|
||||
if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )
|
||||
{
|
||||
vTaskDelay( xSleepTime );
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,893 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_mqueue.c
|
||||
* @brief Implementation of message queue functions in mqueue.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <string.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/fcntl.h"
|
||||
#include "FreeRTOS_POSIX/mqueue.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
/**
|
||||
* @brief Element of the FreeRTOS queues that store mq data.
|
||||
*/
|
||||
typedef struct QueueElement
|
||||
{
|
||||
char * pcData; /**< Data in queue. Type char* to match msg_ptr. */
|
||||
size_t xDataSize; /**< Size of data pointed by pcData. */
|
||||
} QueueElement_t;
|
||||
|
||||
/**
|
||||
* @brief Data structure of an mq.
|
||||
*
|
||||
* FreeRTOS isn't guaranteed to have a file-like abstraction, so message
|
||||
* queues in this implementation are stored as a linked list (in RAM).
|
||||
*/
|
||||
typedef struct QueueListElement
|
||||
{
|
||||
Link_t xLink; /**< Pointer to the next element in the list. */
|
||||
QueueHandle_t xQueue; /**< FreeRTOS queue handle. */
|
||||
size_t xOpenDescriptors; /**< Number of threads that have opened this queue. */
|
||||
char * pcName; /**< Null-terminated queue name. */
|
||||
struct mq_attr xAttr; /**< Queue attibutes. */
|
||||
BaseType_t xPendingUnlink; /**< If pdTRUE, this queue will be unlinked once all descriptors close. */
|
||||
} QueueListElement_t;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Convert an absolute timespec into a tick timeout, taking into account
|
||||
* queue flags.
|
||||
*
|
||||
* @param[in] lMessageQueueFlags Message queue flags to consider.
|
||||
* @param[in] pxAbsoluteTimeout The absolute timespec to convert.
|
||||
* @param[out] pxTimeoutTicks Output parameter of the timeout in ticks.
|
||||
*
|
||||
* @return 0 if successful; EINVAL if pxAbsoluteTimeout is invalid, or ETIMEDOUT
|
||||
* if pxAbsoluteTimeout is in the past.
|
||||
*/
|
||||
static int prvCalculateTickTimeout( long lMessageQueueFlags,
|
||||
const struct timespec * const pxAbsoluteTimeout,
|
||||
TickType_t * pxTimeoutTicks );
|
||||
|
||||
/**
|
||||
* @brief Add a new queue to the queue list.
|
||||
*
|
||||
* @param[out] ppxMessageQueue Pointer to new queue.
|
||||
* @param[in] pxAttr mq_attr of the new queue.
|
||||
* @param[in] pcName Name of new queue.
|
||||
* @param[in] xNameLength Length of pcName.
|
||||
*
|
||||
* @return pdTRUE if the queue is found; pdFALSE otherwise.
|
||||
*/
|
||||
static BaseType_t prvCreateNewMessageQueue( QueueListElement_t ** ppxMessageQueue,
|
||||
const struct mq_attr * const pxAttr,
|
||||
const char * const pcName,
|
||||
size_t xNameLength );
|
||||
|
||||
/**
|
||||
* @brief Free all the resources used by a message queue.
|
||||
*
|
||||
* @param[out] pxMessageQueue Pointer to queue to free.
|
||||
*
|
||||
* @return nothing
|
||||
*/
|
||||
static void prvDeleteMessageQueue( const QueueListElement_t * const pxMessageQueue );
|
||||
|
||||
/**
|
||||
* @brief Attempt to find the queue identified by pcName or xMqId in the queue list.
|
||||
*
|
||||
* Matches queues by pcName first; if pcName is NULL, matches by xMqId.
|
||||
* @param[out] ppxQueueListElement Output parameter set when queue is found.
|
||||
* @param[in] pcName A queue name to match.
|
||||
* @param[in] xMessageQueueDescriptor A queue descriptor to match.
|
||||
*
|
||||
* @return pdTRUE if the queue is found; pdFALSE otherwise.
|
||||
*/
|
||||
static BaseType_t prvFindQueueInList( QueueListElement_t ** const ppxQueueListElement,
|
||||
const char * const pcName,
|
||||
mqd_t xMessageQueueDescriptor );
|
||||
|
||||
/**
|
||||
* @brief Initialize the queue list.
|
||||
*
|
||||
* Performs initialization of the queue list mutex and queue list head.
|
||||
*
|
||||
* @return nothing
|
||||
*/
|
||||
static void prvInitializeQueueList( void );
|
||||
|
||||
/**
|
||||
* @brief Checks that pcName is a valid name for a message queue.
|
||||
*
|
||||
* Also outputs the length of pcName.
|
||||
* @param[in] pcName The name to check.
|
||||
* @param[out] pxNameLength Output parameter for name length.
|
||||
*
|
||||
* @return pdTRUE if the name is valid; pdFALSE otherwise.
|
||||
*/
|
||||
static BaseType_t prvValidateQueueName( const char * const pcName,
|
||||
size_t * pxNameLength );
|
||||
|
||||
/**
|
||||
* @brief Guards access to the list of message queues.
|
||||
*/
|
||||
static StaticSemaphore_t xQueueListMutex = { { 0 }, .u = { 0 } };
|
||||
|
||||
/**
|
||||
* @brief Head of the linked list of queues.
|
||||
*/
|
||||
static Link_t xQueueListHead = { 0 };
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int prvCalculateTickTimeout( long lMessageQueueFlags,
|
||||
const struct timespec * const pxAbsoluteTimeout,
|
||||
TickType_t * pxTimeoutTicks )
|
||||
{
|
||||
int iStatus = 0;
|
||||
|
||||
/* Check for nonblocking queue. */
|
||||
if( lMessageQueueFlags & O_NONBLOCK )
|
||||
{
|
||||
/* No additional checks are done for nonblocking queues. Timeout is 0. */
|
||||
*pxTimeoutTicks = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No absolute timeout given. Block forever. */
|
||||
if( pxAbsoluteTimeout == NULL )
|
||||
{
|
||||
*pxTimeoutTicks = portMAX_DELAY;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Check that the given timespec is valid. */
|
||||
if( UTILS_ValidateTimespec( pxAbsoluteTimeout ) == false )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Get current time */
|
||||
if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Convert absolute timespec to ticks. */
|
||||
if( ( iStatus == 0 ) &&
|
||||
( UTILS_AbsoluteTimespecToDeltaTicks( pxAbsoluteTimeout, &xCurrentTime, pxTimeoutTicks ) != 0 ) )
|
||||
{
|
||||
iStatus = ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static BaseType_t prvCreateNewMessageQueue( QueueListElement_t ** ppxMessageQueue,
|
||||
const struct mq_attr * const pxAttr,
|
||||
const char * const pcName,
|
||||
size_t xNameLength )
|
||||
{
|
||||
BaseType_t xStatus = pdTRUE;
|
||||
|
||||
/* Allocate space for a new queue element. */
|
||||
*ppxMessageQueue = pvPortMalloc( sizeof( QueueListElement_t ) );
|
||||
|
||||
/* Check that memory allocation succeeded. */
|
||||
if( *ppxMessageQueue == NULL )
|
||||
{
|
||||
xStatus = pdFALSE;
|
||||
}
|
||||
|
||||
/* Create the FreeRTOS queue. */
|
||||
if( xStatus == pdTRUE )
|
||||
{
|
||||
( *ppxMessageQueue )->xQueue =
|
||||
xQueueCreate( pxAttr->mq_maxmsg, sizeof( QueueElement_t ) );
|
||||
|
||||
/* Check that queue creation succeeded. */
|
||||
if( ( *ppxMessageQueue )->xQueue == NULL )
|
||||
{
|
||||
vPortFree( *ppxMessageQueue );
|
||||
xStatus = pdFALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if( xStatus == pdTRUE )
|
||||
{
|
||||
/* Allocate space for the queue name plus null-terminator. */
|
||||
( *ppxMessageQueue )->pcName = pvPortMalloc( xNameLength + 1 );
|
||||
|
||||
/* Check that memory was successfully allocated for queue name. */
|
||||
if( ( *ppxMessageQueue )->pcName == NULL )
|
||||
{
|
||||
vQueueDelete( ( *ppxMessageQueue )->xQueue );
|
||||
vPortFree( *ppxMessageQueue );
|
||||
xStatus = pdFALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Copy queue name. Copying xNameLength+1 will cause strncpy to add
|
||||
* the null-terminator. */
|
||||
( void ) strncpy( ( *ppxMessageQueue )->pcName, pcName, xNameLength + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
if( xStatus == pdTRUE )
|
||||
{
|
||||
/* Copy attributes. */
|
||||
( *ppxMessageQueue )->xAttr = *pxAttr;
|
||||
|
||||
/* A newly-created queue will have 1 open descriptor for it. */
|
||||
( *ppxMessageQueue )->xOpenDescriptors = 1;
|
||||
|
||||
/* A newly-created queue will not be pending unlink. */
|
||||
( *ppxMessageQueue )->xPendingUnlink = pdFALSE;
|
||||
|
||||
/* Add the new queue to the list. */
|
||||
listADD( &xQueueListHead, &( *ppxMessageQueue )->xLink );
|
||||
}
|
||||
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvDeleteMessageQueue( const QueueListElement_t * const pxMessageQueue )
|
||||
{
|
||||
QueueElement_t xQueueElement = { 0 };
|
||||
|
||||
/* Free all data in the queue. It's assumed that no more data will be added
|
||||
* to the queue, so xQueueReceive does not block. */
|
||||
while( xQueueReceive( pxMessageQueue->xQueue,
|
||||
( void * ) &xQueueElement,
|
||||
0 ) == pdTRUE )
|
||||
{
|
||||
vPortFree( xQueueElement.pcData );
|
||||
}
|
||||
|
||||
/* Free memory used by this message queue. */
|
||||
vQueueDelete( pxMessageQueue->xQueue );
|
||||
vPortFree( ( void * ) pxMessageQueue->pcName );
|
||||
vPortFree( ( void * ) pxMessageQueue );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static BaseType_t prvFindQueueInList( QueueListElement_t ** const ppxQueueListElement,
|
||||
const char * const pcName,
|
||||
mqd_t xMessageQueueDescriptor )
|
||||
{
|
||||
Link_t * pxQueueListLink = NULL;
|
||||
QueueListElement_t * pxMessageQueue = NULL;
|
||||
BaseType_t xQueueFound = pdFALSE;
|
||||
|
||||
/* Iterate through the list of queues. */
|
||||
listFOR_EACH( pxQueueListLink, &xQueueListHead )
|
||||
{
|
||||
pxMessageQueue = listCONTAINER( pxQueueListLink, QueueListElement_t, xLink );
|
||||
|
||||
/* Match by name first if provided. */
|
||||
if( ( pcName != NULL ) && ( strcmp( pxMessageQueue->pcName, pcName ) == 0 ) )
|
||||
{
|
||||
xQueueFound = pdTRUE;
|
||||
break;
|
||||
}
|
||||
/* If name doesn't match, match by descriptor. */
|
||||
else
|
||||
{
|
||||
if( ( mqd_t ) pxMessageQueue == xMessageQueueDescriptor )
|
||||
{
|
||||
xQueueFound = pdTRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If the queue was found, set the output parameter. */
|
||||
if( ( xQueueFound == pdTRUE ) && ( ppxQueueListElement != NULL ) )
|
||||
{
|
||||
*ppxQueueListElement = pxMessageQueue;
|
||||
}
|
||||
|
||||
return xQueueFound;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvInitializeQueueList( void )
|
||||
{
|
||||
/* Keep track of whether the queue list has been initialized. */
|
||||
static BaseType_t xQueueListInitialized = pdFALSE;
|
||||
|
||||
/* Check if queue list needs to be initialized. */
|
||||
if( xQueueListInitialized == pdFALSE )
|
||||
{
|
||||
/* Initialization must be in a critical section to prevent two threads
|
||||
* from initializing at the same time. */
|
||||
taskENTER_CRITICAL();
|
||||
|
||||
/* Check again that queue list is still uninitialized, i.e. it wasn't
|
||||
* initialized while this function was waiting to enter the critical
|
||||
* section. */
|
||||
if( xQueueListInitialized == pdFALSE )
|
||||
{
|
||||
/* Initialize the queue list mutex and list head. */
|
||||
( void ) xSemaphoreCreateMutexStatic( &xQueueListMutex );
|
||||
listINIT_HEAD( &xQueueListHead );
|
||||
xQueueListInitialized = pdTRUE;
|
||||
}
|
||||
|
||||
/* Exit the critical section. */
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static BaseType_t prvValidateQueueName( const char * const pcName,
|
||||
size_t * pxNameLength )
|
||||
{
|
||||
BaseType_t xStatus = pdTRUE;
|
||||
size_t xNameLength = 0;
|
||||
|
||||
/* All message queue names must start with '/'. */
|
||||
if( pcName[ 0 ] != '/' )
|
||||
{
|
||||
xStatus = pdFALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Get the length of pcName, excluding the first '/' and null-terminator. */
|
||||
xNameLength = UTILS_strnlen( pcName, NAME_MAX + 2 );
|
||||
|
||||
if( xNameLength == NAME_MAX + 2 )
|
||||
{
|
||||
/* Name too long. */
|
||||
xStatus = pdFALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Name length passes, set output parameter. */
|
||||
*pxNameLength = xNameLength;
|
||||
}
|
||||
}
|
||||
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int mq_close( mqd_t mqdes )
|
||||
{
|
||||
int iStatus = 0;
|
||||
QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes;
|
||||
BaseType_t xQueueRemoved = pdFALSE;
|
||||
|
||||
/* Initialize the queue list, if needed. */
|
||||
prvInitializeQueueList();
|
||||
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Attempt to find the message queue based on the given descriptor. */
|
||||
if( prvFindQueueInList( NULL, NULL, mqdes ) == pdTRUE )
|
||||
{
|
||||
/* Decrement the number of open descriptors. */
|
||||
if( pxMessageQueue->xOpenDescriptors > 0 )
|
||||
{
|
||||
pxMessageQueue->xOpenDescriptors--;
|
||||
}
|
||||
|
||||
/* Check if the queue has any more open descriptors. */
|
||||
if( pxMessageQueue->xOpenDescriptors == 0 )
|
||||
{
|
||||
/* If no open descriptors remain and mq_unlink has already been called,
|
||||
* remove the queue. */
|
||||
if( pxMessageQueue->xPendingUnlink == pdTRUE )
|
||||
{
|
||||
listREMOVE( &pxMessageQueue->xLink );
|
||||
|
||||
/* Set the flag to delete the queue. Deleting the queue is deferred
|
||||
* until xQueueListMutex is released. */
|
||||
xQueueRemoved = pdTRUE;
|
||||
}
|
||||
/* Otherwise, wait for the call to mq_unlink. */
|
||||
else
|
||||
{
|
||||
pxMessageQueue->xPendingUnlink = pdTRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Queue not found; bad descriptor. */
|
||||
errno = EBADF;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
|
||||
/* Delete all resources used by the queue if needed. */
|
||||
if( xQueueRemoved == pdTRUE )
|
||||
{
|
||||
prvDeleteMessageQueue( pxMessageQueue );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int mq_getattr( mqd_t mqdes,
|
||||
struct mq_attr * mqstat )
|
||||
{
|
||||
int iStatus = 0;
|
||||
QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes;
|
||||
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Find the mq referenced by mqdes. */
|
||||
if( prvFindQueueInList( NULL, NULL, mqdes ) == pdTRUE )
|
||||
{
|
||||
/* Update the number of messages in the queue and copy the attributes
|
||||
* into mqstat. */
|
||||
pxMessageQueue->xAttr.mq_curmsgs = ( long ) uxQueueMessagesWaiting( pxMessageQueue->xQueue );
|
||||
*mqstat = pxMessageQueue->xAttr;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Queue not found; bad descriptor. */
|
||||
errno = EBADF;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
mqd_t mq_open( const char * name,
|
||||
int oflag,
|
||||
mode_t mode,
|
||||
struct mq_attr * attr )
|
||||
{
|
||||
mqd_t xMessageQueue = NULL;
|
||||
size_t xNameLength = 0;
|
||||
|
||||
/* Default mq_attr. */
|
||||
struct mq_attr xQueueCreationAttr =
|
||||
{
|
||||
.mq_flags = 0,
|
||||
.mq_maxmsg = posixconfigMQ_MAX_MESSAGES,
|
||||
.mq_msgsize = posixconfigMQ_MAX_SIZE,
|
||||
.mq_curmsgs = 0
|
||||
};
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) mode;
|
||||
|
||||
/* Initialize the queue list, if needed. */
|
||||
prvInitializeQueueList();
|
||||
|
||||
/* Check queue name. */
|
||||
if( prvValidateQueueName( name, &xNameLength ) == pdFALSE )
|
||||
{
|
||||
/* Invalid name. */
|
||||
errno = EINVAL;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
|
||||
/* Check attributes, if given. */
|
||||
if( xMessageQueue == NULL )
|
||||
{
|
||||
if( ( oflag & O_CREAT ) && ( attr != NULL ) && ( ( attr->mq_maxmsg <= 0 ) || ( attr->mq_msgsize <= 0 ) ) )
|
||||
{
|
||||
/* Invalid mq_attr.mq_maxmsg or mq_attr.mq_msgsize. */
|
||||
errno = EINVAL;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
}
|
||||
|
||||
if( xMessageQueue == NULL )
|
||||
{
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Search the queue list to check if the queue exists. */
|
||||
if( prvFindQueueInList( ( QueueListElement_t ** ) &xMessageQueue,
|
||||
name,
|
||||
( mqd_t ) NULL ) == pdTRUE )
|
||||
{
|
||||
/* If the mq exists, check that this function wasn't called with
|
||||
* O_CREAT and O_EXCL. */
|
||||
if( ( oflag & O_EXCL ) && ( oflag & O_CREAT ) )
|
||||
{
|
||||
errno = EEXIST;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Check if the mq has been unlinked and is pending removal. */
|
||||
if( ( ( QueueListElement_t * ) xMessageQueue )->xPendingUnlink == pdTRUE )
|
||||
{
|
||||
/* Queue pending deletion. Don't allow it to be re-opened. */
|
||||
errno = EINVAL;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Increase count of open file descriptors for queue. */
|
||||
( ( QueueListElement_t * ) xMessageQueue )->xOpenDescriptors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Queue does not exist. */
|
||||
else
|
||||
{
|
||||
/* Only create the new queue if O_CREAT was specified. */
|
||||
if( oflag & O_CREAT )
|
||||
{
|
||||
/* Copy attributes if provided. */
|
||||
if( attr != NULL )
|
||||
{
|
||||
xQueueCreationAttr = *attr;
|
||||
}
|
||||
|
||||
/* Copy oflags. */
|
||||
xQueueCreationAttr.mq_flags = ( long ) oflag;
|
||||
|
||||
/* Create the new message queue. */
|
||||
if( prvCreateNewMessageQueue( ( QueueListElement_t ** ) &xMessageQueue,
|
||||
&xQueueCreationAttr,
|
||||
name,
|
||||
xNameLength ) == pdFALSE )
|
||||
{
|
||||
errno = ENOSPC;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = ENOENT;
|
||||
xMessageQueue = ( mqd_t ) -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
}
|
||||
|
||||
return xMessageQueue;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
ssize_t mq_receive( mqd_t mqdes,
|
||||
char * msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned int * msg_prio )
|
||||
{
|
||||
return mq_timedreceive( mqdes, msg_ptr, msg_len, msg_prio, NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int mq_send( mqd_t mqdes,
|
||||
const char * msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned msg_prio )
|
||||
{
|
||||
return mq_timedsend( mqdes, msg_ptr, msg_len, msg_prio, NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
ssize_t mq_timedreceive( mqd_t mqdes,
|
||||
char * msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned * msg_prio,
|
||||
const struct timespec * abstime )
|
||||
{
|
||||
ssize_t xStatus = 0;
|
||||
int iCalculateTimeoutReturn = 0;
|
||||
TickType_t xTimeoutTicks = 0;
|
||||
QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes;
|
||||
QueueElement_t xReceiveData = { 0 };
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) msg_prio;
|
||||
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Find the mq referenced by mqdes. */
|
||||
if( prvFindQueueInList( NULL, NULL, mqdes ) == pdFALSE )
|
||||
{
|
||||
/* Queue not found; bad descriptor. */
|
||||
errno = EBADF;
|
||||
xStatus = -1;
|
||||
}
|
||||
|
||||
/* Verify that msg_len is large enough. */
|
||||
if( xStatus == 0 )
|
||||
{
|
||||
if( msg_len < ( size_t ) pxMessageQueue->xAttr.mq_msgsize )
|
||||
{
|
||||
/* msg_len too small. */
|
||||
errno = EMSGSIZE;
|
||||
xStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if( xStatus == 0 )
|
||||
{
|
||||
/* Convert abstime to a tick timeout. */
|
||||
iCalculateTimeoutReturn = prvCalculateTickTimeout( pxMessageQueue->xAttr.mq_flags,
|
||||
abstime,
|
||||
&xTimeoutTicks );
|
||||
|
||||
if( iCalculateTimeoutReturn != 0 )
|
||||
{
|
||||
errno = iCalculateTimeoutReturn;
|
||||
xStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
|
||||
if( xStatus == 0 )
|
||||
{
|
||||
/* Receive data from the FreeRTOS queue. */
|
||||
if( xQueueReceive( pxMessageQueue->xQueue,
|
||||
&xReceiveData,
|
||||
xTimeoutTicks ) == pdFALSE )
|
||||
{
|
||||
/* If queue receive fails, set the appropriate errno. */
|
||||
if( pxMessageQueue->xAttr.mq_flags & O_NONBLOCK )
|
||||
{
|
||||
/* Set errno to EAGAIN for nonblocking mq. */
|
||||
errno = EAGAIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Otherwise, set errno to ETIMEDOUT. */
|
||||
errno = ETIMEDOUT;
|
||||
}
|
||||
|
||||
xStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if( xStatus == 0 )
|
||||
{
|
||||
/* Get the length of data for return value. */
|
||||
xStatus = ( ssize_t ) xReceiveData.xDataSize;
|
||||
|
||||
/* Copy received data into given buffer, then free it. */
|
||||
( void ) memcpy( msg_ptr, xReceiveData.pcData, xReceiveData.xDataSize );
|
||||
vPortFree( xReceiveData.pcData );
|
||||
}
|
||||
|
||||
return xStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int mq_timedsend( mqd_t mqdes,
|
||||
const char * msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned int msg_prio,
|
||||
const struct timespec * abstime )
|
||||
{
|
||||
int iStatus = 0, iCalculateTimeoutReturn = 0;
|
||||
TickType_t xTimeoutTicks = 0;
|
||||
QueueListElement_t * pxMessageQueue = ( QueueListElement_t * ) mqdes;
|
||||
QueueElement_t xSendData = { 0 };
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) msg_prio;
|
||||
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Find the mq referenced by mqdes. */
|
||||
if( prvFindQueueInList( NULL, NULL, mqdes ) == pdFALSE )
|
||||
{
|
||||
/* Queue not found; bad descriptor. */
|
||||
errno = EBADF;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* Verify that mq_msgsize is large enough. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
if( msg_len > ( size_t ) pxMessageQueue->xAttr.mq_msgsize )
|
||||
{
|
||||
/* msg_len too large. */
|
||||
errno = EMSGSIZE;
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Convert abstime to a tick timeout. */
|
||||
iCalculateTimeoutReturn = prvCalculateTickTimeout( pxMessageQueue->xAttr.mq_flags,
|
||||
abstime,
|
||||
&xTimeoutTicks );
|
||||
|
||||
if( iCalculateTimeoutReturn != 0 )
|
||||
{
|
||||
errno = iCalculateTimeoutReturn;
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
|
||||
/* Allocate memory for the message. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
xSendData.xDataSize = msg_len;
|
||||
xSendData.pcData = pvPortMalloc( msg_len );
|
||||
|
||||
/* Check that memory allocation succeeded. */
|
||||
if( xSendData.pcData == NULL )
|
||||
{
|
||||
/* msg_len too large. */
|
||||
errno = EMSGSIZE;
|
||||
iStatus = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Copy the data to send. */
|
||||
( void ) memcpy( xSendData.pcData, msg_ptr, msg_len );
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Send data to the FreeRTOS queue. */
|
||||
if( xQueueSend( pxMessageQueue->xQueue,
|
||||
&xSendData,
|
||||
xTimeoutTicks ) == pdFALSE )
|
||||
{
|
||||
/* If queue send fails, set the appropriate errno. */
|
||||
if( pxMessageQueue->xAttr.mq_flags & O_NONBLOCK )
|
||||
{
|
||||
/* Set errno to EAGAIN for nonblocking mq. */
|
||||
errno = EAGAIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Otherwise, set errno to ETIMEDOUT. */
|
||||
errno = ETIMEDOUT;
|
||||
}
|
||||
|
||||
/* Free the allocated queue data. */
|
||||
vPortFree( xSendData.pcData );
|
||||
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int mq_unlink( const char * name )
|
||||
{
|
||||
int iStatus = 0;
|
||||
size_t xNameSize = 0;
|
||||
BaseType_t xQueueRemoved = pdFALSE;
|
||||
QueueListElement_t * pxMessageQueue = NULL;
|
||||
|
||||
/* Initialize the queue list, if needed. */
|
||||
prvInitializeQueueList();
|
||||
|
||||
/* Check queue name. */
|
||||
if( prvValidateQueueName( name, &xNameSize ) == pdFALSE )
|
||||
{
|
||||
/* Error with mq name. */
|
||||
errno = EINVAL;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Lock the mutex that guards access to the queue list. This call will
|
||||
* never fail because it blocks forever. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &xQueueListMutex, portMAX_DELAY );
|
||||
|
||||
/* Check if the named queue exists. */
|
||||
if( prvFindQueueInList( &pxMessageQueue, name, ( mqd_t ) NULL ) == pdTRUE )
|
||||
{
|
||||
/* If the queue exists and there are no open descriptors to it,
|
||||
* remove it from the list. */
|
||||
if( pxMessageQueue->xOpenDescriptors == 0 )
|
||||
{
|
||||
listREMOVE( &pxMessageQueue->xLink );
|
||||
|
||||
/* Set the flag to delete the queue. Deleting the queue is deferred
|
||||
* until xQueueListMutex is released. */
|
||||
xQueueRemoved = pdTRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If the queue has open descriptors, set the pending unlink flag
|
||||
* so that mq_close will free its resources. */
|
||||
pxMessageQueue->xPendingUnlink = pdTRUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The named message queue doesn't exist. */
|
||||
errno = ENOENT;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* Release the mutex protecting the queue list. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &xQueueListMutex );
|
||||
}
|
||||
|
||||
/* Delete all resources used by the queue if needed. */
|
||||
if( xQueueRemoved == pdTRUE )
|
||||
{
|
||||
prvDeleteMessageQueue( pxMessageQueue );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,543 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_pthread.c
|
||||
* @brief Implementation of thread functions in pthread.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/pthread.h"
|
||||
|
||||
/**
|
||||
* @brief Thread attribute object.
|
||||
*/
|
||||
typedef struct pthread_attr_internal
|
||||
{
|
||||
uint16_t usStackSize; /**< Stack size. */
|
||||
uint16_t usSchedPriorityDetachState; /**< Schedule priority 15 bits (LSB) Detach state: 1 bits (MSB) */
|
||||
} pthread_attr_internal_t;
|
||||
|
||||
#define pthreadDETACH_STATE_MASK 0x8000
|
||||
#define pthreadSCHED_PRIORITY_MASK 0x7FFF
|
||||
#define pthreadDETACH_STATE_SHIFT 15
|
||||
#define pthreadGET_SCHED_PRIORITY( var ) ( ( var ) & ( pthreadSCHED_PRIORITY_MASK ) )
|
||||
#define pthreadIS_JOINABLE( var ) ( ( ( var ) & ( pthreadDETACH_STATE_MASK ) ) == pthreadDETACH_STATE_MASK )
|
||||
|
||||
/**
|
||||
* @brief Thread object.
|
||||
*/
|
||||
typedef struct pthread_internal
|
||||
{
|
||||
pthread_attr_internal_t xAttr; /**< Thread attributes. */
|
||||
void * ( *pvStartRoutine )( void * ); /**< Application thread function. */
|
||||
void * xTaskArg; /**< Arguments for application thread function. */
|
||||
TaskHandle_t xTaskHandle; /**< FreeRTOS task handle. */
|
||||
StaticSemaphore_t xJoinBarrier; /**< Synchronizes the two callers of pthread_join. */
|
||||
StaticSemaphore_t xJoinMutex; /**< Ensures that only one other thread may join this thread. */
|
||||
void * xReturn; /**< Return value of pvStartRoutine. */
|
||||
} pthread_internal_t;
|
||||
|
||||
/**
|
||||
* @brief Terminates the calling thread.
|
||||
*
|
||||
* For joinable threads, this function waits for pthread_join. Otherwise,
|
||||
* it deletes the thread and frees up resources used by the thread.
|
||||
*
|
||||
* @return This function does not return.
|
||||
*/
|
||||
static void prvExitThread( void );
|
||||
|
||||
/**
|
||||
* @brief Wrapper function for the user's thread routine.
|
||||
*
|
||||
* This function is executed as a FreeRTOS task function.
|
||||
* @param[in] pxArg A pointer to a pthread_internal_t.
|
||||
*
|
||||
* @return nothing
|
||||
*/
|
||||
static void prvRunThread( void * pxArg );
|
||||
|
||||
/**
|
||||
* @brief Default pthread_attr_t.
|
||||
*/
|
||||
static const pthread_attr_internal_t xDefaultThreadAttributes =
|
||||
{
|
||||
.usStackSize = PTHREAD_STACK_MIN,
|
||||
.usSchedPriorityDetachState = ( ( uint16_t ) tskIDLE_PRIORITY & pthreadSCHED_PRIORITY_MASK ) | ( PTHREAD_CREATE_JOINABLE << pthreadDETACH_STATE_SHIFT ),
|
||||
};
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvExitThread( void )
|
||||
{
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread_self();
|
||||
|
||||
/* If this thread is joinable, wait for a call to pthread_join. */
|
||||
if( pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
|
||||
{
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
|
||||
|
||||
/* Suspend until the call to pthread_join. The caller of pthread_join
|
||||
* will perform cleanup. */
|
||||
vTaskSuspend( NULL );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* For a detached thread, perform cleanup of thread object. */
|
||||
vPortFree( pxThread );
|
||||
vTaskDelete( NULL );
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvRunThread( void * pxArg )
|
||||
{
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) pxArg;
|
||||
|
||||
/* Run the thread routine. */
|
||||
pxThread->xReturn = pxThread->pvStartRoutine( ( void * ) pxThread->xTaskArg );
|
||||
|
||||
/* Exit once finished. This function does not return. */
|
||||
prvExitThread();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_destroy( pthread_attr_t * attr )
|
||||
{
|
||||
( void ) attr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_getdetachstate( const pthread_attr_t * attr,
|
||||
int * detachstate )
|
||||
{
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
if( pthreadIS_JOINABLE( pxAttr->usSchedPriorityDetachState ) )
|
||||
{
|
||||
*detachstate = PTHREAD_CREATE_JOINABLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
*detachstate = PTHREAD_CREATE_DETACHED;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_getschedparam( const pthread_attr_t * attr,
|
||||
struct sched_param * param )
|
||||
{
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
param->sched_priority = ( int ) ( pthreadGET_SCHED_PRIORITY( pxAttr->usSchedPriorityDetachState ) );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_getstacksize( const pthread_attr_t * attr,
|
||||
size_t * stacksize )
|
||||
{
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
*stacksize = ( size_t ) pxAttr->usStackSize;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_init( pthread_attr_t * attr )
|
||||
{
|
||||
/* Copy the default values into the new thread attributes object. */
|
||||
*( ( pthread_attr_internal_t * ) ( attr ) ) = xDefaultThreadAttributes;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_setdetachstate( pthread_attr_t * attr,
|
||||
int detachstate )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
if( ( detachstate != PTHREAD_CREATE_DETACHED ) && ( detachstate != PTHREAD_CREATE_JOINABLE ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* clear and then set msb bit to detachstate) */
|
||||
pxAttr->usSchedPriorityDetachState &= ~pthreadDETACH_STATE_MASK;
|
||||
pxAttr->usSchedPriorityDetachState |= ( ( uint16_t ) detachstate << pthreadDETACH_STATE_SHIFT );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_setschedparam( pthread_attr_t * attr,
|
||||
const struct sched_param * param )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
/* Check for NULL param. */
|
||||
if( param == NULL )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Ensure that param.sched_priority is valid. */
|
||||
if( ( iStatus == 0 ) &&
|
||||
( ( param->sched_priority > sched_get_priority_max( SCHED_OTHER ) ) ||
|
||||
( param->sched_priority < 0 ) ) )
|
||||
{
|
||||
iStatus = ENOTSUP;
|
||||
}
|
||||
|
||||
/* Set the sched_param. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* clear and then set 15 LSB to schedule priority) */
|
||||
pxAttr->usSchedPriorityDetachState &= ~pthreadSCHED_PRIORITY_MASK;
|
||||
pxAttr->usSchedPriorityDetachState |= ( ( uint16_t ) param->sched_priority );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_setschedpolicy( pthread_attr_t * attr,
|
||||
int policy )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) attr;
|
||||
( void ) policy;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_setinheritsched( pthread_attr_t *attr,
|
||||
int inheritsched)
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) attr;
|
||||
( void ) inheritsched;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_attr_setstacksize( pthread_attr_t * attr,
|
||||
size_t stacksize )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_attr_internal_t * pxAttr = ( pthread_attr_internal_t * ) ( attr );
|
||||
|
||||
if( stacksize < PTHREAD_STACK_MIN )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxAttr->usStackSize = ( uint16_t ) stacksize;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_create( pthread_t * thread,
|
||||
const pthread_attr_t * attr,
|
||||
void *( *startroutine )( void * ),
|
||||
void * arg )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_internal_t * pxThread = NULL;
|
||||
struct sched_param xSchedParam = { .sched_priority = tskIDLE_PRIORITY };
|
||||
|
||||
/* Allocate memory for new thread object. */
|
||||
pxThread = ( pthread_internal_t * ) pvPortMalloc( sizeof( pthread_internal_t ) );
|
||||
|
||||
if( pxThread == NULL )
|
||||
{
|
||||
/* No memory. */
|
||||
iStatus = EAGAIN;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* No attributes given, use default attributes. */
|
||||
if( attr == NULL )
|
||||
{
|
||||
pxThread->xAttr = xDefaultThreadAttributes;
|
||||
}
|
||||
/* Otherwise, use provided attributes. */
|
||||
else
|
||||
{
|
||||
pxThread->xAttr = *( ( pthread_attr_internal_t * ) ( attr ) );
|
||||
}
|
||||
|
||||
/* Get priority from attributes */
|
||||
xSchedParam.sched_priority = ( int ) pthreadGET_SCHED_PRIORITY( pxThread->xAttr.usSchedPriorityDetachState );
|
||||
|
||||
/* Set argument and start routine. */
|
||||
pxThread->xTaskArg = arg;
|
||||
pxThread->pvStartRoutine = startroutine;
|
||||
|
||||
/* If this thread is joinable, create the synchronization mechanisms for
|
||||
* pthread_join. */
|
||||
|
||||
if( pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
|
||||
{
|
||||
/* These calls will not fail when their arguments aren't NULL. */
|
||||
( void ) xSemaphoreCreateMutexStatic( &pxThread->xJoinMutex );
|
||||
( void ) xSemaphoreCreateBinaryStatic( &pxThread->xJoinBarrier );
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Suspend all tasks to create a critical section. This ensures that
|
||||
* the new thread doesn't exit before a tag is assigned. */
|
||||
vTaskSuspendAll();
|
||||
|
||||
/* Create the FreeRTOS task that will run the pthread. */
|
||||
if( xTaskCreate( prvRunThread,
|
||||
posixconfigPTHREAD_TASK_NAME,
|
||||
( uint16_t ) ( pxThread->xAttr.usStackSize / sizeof( StackType_t ) ),
|
||||
( void * ) pxThread,
|
||||
xSchedParam.sched_priority,
|
||||
&pxThread->xTaskHandle ) != pdPASS )
|
||||
{
|
||||
/* Task creation failed, no memory. */
|
||||
vPortFree( pxThread );
|
||||
iStatus = EAGAIN;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Store the pointer to the thread object in the task tag. */
|
||||
vTaskSetApplicationTaskTag( pxThread->xTaskHandle, ( TaskHookFunction_t ) pxThread );
|
||||
|
||||
/* Set the thread object for the user. */
|
||||
*thread = ( pthread_t ) pxThread;
|
||||
}
|
||||
|
||||
/* End the critical section. */
|
||||
xTaskResumeAll();
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
|
||||
int pthread_setname_np(pthread_t thread, const char *name)
|
||||
{
|
||||
pthread_internal_t * pxThread = NULL;
|
||||
TaskHandle_t task_t;
|
||||
int len;
|
||||
int ret = EINVAL;
|
||||
|
||||
if ((len = strnlen(name, 16)) > 15)
|
||||
return ERANGE;
|
||||
vTaskSuspendAll();
|
||||
pxThread = thread;
|
||||
task_t = pxThread->xTaskHandle;
|
||||
if (task_t && name) {
|
||||
pcTaskSetName(task_t, name);
|
||||
ret = 0;
|
||||
}
|
||||
xTaskResumeAll();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_getschedparam( pthread_t thread,
|
||||
int * policy,
|
||||
struct sched_param * param )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) thread;
|
||||
|
||||
*policy = SCHED_OTHER;
|
||||
param->sched_priority = ( int ) pthreadGET_SCHED_PRIORITY( pxThread->xAttr.usSchedPriorityDetachState );
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_equal( pthread_t t1,
|
||||
pthread_t t2 )
|
||||
{
|
||||
return t1 == t2;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void pthread_exit( void * value_ptr )
|
||||
{
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread_self();
|
||||
|
||||
/* Set the return value. */
|
||||
pxThread->xReturn = value_ptr;
|
||||
|
||||
/* Exit this thread. */
|
||||
prvExitThread();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_join( pthread_t pthread,
|
||||
void ** retval )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread;
|
||||
|
||||
/* Make sure pthread is joinable. Otherwise, this function would block
|
||||
* forever waiting for an unjoinable thread. */
|
||||
if( !pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
|
||||
{
|
||||
iStatus = EDEADLK;
|
||||
}
|
||||
|
||||
/* Only one thread may attempt to join another. Lock the join mutex
|
||||
* to prevent other threads from calling pthread_join on the same thread. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxThread->xJoinMutex, 0 ) != pdPASS )
|
||||
{
|
||||
/* Another thread has already joined the requested thread, which would
|
||||
* cause this thread to wait forever. */
|
||||
iStatus = EDEADLK;
|
||||
}
|
||||
}
|
||||
|
||||
/* Attempting to join the calling thread would cause a deadlock. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
if( pthread_equal( pthread_self(), pthread ) != 0 )
|
||||
{
|
||||
iStatus = EDEADLK;
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Wait for the joining thread to finish. Because this call waits forever,
|
||||
* it should never fail. */
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier, portMAX_DELAY );
|
||||
|
||||
/* Create a critical section to clean up the joined thread. */
|
||||
vTaskSuspendAll();
|
||||
|
||||
/* Release xJoinBarrier and delete it. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
|
||||
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
|
||||
|
||||
/* Release xJoinMutex and delete it. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
|
||||
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
|
||||
|
||||
/* Delete the FreeRTOS task that ran the thread. */
|
||||
vTaskDelete( pxThread->xTaskHandle );
|
||||
|
||||
/* Set the return value. */
|
||||
if( retval != NULL )
|
||||
{
|
||||
*retval = pxThread->xReturn;
|
||||
}
|
||||
|
||||
/* Free the thread object. */
|
||||
vPortFree( pxThread );
|
||||
|
||||
/* End the critical section. */
|
||||
xTaskResumeAll();
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
pthread_t pthread_self( void )
|
||||
{
|
||||
/* Return a reference to this pthread object, which is stored in the
|
||||
* FreeRTOS task tag. */
|
||||
return ( pthread_t ) xTaskGetApplicationTaskTag( NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_setschedparam( pthread_t thread,
|
||||
int policy,
|
||||
const struct sched_param * param )
|
||||
{
|
||||
int iStatus = 0;
|
||||
|
||||
pthread_internal_t * pxThread = ( pthread_internal_t * ) thread;
|
||||
|
||||
/* Silence compiler warnings about unused parameters. */
|
||||
( void ) policy;
|
||||
|
||||
/* Copy the given sched_param. */
|
||||
iStatus = pthread_attr_setschedparam( ( pthread_attr_t * ) &pxThread->xAttr, param );
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Change the priority of the FreeRTOS task. */
|
||||
vTaskPrioritySet( pxThread->xTaskHandle, param->sched_priority );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_pthread_barrier.c
|
||||
* @brief Implementation of barrier functions in pthread.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/pthread.h"
|
||||
|
||||
#include "atomic.h"
|
||||
|
||||
/*
|
||||
* @brief barrier max count
|
||||
*
|
||||
* Barriers are implemented on FreeRTOS event groups, of which 8 bits are usable
|
||||
* when configUSE_16_BIT_TICKS is 1. Otherwise, 24 bits are usable.
|
||||
*/
|
||||
/**@{ */
|
||||
#if ( configUSE_16_BIT_TICKS == 1 )
|
||||
#define posixPTHREAD_BARRIER_MAX_COUNT ( 8 )
|
||||
#else
|
||||
#define posixPTHREAD_BARRIER_MAX_COUNT ( 24 )
|
||||
#endif
|
||||
/**@} */
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_barrier_destroy( pthread_barrier_t * barrier )
|
||||
{
|
||||
pthread_barrier_internal_t * pxBarrier = ( pthread_barrier_internal_t * ) ( barrier );
|
||||
|
||||
/* Free all resources used by the barrier. */
|
||||
( void ) vEventGroupDelete( ( EventGroupHandle_t ) &pxBarrier->xBarrierEventGroup );
|
||||
( void ) vSemaphoreDelete( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_barrier_init( pthread_barrier_t * barrier,
|
||||
const pthread_barrierattr_t * attr,
|
||||
unsigned count )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_barrier_internal_t * pxNewBarrier = ( pthread_barrier_internal_t * ) ( barrier );
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) attr;
|
||||
|
||||
/* Ensure count is greater than 0. */
|
||||
if( count == 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Ensure that count will fit in a FreeRTOS event group. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
if( count > posixPTHREAD_BARRIER_MAX_COUNT )
|
||||
{
|
||||
/* No memory exists in the event group for more than
|
||||
* posixPTHREAD_BARRIER_MAX_COUNT threads. */
|
||||
iStatus = ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Set the current thread count and threshold. */
|
||||
pxNewBarrier->uThreadCount = 0;
|
||||
pxNewBarrier->uThreshold = count;
|
||||
|
||||
/* Create the FreeRTOS event group. This call will not fail when its
|
||||
* argument isn't NULL. */
|
||||
( void ) xEventGroupCreateStatic( &pxNewBarrier->xBarrierEventGroup );
|
||||
|
||||
/* Create the semaphore that prevents more than count threads from being
|
||||
* unblocked by a single successful pthread_barrier_wait. This semaphore
|
||||
* counts down from count and cannot decrement below 0. */
|
||||
( void ) xSemaphoreCreateCountingStatic( ( UBaseType_t ) count, /* Max count. */
|
||||
( UBaseType_t ) count, /* Initial count. */
|
||||
&pxNewBarrier->xThreadCountSemaphore );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_barrier_wait( pthread_barrier_t * barrier )
|
||||
{
|
||||
int iStatus = 0;
|
||||
unsigned i = 0; /* Loop iterator. */
|
||||
pthread_barrier_internal_t * pxBarrier = ( pthread_barrier_internal_t * ) ( barrier );
|
||||
unsigned uThreadNumber = 0;
|
||||
|
||||
/* Decrement the number of threads waiting on this barrier. This will prevent more
|
||||
* than pxBarrier->uThreshold threads from being unblocked by a single successful
|
||||
* pthread_barrier_wait call.
|
||||
*
|
||||
* This call will never fail because it blocks forever.
|
||||
*/
|
||||
( void ) xSemaphoreTake( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore, portMAX_DELAY );
|
||||
|
||||
uThreadNumber = Atomic_Increment_u32( ( uint32_t * ) &pxBarrier->uThreadCount );
|
||||
|
||||
/* Set the bit in the event group representing this thread, then wait for the other
|
||||
* threads to set their bit. This call should wait forever until all threads have set
|
||||
* their bit, so the return value is ignored. */
|
||||
( void ) xEventGroupSync( ( EventGroupHandle_t ) &pxBarrier->xBarrierEventGroup,
|
||||
1 << uThreadNumber, /* Which bit in the event group to set. */
|
||||
( 1 << pxBarrier->uThreshold ) - 1, /* Wait for all threads to set their bits. */
|
||||
portMAX_DELAY );
|
||||
|
||||
/* The first thread to enter the barrier gets PTHREAD_BARRIER_SERIAL_THREAD as its
|
||||
* return value and resets xThreadCountSemaphore. */
|
||||
|
||||
if( uThreadNumber == 0 )
|
||||
{
|
||||
iStatus = PTHREAD_BARRIER_SERIAL_THREAD;
|
||||
|
||||
/* uThreadCount can be safely changed without locking xThreadCountMutex
|
||||
* because xThreadCountSemaphore is currently 0. */
|
||||
pxBarrier->uThreadCount = 0;
|
||||
|
||||
/* Reset xThreadCountSemaphore. This allows more threads to enter the
|
||||
* barrier, starting a new cycle. */
|
||||
for( i = 0; i < pxBarrier->uThreshold; i++ )
|
||||
{
|
||||
xSemaphoreGive( ( SemaphoreHandle_t ) &pxBarrier->xThreadCountSemaphore );
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,296 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_pthread_cond.c
|
||||
* @brief Implementation of condition variable functions in pthread.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <limits.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/pthread.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
#include "atomic.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize a PTHREAD_COND_INITIALIZER cond.
|
||||
*
|
||||
* PTHREAD_COND_INITIALIZER sets a flag for a cond to be initialized later.
|
||||
* This function performs the initialization.
|
||||
* @param[in] pxCond The cond to initialize.
|
||||
*
|
||||
* @return nothing
|
||||
*/
|
||||
static void prvInitializeStaticCond( pthread_cond_internal_t * pxCond );
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvInitializeStaticCond( pthread_cond_internal_t * pxCond )
|
||||
{
|
||||
/* Check if the condition variable needs to be initialized. */
|
||||
if( pxCond->xIsInitialized == pdFALSE )
|
||||
{
|
||||
/* Cond initialization must be in a critical section to prevent two threads
|
||||
* from initializing it at the same time. */
|
||||
taskENTER_CRITICAL();
|
||||
|
||||
/* Check again that the cond is still uninitialized, i.e. it wasn't
|
||||
* initialized while this function was waiting to enter the critical
|
||||
* section. */
|
||||
if( pxCond->xIsInitialized == pdFALSE )
|
||||
{
|
||||
/* Set the members of the cond. The semaphore create calls will never fail
|
||||
* when their arguments aren't NULL. */
|
||||
pxCond->xIsInitialized = pdTRUE;
|
||||
( void ) xSemaphoreCreateCountingStatic( INT_MAX, 0U, &pxCond->xCondWaitSemaphore );
|
||||
pxCond->iWaitingThreads = 0;
|
||||
}
|
||||
|
||||
/* Exit the critical section. */
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check "atomically" if iLocalWaitingThreads == pxCond->iWaitingThreads and decrement.
|
||||
*/
|
||||
static void prvTestAndDecrement( pthread_cond_t * pxCond,
|
||||
unsigned iLocalWaitingThreads )
|
||||
{
|
||||
/* Test local copy of threads waiting is larger than zero. */
|
||||
while( iLocalWaitingThreads > 0 )
|
||||
{
|
||||
/* Test-and-set. Atomically check whether the copy in memory has changed.
|
||||
* And, if not decrease the copy of threads waiting in memory. */
|
||||
if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, ( uint32_t ) iLocalWaitingThreads - 1, ( uint32_t ) iLocalWaitingThreads ) )
|
||||
{
|
||||
/* Signal one succeeded. Break. */
|
||||
break;
|
||||
}
|
||||
|
||||
/* Local copy may be out dated. Reload, and retry. */
|
||||
iLocalWaitingThreads = pxCond->iWaitingThreads;
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_broadcast( pthread_cond_t * cond )
|
||||
{
|
||||
unsigned i = 0;
|
||||
pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond );
|
||||
|
||||
/* If the cond is uninitialized, perform initialization. */
|
||||
prvInitializeStaticCond( pxCond );
|
||||
|
||||
/* Local copy of number of threads waiting. */
|
||||
unsigned iLocalWaitingThreads = pxCond->iWaitingThreads;
|
||||
|
||||
/* Test local copy of threads waiting is larger than zero. */
|
||||
while( iLocalWaitingThreads > 0 )
|
||||
{
|
||||
/* Test-and-set. Atomically check whether the copy in memory has changed.
|
||||
* And, if not set the copy of threads waiting in memory to zero. */
|
||||
if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, 0, ( uint32_t ) iLocalWaitingThreads ) )
|
||||
{
|
||||
/* Unblock all. */
|
||||
for( i = 0; i < iLocalWaitingThreads; i++ )
|
||||
{
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore );
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Local copy is out dated. Reload, and retry. */
|
||||
iLocalWaitingThreads = pxCond->iWaitingThreads;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_destroy( pthread_cond_t * cond )
|
||||
{
|
||||
pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond );
|
||||
|
||||
/* Free all resources in use by the cond. */
|
||||
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_init( pthread_cond_t * cond,
|
||||
const pthread_condattr_t * attr )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) cond;
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) attr;
|
||||
|
||||
if( pxCond == NULL )
|
||||
{
|
||||
iStatus = ENOMEM;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Set the members of the cond. The semaphore create calls will never fail
|
||||
* when their arguments aren't NULL. */
|
||||
pxCond->xIsInitialized = pdTRUE;
|
||||
|
||||
( void ) xSemaphoreCreateCountingStatic( INT_MAX, 0U, &pxCond->xCondWaitSemaphore );
|
||||
pxCond->iWaitingThreads = 0;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_signal( pthread_cond_t * cond )
|
||||
{
|
||||
pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond );
|
||||
|
||||
/* If the cond is uninitialized, perform initialization. */
|
||||
prvInitializeStaticCond( pxCond );
|
||||
|
||||
/* Local copy of number of threads waiting. */
|
||||
unsigned iLocalWaitingThreads = pxCond->iWaitingThreads;
|
||||
|
||||
/* Test local copy of threads waiting is larger than zero. */
|
||||
while( iLocalWaitingThreads > 0 )
|
||||
{
|
||||
/* Test-and-set. Atomically check whether the copy in memory has changed.
|
||||
* And, if not decrease the copy of threads waiting in memory. */
|
||||
if( ATOMIC_COMPARE_AND_SWAP_SUCCESS == Atomic_CompareAndSwap_u32( ( uint32_t * ) &pxCond->iWaitingThreads, ( uint32_t ) iLocalWaitingThreads - 1, ( uint32_t ) iLocalWaitingThreads ) )
|
||||
{
|
||||
/* Unblock one. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore );
|
||||
|
||||
/* Signal one succeeded. Break. */
|
||||
break;
|
||||
}
|
||||
|
||||
/* Local copy may be out dated. Reload, and retry. */
|
||||
iLocalWaitingThreads = pxCond->iWaitingThreads;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_timedwait( pthread_cond_t * cond,
|
||||
pthread_mutex_t * mutex,
|
||||
const struct timespec * abstime )
|
||||
{
|
||||
unsigned iLocalWaitingThreads;
|
||||
int iStatus = 0;
|
||||
pthread_cond_internal_t * pxCond = ( pthread_cond_internal_t * ) ( cond );
|
||||
TickType_t xDelay = portMAX_DELAY;
|
||||
|
||||
/* If the cond is uninitialized, perform initialization. */
|
||||
prvInitializeStaticCond( pxCond );
|
||||
|
||||
/* Convert abstime to a delay in TickType_t if provided. */
|
||||
if( abstime != NULL )
|
||||
{
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Get current time */
|
||||
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay );
|
||||
}
|
||||
}
|
||||
|
||||
/* Increase the counter of threads blocking on condition variable, then
|
||||
* unlock mutex. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Atomically increments thread waiting by 1, and
|
||||
* stores number of threads waiting before increment. */
|
||||
iLocalWaitingThreads = Atomic_Increment_u32( ( uint32_t * ) &pxCond->iWaitingThreads );
|
||||
|
||||
iStatus = pthread_mutex_unlock( mutex );
|
||||
}
|
||||
|
||||
/* Wait on the condition variable. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxCond->xCondWaitSemaphore,
|
||||
xDelay ) == pdPASS )
|
||||
{
|
||||
/* When successful, relock mutex. */
|
||||
iStatus = pthread_mutex_lock( mutex );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Timeout. Relock mutex and decrement number of waiting threads. */
|
||||
iStatus = ETIMEDOUT;
|
||||
( void ) pthread_mutex_lock( mutex );
|
||||
|
||||
/* Atomically decrements thread waiting by 1.
|
||||
* If iLocalWaitingThreads is updated by other thread(s) in between,
|
||||
* this implementation guarantees to decrement by 1 based on the
|
||||
* value currently in pxCond->iWaitingThreads. */
|
||||
prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Atomically decrements thread waiting by 1.
|
||||
* If iLocalWaitingThreads is updated by other thread(s) in between,
|
||||
* this implementation guarantees to decrement by 1 based on the
|
||||
* value currently in pxCond->iWaitingThreads. */
|
||||
prvTestAndDecrement( pxCond, iLocalWaitingThreads + 1 );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_cond_wait( pthread_cond_t * cond,
|
||||
pthread_mutex_t * mutex )
|
||||
{
|
||||
return pthread_cond_timedwait( cond, mutex, NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,438 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_pthread_mutex.c
|
||||
* @brief Implementation of mutex functions in pthread.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/pthread.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize a PTHREAD_MUTEX_INITIALIZER mutex.
|
||||
*
|
||||
* PTHREAD_MUTEX_INITIALIZER sets a flag for a mutex to be initialized later.
|
||||
* This function performs the initialization.
|
||||
* @param[in] pxMutex The mutex to initialize.
|
||||
*
|
||||
* @return nothing
|
||||
*/
|
||||
static void prvInitializeStaticMutex( pthread_mutex_internal_t * pxMutex );
|
||||
|
||||
/**
|
||||
* @brief Default pthread_mutexattr_t.
|
||||
*/
|
||||
static const pthread_mutexattr_internal_t xDefaultMutexAttributes =
|
||||
{
|
||||
.iType = PTHREAD_MUTEX_DEFAULT,
|
||||
};
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvInitializeStaticMutex( pthread_mutex_internal_t * pxMutex )
|
||||
{
|
||||
/* Check if the mutex needs to be initialized. */
|
||||
if( pxMutex->xIsInitialized == pdFALSE )
|
||||
{
|
||||
/* Mutex initialization must be in a critical section to prevent two threads
|
||||
* from initializing it at the same time. */
|
||||
taskENTER_CRITICAL();
|
||||
|
||||
/* Check again that the mutex is still uninitialized, i.e. it wasn't
|
||||
* initialized while this function was waiting to enter the critical
|
||||
* section. */
|
||||
if( pxMutex->xIsInitialized == pdFALSE )
|
||||
{
|
||||
/* Set the mutex as the default type. */
|
||||
pxMutex->xAttr.iType = PTHREAD_MUTEX_DEFAULT;
|
||||
|
||||
/* Call the correct FreeRTOS mutex initialization function based on
|
||||
* the mutex type. */
|
||||
#if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_RECURSIVE
|
||||
( void ) xSemaphoreCreateRecursiveMutexStatic( &pxMutex->xMutex );
|
||||
#else
|
||||
( void ) xSemaphoreCreateMutexStatic( &pxMutex->xMutex );
|
||||
#endif
|
||||
|
||||
pxMutex->xIsInitialized = pdTRUE;
|
||||
}
|
||||
|
||||
/* Exit the critical section. */
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_destroy( pthread_mutex_t * mutex )
|
||||
{
|
||||
pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex );
|
||||
|
||||
/* Free resources in use by the mutex. */
|
||||
if( pxMutex->xTaskOwner == NULL )
|
||||
{
|
||||
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxMutex->xMutex );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_init( pthread_mutex_t * mutex,
|
||||
const pthread_mutexattr_t * attr )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) mutex;
|
||||
|
||||
if( pxMutex == NULL )
|
||||
{
|
||||
/* No memory. */
|
||||
iStatus = ENOMEM;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
*pxMutex = FREERTOS_POSIX_MUTEX_INITIALIZER;
|
||||
|
||||
/* No attributes given, use default attributes. */
|
||||
if( attr == NULL )
|
||||
{
|
||||
pxMutex->xAttr = xDefaultMutexAttributes;
|
||||
}
|
||||
/* Otherwise, use provided attributes. */
|
||||
else
|
||||
{
|
||||
pxMutex->xAttr = *( ( pthread_mutexattr_internal_t * ) ( attr ) );
|
||||
}
|
||||
|
||||
/* Call the correct FreeRTOS mutex creation function based on mutex type. */
|
||||
if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE )
|
||||
{
|
||||
/* Recursive mutex. */
|
||||
( void ) xSemaphoreCreateRecursiveMutexStatic( &pxMutex->xMutex );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* All other mutex types. */
|
||||
( void ) xSemaphoreCreateMutexStatic( &pxMutex->xMutex );
|
||||
}
|
||||
|
||||
/* Ensure that the FreeRTOS mutex was successfully created. */
|
||||
if( ( SemaphoreHandle_t ) &pxMutex->xMutex == NULL )
|
||||
{
|
||||
/* Failed to create mutex. Set error EAGAIN and free mutex object. */
|
||||
iStatus = EAGAIN;
|
||||
vPortFree( pxMutex );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Mutex successfully created. */
|
||||
pxMutex->xIsInitialized = pdTRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_lock( pthread_mutex_t * mutex )
|
||||
{
|
||||
return pthread_mutex_timedlock( mutex, NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_timedlock( pthread_mutex_t * mutex,
|
||||
const struct timespec * abstime )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex );
|
||||
TickType_t xDelay = portMAX_DELAY;
|
||||
BaseType_t xFreeRTOSMutexTakeStatus = pdFALSE;
|
||||
|
||||
/* If mutex in uninitialized, perform initialization. */
|
||||
prvInitializeStaticMutex( pxMutex );
|
||||
|
||||
/* At this point, the mutex should be initialized. */
|
||||
configASSERT( pxMutex->xIsInitialized == pdTRUE );
|
||||
|
||||
/* Convert abstime to a delay in TickType_t if provided. */
|
||||
if( abstime != NULL )
|
||||
{
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Get current time */
|
||||
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay );
|
||||
}
|
||||
|
||||
/* If abstime was in the past, still attempt to lock the mutex without
|
||||
* blocking, per POSIX spec. */
|
||||
if( iStatus == ETIMEDOUT )
|
||||
{
|
||||
xDelay = 0;
|
||||
iStatus = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if trying to lock a currently owned mutex. */
|
||||
if( ( iStatus == 0 ) &&
|
||||
( pxMutex->xAttr.iType == PTHREAD_MUTEX_ERRORCHECK ) && /* Only PTHREAD_MUTEX_ERRORCHECK type detects deadlock. */
|
||||
( pxMutex->xTaskOwner == xTaskGetCurrentTaskHandle() ) ) /* Check if locking a currently owned mutex. */
|
||||
{
|
||||
iStatus = EDEADLK;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Call the correct FreeRTOS mutex take function based on mutex type. */
|
||||
if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE )
|
||||
{
|
||||
xFreeRTOSMutexTakeStatus = xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) &pxMutex->xMutex, xDelay );
|
||||
}
|
||||
else
|
||||
{
|
||||
xFreeRTOSMutexTakeStatus = xSemaphoreTake( ( SemaphoreHandle_t ) &pxMutex->xMutex, xDelay );
|
||||
}
|
||||
|
||||
/* If the mutex was successfully taken, set its owner. */
|
||||
if( xFreeRTOSMutexTakeStatus == pdPASS )
|
||||
{
|
||||
pxMutex->xTaskOwner = xTaskGetCurrentTaskHandle();
|
||||
}
|
||||
/* Otherwise, the mutex take timed out. */
|
||||
else
|
||||
{
|
||||
iStatus = ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_trylock( pthread_mutex_t * mutex )
|
||||
{
|
||||
int iStatus = 0;
|
||||
struct timespec xTimeout =
|
||||
{
|
||||
.tv_sec = 0,
|
||||
.tv_nsec = 0
|
||||
};
|
||||
|
||||
/* Attempt to lock with no timeout. */
|
||||
iStatus = pthread_mutex_timedlock( mutex, &xTimeout );
|
||||
|
||||
/* POSIX specifies that this function should return EBUSY instead of
|
||||
* ETIMEDOUT for attempting to lock a locked mutex. */
|
||||
if( iStatus == ETIMEDOUT )
|
||||
{
|
||||
iStatus = EBUSY;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutex_unlock( pthread_mutex_t * mutex )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_mutex_internal_t * pxMutex = ( pthread_mutex_internal_t * ) ( mutex );
|
||||
|
||||
/* If mutex in uninitialized, perform initialization. */
|
||||
prvInitializeStaticMutex( pxMutex );
|
||||
|
||||
/* Check if trying to unlock an unowned mutex. */
|
||||
if( ( ( pxMutex->xAttr.iType == PTHREAD_MUTEX_ERRORCHECK ) ||
|
||||
( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE ) ) &&
|
||||
( pxMutex->xTaskOwner != xTaskGetCurrentTaskHandle() ) )
|
||||
{
|
||||
iStatus = EPERM;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Suspend the scheduler so that
|
||||
* mutex is unlocked AND owner is updated atomically */
|
||||
vTaskSuspendAll();
|
||||
|
||||
/* Call the correct FreeRTOS mutex unlock function based on mutex type. */
|
||||
if( pxMutex->xAttr.iType == PTHREAD_MUTEX_RECURSIVE )
|
||||
{
|
||||
( void ) xSemaphoreGiveRecursive( ( SemaphoreHandle_t ) &pxMutex->xMutex );
|
||||
}
|
||||
else
|
||||
{
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxMutex->xMutex );
|
||||
}
|
||||
|
||||
/* Update the owner of the mutex. A recursive mutex may still have an
|
||||
* owner, so it should be updated with xSemaphoreGetMutexHolder. */
|
||||
pxMutex->xTaskOwner = xSemaphoreGetMutexHolder( ( SemaphoreHandle_t ) &pxMutex->xMutex );
|
||||
|
||||
/* Resume the scheduler */
|
||||
( void ) xTaskResumeAll();
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutexattr_destroy( pthread_mutexattr_t * attr )
|
||||
{
|
||||
( void ) attr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutexattr_gettype( const pthread_mutexattr_t * attr,
|
||||
int * type )
|
||||
{
|
||||
pthread_mutexattr_internal_t * pxAttr = ( pthread_mutexattr_internal_t * ) ( attr );
|
||||
|
||||
*type = pxAttr->iType;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutexattr_init( pthread_mutexattr_t * attr )
|
||||
{
|
||||
*( ( pthread_mutexattr_internal_t * ) ( attr ) ) = xDefaultMutexAttributes;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int pthread_mutexattr_settype( pthread_mutexattr_t * attr,
|
||||
int type )
|
||||
{
|
||||
int iStatus = 0;
|
||||
pthread_mutexattr_internal_t * pxAttr = ( pthread_mutexattr_internal_t * ) ( attr );
|
||||
|
||||
switch( type )
|
||||
{
|
||||
case PTHREAD_MUTEX_NORMAL:
|
||||
case PTHREAD_MUTEX_RECURSIVE:
|
||||
case PTHREAD_MUTEX_ERRORCHECK:
|
||||
pxAttr->iType = type;
|
||||
break;
|
||||
|
||||
default:
|
||||
iStatus = EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
static __always_inline int
|
||||
futex_supports_pshared (int pshared)
|
||||
{
|
||||
if (pshared == PTHREAD_PROCESS_PRIVATE)
|
||||
return 0;
|
||||
else if (pshared == PTHREAD_PROCESS_SHARED)
|
||||
return 0;
|
||||
else
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
|
||||
{
|
||||
pthread_mutexattr_internal_t *iattr;
|
||||
int err = futex_supports_pshared (pshared);
|
||||
if (err != 0)
|
||||
return err;
|
||||
iattr = (pthread_mutexattr_internal_t *) attr;
|
||||
if (pshared == PTHREAD_PROCESS_PRIVATE)
|
||||
iattr->iType &= ~PTHREAD_MUTEXATTR_FLAG_PSHARED;
|
||||
else
|
||||
iattr->iType |= PTHREAD_MUTEXATTR_FLAG_PSHARED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr, int *pshared)
|
||||
{
|
||||
const pthread_mutexattr_internal_t *iattr;
|
||||
iattr = (const pthread_mutexattr_internal_t *) attr;
|
||||
*pshared = ((iattr->iType & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0
|
||||
? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
pthread_mutexattr_setrobust (pthread_mutexattr_t *attr, int robustness)
|
||||
{
|
||||
if (robustness != PTHREAD_MUTEX_STALLED_NP
|
||||
&& __builtin_expect (robustness != PTHREAD_MUTEX_ROBUST_NP, 0))
|
||||
return EINVAL;
|
||||
pthread_mutexattr_internal_t *iattr = (pthread_mutexattr_internal_t *) attr;
|
||||
/* We use bit 30 to signal whether the mutex is going to be
|
||||
*robust or not.
|
||||
*/
|
||||
if (robustness == PTHREAD_MUTEX_STALLED_NP)
|
||||
iattr->iType &= ~PTHREAD_MUTEXATTR_FLAG_ROBUST;
|
||||
else
|
||||
iattr->iType |= PTHREAD_MUTEXATTR_FLAG_ROBUST;
|
||||
return 0;
|
||||
}
|
||||
#define pthread_mutexattr_setrobust_np pthread_mutexattr_setrobust
|
||||
|
||||
int
|
||||
pthread_mutexattr_getrobust (const pthread_mutexattr_t *attr, int *robustness)
|
||||
{
|
||||
const pthread_mutexattr_internal_t *iattr;
|
||||
iattr = (const pthread_mutexattr_internal_t *) attr;
|
||||
*robustness = ((iattr->iType & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0
|
||||
? PTHREAD_MUTEX_ROBUST_NP : PTHREAD_MUTEX_STALLED_NP);
|
||||
return 0;
|
||||
}
|
||||
#define pthread_mutexattr_getrobust_np pthread_mutexattr_getrobust
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_sched.c
|
||||
* @brief Implementation of scheduler functions in sched.h
|
||||
*/
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/sched.h"
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sched_get_priority_max( int policy )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) policy;
|
||||
|
||||
return configMAX_PRIORITIES - 1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sched_get_priority_min( int policy )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) policy;
|
||||
|
||||
return tskIDLE_PRIORITY;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sched_yield( void )
|
||||
{
|
||||
taskYIELD();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,232 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_semaphore.c
|
||||
* @brief Implementation of functions in semaphore.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/semaphore.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
#include "atomic.h"
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_destroy( sem_t * sem )
|
||||
{
|
||||
sem_internal_t * pxSem = ( sem_internal_t * ) ( sem );
|
||||
|
||||
/* Free the resources in use by the semaphore. */
|
||||
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxSem->xSemaphore );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_getvalue( sem_t * sem,
|
||||
int * sval )
|
||||
{
|
||||
sem_internal_t * pxSem = ( sem_internal_t * ) ( sem );
|
||||
|
||||
/* Get value does not need atomic operation, since -- Open Group
|
||||
* states "the updated value represents an actual semaphore value that
|
||||
* occurred at some unspecified time during the call, but it need not be the
|
||||
* actual value of the semaphore when it is returned to the calling process."
|
||||
*/
|
||||
*sval = pxSem->value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_init( sem_t * sem,
|
||||
int pshared,
|
||||
unsigned value )
|
||||
{
|
||||
int iStatus = 0;
|
||||
sem_internal_t * pxSem = ( sem_internal_t * ) ( sem );
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) pshared;
|
||||
|
||||
/* Check value parameter. */
|
||||
if( value > SEM_VALUE_MAX )
|
||||
{
|
||||
errno = EINVAL;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* value is guaranteed to not exceed INT32_MAX, which is the default value of SEM_VALUE_MAX (0x7FFFU). */
|
||||
pxSem->value = ( int ) value;
|
||||
|
||||
/* Create the FreeRTOS semaphore.
|
||||
* This is only used to queue threads when no semaphore is available.
|
||||
* Initializing with semaphore initial count zero.
|
||||
* This call will not fail because the memory for the semaphore has already been allocated.
|
||||
*/
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
( void ) xSemaphoreCreateCountingStatic( SEM_VALUE_MAX, 0, &pxSem->xSemaphore );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_post( sem_t * sem )
|
||||
{
|
||||
sem_internal_t * pxSem = ( sem_internal_t * ) ( sem );
|
||||
|
||||
int iPreviouValue = Atomic_Increment_u32( ( uint32_t * ) &pxSem->value );
|
||||
|
||||
/* If previous semaphore value is equal or larger than zero, there is no
|
||||
* thread waiting for this semaphore. Otherwise (<0), call FreeRTOS interface
|
||||
* to wake up a thread. */
|
||||
if( iPreviouValue < 0 )
|
||||
{
|
||||
/* Give the semaphore using the FreeRTOS API. */
|
||||
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxSem->xSemaphore );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_timedwait( sem_t * sem,
|
||||
const struct timespec * abstime )
|
||||
{
|
||||
int iStatus = 0;
|
||||
sem_internal_t * pxSem = ( sem_internal_t * ) ( sem );
|
||||
TickType_t xDelay = portMAX_DELAY;
|
||||
int iPreviousValue = Atomic_Decrement_u32( ( uint32_t * ) &pxSem->value );
|
||||
|
||||
if( abstime != NULL )
|
||||
{
|
||||
/* If the provided timespec is invalid, still attempt to take the
|
||||
* semaphore without blocking, per POSIX spec. */
|
||||
if( UTILS_ValidateTimespec( abstime ) == false )
|
||||
{
|
||||
xDelay = 0;
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Get current time */
|
||||
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = UTILS_AbsoluteTimespecToDeltaTicks( abstime, &xCurrentTime, &xDelay );
|
||||
}
|
||||
|
||||
/* If abstime was in the past, still attempt to take the semaphore without
|
||||
* blocking, per POSIX spec. */
|
||||
if( iStatus == ETIMEDOUT )
|
||||
{
|
||||
xDelay = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If previous semaphore value is larger than zero, the thread entering this function call
|
||||
* can take the semaphore without yielding. Else (<=0), calling into FreeRTOS API to yield.
|
||||
*/
|
||||
if( iPreviousValue > 0 )
|
||||
{
|
||||
/* Under no circumstance shall the function fail with a timeout if the semaphore can be locked immediately. */
|
||||
iStatus = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Take the semaphore using the FreeRTOS API. */
|
||||
if( xSemaphoreTake( ( SemaphoreHandle_t ) &pxSem->xSemaphore,
|
||||
xDelay ) != pdTRUE )
|
||||
{
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
errno = ETIMEDOUT;
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = iStatus;
|
||||
}
|
||||
|
||||
iStatus = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_trywait( sem_t * sem )
|
||||
{
|
||||
int iStatus = 0;
|
||||
|
||||
/* Setting an absolute timeout of 0 (i.e. in the past) will cause sem_timedwait
|
||||
* to not block. */
|
||||
struct timespec xTimeout = { 0 };
|
||||
|
||||
iStatus = sem_timedwait( sem, &xTimeout );
|
||||
|
||||
/* POSIX specifies that this function should set errno to EAGAIN and not
|
||||
* ETIMEDOUT. */
|
||||
if( ( iStatus == -1 ) && ( errno == ETIMEDOUT ) )
|
||||
{
|
||||
errno = EAGAIN;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int sem_wait( sem_t * sem )
|
||||
{
|
||||
return sem_timedwait( sem, NULL );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,330 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_timer.c
|
||||
* @brief Implementation of timer functions in time.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/pthread.h"
|
||||
#include "FreeRTOS_POSIX/signal.h"
|
||||
#include "FreeRTOS_POSIX/time.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
/* FreeRTOS timer include. */
|
||||
#include "timers.h"
|
||||
|
||||
/* Timespec zero check macros. */
|
||||
#define TIMESPEC_IS_ZERO( xTimespec ) ( xTimespec.tv_sec == 0 && xTimespec.tv_nsec == 0 ) /**< Check for 0. */
|
||||
#define TIMESPEC_IS_NOT_ZERO( xTimespec ) ( !( TIMESPEC_IS_ZERO( xTimespec ) ) ) /**< Check for not 0. */
|
||||
|
||||
/**
|
||||
* @brief Internal timer structure.
|
||||
*/
|
||||
typedef struct timer_internal
|
||||
{
|
||||
StaticTimer_t xTimerBuffer; /**< Memory that holds the FreeRTOS timer. */
|
||||
struct sigevent xTimerEvent; /**< What to do when this timer expires. */
|
||||
TickType_t xTimerPeriod; /**< Period of this timer. */
|
||||
} timer_internal_t;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void prvTimerCallback( TimerHandle_t xTimerHandle )
|
||||
{
|
||||
timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle );
|
||||
pthread_t xTimerNotificationThread;
|
||||
|
||||
/* The value of the timer ID, set in timer_create, should not be NULL. */
|
||||
configASSERT( pxTimer != NULL );
|
||||
|
||||
/* A value of SIGEV_SIGNAL isn't supported and should not have been successfully
|
||||
* set. */
|
||||
configASSERT( pxTimer->xTimerEvent.sigev_notify != SIGEV_SIGNAL );
|
||||
|
||||
/* Update the timer period, which may need to be set to an it_interval
|
||||
* argument. This call should not block. */
|
||||
if( pxTimer->xTimerPeriod > 0 )
|
||||
{
|
||||
xTimerChangePeriod( xTimerHandle, pxTimer->xTimerPeriod, 0 );
|
||||
}
|
||||
|
||||
/* Create the timer notification thread if requested. */
|
||||
if( pxTimer->xTimerEvent.sigev_notify == SIGEV_THREAD )
|
||||
{
|
||||
/* if the user has provided thread attributes, create a thread
|
||||
* with the provided attributes. Otherwise dispatch callback directly */
|
||||
if( pxTimer->xTimerEvent.sigev_notify_attributes == NULL )
|
||||
{
|
||||
( *pxTimer->xTimerEvent.sigev_notify_function )( pxTimer->xTimerEvent.sigev_value );
|
||||
}
|
||||
else
|
||||
{
|
||||
( void ) pthread_create( &xTimerNotificationThread,
|
||||
pxTimer->xTimerEvent.sigev_notify_attributes,
|
||||
( void * ( * )( void * ) )pxTimer->xTimerEvent.sigev_notify_function,
|
||||
pxTimer->xTimerEvent.sigev_value.sival_ptr );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int timer_create( clockid_t clockid,
|
||||
struct sigevent * evp,
|
||||
timer_t * timerid )
|
||||
{
|
||||
int iStatus = 0;
|
||||
timer_internal_t * pxTimer = NULL;
|
||||
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) clockid;
|
||||
|
||||
/* POSIX specifies that when evp is NULL, the behavior shall be as is
|
||||
* sigev_notify is SIGEV_SIGNAL. SIGEV_SIGNAL is currently not supported. */
|
||||
if( ( evp == NULL ) || ( evp->sigev_notify == SIGEV_SIGNAL ) )
|
||||
{
|
||||
errno = ENOTSUP;
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
/* Allocate memory for a new timer object. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
pxTimer = pvPortMalloc( sizeof( timer_internal_t ) );
|
||||
|
||||
if( pxTimer == NULL )
|
||||
{
|
||||
errno = EAGAIN;
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Copy the event notification structure and set the current timer period. */
|
||||
pxTimer->xTimerEvent = *evp;
|
||||
pxTimer->xTimerPeriod = 0;
|
||||
|
||||
/* Create a new FreeRTOS timer. This call will not fail because the
|
||||
* memory for it has already been allocated, so the output parameter is
|
||||
* also set. */
|
||||
*timerid = ( timer_t ) xTimerCreateStatic( posixconfigTIMER_NAME, /* Timer name. */
|
||||
portMAX_DELAY, /* Initial timer period. Timers are created disarmed. */
|
||||
pdFALSE, /* Don't auto-reload timer. */
|
||||
( void * ) pxTimer, /* Timer id. */
|
||||
prvTimerCallback, /* Timer expiration callback. */
|
||||
&pxTimer->xTimerBuffer ); /* Pre-allocated memory for timer. */
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int timer_delete( timer_t timerid )
|
||||
{
|
||||
TimerHandle_t xTimerHandle = timerid;
|
||||
timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle );
|
||||
|
||||
/* The value of the timer ID, set in timer_create, should not be NULL. */
|
||||
configASSERT( pxTimer != NULL );
|
||||
|
||||
/* Stop the FreeRTOS timer. Because the timer is statically allocated, no call
|
||||
* to xTimerDelete is necessary. The timer is stopped so that it's not referenced
|
||||
* anywhere. xTimerStop will not fail when it has unlimited block time. */
|
||||
( void ) xTimerStop( xTimerHandle, portMAX_DELAY );
|
||||
|
||||
/* Wait until the timer stop command is processed. */
|
||||
while( xTimerIsTimerActive( xTimerHandle ) == pdTRUE )
|
||||
{
|
||||
vTaskDelay( 1 );
|
||||
}
|
||||
|
||||
/* Free the memory in use by the timer. */
|
||||
vPortFree( pxTimer );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int timer_getoverrun( timer_t timerid )
|
||||
{
|
||||
/* Silence warnings about unused parameters. */
|
||||
( void ) timerid;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int timer_settime( timer_t timerid,
|
||||
int flags,
|
||||
const struct itimerspec * value,
|
||||
struct itimerspec * ovalue )
|
||||
{
|
||||
int iStatus = 0;
|
||||
TimerHandle_t xTimerHandle = timerid;
|
||||
timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle );
|
||||
TickType_t xNextTimerExpiration = 0, xTimerExpirationPeriod = 0;
|
||||
|
||||
/* Validate the value argument, but only if the timer isn't being disarmed. */
|
||||
if( TIMESPEC_IS_NOT_ZERO( value->it_value ) )
|
||||
{
|
||||
if( ( UTILS_ValidateTimespec( &value->it_interval ) == false ) ||
|
||||
( UTILS_ValidateTimespec( &value->it_value ) == false ) )
|
||||
{
|
||||
errno = EINVAL;
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set ovalue, if given. */
|
||||
if( ovalue != NULL )
|
||||
{
|
||||
( void ) timer_gettime( timerid, ovalue );
|
||||
}
|
||||
|
||||
/* Stop the timer if it's currently active. */
|
||||
if( ( iStatus == 0 ) && xTimerIsTimerActive( xTimerHandle ) )
|
||||
{
|
||||
( void ) xTimerStop( xTimerHandle, portMAX_DELAY );
|
||||
}
|
||||
|
||||
/* Only restart the timer if it_value is not zero. */
|
||||
if( ( iStatus == 0 ) && TIMESPEC_IS_NOT_ZERO( value->it_value ) )
|
||||
{
|
||||
/* Convert it_interval to ticks, but only if it_interval is not 0. If
|
||||
* it_interval is 0, then the timer is not periodic. */
|
||||
if( TIMESPEC_IS_NOT_ZERO( value->it_interval ) )
|
||||
{
|
||||
( void ) UTILS_TimespecToTicks( &value->it_interval, &xTimerExpirationPeriod );
|
||||
}
|
||||
|
||||
/* Set the new timer period. A non-periodic timer will have its period set
|
||||
* to portMAX_DELAY. */
|
||||
pxTimer->xTimerPeriod = xTimerExpirationPeriod;
|
||||
|
||||
/* Convert it_value to ticks, but only if it_value is not 0. If it_value
|
||||
* is 0, then the timer will remain disarmed. */
|
||||
if( TIMESPEC_IS_NOT_ZERO( value->it_value ) )
|
||||
{
|
||||
/* Absolute timeout. */
|
||||
if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME )
|
||||
{
|
||||
struct timespec xCurrentTime = { 0 };
|
||||
|
||||
/* Get current time */
|
||||
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = UTILS_AbsoluteTimespecToDeltaTicks( &value->it_value, &xCurrentTime, &xNextTimerExpiration );
|
||||
}
|
||||
|
||||
/* Make sure xNextTimerExpiration is zero in case we got negative time difference */
|
||||
if( iStatus != 0 )
|
||||
{
|
||||
xNextTimerExpiration = 0;
|
||||
|
||||
if( iStatus == ETIMEDOUT )
|
||||
{
|
||||
/* Set Status to 0 as absolute time is past is treated as expiry but not an error */
|
||||
iStatus = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Relative timeout. */
|
||||
else
|
||||
{
|
||||
( void ) UTILS_TimespecToTicks( &value->it_value, &xNextTimerExpiration );
|
||||
}
|
||||
}
|
||||
|
||||
/* If xNextTimerExpiration is still 0, that means that it_value specified
|
||||
* an absolute timeout in the past. Per POSIX spec, a notification should be
|
||||
* triggered immediately. */
|
||||
if( xNextTimerExpiration == 0 )
|
||||
{
|
||||
prvTimerCallback( xTimerHandle );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Set the timer to expire at the it_value, then start it. */
|
||||
( void ) xTimerChangePeriod( xTimerHandle, xNextTimerExpiration, portMAX_DELAY );
|
||||
( void ) xTimerStart( xTimerHandle, xNextTimerExpiration );
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int timer_gettime( timer_t timerid,
|
||||
struct itimerspec * value )
|
||||
{
|
||||
TimerHandle_t xTimerHandle = timerid;
|
||||
timer_internal_t * pxTimer = ( timer_internal_t * ) pvTimerGetTimerID( xTimerHandle );
|
||||
TickType_t xNextExpirationTime = xTimerGetExpiryTime( xTimerHandle ) - xTaskGetTickCount(),
|
||||
xTimerExpirationPeriod = pxTimer->xTimerPeriod;
|
||||
|
||||
/* Set it_value only if the timer is armed. Otherwise, set it to 0. */
|
||||
if( xTimerIsTimerActive( xTimerHandle ) != pdFALSE )
|
||||
{
|
||||
value->it_value.tv_sec = ( time_t ) ( xNextExpirationTime / configTICK_RATE_HZ );
|
||||
value->it_value.tv_nsec = ( long ) ( ( xNextExpirationTime % configTICK_RATE_HZ ) * NANOSECONDS_PER_TICK );
|
||||
}
|
||||
else
|
||||
{
|
||||
value->it_value.tv_sec = 0;
|
||||
value->it_value.tv_nsec = 0;
|
||||
}
|
||||
|
||||
/* Set it_interval only if the timer is periodic. Otherwise, set it to 0. */
|
||||
if( xTimerExpirationPeriod != portMAX_DELAY )
|
||||
{
|
||||
value->it_interval.tv_sec = ( time_t ) ( xTimerExpirationPeriod / configTICK_RATE_HZ );
|
||||
value->it_interval.tv_nsec = ( long ) ( ( xTimerExpirationPeriod % configTICK_RATE_HZ ) * NANOSECONDS_PER_TICK );
|
||||
}
|
||||
else
|
||||
{
|
||||
value->it_interval.tv_sec = 0;
|
||||
value->it_interval.tv_nsec = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_unistd.c
|
||||
* @brief Implementation of functions in unistd.h
|
||||
*/
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/unistd.h"
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
unsigned sleep( unsigned seconds )
|
||||
{
|
||||
vTaskDelay( pdMS_TO_TICKS( seconds * 1000 ) );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int usleep( useconds_t usec )
|
||||
{
|
||||
/* To avoid delaying for less than usec, always round up. */
|
||||
vTaskDelay( pdMS_TO_TICKS( usec / 1000 + ( usec % 1000 != 0 ) ) );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
@ -0,0 +1,388 @@
|
||||
/*
|
||||
* Amazon FreeRTOS POSIX V1.1.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* http://aws.amazon.com/freertos
|
||||
* http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file FreeRTOS_POSIX_utils.c
|
||||
* @brief Implementation of utility functions in utils.h
|
||||
*/
|
||||
|
||||
/* C standard library includes. */
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
|
||||
/* FreeRTOS+POSIX includes. */
|
||||
#include "FreeRTOS_POSIX.h"
|
||||
#include "FreeRTOS_POSIX/errno.h"
|
||||
#include "FreeRTOS_POSIX/utils.h"
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
size_t UTILS_strnlen( const char * const pcString,
|
||||
size_t xMaxLength )
|
||||
{
|
||||
const char * pcCharPointer = pcString;
|
||||
size_t xLength = 0;
|
||||
|
||||
if( pcString != NULL )
|
||||
{
|
||||
while( ( *pcCharPointer != '\0' ) && ( xLength < xMaxLength ) )
|
||||
{
|
||||
xLength++;
|
||||
pcCharPointer++;
|
||||
}
|
||||
}
|
||||
|
||||
return xLength;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime,
|
||||
const struct timespec * const pxCurrentTime,
|
||||
TickType_t * const pxResult )
|
||||
{
|
||||
int iStatus = 0;
|
||||
struct timespec xDifference = { 0 };
|
||||
|
||||
/* Check parameters. */
|
||||
if( ( pxAbsoluteTime == NULL ) || ( pxCurrentTime == NULL ) || ( pxResult == NULL ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
/* Calculate the difference between the current time and absolute time. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
iStatus = UTILS_TimespecSubtract( pxAbsoluteTime, pxCurrentTime, &xDifference );
|
||||
|
||||
if( iStatus == 1 )
|
||||
{
|
||||
/* pxAbsoluteTime was in the past. */
|
||||
iStatus = ETIMEDOUT;
|
||||
}
|
||||
else if( iStatus == -1 )
|
||||
{
|
||||
/* error */
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Convert the time difference to ticks. */
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
iStatus = UTILS_TimespecToTicks( &xDifference, pxResult );
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_TimespecToTicks( const struct timespec * const pxTimespec,
|
||||
TickType_t * const pxResult )
|
||||
{
|
||||
int iStatus = 0;
|
||||
int64_t llTotalTicks = 0;
|
||||
long lNanoseconds = 0;
|
||||
|
||||
/* Check parameters. */
|
||||
if( ( pxTimespec == NULL ) || ( pxResult == NULL ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else if( ( iStatus == 0 ) && ( UTILS_ValidateTimespec( pxTimespec ) == false ) )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Convert timespec.tv_sec to ticks. */
|
||||
llTotalTicks = ( int64_t ) configTICK_RATE_HZ * ( pxTimespec->tv_sec );
|
||||
|
||||
/* Convert timespec.tv_nsec to ticks. This value does not have to be checked
|
||||
* for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and
|
||||
* NANOSECONDS_PER_TICK > 1. */
|
||||
lNanoseconds = pxTimespec->tv_nsec / ( long ) NANOSECONDS_PER_TICK + /* Whole nanoseconds. */
|
||||
( long ) ( pxTimespec->tv_nsec % ( long ) NANOSECONDS_PER_TICK != 0 ); /* Add 1 to round up if needed. */
|
||||
|
||||
/* Add the nanoseconds to the total ticks. */
|
||||
llTotalTicks += ( int64_t ) lNanoseconds;
|
||||
|
||||
/* Check for overflow */
|
||||
if( llTotalTicks < 0 )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* check if TickType_t is 32 bit or 64 bit */
|
||||
uint32_t ulTickTypeSize = sizeof( TickType_t );
|
||||
|
||||
/* check for downcast overflow */
|
||||
if( ulTickTypeSize == sizeof( uint32_t ) )
|
||||
{
|
||||
if( llTotalTicks > UINT_MAX )
|
||||
{
|
||||
iStatus = EINVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Write result. */
|
||||
*pxResult = ( TickType_t ) llTotalTicks;
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void UTILS_NanosecondsToTimespec( int64_t llSource,
|
||||
struct timespec * const pxDestination )
|
||||
{
|
||||
long lCarrySec = 0;
|
||||
|
||||
/* Convert to timespec. */
|
||||
pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND );
|
||||
pxDestination->tv_nsec = ( long ) ( llSource % NANOSECONDS_PER_SECOND );
|
||||
|
||||
/* Subtract from tv_sec if tv_nsec < 0. */
|
||||
if( pxDestination->tv_nsec < 0L )
|
||||
{
|
||||
/* Compute the number of seconds to carry. */
|
||||
lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L;
|
||||
|
||||
pxDestination->tv_sec -= ( time_t ) ( lCarrySec );
|
||||
pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_TimespecAdd( const struct timespec * const x,
|
||||
const struct timespec * const y,
|
||||
struct timespec * const pxResult )
|
||||
{
|
||||
int64_t llPartialSec = 0;
|
||||
int iStatus = 0;
|
||||
|
||||
/* Check parameters. */
|
||||
if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* Perform addition. */
|
||||
pxResult->tv_nsec = x->tv_nsec + y->tv_nsec;
|
||||
|
||||
/* check for overflow in case nsec value was invalid */
|
||||
if( pxResult->tv_nsec < 0 )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
llPartialSec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND;
|
||||
pxResult->tv_nsec = ( pxResult->tv_nsec ) % NANOSECONDS_PER_SECOND;
|
||||
pxResult->tv_sec = x->tv_sec + y->tv_sec + llPartialSec;
|
||||
|
||||
/* check for overflow */
|
||||
if( pxResult->tv_sec < 0 )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_TimespecAddNanoseconds( const struct timespec * const x,
|
||||
int64_t llNanoseconds,
|
||||
struct timespec * const pxResult )
|
||||
{
|
||||
int64_t llTotalNSec = 0;
|
||||
int iStatus = 0;
|
||||
|
||||
/* Check parameters. */
|
||||
if( ( pxResult == NULL ) || ( x == NULL ) )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
/* add nano seconds */
|
||||
llTotalNSec = x->tv_nsec + llNanoseconds;
|
||||
|
||||
/* check for nano seconds overflow */
|
||||
if( llTotalNSec < 0 )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxResult->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND;
|
||||
pxResult->tv_sec = x->tv_sec + ( llTotalNSec / NANOSECONDS_PER_SECOND );
|
||||
|
||||
/* check for seconds overflow */
|
||||
if( pxResult->tv_sec < 0 )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_TimespecSubtract( const struct timespec * const x,
|
||||
const struct timespec * const y,
|
||||
struct timespec * const pxResult )
|
||||
{
|
||||
int iCompareResult = 0;
|
||||
int iStatus = 0;
|
||||
|
||||
/* Check parameters. */
|
||||
if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
|
||||
if( iStatus == 0 )
|
||||
{
|
||||
iCompareResult = UTILS_TimespecCompare( x, y );
|
||||
|
||||
/* if x < y then result would be negative, return 1 */
|
||||
if( iCompareResult == -1 )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else if( iCompareResult == 0 )
|
||||
{
|
||||
/* if times are the same return zero */
|
||||
pxResult->tv_sec = 0;
|
||||
pxResult->tv_nsec = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If x > y Perform subtraction. */
|
||||
pxResult->tv_sec = x->tv_sec - y->tv_sec;
|
||||
pxResult->tv_nsec = x->tv_nsec - y->tv_nsec;
|
||||
|
||||
/* check if nano seconds value needs to borrow */
|
||||
if( pxResult->tv_nsec < 0 )
|
||||
{
|
||||
/* Based on comparison, tv_sec > 0 */
|
||||
pxResult->tv_sec--;
|
||||
pxResult->tv_nsec += ( long ) NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
|
||||
/* if nano second is negative after borrow, it is an overflow error */
|
||||
if( pxResult->tv_nsec < 0 )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int UTILS_TimespecCompare( const struct timespec * const x,
|
||||
const struct timespec * const y )
|
||||
{
|
||||
int iStatus = 0;
|
||||
|
||||
/* Check parameters */
|
||||
if( ( x == NULL ) && ( y == NULL ) )
|
||||
{
|
||||
iStatus = 0;
|
||||
}
|
||||
else if( y == NULL )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else if( x == NULL )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
else if( x->tv_sec > y->tv_sec )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else if( x->tv_sec < y->tv_sec )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* seconds are equal compare nano seconds */
|
||||
if( x->tv_nsec > y->tv_nsec )
|
||||
{
|
||||
iStatus = 1;
|
||||
}
|
||||
else if( x->tv_nsec < y->tv_nsec )
|
||||
{
|
||||
iStatus = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
iStatus = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return iStatus;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec )
|
||||
{
|
||||
bool xReturn = false;
|
||||
|
||||
if( pxTimespec != NULL )
|
||||
{
|
||||
/* Verify 0 <= tv_nsec < 1000000000. */
|
||||
if( ( pxTimespec->tv_nsec >= 0 ) &&
|
||||
( pxTimespec->tv_nsec < NANOSECONDS_PER_SECOND ) )
|
||||
{
|
||||
xReturn = true;
|
||||
}
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
Reference in New Issue
Block a user