rt-thread-official/components/libc/pthreads/pthread_attr.c

208 lines
4.6 KiB
C
Raw Normal View History

2013-06-26 23:18:30 +08:00
/*
2018-10-14 19:28:18 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2013-06-26 23:18:30 +08:00
*
2018-10-14 19:28:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2013-06-26 23:18:30 +08:00
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
2013-01-08 22:40:58 +08:00
#include <rtthread.h>
#include "pthread.h"
#include "sched.h"
#include <string.h>
2013-06-26 23:18:30 +08:00
#define DEFAULT_STACK_SIZE 2048
#define DEFAULT_PRIORITY (RT_THREAD_PRIORITY_MAX/2 + RT_THREAD_PRIORITY_MAX/4)
2013-01-08 22:40:58 +08:00
const pthread_attr_t pthread_default_attr =
{
2013-06-26 23:18:30 +08:00
0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */
PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
2013-06-26 23:18:30 +08:00
SCHED_FIFO, /* scheduler policy */
{
DEFAULT_PRIORITY, /* scheduler priority */
},
PTHREAD_CREATE_JOINABLE, /* detach state */
2013-01-08 22:40:58 +08:00
};
int pthread_attr_init(pthread_attr_t *attr)
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
*attr = pthread_default_attr;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_init);
int pthread_attr_destroy(pthread_attr_t *attr)
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
memset(attr, 0, sizeof(pthread_attr_t));
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_destroy);
2013-06-26 23:18:30 +08:00
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
return EINVAL;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
attr->detachstate = state;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setdetachstate);
2013-06-26 23:18:30 +08:00
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
*state = (int)attr->detachstate;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getdetachstate);
2013-06-26 23:18:30 +08:00
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2013-01-08 22:40:58 +08:00
attr->schedpolicy = policy;
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setschedpolicy);
int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
*policy = (int)attr->schedpolicy;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getschedpolicy);
2013-06-26 23:18:30 +08:00
int pthread_attr_setschedparam(pthread_attr_t *attr,
struct sched_param const *param)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
attr->schedparam.sched_priority = param->sched_priority;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setschedparam);
2013-06-26 23:18:30 +08:00
int pthread_attr_getschedparam(pthread_attr_t const *attr,
struct sched_param *param)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
2013-01-08 22:40:58 +08:00
param->sched_priority = attr->schedparam.sched_priority;
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getschedparam);
2013-06-26 23:18:30 +08:00
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
attr->stacksize = stack_size;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setstacksize);
2013-06-26 23:18:30 +08:00
int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
*stack_size = attr->stacksize;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getstacksize);
2013-06-26 23:18:30 +08:00
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2014-03-11 16:05:14 +08:00
return EOPNOTSUPP;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setstackaddr);
2013-06-26 23:18:30 +08:00
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2014-03-11 16:05:14 +08:00
return EOPNOTSUPP;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getstackaddr);
2013-06-26 23:18:30 +08:00
int pthread_attr_setstack(pthread_attr_t *attr,
void *stack_base,
size_t stack_size)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2013-01-08 22:40:58 +08:00
attr->stackaddr = stack_base;
attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_setstack);
2013-06-26 23:18:30 +08:00
int pthread_attr_getstack(pthread_attr_t const *attr,
void **stack_base,
size_t *stack_size)
2013-01-08 22:40:58 +08:00
{
2013-06-26 23:18:30 +08:00
RT_ASSERT(attr != RT_NULL);
2013-01-08 22:40:58 +08:00
*stack_base = attr->stackaddr;
*stack_size = attr->stacksize;
2013-01-08 22:40:58 +08:00
2013-06-26 23:18:30 +08:00
return 0;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getstack);
2013-06-26 23:18:30 +08:00
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size)
2013-01-08 22:40:58 +08:00
{
2014-03-11 16:05:14 +08:00
return EOPNOTSUPP;
2013-01-08 22:40:58 +08:00
}
2013-06-26 23:18:30 +08:00
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
2013-01-08 22:40:58 +08:00
{
2014-03-11 16:05:14 +08:00
return EOPNOTSUPP;
2013-01-08 22:40:58 +08:00
}
RTM_EXPORT(pthread_attr_getguardsize);
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
{
if (scope == PTHREAD_SCOPE_SYSTEM)
return 0;
if (scope == PTHREAD_SCOPE_PROCESS)
2014-03-11 16:05:14 +08:00
return EOPNOTSUPP;
2013-01-08 22:40:58 +08:00
return EINVAL;
}
RTM_EXPORT(pthread_attr_setscope);
int pthread_attr_getscope(pthread_attr_t const *attr)
{
return PTHREAD_SCOPE_SYSTEM;
}
RTM_EXPORT(pthread_attr_getscope);