| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 33 | int pthread_attr_init(pthread_attr_t* attr) { | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 34 | attr->flags = 0; | 
|  | 35 | attr->stack_base = NULL; | 
| Brian Carlstrom | 50af69e | 2013-09-13 16:34:43 -0700 | [diff] [blame] | 36 | attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT; | 
| Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 37 | attr->guard_size = PAGE_SIZE; | 
|  | 38 | attr->sched_policy = SCHED_NORMAL; | 
|  | 39 | attr->sched_priority = 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 40 | return 0; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | int pthread_attr_destroy(pthread_attr_t* attr) { | 
|  | 44 | memset(attr, 0x42, sizeof(pthread_attr_t)); | 
|  | 45 | return 0; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) { | 
|  | 49 | if (state == PTHREAD_CREATE_DETACHED) { | 
|  | 50 | attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; | 
|  | 51 | } else if (state == PTHREAD_CREATE_JOINABLE) { | 
|  | 52 | attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED; | 
|  | 53 | } else { | 
|  | 54 | return EINVAL; | 
|  | 55 | } | 
|  | 56 | return 0; | 
|  | 57 | } | 
|  | 58 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 59 | int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 60 | *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; | 
|  | 61 | return 0; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) { | 
|  | 65 | attr->sched_policy = policy; | 
|  | 66 | return 0; | 
|  | 67 | } | 
|  | 68 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 69 | int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 70 | *policy = attr->sched_policy; | 
|  | 71 | return 0; | 
|  | 72 | } | 
|  | 73 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 74 | int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 75 | attr->sched_priority = param->sched_priority; | 
|  | 76 | return 0; | 
|  | 77 | } | 
|  | 78 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 79 | int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 80 | param->sched_priority = attr->sched_priority; | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) { | 
| Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 85 | if (stack_size < PTHREAD_STACK_MIN) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 86 | return EINVAL; | 
|  | 87 | } | 
|  | 88 | attr->stack_size = stack_size; | 
|  | 89 | return 0; | 
|  | 90 | } | 
|  | 91 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 92 | int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 93 | *stack_size = attr->stack_size; | 
|  | 94 | return 0; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | int pthread_attr_setstackaddr(pthread_attr_t*, void*) { | 
|  | 98 | // This was removed from POSIX.1-2008, and is not implemented on bionic. | 
|  | 99 | // Needed for ABI compatibility with the NDK. | 
|  | 100 | return ENOSYS; | 
|  | 101 | } | 
|  | 102 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 103 | int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 104 | // This was removed from POSIX.1-2008. | 
|  | 105 | // Needed for ABI compatibility with the NDK. | 
|  | 106 | *stack_addr = (char*)attr->stack_base + attr->stack_size; | 
|  | 107 | return 0; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) { | 
|  | 111 | if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) { | 
|  | 112 | return EINVAL; | 
|  | 113 | } | 
| Elliott Hughes | 405f855 | 2013-10-01 17:25:28 -0700 | [diff] [blame] | 114 | if (reinterpret_cast<uintptr_t>(stack_base) & (PAGE_SIZE - 1)) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 115 | return EINVAL; | 
|  | 116 | } | 
|  | 117 | attr->stack_base = stack_base; | 
|  | 118 | attr->stack_size = stack_size; | 
|  | 119 | return 0; | 
|  | 120 | } | 
|  | 121 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 122 | int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 123 | *stack_base = attr->stack_base; | 
|  | 124 | *stack_size = attr->stack_size; | 
|  | 125 | return 0; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 129 | attr->guard_size = guard_size; | 
|  | 130 | return 0; | 
|  | 131 | } | 
|  | 132 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 133 | int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 134 | *guard_size = attr->guard_size; | 
|  | 135 | return 0; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | int pthread_getattr_np(pthread_t thid, pthread_attr_t* attr) { | 
|  | 139 | pthread_internal_t* thread = (pthread_internal_t*) thid; | 
|  | 140 | *attr = thread->attr; | 
|  | 141 | return 0; | 
|  | 142 | } | 
|  | 143 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 144 | int pthread_attr_setscope(pthread_attr_t*, int scope) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 145 | if (scope == PTHREAD_SCOPE_SYSTEM) { | 
|  | 146 | return 0; | 
|  | 147 | } | 
|  | 148 | if (scope == PTHREAD_SCOPE_PROCESS) { | 
|  | 149 | return ENOTSUP; | 
|  | 150 | } | 
|  | 151 | return EINVAL; | 
|  | 152 | } | 
|  | 153 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 154 | int pthread_attr_getscope(const pthread_attr_t*, int* scope) { | 
|  | 155 | *scope = PTHREAD_SCOPE_SYSTEM; | 
|  | 156 | return 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 157 | } |