1 /* 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 33 // 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) 38 39 int pthread_attr_init(pthread_attr_t* attr) { 40 attr->flags = 0; 41 attr->stack_base = NULL; 42 attr->stack_size = DEFAULT_THREAD_STACK_SIZE; 43 attr->guard_size = PAGE_SIZE; 44 attr->sched_policy = SCHED_NORMAL; 45 attr->sched_priority = 0; 46 return 0; 47 } 48 49 int pthread_attr_destroy(pthread_attr_t* attr) { 50 memset(attr, 0x42, sizeof(pthread_attr_t)); 51 return 0; 52 } 53 54 int 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 65 int 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 70 int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) { 71 attr->sched_policy = policy; 72 return 0; 73 } 74 75 int pthread_attr_getschedpolicy(pthread_attr_t const* attr, int* policy) { 76 *policy = attr->sched_policy; 77 return 0; 78 } 79 80 int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const* param) { 81 attr->sched_priority = param->sched_priority; 82 return 0; 83 } 84 85 int pthread_attr_getschedparam(pthread_attr_t const* attr, struct sched_param* param) { 86 param->sched_priority = attr->sched_priority; 87 return 0; 88 } 89 90 int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) { 91 if (stack_size < PTHREAD_STACK_MIN) { 92 return EINVAL; 93 } 94 attr->stack_size = stack_size; 95 return 0; 96 } 97 98 int pthread_attr_getstacksize(pthread_attr_t const* attr, size_t* stack_size) { 99 *stack_size = attr->stack_size; 100 return 0; 101 } 102 103 int 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 109 int 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 116 int 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 128 int 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 134 int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) { 135 attr->guard_size = guard_size; 136 return 0; 137 } 138 139 int pthread_attr_getguardsize(pthread_attr_t const* attr, size_t* guard_size) { 140 *guard_size = attr->guard_size; 141 return 0; 142 } 143 144 int 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 150 int 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 160 int pthread_attr_getscope(pthread_attr_t const*) { 161 return PTHREAD_SCOPE_SYSTEM; 162 } 163