1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup POSIXAPI |
---|
5 | * |
---|
6 | * @brief Pthread Get Attribute |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 2014. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #define _GNU_SOURCE |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <pthread.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <string.h> |
---|
27 | |
---|
28 | #include <rtems/posix/pthreadimpl.h> |
---|
29 | #include <rtems/posix/pthreadattrimpl.h> |
---|
30 | #include <rtems/posix/priorityimpl.h> |
---|
31 | #include <rtems/score/schedulerimpl.h> |
---|
32 | #include <rtems/score/threadimpl.h> |
---|
33 | |
---|
34 | int pthread_getattr_np( |
---|
35 | pthread_t thread, |
---|
36 | pthread_attr_t *attr |
---|
37 | ) |
---|
38 | { |
---|
39 | Thread_Control *the_thread; |
---|
40 | ISR_lock_Context lock_context; |
---|
41 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
42 | const Scheduler_Control *scheduler; |
---|
43 | Priority_Control priority; |
---|
44 | bool ok; |
---|
45 | |
---|
46 | if ( attr == NULL ) { |
---|
47 | return EINVAL; |
---|
48 | } |
---|
49 | |
---|
50 | attr = memset( attr, 0, sizeof( *attr ) ); |
---|
51 | |
---|
52 | the_thread = _Thread_Get( thread, &lock_context ); |
---|
53 | |
---|
54 | if ( the_thread == NULL ) { |
---|
55 | return ESRCH; |
---|
56 | } |
---|
57 | |
---|
58 | _Thread_State_acquire_critical( the_thread, &lock_context ); |
---|
59 | |
---|
60 | attr->stackaddr = the_thread->Start.Initial_stack.area; |
---|
61 | attr->stacksize = the_thread->Start.Initial_stack.size; |
---|
62 | |
---|
63 | if ( the_thread->was_created_with_inherited_scheduler ) { |
---|
64 | attr->inheritsched = PTHREAD_INHERIT_SCHED; |
---|
65 | } else { |
---|
66 | attr->inheritsched = PTHREAD_EXPLICIT_SCHED; |
---|
67 | } |
---|
68 | |
---|
69 | scheduler = _Thread_Scheduler_get_home( the_thread ); |
---|
70 | _POSIX_Threads_Get_sched_param_sporadic( |
---|
71 | the_thread, |
---|
72 | scheduler, |
---|
73 | &attr->schedparam |
---|
74 | ); |
---|
75 | priority = the_thread->Real_priority.priority; |
---|
76 | |
---|
77 | if ( _Thread_Is_joinable( the_thread ) ) { |
---|
78 | attr->detachstate = PTHREAD_CREATE_JOINABLE; |
---|
79 | } else { |
---|
80 | attr->detachstate = PTHREAD_CREATE_DETACHED; |
---|
81 | } |
---|
82 | |
---|
83 | attr->affinityset = &attr->affinitysetpreallocated; |
---|
84 | attr->affinitysetsize = sizeof( attr->affinitysetpreallocated ); |
---|
85 | ok = _Scheduler_Get_affinity( |
---|
86 | the_thread, |
---|
87 | attr->affinitysetsize, |
---|
88 | attr->affinityset |
---|
89 | ); |
---|
90 | |
---|
91 | budget_algorithm = the_thread->budget_algorithm; |
---|
92 | |
---|
93 | _Thread_State_release( the_thread, &lock_context ); |
---|
94 | |
---|
95 | attr->is_initialized = true; |
---|
96 | attr->contentionscope = PTHREAD_SCOPE_PROCESS; |
---|
97 | attr->cputime_clock_allowed = 1; |
---|
98 | attr->schedparam.sched_priority = _POSIX_Priority_From_core( |
---|
99 | scheduler, |
---|
100 | priority |
---|
101 | ); |
---|
102 | attr->schedpolicy = |
---|
103 | _POSIX_Thread_Translate_to_sched_policy( budget_algorithm ); |
---|
104 | |
---|
105 | return ok ? 0 : EINVAL; |
---|
106 | } |
---|