pthreads: Add support static pthread stack. Add standard pthread_attr_setstack() and pthread_attr_getstack(). In all cases where the stack is released, add check to see which allocator must be used to free the stack: The user or the kernel allocator.

This commit is contained in:
xuanlin
2018-08-23 09:48:53 -06:00
committed by Gregory Nutt
parent 86eef8ce3a
commit 2c93467436
19 changed files with 300 additions and 27 deletions
+6
View File
@@ -103,16 +103,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -93,16 +93,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -103,16 +103,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+3
View File
@@ -80,9 +80,12 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Is there a stack allocated? */ /* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{
if (umm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
@@ -92,16 +92,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+3
View File
@@ -89,8 +89,11 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Is there a stack allocated? */ /* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{
if (umm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */ /* Mark the stack freed */
+6
View File
@@ -100,16 +100,22 @@ void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
/* Use the kernel allocator if this is a kernel thread */ /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
if (kmm_heapmember(dtcb->stack_alloc_ptr))
{ {
sched_kfree(dtcb->stack_alloc_ptr); sched_kfree(dtcb->stack_alloc_ptr);
} }
}
else else
#endif #endif
{ {
/* Use the user-space allocator if this is a task or pthread */ /* Use the user-space allocator if this is a task or pthread */
if (umm_heapmember(dtcb->stack_alloc_ptr))
{
sched_ufree(dtcb->stack_alloc_ptr); sched_ufree(dtcb->stack_alloc_ptr);
} }
}
/* Mark the stack freed */ /* Mark the stack freed */
+4
View File
@@ -71,6 +71,7 @@
0, /* low_priority */ \ 0, /* low_priority */ \
0, /* max_repl */ \ 0, /* max_repl */ \
0, /* affinity */ \ 0, /* affinity */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
{0, 0}, /* repl_period */ \ {0, 0}, /* repl_period */ \
{0, 0} /* budget */ \ {0, 0} /* budget */ \
@@ -83,6 +84,7 @@
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \ PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
0, /* low_priority */ \ 0, /* low_priority */ \
0, /* max_repl */ \ 0, /* max_repl */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
{0, 0}, /* repl_period */ \ {0, 0}, /* repl_period */ \
{0, 0}, /* budget */ \ {0, 0}, /* budget */ \
@@ -94,6 +96,7 @@
PTHREAD_DEFAULT_POLICY, /* policy */ \ PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \ PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
0, /* affinity */ \ 0, /* affinity */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
} }
#else #else
@@ -102,6 +105,7 @@
PTHREAD_DEFAULT_PRIORITY, /* priority */ \ PTHREAD_DEFAULT_PRIORITY, /* priority */ \
PTHREAD_DEFAULT_POLICY, /* policy */ \ PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \ PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
} }
#endif #endif
+8
View File
@@ -227,6 +227,7 @@ struct pthread_attr_s
cpu_set_t affinity; /* Set of permitted CPUs for the thread */ cpu_set_t affinity; /* Set of permitted CPUs for the thread */
#endif #endif
FAR void *stackaddr; /* Address of memory to be used as stack */
size_t stacksize; /* Size of the stack allocated for the pthread */ size_t stacksize; /* Size of the stack allocated for the pthread */
#ifdef CONFIG_SCHED_SPORADIC #ifdef CONFIG_SCHED_SPORADIC
@@ -406,6 +407,13 @@ 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_setstacksize(FAR pthread_attr_t *attr, long stacksize);
int pthread_attr_getstacksize(FAR const pthread_attr_t *attr, long *stackaddr); int pthread_attr_getstacksize(FAR const pthread_attr_t *attr, long *stackaddr);
/* Set or obtain stack address and size */
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);
/* To create a thread object and runnable thread, a routine must be specified /* 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 * 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 * routine, as an untyped address; an untyped address may also be returned as
+1
View File
@@ -41,6 +41,7 @@ CSRCS += pthread_attr_init.c pthread_attr_destroy.c
CSRCS += pthread_attr_setschedpolicy.c pthread_attr_getschedpolicy.c CSRCS += pthread_attr_setschedpolicy.c pthread_attr_getschedpolicy.c
CSRCS += pthread_attr_setinheritsched.c pthread_attr_getinheritsched.c CSRCS += pthread_attr_setinheritsched.c pthread_attr_getinheritsched.c
CSRCS += pthread_attr_setstacksize.c pthread_attr_getstacksize.c CSRCS += pthread_attr_setstacksize.c pthread_attr_getstacksize.c
CSRCS += pthread_attr_setstack.c pthread_attr_getstack.c
CSRCS += pthread_attr_setschedparam.c pthread_attr_getschedparam.c CSRCS += pthread_attr_setschedparam.c pthread_attr_getschedparam.c
CSRCS += pthread_barrierattr_init.c pthread_barrierattr_destroy.c CSRCS += pthread_barrierattr_init.c pthread_barrierattr_destroy.c
CSRCS += pthread_barrierattr_getpshared.c pthread_barrierattr_setpshared.c CSRCS += pthread_barrierattr_getpshared.c pthread_barrierattr_setpshared.c
+88
View File
@@ -0,0 +1,88 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_getstack.c
*
* Copyright (C) 2018 Pinecone. All rights reserved.
* Author:
*
* 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 <sys/types.h>
#include <pthread.h>
#include <string.h>
#include <debug.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_getstack
*
* Description:
*
* Parameters:
* attr
* stacksize
*
* Return Value:
* 0 if successful. Otherwise, an error code.
*
* Assumptions:
*
****************************************************************************/
int pthread_attr_getstack(FAR pthread_attr_t *attr,
FAR void **stackaddr, FAR long *stacksize)
{
int ret;
linfo("attr=0x%p stackaddr=0x%p stacksize=0x%p\n", attr, stackaddr, stacksize);
if (!attr || !stackaddr || !stacksize)
{
ret = EINVAL;
}
else
{
*stackaddr = attr->stackaddr;
*stacksize = attr->stacksize;
ret = OK;
}
linfo("Returning %d\n", ret);
return ret;
}
+89
View File
@@ -0,0 +1,89 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_setstack.c
*
* Copyright (C) 2018 Pinecone. All rights reserved.
* Author:
*
* 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 <pthread.h>
#include <string.h>
#include <debug.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_setstack
*
* Description:
*
* Parameters:
* attr
* stackaddr
* stacksize
*
* Return Value:
* 0 if successful. Otherwise, an error code.
*
* Assumptions:
*
****************************************************************************/
int pthread_attr_setstack(FAR pthread_attr_t *attr,
FAR void *stackaddr, long stacksize)
{
int ret;
linfo("attr=0x%p stackaddr=0x%p stacksize=%ld\n", attr, stackaddr, stacksize);
if (!attr || !stackaddr || stacksize < PTHREAD_STACK_MIN)
{
ret = EINVAL;
}
else
{
attr->stackaddr = stackaddr;
attr->stacksize = stacksize;
ret = OK;
}
linfo("Returning %d\n", ret);
return ret;
}
+11
View File
@@ -302,10 +302,21 @@ int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
goto errout_with_tcb; goto errout_with_tcb;
} }
if (attr->stackaddr)
{
/* Use pre-allocated stack */
ret = up_use_stack((FAR struct tcb_s *)ptcb, attr->stackaddr,
attr->stacksize);
}
else
{
/* Allocate the stack for the TCB */ /* Allocate the stack for the TCB */
ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize, ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize,
TCB_FLAG_TTYPE_PTHREAD); TCB_FLAG_TTYPE_PTHREAD);
}
if (ret != OK) if (ret != OK)
{ {
errcode = ENOMEM; errcode = ENOMEM;