| 1 | /* | 
|---|
| 2 |  * pthread_attr.c - pthread attributes related functions | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless | 
|---|
| 5 |  * Copyright (c) 2011,2012 UPMC Sorbonne Universites | 
|---|
| 6 |  * | 
|---|
| 7 |  * This file is part of ALMOS. | 
|---|
| 8 |  * | 
|---|
| 9 |  * ALMOS is free software; you can redistribute it and/or modify it | 
|---|
| 10 |  * under the terms of the GNU General Public License as published by | 
|---|
| 11 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 12 |  * | 
|---|
| 13 |  * ALMOS is distributed in the hope that it will be useful, but | 
|---|
| 14 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 16 |  * General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  * You should have received a copy of the GNU General Public License | 
|---|
| 19 |  * along with ALMOS; if not, write to the Free Software Foundation, | 
|---|
| 20 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 21 |  */ | 
|---|
| 22 |  | 
|---|
| 23 | #include <errno.h> | 
|---|
| 24 | #include <sched.h> | 
|---|
| 25 | #include <cpu-syscall.h> | 
|---|
| 26 |  | 
|---|
| 27 | #include <pthread.h> | 
|---|
| 28 |  | 
|---|
| 29 | #define PT_DEFAULT_ATTRS (PT_ATTR_AUTO_NXTT | PT_ATTR_AUTO_MGRT | PT_ATTR_MEM_PRIO) | 
|---|
| 30 |  | 
|---|
| 31 | int pthread_attr_init (pthread_attr_t *attr) | 
|---|
| 32 | { | 
|---|
| 33 |         attr->flags                      = PT_DEFAULT_ATTRS; | 
|---|
| 34 |         attr->sched_policy               = SCHED_RR; | 
|---|
| 35 |         attr->inheritsched               = PTHREAD_EXPLICIT_SCHED; | 
|---|
| 36 |         attr->stack_addr                 = NULL; | 
|---|
| 37 |         attr->stack_size                 = 0;//PTHREAD_STACK_SIZE; | 
|---|
| 38 |         attr->sched_param.sched_priority = -1; | 
|---|
| 39 |         attr->cpu_gid                    = -1; | 
|---|
| 40 |         return 0; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | int pthread_attr_destroy (pthread_attr_t *attr) | 
|---|
| 44 | { | 
|---|
| 45 |         return 0; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) | 
|---|
| 49 | { | 
|---|
| 50 |         if(attr == NULL) | 
|---|
| 51 |                 return EINVAL; | 
|---|
| 52 |    | 
|---|
| 53 |         attr->flags = (detachstate == PTHREAD_CREATE_DETACHED) ? __PT_ATTR_DETACH : attr->flags; | 
|---|
| 54 |         return 0; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) | 
|---|
| 58 | { | 
|---|
| 59 |         if((attr == NULL) || (detachstate == NULL)) | 
|---|
| 60 |                 return EINVAL; | 
|---|
| 61 |    | 
|---|
| 62 |         *detachstate = (attr->flags & __PT_ATTR_DETACH) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; | 
|---|
| 63 |         return 0; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) | 
|---|
| 67 | { | 
|---|
| 68 |         if((attr = NULL) || (policy < 0) || (policy > 2)) | 
|---|
| 69 |                 return EINVAL; | 
|---|
| 70 |  | 
|---|
| 71 |         attr->sched_policy = policy; | 
|---|
| 72 |         return 0; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) | 
|---|
| 76 | { | 
|---|
| 77 |         if((attr = NULL) || (policy == NULL)) | 
|---|
| 78 |                 return EINVAL; | 
|---|
| 79 |  | 
|---|
| 80 |         *policy = attr->sched_policy; | 
|---|
| 81 |         return 0; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) | 
|---|
| 85 | { | 
|---|
| 86 |         if((attr = NULL) || (param == NULL)) | 
|---|
| 87 |                 return EINVAL; | 
|---|
| 88 |    | 
|---|
| 89 |         attr->sched_param = *param; | 
|---|
| 90 |         return 0; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) | 
|---|
| 94 | { | 
|---|
| 95 |         if((attr = NULL) || (param == NULL)) | 
|---|
| 96 |                 return EINVAL; | 
|---|
| 97 |  | 
|---|
| 98 |         *param = attr->sched_param; | 
|---|
| 99 |         return 0; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) | 
|---|
| 103 | { | 
|---|
| 104 |         if((attr == NULL) || (inherit != PTHREAD_EXPLICIT_SCHED)) | 
|---|
| 105 |                 return EINVAL; | 
|---|
| 106 |    | 
|---|
| 107 |         return 0; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit) | 
|---|
| 111 | { | 
|---|
| 112 |         if((attr == NULL) || (inherit == NULL)) | 
|---|
| 113 |                 return EINVAL; | 
|---|
| 114 |  | 
|---|
| 115 |         *inherit = PTHREAD_EXPLICIT_SCHED; | 
|---|
| 116 |         return 0; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | int pthread_attr_setscope(pthread_attr_t *attr, int scope) | 
|---|
| 120 | { | 
|---|
| 121 |         if((attr == NULL) || (scope != PTHREAD_SCOPE_SYSTEM)) | 
|---|
| 122 |                 return EINVAL; | 
|---|
| 123 |  | 
|---|
| 124 |         return 0; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | int pthread_attr_getscope(const pthread_attr_t *attr, int *scope) | 
|---|
| 128 | { | 
|---|
| 129 |         if((attr == NULL) || (scope == NULL)) | 
|---|
| 130 |                 return 0; | 
|---|
| 131 |  | 
|---|
| 132 |         *scope = PTHREAD_SCOPE_SYSTEM; | 
|---|
| 133 |         return 0; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | int pthread_attr_setstacksize(pthread_attr_t *attr, unsigned int stacksize) | 
|---|
| 137 | { | 
|---|
| 138 |         if((attr == NULL) || (stacksize < PTHREAD_STACK_MIN)) | 
|---|
| 139 |                 return EINVAL; | 
|---|
| 140 |  | 
|---|
| 141 |         attr->stack_size = stacksize; | 
|---|
| 142 |         return 0; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize) | 
|---|
| 146 | { | 
|---|
| 147 |         if((attr == NULL) || (stacksize == NULL)) | 
|---|
| 148 |                 return EINVAL; | 
|---|
| 149 |  | 
|---|
| 150 |         *stacksize = attr->stack_size; | 
|---|
| 151 |         return 0; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize) | 
|---|
| 155 | { | 
|---|
| 156 |         if((attr == NULL)                    || | 
|---|
| 157 |            ((uint32_t)stackaddr & 0x3)       ||  | 
|---|
| 158 |            (stacksize < PTHREAD_STACK_MIN)) | 
|---|
| 159 |                 return EINVAL; | 
|---|
| 160 |  | 
|---|
| 161 |         attr->stack_addr = stackaddr; | 
|---|
| 162 |         attr->stack_size = stacksize; | 
|---|
| 163 |         return 0; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize) | 
|---|
| 167 | { | 
|---|
| 168 |         if((attr == NULL)                  || | 
|---|
| 169 |            (stackaddr == NULL)             ||  | 
|---|
| 170 |            (stacksize == NULL)) | 
|---|
| 171 |                 return EINVAL; | 
|---|
| 172 |  | 
|---|
| 173 |         *stackaddr = attr->stack_addr; | 
|---|
| 174 |         *stacksize = attr->stack_size; | 
|---|
| 175 |         return 0; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | int pthread_attr_setflags_np(pthread_attr_t *attr, unsigned int flags, unsigned int *old) | 
|---|
| 179 | { | 
|---|
| 180 |         if(attr == NULL) | 
|---|
| 181 |                 return EINVAL; | 
|---|
| 182 |  | 
|---|
| 183 |         if(old != NULL) | 
|---|
| 184 |                 *old = attr->flags; | 
|---|
| 185 |  | 
|---|
| 186 |         attr->flags = flags; | 
|---|
| 187 |         return 0; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | int pthread_attr_setcpuid_np(pthread_attr_t * attr, int cpu_id, int *old_cpu_id) | 
|---|
| 191 | { | 
|---|
| 192 |         if(attr == NULL) | 
|---|
| 193 |                 return EINVAL; | 
|---|
| 194 |  | 
|---|
| 195 |         if(old_cpu_id) | 
|---|
| 196 |                 *old_cpu_id = attr->cpu_gid; | 
|---|
| 197 |  | 
|---|
| 198 |         attr->cpu_gid = cpu_id; | 
|---|
| 199 |         return 0; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | int pthread_attr_getcpuid_np(int *cpu_id) | 
|---|
| 203 | { | 
|---|
| 204 |         if(cpu_id == NULL) return EINVAL; | 
|---|
| 205 |  | 
|---|
| 206 |         *cpu_id = (int)(((__pthread_tls_t*)cpu_get_tls())->attr.cpu_gid); | 
|---|
| 207 |         return 0; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | int pthread_attr_setforkinfo_np(int flags) | 
|---|
| 211 | { | 
|---|
| 212 |         register __pthread_tls_t *tls; | 
|---|
| 213 |  | 
|---|
| 214 |         tls = cpu_get_tls(); | 
|---|
| 215 |         __pthread_tls_set(tls,__PT_TLS_FORK_FLAGS,flags); | 
|---|
| 216 |         return 0; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | int pthread_attr_setforkcpuid_np(int cpu_id) | 
|---|
| 220 | { | 
|---|
| 221 |         register __pthread_tls_t *tls; | 
|---|
| 222 |         register uint_t flags; | 
|---|
| 223 |  | 
|---|
| 224 |         tls    = cpu_get_tls(); | 
|---|
| 225 |         flags  = __pthread_tls_get(tls,__PT_TLS_FORK_FLAGS); | 
|---|
| 226 |         flags |= PT_FORK_TARGET_CPU; | 
|---|
| 227 |  | 
|---|
| 228 |         __pthread_tls_set(tls,__PT_TLS_FORK_FLAGS,flags); | 
|---|
| 229 |         __pthread_tls_set(tls,__PT_TLS_FORK_CPUID,cpu_id); | 
|---|
| 230 |         return 0; | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | int pthread_attr_getforkcpuid_np(int *cpu_id) | 
|---|
| 234 | { | 
|---|
| 235 |         register __pthread_tls_t *tls; | 
|---|
| 236 |         register uint_t flags; | 
|---|
| 237 |  | 
|---|
| 238 |         if(cpu_id == NULL)  | 
|---|
| 239 |                 return EINVAL; | 
|---|
| 240 |  | 
|---|
| 241 |         tls   = cpu_get_tls(); | 
|---|
| 242 |         flags = __pthread_tls_get(tls,__PT_TLS_FORK_FLAGS); | 
|---|
| 243 |  | 
|---|
| 244 |         if(flags & PT_FORK_TARGET_CPU) | 
|---|
| 245 |                 *cpu_id = (int)__pthread_tls_get(tls,__PT_TLS_FORK_CPUID); | 
|---|
| 246 |         else | 
|---|
| 247 |                 *cpu_id = -1; | 
|---|
| 248 |  | 
|---|
| 249 |         return 0; | 
|---|
| 250 | } | 
|---|