| 1 | /* GNU Objective C Runtime Thread Interface | 
|---|
| 2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. | 
|---|
| 3 | Contributed by Galen C. Hunt (gchunt@cs.rochester.edu) | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of GNU CC. | 
|---|
| 6 |  | 
|---|
| 7 | GNU CC is free software; you can redistribute it and/or modify it under the | 
|---|
| 8 | terms of the GNU General Public License as published by the Free Software | 
|---|
| 9 | Foundation; either version 2, or (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY | 
|---|
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 13 | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more | 
|---|
| 14 | details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License along with | 
|---|
| 17 | GNU CC; see the file COPYING.  If not, write to the Free Software | 
|---|
| 18 | Foundation, 59 Temple Place - Suite 330, | 
|---|
| 19 | Boston, MA 02111-1307, USA.  */ | 
|---|
| 20 |  | 
|---|
| 21 | /* As a special exception, if you link this library with files compiled with | 
|---|
| 22 | GCC to produce an executable, this does not cause the resulting executable | 
|---|
| 23 | to be covered by the GNU General Public License. This exception does not | 
|---|
| 24 | however invalidate any other reasons why the executable file might be | 
|---|
| 25 | covered by the GNU General Public License.  */ | 
|---|
| 26 |  | 
|---|
| 27 | #include <pthread.h> | 
|---|
| 28 | #include <thr.h> | 
|---|
| 29 | #include "runtime.h" | 
|---|
| 30 |  | 
|---|
| 31 | /* Key structure for maintaining thread specific storage */ | 
|---|
| 32 | static pthread_key_t _objc_thread_storage; | 
|---|
| 33 |  | 
|---|
| 34 | /* Backend initialization functions */ | 
|---|
| 35 |  | 
|---|
| 36 | /* Initialize the threads subsystem. */ | 
|---|
| 37 | int | 
|---|
| 38 | __objc_init_thread_system(void) | 
|---|
| 39 | { | 
|---|
| 40 | /* Initialize the thread storage key */ | 
|---|
| 41 | return pthread_keycreate(&_objc_thread_storage, NULL); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | /* Close the threads subsystem. */ | 
|---|
| 45 | int | 
|---|
| 46 | __objc_close_thread_system(void) | 
|---|
| 47 | { | 
|---|
| 48 | /* Destroy the thread storage key */ | 
|---|
| 49 | /* Not implemented yet */ | 
|---|
| 50 | /* return pthread_key_delete(&_objc_thread_storage); */ | 
|---|
| 51 | return 0; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | /* Backend thread functions */ | 
|---|
| 55 |  | 
|---|
| 56 | /* Create a new thread of execution. */ | 
|---|
| 57 | objc_thread_t | 
|---|
| 58 | __objc_thread_detach(void (*func)(void *arg), void *arg) | 
|---|
| 59 | { | 
|---|
| 60 | objc_thread_t thread_id; | 
|---|
| 61 | pthread_t new_thread_handle; | 
|---|
| 62 |  | 
|---|
| 63 | if (pthread_create(&new_thread_handle, pthread_attr_default, | 
|---|
| 64 | (void *)func, arg) == 0) | 
|---|
| 65 | { | 
|---|
| 66 | /* ??? May not work! (64bit) */ | 
|---|
| 67 | thread_id = *(objc_thread_t *)&new_thread_handle; | 
|---|
| 68 | pthread_detach(&new_thread_handle);     /* Fully detach thread.     */ | 
|---|
| 69 | } | 
|---|
| 70 | else | 
|---|
| 71 | thread_id = NULL; | 
|---|
| 72 |  | 
|---|
| 73 | return thread_id; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /* Set the current thread's priority. */ | 
|---|
| 77 | int | 
|---|
| 78 | __objc_thread_set_priority(int priority) | 
|---|
| 79 | { | 
|---|
| 80 | int sys_priority = 0; | 
|---|
| 81 |  | 
|---|
| 82 | switch (priority) | 
|---|
| 83 | { | 
|---|
| 84 | case OBJC_THREAD_INTERACTIVE_PRIORITY: | 
|---|
| 85 | sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2; | 
|---|
| 86 | break; | 
|---|
| 87 | default: | 
|---|
| 88 | case OBJC_THREAD_BACKGROUND_PRIORITY: | 
|---|
| 89 | sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2; | 
|---|
| 90 | break; | 
|---|
| 91 | case OBJC_THREAD_LOW_PRIORITY: | 
|---|
| 92 | sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2; | 
|---|
| 93 | break; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | /* Change the priority. */ | 
|---|
| 97 | if (pthread_setprio(pthread_self(), sys_priority) >= 0) | 
|---|
| 98 | return 0; | 
|---|
| 99 | else | 
|---|
| 100 | /* Failed */ | 
|---|
| 101 | return -1; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | /* Return the current thread's priority. */ | 
|---|
| 105 | int | 
|---|
| 106 | __objc_thread_get_priority(void) | 
|---|
| 107 | { | 
|---|
| 108 | int sys_priority; | 
|---|
| 109 |  | 
|---|
| 110 | if ((sys_priority = pthread_getprio(pthread_self())) >= 0) { | 
|---|
| 111 | if (sys_priority >= PRI_FG_MIN_NP && sys_priority <= PRI_FG_MAX_NP) | 
|---|
| 112 | return OBJC_THREAD_INTERACTIVE_PRIORITY; | 
|---|
| 113 | if (sys_priority >= PRI_BG_MIN_NP && sys_priority <= PRI_BG_MAX_NP) | 
|---|
| 114 | return OBJC_THREAD_BACKGROUND_PRIORITY; | 
|---|
| 115 | return OBJC_THREAD_LOW_PRIORITY; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /* Failed */ | 
|---|
| 119 | return -1; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /* Yield our process time to another thread. */ | 
|---|
| 123 | void | 
|---|
| 124 | __objc_thread_yield(void) | 
|---|
| 125 | { | 
|---|
| 126 | pthread_yield(); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | /* Terminate the current thread. */ | 
|---|
| 130 | int | 
|---|
| 131 | __objc_thread_exit(void) | 
|---|
| 132 | { | 
|---|
| 133 | /* exit the thread */ | 
|---|
| 134 | pthread_exit(&__objc_thread_exit_status); | 
|---|
| 135 |  | 
|---|
| 136 | /* Failed if we reached here */ | 
|---|
| 137 | return -1; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /* Returns an integer value which uniquely describes a thread. */ | 
|---|
| 141 | objc_thread_t | 
|---|
| 142 | __objc_thread_id(void) | 
|---|
| 143 | { | 
|---|
| 144 | pthread_t self = pthread_self(); | 
|---|
| 145 |  | 
|---|
| 146 | return (objc_thread_t) pthread_getunique_np (&self); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /* Sets the thread's local storage pointer. */ | 
|---|
| 150 | int | 
|---|
| 151 | __objc_thread_set_data(void *value) | 
|---|
| 152 | { | 
|---|
| 153 | return pthread_setspecific(_objc_thread_storage, value); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | /* Returns the thread's local storage pointer. */ | 
|---|
| 157 | void * | 
|---|
| 158 | __objc_thread_get_data(void) | 
|---|
| 159 | { | 
|---|
| 160 | void *value = NULL; | 
|---|
| 161 |  | 
|---|
| 162 | if ( !(pthread_getspecific(_objc_thread_storage, &value)) ) | 
|---|
| 163 | return value; | 
|---|
| 164 |  | 
|---|
| 165 | return NULL; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | /* Backend mutex functions */ | 
|---|
| 169 |  | 
|---|
| 170 | /* Allocate a mutex. */ | 
|---|
| 171 | int | 
|---|
| 172 | __objc_mutex_allocate(objc_mutex_t mutex) | 
|---|
| 173 | { | 
|---|
| 174 | if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), | 
|---|
| 175 | pthread_mutexattr_default)) | 
|---|
| 176 | return -1; | 
|---|
| 177 | else | 
|---|
| 178 | return 0; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /* Deallocate a mutex. */ | 
|---|
| 182 | int | 
|---|
| 183 | __objc_mutex_deallocate(objc_mutex_t mutex) | 
|---|
| 184 | { | 
|---|
| 185 | if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend)))) | 
|---|
| 186 | return -1; | 
|---|
| 187 | else | 
|---|
| 188 | return 0; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | /* Grab a lock on a mutex. */ | 
|---|
| 192 | int | 
|---|
| 193 | __objc_mutex_lock(objc_mutex_t mutex) | 
|---|
| 194 | { | 
|---|
| 195 | return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend))); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | /* Try to grab a lock on a mutex. */ | 
|---|
| 199 | int | 
|---|
| 200 | __objc_mutex_trylock(objc_mutex_t mutex) | 
|---|
| 201 | { | 
|---|
| 202 | if (pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend))) != 1) | 
|---|
| 203 | return -1; | 
|---|
| 204 | else | 
|---|
| 205 | return 0; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | /* Unlock the mutex */ | 
|---|
| 209 | int | 
|---|
| 210 | __objc_mutex_unlock(objc_mutex_t mutex) | 
|---|
| 211 | { | 
|---|
| 212 | return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend))); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | /* Backend condition mutex functions */ | 
|---|
| 216 |  | 
|---|
| 217 | /* Allocate a condition. */ | 
|---|
| 218 | int | 
|---|
| 219 | __objc_condition_allocate(objc_condition_t condition) | 
|---|
| 220 | { | 
|---|
| 221 | /* Unimplemented. */ | 
|---|
| 222 | return -1; | 
|---|
| 223 |  | 
|---|
| 224 | /* | 
|---|
| 225 | if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL)) | 
|---|
| 226 | return -1; | 
|---|
| 227 | else | 
|---|
| 228 | return 0; | 
|---|
| 229 | */ | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | /* Deallocate a condition. */ | 
|---|
| 233 | int | 
|---|
| 234 | __objc_condition_deallocate(objc_condition_t condition) | 
|---|
| 235 | { | 
|---|
| 236 | /* Unimplemented. */ | 
|---|
| 237 | return -1; | 
|---|
| 238 |  | 
|---|
| 239 | /* | 
|---|
| 240 | return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend))); | 
|---|
| 241 | */ | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | /* Wait on the condition */ | 
|---|
| 245 | int | 
|---|
| 246 | __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex) | 
|---|
| 247 | { | 
|---|
| 248 | /* Unimplemented. */ | 
|---|
| 249 | return -1; | 
|---|
| 250 |  | 
|---|
| 251 | /* | 
|---|
| 252 | return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)), | 
|---|
| 253 | (pthread_mutex_t *)(&(mutex->backend))); | 
|---|
| 254 | */ | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | /* Wake up all threads waiting on this condition. */ | 
|---|
| 258 | int | 
|---|
| 259 | __objc_condition_broadcast(objc_condition_t condition) | 
|---|
| 260 | { | 
|---|
| 261 | /* Unimplemented. */ | 
|---|
| 262 | return -1; | 
|---|
| 263 |  | 
|---|
| 264 | /* | 
|---|
| 265 | return pthread_cond_broadcast((pthread_cond_t *)(&(condition->backend))); | 
|---|
| 266 | */ | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | /* Wake up one thread waiting on this condition. */ | 
|---|
| 270 | int | 
|---|
| 271 | __objc_condition_signal(objc_condition_t condition) | 
|---|
| 272 | { | 
|---|
| 273 | /* Unimplemented. */ | 
|---|
| 274 | return -1; | 
|---|
| 275 |  | 
|---|
| 276 | /* | 
|---|
| 277 | return pthread_cond_signal((pthread_cond_t *)(&(condition->backend))); | 
|---|
| 278 | */ | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | /* End of File */ | 
|---|