blob: d7c6c1396394223dca18338bdac63b085e06161a [file] [log] [blame]
Elliott Hughes3e898472013-02-12 16:40:24 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <pthread.h>
30
31#include "pthread_internal.h"
32
Elliott Hughes84114c82013-07-17 13:33:19 -070033// Traditionally we give threads a 1MiB stack. When we started allocating per-thread
34// alternate signal stacks to ease debugging of stack overflows, we subtracted the
35// same amount we were using there from the default thread stack size. This should
36// keep memory usage roughly constant.
37#define DEFAULT_THREAD_STACK_SIZE ((1 * 1024 * 1024) - SIGSTKSZ)
Elliott Hughes3e898472013-02-12 16:40:24 +000038
39int pthread_attr_init(pthread_attr_t* attr) {
Elliott Hughes6d339182013-02-12 16:36:04 -080040 attr->flags = 0;
41 attr->stack_base = NULL;
Elliott Hughes84114c82013-07-17 13:33:19 -070042 attr->stack_size = DEFAULT_THREAD_STACK_SIZE;
Elliott Hughes6d339182013-02-12 16:36:04 -080043 attr->guard_size = PAGE_SIZE;
44 attr->sched_policy = SCHED_NORMAL;
45 attr->sched_priority = 0;
Elliott Hughes3e898472013-02-12 16:40:24 +000046 return 0;
47}
48
49int pthread_attr_destroy(pthread_attr_t* attr) {
50 memset(attr, 0x42, sizeof(pthread_attr_t));
51 return 0;
52}
53
54int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) {
55 if (state == PTHREAD_CREATE_DETACHED) {
56 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
57 } else if (state == PTHREAD_CREATE_JOINABLE) {
58 attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED;
59 } else {
60 return EINVAL;
61 }
62 return 0;
63}
64
65int pthread_attr_getdetachstate(pthread_attr_t const* attr, int* state) {
66 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
67 return 0;
68}
69
70int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) {
71 attr->sched_policy = policy;
72 return 0;
73}
74
75int pthread_attr_getschedpolicy(pthread_attr_t const* attr, int* policy) {
76 *policy = attr->sched_policy;
77 return 0;
78}
79
80int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const* param) {
81 attr->sched_priority = param->sched_priority;
82 return 0;
83}
84
85int pthread_attr_getschedparam(pthread_attr_t const* attr, struct sched_param* param) {
86 param->sched_priority = attr->sched_priority;
87 return 0;
88}
89
90int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070091 if (stack_size < PTHREAD_STACK_MIN) {
Elliott Hughes3e898472013-02-12 16:40:24 +000092 return EINVAL;
93 }
94 attr->stack_size = stack_size;
95 return 0;
96}
97
98int pthread_attr_getstacksize(pthread_attr_t const* attr, size_t* stack_size) {
99 *stack_size = attr->stack_size;
100 return 0;
101}
102
103int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
104 // This was removed from POSIX.1-2008, and is not implemented on bionic.
105 // Needed for ABI compatibility with the NDK.
106 return ENOSYS;
107}
108
109int pthread_attr_getstackaddr(pthread_attr_t const* attr, void** stack_addr) {
110 // This was removed from POSIX.1-2008.
111 // Needed for ABI compatibility with the NDK.
112 *stack_addr = (char*)attr->stack_base + attr->stack_size;
113 return 0;
114}
115
116int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) {
117 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
118 return EINVAL;
119 }
120 if ((uint32_t)stack_base & (PAGE_SIZE - 1)) {
121 return EINVAL;
122 }
123 attr->stack_base = stack_base;
124 attr->stack_size = stack_size;
125 return 0;
126}
127
128int pthread_attr_getstack(pthread_attr_t const* attr, void** stack_base, size_t* stack_size) {
129 *stack_base = attr->stack_base;
130 *stack_size = attr->stack_size;
131 return 0;
132}
133
134int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) {
Elliott Hughes3e898472013-02-12 16:40:24 +0000135 attr->guard_size = guard_size;
136 return 0;
137}
138
139int pthread_attr_getguardsize(pthread_attr_t const* attr, size_t* guard_size) {
140 *guard_size = attr->guard_size;
141 return 0;
142}
143
144int pthread_getattr_np(pthread_t thid, pthread_attr_t* attr) {
145 pthread_internal_t* thread = (pthread_internal_t*) thid;
146 *attr = thread->attr;
147 return 0;
148}
149
150int pthread_attr_setscope(pthread_attr_t* , int scope) {
151 if (scope == PTHREAD_SCOPE_SYSTEM) {
152 return 0;
153 }
154 if (scope == PTHREAD_SCOPE_PROCESS) {
155 return ENOTSUP;
156 }
157 return EINVAL;
158}
159
160int pthread_attr_getscope(pthread_attr_t const*) {
161 return PTHREAD_SCOPE_SYSTEM;
162}