diff --git a/include/pthread.h b/include/pthread.h index b1ac5ab249..0fd42902e2 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -466,13 +466,18 @@ int pthread_attr_getaffinity_np(FAR const pthread_attr_t *attr, int pthread_attr_setstacksize(FAR pthread_attr_t *attr, long stacksize); int pthread_attr_getstacksize(FAR const pthread_attr_t *attr, long *stackaddr); -/* Set or obtain stack address and size */ +/* Set or obtain stack address and size attributes */ int pthread_attr_setstack(FAR pthread_attr_t *attr, FAR void *stackaddr, long stacksize); int pthread_attr_getstack(FAR pthread_attr_t *attr, FAR void **stackaddr, FAR long *stacksize); +/* Get run-time stack address and size */ + +FAR void *pthread_get_stackaddr_np(FAR pthread_t thread); +ssize_t pthread_get_stacksize_np(FAR pthread_t thread); + /* To create a thread object and runnable thread, a routine must be specified * as the new thread's start routine. An argument may be passed to this * routine, as an untyped address; an untyped address may also be returned as diff --git a/include/sys/syscall.h b/include/sys/syscall.h index 0179e81588..50c4e9d9dd 100644 --- a/include/sys/syscall.h +++ b/include/sys/syscall.h @@ -425,22 +425,24 @@ # define SYS_pthread_create (__SYS_pthread + 4) # define SYS_pthread_detach (__SYS_pthread + 5) # define SYS_pthread_exit (__SYS_pthread + 6) -# 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) +# 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) #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE -# define SYS_pthread_mutex_consistent (__SYS_pthread + 17) -# define __SYS_pthread_setschedparam (__SYS_pthread + 18) +# define SYS_pthread_mutex_consistent (__SYS_pthread + 19) +# define __SYS_pthread_setschedparam (__SYS_pthread + 20) #else -# define __SYS_pthread_setschedparam (__SYS_pthread + 17) +# define __SYS_pthread_setschedparam (__SYS_pthread + 19) #endif # define SYS_pthread_setschedparam (__SYS_pthread_setschedparam + 0) diff --git a/sched/pthread/Make.defs b/sched/pthread/Make.defs index 317d313c58..e05ef67174 100644 --- a/sched/pthread/Make.defs +++ b/sched/pthread/Make.defs @@ -46,6 +46,7 @@ CSRCS += pthread_condwait.c pthread_condsignal.c pthread_condbroadcast.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 diff --git a/sched/pthread/pthread_get_stackaddr_np.c b/sched/pthread/pthread_get_stackaddr_np.c new file mode 100644 index 0000000000..a22a7f7659 --- /dev/null +++ b/sched/pthread/pthread_get_stackaddr_np.c @@ -0,0 +1,77 @@ +/**************************************************************************** + * sched/pthread/pthread_get_stackaddr_np.c + * + * Copyright (C) 2018 Geoff Norton. All rights reserved. + * Author: Geoff Norton + * + * 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 "pthread/pthread.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_get_stackaddr_np + * + * Description: + * Get a pointer to the base of the pthread stack + * + * Parameters: + * pthread_t - The pthread being queried + * + * Returned Value: + * The pthread stack address is returned on success. NULL is returned + * if the 'thread' argument does not refer to a valid pthread. + * + ****************************************************************************/ + +void *pthread_get_stackaddr_np(FAR pthread_t thread) +{ + FAR struct pthread_tcb_s *tcb = + (FAR struct pthread_tcb_s *)sched_gettcb((pid_t)thread); + + if (tcb == NULL) + { + return NULL; + } + + return tcb->cmn.adj_stack_ptr; +} diff --git a/sched/pthread/pthread_get_stacksize_np.c b/sched/pthread/pthread_get_stacksize_np.c new file mode 100644 index 0000000000..08e597cae4 --- /dev/null +++ b/sched/pthread/pthread_get_stacksize_np.c @@ -0,0 +1,79 @@ +/**************************************************************************** + * sched/pthread/pthread_get_stacksize_np.c + * + * Copyright (C) 2018 Geoff Norton. All rights reserved. + * Author: Geoff Norton + * + * 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 + +#include "pthread/pthread.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_get_stacksize_np + * + * Description: + * Get a pointer to the base of the pthread stack + * + * Parameters: + * pthread_t - The pthread being queried + * + * Returned Value: + * A positive, non-zerp 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. + * + ****************************************************************************/ + +ssize_t pthread_get_stacksize_np(FAR pthread_t thread) +{ + FAR struct pthread_tcb_s *tcb = + (FAR struct pthread_tcb_s *)sched_gettcb((pid_t)thread); + + if (tcb == NULL) + { + return -EINVAL; + } + + return tcb->cmn.adj_stack_size; +} diff --git a/syscall/syscall.csv b/syscall/syscall.csv index fcbb9fbdae..4423f9a186 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -84,6 +84,8 @@ "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)","size_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" diff --git a/syscall/syscall_lookup.h b/syscall/syscall_lookup.h index 7a90ce2e5a..3786ce69d8 100644 --- a/syscall/syscall_lookup.h +++ b/syscall/syscall_lookup.h @@ -304,6 +304,8 @@ SYSCALL_LOOKUP(up_assert, 2, STUB_up_assert) SYSCALL_LOOKUP(pthread_join, 2, STUB_pthread_join) SYSCALL_LOOKUP(pthread_key_create, 2, STUB_pthread_key_create) SYSCALL_LOOKUP(pthread_key_delete, 1, STUB_pthread_key_delete) + 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_mutex_destroy, 1, STUB_pthread_mutex_destroy) SYSCALL_LOOKUP(pthread_mutex_init, 2, STUB_pthread_mutex_init) SYSCALL_LOOKUP(pthread_mutex_timedlock, 2, STUB_pthread_mutex_timedlock) diff --git a/syscall/syscall_stublookup.c b/syscall/syscall_stublookup.c index 56ac32142f..0065e2ae21 100644 --- a/syscall/syscall_stublookup.c +++ b/syscall/syscall_stublookup.c @@ -308,6 +308,8 @@ 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);