mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 19:36:35 +08:00
Correct errno handling
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@136 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/************************************************************
|
/****************************************************************************
|
||||||
* pthread_getschedparam.c
|
* pthread_getschedparam.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
@@ -31,11 +31,11 @@
|
|||||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Included Files
|
* Included Files
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
@@ -44,47 +44,64 @@
|
|||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include "pthread_internal.h"
|
#include "pthread_internal.h"
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Definitions
|
* Definitions
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Type Declarations
|
* Private Type Declarations
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Global Variables
|
* Global Variables
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Variables
|
* Private Variables
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Function: pthread_getschedparam
|
* Function: pthread_getschedparam
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Obtain the thread scheduling parameters.
|
* The pthread_getschedparam() functions will get the scheduling policy and
|
||||||
|
* parameters of threads. For SCHED_FIFO and SCHED_RR, the only required
|
||||||
|
* member of the sched_param structure is the priority sched_priority.
|
||||||
*
|
*
|
||||||
|
* The pthread_getschedparam() function will retrieve the scheduling policy
|
||||||
|
* and scheduling parameters for the thread whose thread ID is given by
|
||||||
|
* 'thread' and will store those values in 'policy' and 'param',
|
||||||
|
* respectively. The priority value returned from pthread_getschedparam()
|
||||||
|
* will be the value specified by the most recent pthread_setschedparam(),
|
||||||
|
* pthread_setschedprio(), or pthread_create() call affecting the target
|
||||||
|
* thread. It will not reflect any temporary adjustments to its priority (such
|
||||||
|
* as might result of any priority inheritance, for example).
|
||||||
|
*
|
||||||
|
* The policy parameter may have the value SCHED_FIFO, or SCHED_RR
|
||||||
|
* (SCHED_OTHER and SCHED_SPORADIC, in particular, are not supported).
|
||||||
|
* The SCHED_FIFO and SCHED_RR policies will have a single scheduling
|
||||||
|
* parameter, sched_priority.
|
||||||
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* thread
|
* thread - The ID of thread whose scheduling parameters will be queried.
|
||||||
* policy
|
* policy - The location to store the thread's scheduling policy.
|
||||||
* param
|
* param - The location to store the thread's priority.
|
||||||
*
|
*
|
||||||
* Return Value:
|
* Return Value:
|
||||||
* 0 if successful. Otherwise, an error code.
|
* 0 if successful. Otherwise, the error code ESRCH if the value specified
|
||||||
|
* by thread does not refer to an existing thread.
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
*
|
*
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int pthread_getschedparam(pthread_t thread, int *policy,
|
int pthread_getschedparam(pthread_t thread, int *policy,
|
||||||
struct sched_param *param)
|
struct sched_param *param)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/************************************************************
|
/****************************************************************************
|
||||||
* pthread_setschedparam.c
|
* pthread_setschedparam.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
@@ -31,11 +31,11 @@
|
|||||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
* POSSIBILITY OF SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Included Files
|
* Included Files
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
@@ -44,54 +44,96 @@
|
|||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include "pthread_internal.h"
|
#include "pthread_internal.h"
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Definitions
|
* Definitions
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Type Declarations
|
* Private Type Declarations
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Global Variables
|
* Global Variables
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Variables
|
* Private Variables
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/************************************************************
|
/****************************************************************************
|
||||||
* Function: pthread_setschedparam
|
* Function: pthread_setschedparam
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Set thread scheduling parameters.
|
* The pthread_setschedparam() functions will set the scheduling policy and
|
||||||
|
* parameters of threads. For SCHED_FIFO and SCHED_RR, the only required
|
||||||
|
* member of the sched_param structure is the priority sched_priority.
|
||||||
|
*
|
||||||
|
* The pthread_setschedparam() function will set the scheduling policy and
|
||||||
|
* associated scheduling parameters for the thread whose thread ID is
|
||||||
|
* given by 'thread' to the policy and associated parameters provided in
|
||||||
|
* 'policy' and 'param', respectively.
|
||||||
|
*
|
||||||
|
* The policy parameter may have the value SCHED_FIFO, or SCHED_RR
|
||||||
|
* (SCHED_OTHER and SCHED_SPORADIC, in particular, are not supported).
|
||||||
|
* The SCHED_FIFO and SCHED_RR policies will have a single scheduling
|
||||||
|
* parameter, sched_priority.
|
||||||
|
*
|
||||||
|
* If the pthread_setschedparam() function fails, the scheduling parameters
|
||||||
|
* will not be changed for the target thread.
|
||||||
*
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* thread
|
* thread - The ID of thread whose scheduling parameters will be modified.
|
||||||
* policy
|
* policy - The new scheduling policy of the thread. Either SCHED_FIFO or
|
||||||
* param
|
* SCHED_RR. SCHED_OTHER and SCHED_SPORADIC are not supported.
|
||||||
|
* param - Provides the new priority of the thread.
|
||||||
*
|
*
|
||||||
* Return Value:
|
* Return Value:
|
||||||
* 0 if successful. Otherwise, an error code.
|
* 0 if successful. Otherwise, an error code identifying the cause of the
|
||||||
|
* failure:
|
||||||
|
*
|
||||||
|
* EINVAL The value specified by 'policy' or one of the scheduling parameters
|
||||||
|
* associated with the scheduling policy 'policy' is invalid.
|
||||||
|
* ENOTSUP An attempt was made to set the policy or scheduling parameters
|
||||||
|
* to an unsupported value (SCHED_OTHER and SCHED_SPORADIC in
|
||||||
|
* particular are not supported)
|
||||||
|
* EPERM The caller does not have the appropriate permission to set either
|
||||||
|
* the scheduling parameters or the scheduling policy of the
|
||||||
|
* specified thread. Or, the implementation does not allow the
|
||||||
|
* application to modify one of the parameters to the value
|
||||||
|
* specified.
|
||||||
|
* ESRCH The value specified by thread does not refer to a existing thread.
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
*
|
*
|
||||||
************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int pthread_setschedparam(pthread_t thread, int policy,
|
int pthread_setschedparam(pthread_t thread, int policy,
|
||||||
const struct sched_param *param)
|
const struct sched_param *param)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
dbg("thread ID=%d policy=%d param=0x%p\n", thread, policy, param);
|
dbg("thread ID=%d policy=%d param=0x%p\n", thread, policy, param);
|
||||||
|
|
||||||
|
/* Set the errno to some non-zero value (failsafe) */
|
||||||
|
|
||||||
|
*get_errno_ptr() = EINVAL;
|
||||||
|
|
||||||
/* Let sched_setscheduler do all of the work */
|
/* Let sched_setscheduler do all of the work */
|
||||||
|
|
||||||
return sched_setscheduler((pid_t)thread, policy, param);
|
ret = sched_setscheduler((pid_t)thread, policy, param);
|
||||||
}
|
if (ret != OK)
|
||||||
|
{
|
||||||
|
/* If sched_setscheduler() fails, return the errno */
|
||||||
|
|
||||||
|
ret = *get_errno_ptr();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|||||||
+10
-9
@@ -40,6 +40,7 @@
|
|||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <nuttx/arch.h>
|
#include <nuttx/arch.h>
|
||||||
#include <nuttx/os_external.h>
|
#include <nuttx/os_external.h>
|
||||||
#include "os_internal.h"
|
#include "os_internal.h"
|
||||||
@@ -90,14 +91,13 @@
|
|||||||
* SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
|
* SCHED_PRIORITY_MIN through SCHED_PRIORITY_MAX.
|
||||||
*
|
*
|
||||||
* Return Value:
|
* Return Value:
|
||||||
* OK if successful, otherwise ERROR. This function can
|
* On success, sched_setparam() returns 0 (OK). On error, -1
|
||||||
* fail for the following reasons:
|
* (ERROR) is returned, and errno is set appropriately.
|
||||||
*
|
*
|
||||||
* (1) parm is NULL or parm->sched_priority is out of
|
* EINVAL The parameter 'param' is invalid or does not make
|
||||||
* range.
|
* sense for the current scheduling policy.
|
||||||
* (2) pid does not correspond to any task.
|
* EPERM The calling task does not have appropriate privileges.
|
||||||
*
|
* ESRCH The task whose ID is pid could not be found.
|
||||||
* (errno is not set).
|
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
*
|
*
|
||||||
@@ -110,7 +110,6 @@ int sched_setparam(pid_t pid, const struct sched_param *param)
|
|||||||
tstate_t task_state;
|
tstate_t task_state;
|
||||||
irqstate_t saved_state;
|
irqstate_t saved_state;
|
||||||
int sched_priority = param->sched_priority;
|
int sched_priority = param->sched_priority;
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
/* Verify that the requested priority is in the valid range */
|
/* Verify that the requested priority is in the valid range */
|
||||||
|
|
||||||
@@ -118,6 +117,7 @@ int sched_setparam(pid_t pid, const struct sched_param *param)
|
|||||||
param->sched_priority < SCHED_PRIORITY_MIN ||
|
param->sched_priority < SCHED_PRIORITY_MIN ||
|
||||||
param->sched_priority > SCHED_PRIORITY_MAX)
|
param->sched_priority > SCHED_PRIORITY_MAX)
|
||||||
{
|
{
|
||||||
|
*get_errno_ptr() = EINVAL;
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +144,7 @@ int sched_setparam(pid_t pid, const struct sched_param *param)
|
|||||||
{
|
{
|
||||||
/* No task with this pid was found */
|
/* No task with this pid was found */
|
||||||
|
|
||||||
|
*get_errno_ptr() = ESRCH;
|
||||||
sched_unlock();
|
sched_unlock();
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
@@ -266,5 +267,5 @@ int sched_setparam(pid_t pid, const struct sched_param *param)
|
|||||||
|
|
||||||
irqrestore(saved_state);
|
irqrestore(saved_state);
|
||||||
sched_unlock();
|
sched_unlock();
|
||||||
return ret;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user