1
0
Fork 0
forked from nuttx/nuttx-update

sched/sched: Add sched_get_stackinfo()

The new OS interface, sched_get_stackinfo() combines two pthread-specific interfaces into a single generic interface.  The existing pthread_get_stackaddr_np() and pthread_get_stacksize_np() are moved from sched/pthread to libs/libc/pthread.

There are two motivations for this change:  First, it reduces the number of system calls.  Secondly, it adds a common hook that is going to used for a future implementation of TLS.
This commit is contained in:
Gregory Nutt 2020-05-03 14:16:36 -06:00 committed by Abdelatif Guettouche
parent f03ed73f91
commit 00933cfece
11 changed files with 186 additions and 123 deletions

View file

@ -382,6 +382,21 @@ struct dspace_s
};
#endif
/* struct stackinfo_s ***********************************************************/
/* Used to report stack information */
struct stackinfo_s
{
size_t adj_stack_size; /* Stack size after adjustment */
/* for hardware, processor, etc. */
/* (for debug purposes only) */
FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
/* Needed to deallocate stack */
FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
/* The initial stack pointer value */
};
/* struct task_group_s **********************************************************/
/* All threads created by pthread_create belong in the same task group (along
@ -650,7 +665,7 @@ struct tcb_s
/* for hardware, processor, etc. */
/* (for debug purposes only) */
FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
/* Need to deallocate stack */
/* Needed to deallocate stack */
FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
/* The initial stack pointer value */
@ -1223,6 +1238,24 @@ int nxsched_setaffinity(pid_t pid, size_t cpusetsize,
FAR const cpu_set_t *mask);
#endif
/********************************************************************************
* Name: sched_get_stackinfo
*
* Description:
* Report information about a thread's stack allocation.
*
* Input Parameters:
* pid_t - Identifies the thread to query. Zero is interpreted as the
* the calling thread
* stackinfo - User-provided location to return the stack information.
*
* Returned Value:
* Zero (OK) if successful. Otherwise, a negated errno value is returned.
*
********************************************************************************/
int sched_get_stackinfo(pid_t pid, FAR struct stackinfo_s *stackinfo);
#undef EXTERN
#if defined(__cplusplus)
}

View file

@ -66,14 +66,15 @@
#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_sched_get_stackinfo (CONFIG_SYS_RESERVED + 13)
#ifdef CONFIG_SMP
# define SYS_sched_getaffinity (CONFIG_SYS_RESERVED + 13)
# define SYS_sched_getcpu (CONFIG_SYS_RESERVED + 14)
# define SYS_sched_setaffinity (CONFIG_SYS_RESERVED + 15)
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 16)
# define SYS_sched_getaffinity (CONFIG_SYS_RESERVED + 14)
# define SYS_sched_getcpu (CONFIG_SYS_RESERVED + 15)
# define SYS_sched_setaffinity (CONFIG_SYS_RESERVED + 16)
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 17)
#else
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 13)
# define __SYS_set_errno (CONFIG_SYS_RESERVED + 14)
#endif
#define SYS_set_errno (__SYS_set_errno + 0)
@ -434,24 +435,22 @@
# define SYS_pthread_create (__SYS_pthread + 4)
# define SYS_pthread_detach (__SYS_pthread + 5)
# define SYS_pthread_exit (__SYS_pthread + 6)
# define SYS_pthread_get_stackaddr_np (__SYS_pthread + 7)
# define SYS_pthread_get_stacksize_np (__SYS_pthread + 8)
# define SYS_pthread_getschedparam (__SYS_pthread + 9)
# define SYS_pthread_getspecific (__SYS_pthread + 10)
# define SYS_pthread_join (__SYS_pthread + 11)
# define SYS_pthread_key_create (__SYS_pthread + 12)
# define SYS_pthread_key_delete (__SYS_pthread + 13)
# define SYS_pthread_mutex_destroy (__SYS_pthread + 14)
# define SYS_pthread_mutex_init (__SYS_pthread + 15)
# define SYS_pthread_mutex_timedlock (__SYS_pthread + 16)
# define SYS_pthread_mutex_trylock (__SYS_pthread + 17)
# define SYS_pthread_mutex_unlock (__SYS_pthread + 18)
# define SYS_pthread_getschedparam (__SYS_pthread + 7)
# define SYS_pthread_getspecific (__SYS_pthread + 8)
# define SYS_pthread_join (__SYS_pthread + 9)
# define SYS_pthread_key_create (__SYS_pthread + 10)
# define SYS_pthread_key_delete (__SYS_pthread + 11)
# define SYS_pthread_mutex_destroy (__SYS_pthread + 12)
# define SYS_pthread_mutex_init (__SYS_pthread + 13)
# define SYS_pthread_mutex_timedlock (__SYS_pthread + 14)
# define SYS_pthread_mutex_trylock (__SYS_pthread + 15)
# define SYS_pthread_mutex_unlock (__SYS_pthread + 16)
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
# define SYS_pthread_mutex_consistent (__SYS_pthread + 19)
# define __SYS_pthread_setschedparam (__SYS_pthread + 20)
# define SYS_pthread_mutex_consistent (__SYS_pthread + 17)
# define __SYS_pthread_setschedparam (__SYS_pthread + 18)
#else
# define __SYS_pthread_setschedparam (__SYS_pthread + 19)
# define __SYS_pthread_setschedparam (__SYS_pthread + 17)
#endif
# define SYS_pthread_setschedparam (__SYS_pthread_setschedparam + 0)

View file

@ -1,35 +1,20 @@
############################################################################
# libs/libc/pthread/Make.defs
#
# Copyright (C) 2011-2012, 2019 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
@ -58,6 +43,7 @@ CSRCS += pthread_setcancelstate.c pthread_setcanceltype.c
CSRCS += pthread_testcancel.c
CSRCS += pthread_rwlock.c pthread_rwlock_rdlock.c pthread_rwlock_wrlock.c
CSRCS += pthread_once.c pthread_yield.c
CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c
ifeq ($(CONFIG_SMP),y)
CSRCS += pthread_attr_getaffinity.c pthread_attr_setaffinity.c

View file

@ -42,7 +42,7 @@
#include <sys/types.h>
#include <pthread.h>
#include "pthread/pthread.h"
#include "nuttx/sched.h"
/****************************************************************************
* Public Functions
@ -63,15 +63,16 @@
*
****************************************************************************/
void *pthread_get_stackaddr_np(pthread_t thread)
FAR void *pthread_get_stackaddr_np(pthread_t thread)
{
FAR struct pthread_tcb_s *tcb =
(FAR struct pthread_tcb_s *)sched_gettcb((pid_t)thread);
struct stackinfo_s stackinfo;
int ret;
if (tcb == NULL)
ret = sched_get_stackinfo((pid_t)thread, &stackinfo);
if (ret < 0)
{
return NULL;
}
return tcb->cmn.adj_stack_ptr;
return stackinfo.adj_stack_ptr;
}

View file

@ -43,7 +43,7 @@
#include <pthread.h>
#include <errno.h>
#include "pthread/pthread.h"
#include "nuttx/sched.h"
/****************************************************************************
* Public Functions
@ -59,7 +59,7 @@
* pthread_t - The pthread being queried
*
* Returned Value:
* A positive, non-zerp pthread stack address is returned on success. A
* A positive, non-zero pthread stack address is returned on success. A
* negative value is returned in the case that the 'thread' argument does
* not refer to a valid thread.
*
@ -67,13 +67,14 @@
ssize_t pthread_get_stacksize_np(pthread_t thread)
{
FAR struct pthread_tcb_s *tcb =
(FAR struct pthread_tcb_s *)sched_gettcb((pid_t)thread);
struct stackinfo_s stackinfo;
int ret;
if (tcb == NULL)
ret = sched_get_stackinfo((pid_t)thread, &stackinfo);
if (ret < 0)
{
return -EINVAL;
return (ssize_t)ret;
}
return tcb->cmn.adj_stack_size;
return stackinfo.adj_stack_size;
}

View file

@ -47,7 +47,6 @@ CSRCS += pthread_condtimedwait.c pthread_kill.c pthread_sigmask.c
CSRCS += pthread_cancel.c
CSRCS += pthread_initialize.c pthread_completejoin.c pthread_findjoininfo.c
CSRCS += pthread_release.c pthread_setschedprio.c
CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c
ifneq ($(CONFIG_PTHREAD_MUTEX_UNSAFE),y)
CSRCS += pthread_mutex.c pthread_mutexconsistent.c pthread_mutexinconsistent.c

View file

@ -1,35 +1,20 @@
############################################################################
# sched/sched/Make.defs
#
# Copyright (C) 2014, 2018, 2020 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
@ -43,7 +28,7 @@ CSRCS += sched_setparam.c sched_setpriority.c sched_getparam.c
CSRCS += sched_setscheduler.c sched_getscheduler.c
CSRCS += sched_yield.c sched_rrgetinterval.c sched_foreach.c
CSRCS += sched_lock.c sched_unlock.c sched_lockcount.c
CSRCS += sched_idletask.c sched_self.c
CSRCS += sched_idletask.c sched_self.c sched_get_stackinfo.c
ifeq ($(CONFIG_PRIORITY_INHERITANCE),y)
CSRCS += sched_reprioritize.c

View file

@ -0,0 +1,77 @@
/****************************************************************************
* sched/sched/sched_get_stackinfo.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <assert.h>
#include "nuttx/sched.h"
#include "sched/sched.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sched_get_stackinfo
*
* Description:
* Report information about a thread's stack allocation.
*
* Input Parameters:
* pid_t - Identifies the thread to query. Zero is interpreted as the
* the calling thread
* stackinfo - User-provided location to return the stack information.
*
* Returned Value:
* Zero (OK) if successful. Otherwise, a negated errno value is returned.
*
****************************************************************************/
int sched_get_stackinfo(pid_t pid, FAR struct stackinfo_s *stackinfo)
{
FAR struct tcb_s *tcb;
DEBUGASSERT(stackinfo != NULL);
if (pid == 0)
{
tcb = this_task();
DEBUGASSERT(tcb != NULL);
}
else
{
tcb = sched_gettcb(pid);
if (tcb == NULL)
{
return -ENOENT;
}
}
stackinfo->adj_stack_size = tcb->adj_stack_size;
stackinfo->stack_alloc_ptr = tcb->stack_alloc_ptr;
stackinfo->adj_stack_ptr = tcb->adj_stack_ptr;
return OK;
}

View file

@ -88,8 +88,6 @@
"pthread_create","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_t*","FAR const pthread_attr_t*","pthread_startroutine_t","pthread_addr_t"
"pthread_detach","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t"
"pthread_exit","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","void","pthread_addr_t"
"pthread_get_stackaddr_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","void*","pthread_t"
"pthread_get_stacksize_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","ssize_t","pthread_t"
"pthread_getaffinity_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SMP)","int","pthread_t","size_t","FAR cpu_set_t*"
"pthread_getschedparam","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","FAR int*","FAR struct sched_param*"
"pthread_getspecific","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","FAR void*","pthread_key_t"
@ -123,6 +121,7 @@
"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*"
"sched_get_stackinfo","nuttx/sched.h","","int","pid_t","struct stackinfo_s*"
"sched_lock","sched.h","","int"
"sched_lockcount","sched.h","","int32_t"
"sched_rr_get_interval","sched.h","","int","pid_t","struct timespec*"

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

View file

@ -42,6 +42,7 @@ 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)
SYSCALL_LOOKUP(sched_get_stackinfo, 2, STUB_sched_get_stackinfo)
#ifdef CONFIG_SMP
SYSCALL_LOOKUP(sched_getaffinity, 3, STUB_sched_getaffinity)
@ -304,8 +305,6 @@ SYSCALL_LOOKUP(up_assert, 2, STUB_up_assert)
SYSCALL_LOOKUP(pthread_create, 4, STUB_pthread_create)
SYSCALL_LOOKUP(pthread_detach, 1, STUB_pthread_detach)
SYSCALL_LOOKUP(pthread_exit, 1, STUB_pthread_exit)
SYSCALL_LOOKUP(pthread_get_stackaddr_np, 1, STUB_pthread_get_stackaddr_np)
SYSCALL_LOOKUP(pthread_get_stacksize_np, 1, STUB_pthread_get_stacksize_np)
SYSCALL_LOOKUP(pthread_getschedparam, 3, STUB_pthread_getschedparam)
SYSCALL_LOOKUP(pthread_getspecific, 1, STUB_pthread_getspecific)
SYSCALL_LOOKUP(pthread_join, 2, STUB_pthread_join)

View file

@ -1,35 +1,20 @@
/****************************************************************************
* syscall/syscall_stublookup.c
*
* Copyright (C) 2011-2013, 2015-2020 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
@ -72,6 +57,7 @@ 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_get_stackinfo(int nbr, uintptr_t parm1, uintptr_t parm2);
uintptr_t STUB_sched_getaffinity(int nbr, uintptr_t parm1, uintptr_t parm2,
uintptr_t parm3);
@ -325,8 +311,6 @@ uintptr_t STUB_pthread_join(int nbr, uintptr_t parm1, uintptr_t parm2);
uintptr_t STUB_pthread_key_create(int nbr, uintptr_t parm1,
uintptr_t parm2);
uintptr_t STUB_pthread_key_delete(int nbr, uintptr_t parm1);
uintptr_t STUB_pthread_get_stackaddr_np(int nbr, uintptr_t parm1);
uintptr_t STUB_pthread_get_stacksize_np(int nbr, uintptr_t parm1);
uintptr_t STUB_pthread_mutex_destroy(int nbr, uintptr_t parm1);
uintptr_t STUB_pthread_mutex_init(int nbr, uintptr_t parm1,
uintptr_t parm2);