1
0
Fork 0
forked from nuttx/nuttx-update

sched/sched/sched_getcpu.c: Implement non-standard interface.

If SMP is enabled this function will return the number of the CPU that the thread is running on.  This is non-standard but follows GLIBC if __GNU_SOURCE is enabled.  The returned CPU number is, however, worthless since it returns the CPU number of the CPU that was executing the task when the function was called.  The application can never know the true CPU number of the CPU tht it is running on since that value is volatile and change change at any time.
This commit is contained in:
Gregory Nutt 2020-01-31 20:25:15 +00:00 committed by Ouss4
parent 80277d1630
commit 2def8035db
7 changed files with 108 additions and 13 deletions

View file

@ -1,8 +1,8 @@
/********************************************************************************
* include/sched.h
*
* Copyright (C) 2007-2009, 2011, 2013, 2015-2016 Gregory Nutt. All rights
* reserved.
* Copyright (C) 2007-2009, 2011, 2013, 2015-2016, 2020 Gregory Nutt. All
* rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -46,6 +46,7 @@
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <nuttx/sched.h>
/********************************************************************************
@ -265,6 +266,7 @@ int sched_setaffinity(pid_t pid, size_t cpusetsize,
FAR const cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize, FAR cpu_set_t *mask);
int sched_cpu_count(FAR const cpu_set_t *set);
int sched_getcpu(void);
#endif /* CONFIG_SMP */
/* Task Switching Interfaces (non-standard) */

View file

@ -71,6 +71,7 @@
#define SYS_exit (CONFIG_SYS_RESERVED + 1)
#define SYS_get_errno (CONFIG_SYS_RESERVED + 2)
#define SYS_getpid (CONFIG_SYS_RESERVED + 3)
#define SYS_sched_getparam (CONFIG_SYS_RESERVED + 4)
#define SYS_sched_getscheduler (CONFIG_SYS_RESERVED + 5)
#define SYS_sched_lock (CONFIG_SYS_RESERVED + 6)
@ -80,9 +81,16 @@
#define SYS_sched_setscheduler (CONFIG_SYS_RESERVED + 10)
#define SYS_sched_unlock (CONFIG_SYS_RESERVED + 11)
#define SYS_sched_yield (CONFIG_SYS_RESERVED + 12)
#define SYS_set_errno (CONFIG_SYS_RESERVED + 13)
#define SYS_uname (CONFIG_SYS_RESERVED + 14)
#define __SYS_uid (CONFIG_SYS_RESERVED + 15)
#ifdef CONFIG_SMP
# define SYS_sched_getcpu (CONFIG_SYS_RESERVED + 13)
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 14)
#else
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 13)
#endif
#define SYS_set_errno (__SYS_set_errno + 0)
#define SYS_uname (__SYS_set_errno + 1)
#define __SYS_uid (__SYS_set_errno + 2)
/* User identity */

View file

@ -1,7 +1,7 @@
############################################################################
# sched/sched/Make.defs
#
# Copyright (C) 2014, 2018 Gregory Nutt. All rights reserved.
# Copyright (C) 2014, 2018, 2020 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
@ -50,7 +50,7 @@ CSRCS += sched_reprioritize.c
endif
ifeq ($(CONFIG_SMP),y)
CSRCS += sched_cpuselect.c sched_cpupause.c
CSRCS += sched_cpuselect.c sched_cpupause.c sched_getcpu.c
CSRCS += sched_getaffinity.c sched_setaffinity.c
endif

View file

@ -0,0 +1,80 @@
/****************************************************************************
* sched/sched/sched_getcpu.c
*
* Copyright (C) 2016, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sched.h>
#include <nuttx/arch.h>
#include "sched/sched.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sched_getcpu
*
* Description:
* sched_getcpu() returns the number of the CPU on which the calling
* thread is currently executing.
*
* The return CPU number is guaranteed to be valid only at the time of
* the call. Unless the CPU affinity has been fixed using
* sched_setaffinity(), the OS might change the CPU at any time. The
* caller must allow for the possibility that the information returned is
* no longer current by the time the call returns.
*
* Non-Standard. Functionally equivalent to the GLIBC __GNU_SOURCE
* interface of the same name.
*
* Input Parameters:
* None
*
* Returned Value:
* A non-negative CPU number is returned on success. -1 (ERROR) is
* returned on failure with the errno value set to indicate the cause of
* the failure.
*
****************************************************************************/
int sched_getcpu(void)
{
return up_cpu_index(); /* Does not fail */
}

View file

@ -119,6 +119,7 @@
"rewinddir","dirent.h","","void","FAR DIR*"
"rmdir","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char*"
"rmmod","nuttx/module.h","defined(CONFIG_MODULE)","int","FAR void *"
"sched_getcpu","sched.h","defined(CONFIG_SMP)","int"
"sched_getparam","sched.h","","int","pid_t","struct sched_param*"
"sched_getscheduler","sched.h","","int","pid_t"
"sched_getstreams","nuttx/sched.h","CONFIG_NFILE_STREAMS > 0","FAR struct streamlist*"

Can't render this file because it has a wrong number of fields in line 2.

View file

@ -1,7 +1,7 @@
/****************************************************************************
* syscall/syscall_lookup.h
*
* Copyright (C) 2011, 2013-2019 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2013-2019, 2020 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -47,6 +47,7 @@ SYSCALL_LOOKUP1(_exit, 1, STUB__exit)
SYSCALL_LOOKUP(exit, 1, STUB_exit)
SYSCALL_LOOKUP(get_errno, 0, STUB_get_errno)
SYSCALL_LOOKUP(getpid, 0, STUB_getpid)
SYSCALL_LOOKUP(sched_getparam, 2, STUB_sched_getparam)
SYSCALL_LOOKUP(sched_getscheduler, 1, STUB_sched_getscheduler)
SYSCALL_LOOKUP(sched_lock, 0, STUB_sched_lock)
@ -56,6 +57,10 @@ SYSCALL_LOOKUP(sched_setparam, 2, STUB_sched_setparam)
SYSCALL_LOOKUP(sched_setscheduler, 3, STUB_sched_setscheduler)
SYSCALL_LOOKUP(sched_unlock, 0, STUB_sched_unlock)
SYSCALL_LOOKUP(sched_yield, 0, STUB_sched_yield)
#ifdef CONFIG_SMP
SYSCALL_LOOKUP(sched_getcpu, 0, STUB_sched_getcpu)
#endif
SYSCALL_LOOKUP(set_errno, 1, STUB_set_errno)
SYSCALL_LOOKUP(uname, 1, STUB_uname)

View file

@ -1,7 +1,7 @@
/****************************************************************************
* syscall/syscall_stublookup.c
*
* Copyright (C) 2011-2013, 2015-2019 Gregory Nutt. All rights reserved.
* Copyright (C) 2011-2013, 2015-2020 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -48,10 +48,6 @@
#if defined(CONFIG_LIB_SYSCALL)
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
/****************************************************************************
* Stub Function Prototypes
****************************************************************************/
@ -64,6 +60,7 @@ uintptr_t STUB__exit(int nbr, uintptr_t parm1);
uintptr_t STUB_exit(int nbr, uintptr_t parm1);
uintptr_t STUB_get_errno(int nbr);
uintptr_t STUB_getpid(int nbr);
uintptr_t STUB_sched_getparam(int nbr, uintptr_t parm1, uintptr_t parm2);
uintptr_t STUB_sched_getscheduler(int nbr, uintptr_t parm1);
uintptr_t STUB_sched_lock(int nbr);
@ -75,6 +72,8 @@ uintptr_t STUB_sched_setscheduler(int nbr, uintptr_t parm1, uintptr_t parm2,
uintptr_t parm3);
uintptr_t STUB_sched_unlock(int nbr);
uintptr_t STUB_sched_yield(int nbr);
uintptr_t STUB_sched_getcpu(int nbr);
uintptr_t STUB_set_errno(int nbr, uintptr_t parm1);
uintptr_t STUB_uname(int nbr, uintptr_t parm1);