[14547] | 1 | /* GNU Objective C Runtime Thread Implementation |
---|
[20088] | 2 | Copyright (C) 1996, 1997, 2002 Free Software Foundation, Inc. |
---|
[14547] | 3 | Contributed by Galen C. Hunt (gchunt@cs.rochester.edu) |
---|
| 4 | Modified for Mach threads by Bill Bumgarner <bbum@friday.com> |
---|
| 5 | Condition functions added by Mircea Oancea <mircea@first.elcom.pub.ro> |
---|
| 6 | |
---|
[21198] | 7 | This file is part of GCC. |
---|
[14547] | 8 | |
---|
[21198] | 9 | GCC is free software; you can redistribute it and/or modify it under the |
---|
[14547] | 10 | terms of the GNU General Public License as published by the Free Software |
---|
| 11 | Foundation; either version 2, or (at your option) any later version. |
---|
| 12 | |
---|
[21198] | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
[14547] | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 16 | details. |
---|
| 17 | |
---|
| 18 | You should have received a copy of the GNU General Public License |
---|
[21198] | 19 | along with GCC; see the file COPYING. If not, write to |
---|
[14547] | 20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
---|
| 21 | Boston, MA 02111-1307, USA. */ |
---|
| 22 | |
---|
| 23 | /* As a special exception, if you link this library with files compiled with |
---|
| 24 | GCC to produce an executable, this does not cause the resulting executable |
---|
| 25 | to be covered by the GNU General Public License. This exception does not |
---|
| 26 | however invalidate any other reasons why the executable file might be |
---|
| 27 | covered by the GNU General Public License. */ |
---|
| 28 | |
---|
| 29 | #include <mach/mach.h> |
---|
| 30 | #include <mach/cthreads.h> |
---|
| 31 | #include <objc/thr.h> |
---|
| 32 | #include "runtime.h" |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | Obtain the maximum thread priority that can set for t. Under the |
---|
| 36 | mach threading model, it is possible for the developer to adjust the |
---|
| 37 | maximum priority downward only-- cannot be raised without superuser |
---|
| 38 | privileges. Once lowered, it cannot be raised. |
---|
| 39 | */ |
---|
[20088] | 40 | static int |
---|
| 41 | __mach_get_max_thread_priority (cthread_t t, int *base) |
---|
[14547] | 42 | { |
---|
| 43 | thread_t threadP; |
---|
| 44 | kern_return_t error; |
---|
| 45 | struct thread_sched_info info; |
---|
| 46 | unsigned int info_count=THREAD_SCHED_INFO_COUNT; |
---|
| 47 | |
---|
| 48 | if (t == NULL) |
---|
| 49 | return -1; |
---|
| 50 | |
---|
[20088] | 51 | threadP = cthread_thread (t); /* get thread underlying */ |
---|
[14547] | 52 | |
---|
[20088] | 53 | error = thread_info (threadP, THREAD_SCHED_INFO, |
---|
| 54 | (thread_info_t) &info, &info_count); |
---|
[14547] | 55 | |
---|
| 56 | if (error != KERN_SUCCESS) |
---|
| 57 | return -1; |
---|
| 58 | |
---|
| 59 | if (base != NULL) |
---|
| 60 | *base = info.base_priority; |
---|
| 61 | |
---|
| 62 | return info.max_priority; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /* Backend initialization functions */ |
---|
| 66 | |
---|
| 67 | /* Initialize the threads subsystem. */ |
---|
| 68 | int |
---|
[20088] | 69 | __objc_init_thread_system (void) |
---|
[14547] | 70 | { |
---|
| 71 | return 0; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* Close the threads subsystem. */ |
---|
| 75 | int |
---|
[20088] | 76 | __objc_close_thread_system (void) |
---|
[14547] | 77 | { |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /* Backend thread functions */ |
---|
| 82 | |
---|
| 83 | /* Create a new thread of execution. */ |
---|
| 84 | objc_thread_t |
---|
[20088] | 85 | __objc_thread_detach (void (*func) (void *arg), void *arg) |
---|
[14547] | 86 | { |
---|
| 87 | objc_thread_t thread_id; |
---|
| 88 | cthread_t new_thread_handle; |
---|
| 89 | |
---|
| 90 | /* create thread */ |
---|
[20088] | 91 | new_thread_handle = cthread_fork ((cthread_fn_t) func, arg); |
---|
[14547] | 92 | |
---|
[20088] | 93 | if (new_thread_handle) |
---|
[14547] | 94 | { |
---|
| 95 | /* this is not terribly portable */ |
---|
[20088] | 96 | thread_id = *(objc_thread_t *) &new_thread_handle; |
---|
| 97 | cthread_detach (new_thread_handle); |
---|
[14547] | 98 | } |
---|
| 99 | else |
---|
| 100 | thread_id = NULL; |
---|
| 101 | |
---|
| 102 | return thread_id; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /* Set the current thread's priority. */ |
---|
| 106 | int |
---|
[20088] | 107 | __objc_thread_set_priority (int priority) |
---|
[14547] | 108 | { |
---|
[20088] | 109 | objc_thread_t *t = objc_thread_id (); |
---|
[14547] | 110 | cthread_t cT = (cthread_t) t; |
---|
[20088] | 111 | int maxPriority = __mach_get_max_thread_priority (cT, NULL); |
---|
[14547] | 112 | int sys_priority = 0; |
---|
| 113 | |
---|
| 114 | if (maxPriority == -1) |
---|
| 115 | return -1; |
---|
| 116 | |
---|
| 117 | switch (priority) |
---|
| 118 | { |
---|
| 119 | case OBJC_THREAD_INTERACTIVE_PRIORITY: |
---|
| 120 | sys_priority = maxPriority; |
---|
| 121 | break; |
---|
| 122 | case OBJC_THREAD_BACKGROUND_PRIORITY: |
---|
| 123 | sys_priority = (maxPriority * 2) / 3; |
---|
| 124 | break; |
---|
| 125 | case OBJC_THREAD_LOW_PRIORITY: |
---|
| 126 | sys_priority = maxPriority / 3; |
---|
| 127 | break; |
---|
| 128 | default: |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | if (sys_priority == 0) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
| 135 | /* Change the priority */ |
---|
[20088] | 136 | if (cthread_priority (cT, sys_priority, 0) == KERN_SUCCESS) |
---|
[14547] | 137 | return 0; |
---|
| 138 | else |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* Return the current thread's priority. */ |
---|
| 143 | int |
---|
[20088] | 144 | __objc_thread_get_priority (void) |
---|
[14547] | 145 | { |
---|
[20088] | 146 | objc_thread_t *t = objc_thread_id (); |
---|
| 147 | cthread_t cT = (cthread_t) t; /* see objc_thread_id () */ |
---|
[14547] | 148 | int basePriority; |
---|
| 149 | int maxPriority; |
---|
| 150 | int sys_priority = 0; |
---|
| 151 | |
---|
| 152 | int interactiveT, backgroundT, lowT; /* thresholds */ |
---|
| 153 | |
---|
[20088] | 154 | maxPriority = __mach_get_max_thread_priority (cT, &basePriority); |
---|
[14547] | 155 | |
---|
[20088] | 156 | if (maxPriority == -1) |
---|
[14547] | 157 | return -1; |
---|
| 158 | |
---|
| 159 | if (basePriority > ( (maxPriority * 2) / 3)) |
---|
| 160 | return OBJC_THREAD_INTERACTIVE_PRIORITY; |
---|
| 161 | |
---|
| 162 | if (basePriority > ( maxPriority / 3)) |
---|
| 163 | return OBJC_THREAD_BACKGROUND_PRIORITY; |
---|
| 164 | |
---|
| 165 | return OBJC_THREAD_LOW_PRIORITY; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* Yield our process time to another thread. */ |
---|
| 169 | void |
---|
[20088] | 170 | __objc_thread_yield (void) |
---|
[14547] | 171 | { |
---|
[20088] | 172 | cthread_yield (); |
---|
[14547] | 173 | } |
---|
| 174 | |
---|
| 175 | /* Terminate the current thread. */ |
---|
| 176 | int |
---|
[20088] | 177 | __objc_thread_exit (void) |
---|
[14547] | 178 | { |
---|
| 179 | /* exit the thread */ |
---|
[20088] | 180 | cthread_exit (&__objc_thread_exit_status); |
---|
[14547] | 181 | |
---|
| 182 | /* Failed if we reached here */ |
---|
| 183 | return -1; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /* Returns an integer value which uniquely describes a thread. */ |
---|
| 187 | objc_thread_t |
---|
[20088] | 188 | __objc_thread_id (void) |
---|
[14547] | 189 | { |
---|
[20088] | 190 | cthread_t self = cthread_self (); |
---|
[14547] | 191 | |
---|
[20088] | 192 | return *(objc_thread_t *) &self; |
---|
[14547] | 193 | } |
---|
| 194 | |
---|
| 195 | /* Sets the thread's local storage pointer. */ |
---|
| 196 | int |
---|
[20088] | 197 | __objc_thread_set_data (void *value) |
---|
[14547] | 198 | { |
---|
[20088] | 199 | cthread_set_data (cthread_self (), (any_t) value); |
---|
[14547] | 200 | return 0; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /* Returns the thread's local storage pointer. */ |
---|
| 204 | void * |
---|
[20088] | 205 | __objc_thread_get_data (void) |
---|
[14547] | 206 | { |
---|
[20088] | 207 | return (void *) cthread_data (cthread_self ()); |
---|
[14547] | 208 | } |
---|
| 209 | |
---|
| 210 | /* Backend mutex functions */ |
---|
| 211 | |
---|
| 212 | /* Allocate a mutex. */ |
---|
| 213 | int |
---|
[20088] | 214 | __objc_mutex_allocate (objc_mutex_t mutex) |
---|
[14547] | 215 | { |
---|
| 216 | int err = 0; |
---|
[20088] | 217 | mutex->backend = objc_malloc (sizeof (struct mutex)); |
---|
[14547] | 218 | |
---|
[20088] | 219 | err = mutex_init ((mutex_t) (mutex->backend)); |
---|
[14547] | 220 | |
---|
| 221 | if (err != 0) |
---|
| 222 | { |
---|
[20088] | 223 | objc_free (mutex->backend); |
---|
[14547] | 224 | return -1; |
---|
| 225 | } |
---|
| 226 | else |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /* Deallocate a mutex. */ |
---|
| 231 | int |
---|
[20088] | 232 | __objc_mutex_deallocate (objc_mutex_t mutex) |
---|
[14547] | 233 | { |
---|
[20088] | 234 | mutex_clear ((mutex_t) (mutex->backend)); |
---|
[14547] | 235 | |
---|
[20088] | 236 | objc_free (mutex->backend); |
---|
[14547] | 237 | mutex->backend = NULL; |
---|
| 238 | return 0; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /* Grab a lock on a mutex. */ |
---|
| 242 | int |
---|
[20088] | 243 | __objc_mutex_lock (objc_mutex_t mutex) |
---|
[14547] | 244 | { |
---|
[20088] | 245 | mutex_lock ((mutex_t) (mutex->backend)); |
---|
[14547] | 246 | return 0; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /* Try to grab a lock on a mutex. */ |
---|
| 250 | int |
---|
[20088] | 251 | __objc_mutex_trylock (objc_mutex_t mutex) |
---|
[14547] | 252 | { |
---|
[20088] | 253 | if (mutex_try_lock ((mutex_t) (mutex->backend)) == 0) |
---|
[14547] | 254 | return -1; |
---|
| 255 | else |
---|
| 256 | return 0; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /* Unlock the mutex */ |
---|
| 260 | int |
---|
[20088] | 261 | __objc_mutex_unlock (objc_mutex_t mutex) |
---|
[14547] | 262 | { |
---|
[20088] | 263 | mutex_unlock ((mutex_t) (mutex->backend)); |
---|
[14547] | 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /* Backend condition mutex functions */ |
---|
| 268 | |
---|
| 269 | /* Allocate a condition. */ |
---|
| 270 | int |
---|
[20088] | 271 | __objc_condition_allocate (objc_condition_t condition) |
---|
[14547] | 272 | { |
---|
[20088] | 273 | condition->backend = objc_malloc (sizeof (struct condition)); |
---|
| 274 | condition_init ((condition_t) (condition->backend)); |
---|
[14547] | 275 | return 0; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /* Deallocate a condition. */ |
---|
| 279 | int |
---|
[20088] | 280 | __objc_condition_deallocate (objc_condition_t condition) |
---|
[14547] | 281 | { |
---|
[20088] | 282 | condition_clear ((condition_t) (condition->backend)); |
---|
| 283 | objc_free (condition->backend); |
---|
[14547] | 284 | condition->backend = NULL; |
---|
| 285 | return 0; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /* Wait on the condition */ |
---|
| 289 | int |
---|
[20088] | 290 | __objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex) |
---|
[14547] | 291 | { |
---|
[20088] | 292 | condition_wait ((condition_t) (condition->backend), |
---|
| 293 | (mutex_t) (mutex->backend)); |
---|
[14547] | 294 | return 0; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /* Wake up all threads waiting on this condition. */ |
---|
| 298 | int |
---|
[20088] | 299 | __objc_condition_broadcast (objc_condition_t condition) |
---|
[14547] | 300 | { |
---|
[20088] | 301 | condition_broadcast ((condition_t) (condition->backend)); |
---|
[14547] | 302 | return 0; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /* Wake up one thread waiting on this condition. */ |
---|
| 306 | int |
---|
[20088] | 307 | __objc_condition_signal (objc_condition_t condition) |
---|
[14547] | 308 | { |
---|
[20088] | 309 | condition_signal ((condition_t) (condition->backend)); |
---|
[14547] | 310 | return 0; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /* End of File */ |
---|