From 2def8035dbf2e8d4fda7e2c68dd1ba182d51b6d5 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 31 Jan 2020 20:25:15 +0000 Subject: [PATCH] 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. --- include/sched.h | 6 ++- include/sys/syscall.h | 14 +++++-- sched/sched/Make.defs | 4 +- sched/sched/sched_getcpu.c | 80 ++++++++++++++++++++++++++++++++++++ syscall/syscall.csv | 1 + syscall/syscall_lookup.h | 7 +++- syscall/syscall_stublookup.c | 9 ++-- 7 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 sched/sched/sched_getcpu.c diff --git a/include/sched.h b/include/sched.h index 8309c941d24..06dc6db5f8e 100644 --- a/include/sched.h +++ b/include/sched.h @@ -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 * * Redistribution and use in source and binary forms, with or without @@ -46,6 +46,7 @@ #include #include #include + #include /******************************************************************************** @@ -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) */ diff --git a/include/sys/syscall.h b/include/sys/syscall.h index 8518c55d494..ec5cbf54e4b 100644 --- a/include/sys/syscall.h +++ b/include/sys/syscall.h @@ -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 */ diff --git a/sched/sched/Make.defs b/sched/sched/Make.defs index 91e239dad09..11f4c94e532 100644 --- a/sched/sched/Make.defs +++ b/sched/sched/Make.defs @@ -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 # # 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 diff --git a/sched/sched/sched_getcpu.c b/sched/sched/sched_getcpu.c new file mode 100644 index 00000000000..9e5ada23e1f --- /dev/null +++ b/sched/sched/sched_getcpu.c @@ -0,0 +1,80 @@ +/**************************************************************************** + * sched/sched/sched_getcpu.c + * + * Copyright (C) 2016, 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include + +#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 */ +} diff --git a/syscall/syscall.csv b/syscall/syscall.csv index 80bde70e119..2f2c9e0f549 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -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*" diff --git a/syscall/syscall_lookup.h b/syscall/syscall_lookup.h index df2f6dfd46a..00f7fb0cf9c 100644 --- a/syscall/syscall_lookup.h +++ b/syscall/syscall_lookup.h @@ -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 * * 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) diff --git a/syscall/syscall_stublookup.c b/syscall/syscall_stublookup.c index f9b2fb1afca..11276e75796 100644 --- a/syscall/syscall_stublookup.c +++ b/syscall/syscall_stublookup.c @@ -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 * * 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);