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