pthread_attr.cpp revision 2a1bb4e64677b9abbc17173c79768ed494565047
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#define DEFAULT_STACKSIZE (1024 * 1024)
34
35const pthread_attr_t gDefaultPthreadAttr = {
36  .flags = 0,
37  .stack_base = NULL,
38  .stack_size = DEFAULT_STACKSIZE,
39  .guard_size = PAGE_SIZE,
40  .sched_policy = SCHED_NORMAL,
41  .sched_priority = 0
42};
43
44int pthread_attr_init(pthread_attr_t* attr) {
45  *attr = gDefaultPthreadAttr;
46  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) {
91  if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
92    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) {
135  if (guard_size & (PAGE_SIZE - 1) || guard_size < PAGE_SIZE) {
136    return EINVAL;
137  }
138  attr->guard_size = guard_size;
139  return 0;
140}
141
142int pthread_attr_getguardsize(pthread_attr_t const* attr, size_t* guard_size) {
143  *guard_size = attr->guard_size;
144  return 0;
145}
146
147int pthread_getattr_np(pthread_t thid, pthread_attr_t* attr) {
148  pthread_internal_t* thread = (pthread_internal_t*) thid;
149  *attr = thread->attr;
150  return 0;
151}
152
153int pthread_attr_setscope(pthread_attr_t* , int scope) {
154  if (scope == PTHREAD_SCOPE_SYSTEM) {
155    return 0;
156  }
157  if (scope == PTHREAD_SCOPE_PROCESS) {
158    return ENOTSUP;
159  }
160  return EINVAL;
161}
162
163int pthread_attr_getscope(pthread_attr_t const*) {
164  return PTHREAD_SCOPE_SYSTEM;
165}
166