| 1 | /* GNU Objective C Runtime Thread Implementation
|
|---|
| 2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|---|
| 3 | Contributed by Galen C. Hunt ([email protected])
|
|---|
| 4 | Modified for Mach threads by Bill Bumgarner <[email protected]>
|
|---|
| 5 | Condition functions added by Mircea Oancea <[email protected]>
|
|---|
| 6 |
|
|---|
| 7 | This file is part of GNU CC.
|
|---|
| 8 |
|
|---|
| 9 | GNU CC is free software; you can redistribute it and/or modify it under the
|
|---|
| 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 |
|
|---|
| 13 | GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 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
|
|---|
| 19 | along with GNU CC; see the file COPYING. If not, write to
|
|---|
| 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 | */
|
|---|
| 40 | static int __mach_get_max_thread_priority(cthread_t t, int *base)
|
|---|
| 41 | {
|
|---|
| 42 | thread_t threadP;
|
|---|
| 43 | kern_return_t error;
|
|---|
| 44 | struct thread_sched_info info;
|
|---|
| 45 | unsigned int info_count=THREAD_SCHED_INFO_COUNT;
|
|---|
| 46 |
|
|---|
| 47 | if (t == NULL)
|
|---|
| 48 | return -1;
|
|---|
| 49 |
|
|---|
| 50 | threadP = cthread_thread(t); /* get thread underlying */
|
|---|
| 51 |
|
|---|
| 52 | error=thread_info(threadP, THREAD_SCHED_INFO,
|
|---|
| 53 | (thread_info_t)&info, &info_count);
|
|---|
| 54 |
|
|---|
| 55 | if (error != KERN_SUCCESS)
|
|---|
| 56 | return -1;
|
|---|
| 57 |
|
|---|
| 58 | if (base != NULL)
|
|---|
| 59 | *base = info.base_priority;
|
|---|
| 60 |
|
|---|
| 61 | return info.max_priority;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /* Backend initialization functions */
|
|---|
| 65 |
|
|---|
| 66 | /* Initialize the threads subsystem. */
|
|---|
| 67 | int
|
|---|
| 68 | __objc_init_thread_system(void)
|
|---|
| 69 | {
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* Close the threads subsystem. */
|
|---|
| 74 | int
|
|---|
| 75 | __objc_close_thread_system(void)
|
|---|
| 76 | {
|
|---|
| 77 | return 0;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /* Backend thread functions */
|
|---|
| 81 |
|
|---|
| 82 | /* Create a new thread of execution. */
|
|---|
| 83 | objc_thread_t
|
|---|
| 84 | __objc_thread_detach(void (*func)(void *arg), void *arg)
|
|---|
| 85 | {
|
|---|
| 86 | objc_thread_t thread_id;
|
|---|
| 87 | cthread_t new_thread_handle;
|
|---|
| 88 |
|
|---|
| 89 | /* create thread */
|
|---|
| 90 | new_thread_handle = cthread_fork((cthread_fn_t)func, arg);
|
|---|
| 91 |
|
|---|
| 92 | if(new_thread_handle)
|
|---|
| 93 | {
|
|---|
| 94 | /* this is not terribly portable */
|
|---|
| 95 | thread_id = *(objc_thread_t *)&new_thread_handle;
|
|---|
| 96 | cthread_detach(new_thread_handle);
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | thread_id = NULL;
|
|---|
| 100 |
|
|---|
| 101 | return thread_id;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /* Set the current thread's priority. */
|
|---|
| 105 | int
|
|---|
| 106 | __objc_thread_set_priority(int priority)
|
|---|
| 107 | {
|
|---|
| 108 | objc_thread_t *t = objc_thread_id();
|
|---|
| 109 | cthread_t cT = (cthread_t) t;
|
|---|
| 110 | int maxPriority = __mach_get_max_thread_priority(cT, NULL);
|
|---|
| 111 | int sys_priority = 0;
|
|---|
| 112 |
|
|---|
| 113 | if (maxPriority == -1)
|
|---|
| 114 | return -1;
|
|---|
| 115 |
|
|---|
| 116 | switch (priority)
|
|---|
| 117 | {
|
|---|
| 118 | case OBJC_THREAD_INTERACTIVE_PRIORITY:
|
|---|
| 119 | sys_priority = maxPriority;
|
|---|
| 120 | break;
|
|---|
| 121 | case OBJC_THREAD_BACKGROUND_PRIORITY:
|
|---|
| 122 | sys_priority = (maxPriority * 2) / 3;
|
|---|
| 123 | break;
|
|---|
| 124 | case OBJC_THREAD_LOW_PRIORITY:
|
|---|
| 125 | sys_priority = maxPriority / 3;
|
|---|
| 126 | break;
|
|---|
| 127 | default:
|
|---|
| 128 | return -1;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if (sys_priority == 0)
|
|---|
| 132 | return -1;
|
|---|
| 133 |
|
|---|
| 134 | /* Change the priority */
|
|---|
| 135 | if (cthread_priority(cT, sys_priority, 0) == KERN_SUCCESS)
|
|---|
| 136 | return 0;
|
|---|
| 137 | else
|
|---|
| 138 | return -1;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /* Return the current thread's priority. */
|
|---|
| 142 | int
|
|---|
| 143 | __objc_thread_get_priority(void)
|
|---|
| 144 | {
|
|---|
| 145 | objc_thread_t *t = objc_thread_id();
|
|---|
| 146 | cthread_t cT = (cthread_t) t; /* see objc_thread_id() */
|
|---|
| 147 | int basePriority;
|
|---|
| 148 | int maxPriority;
|
|---|
| 149 | int sys_priority = 0;
|
|---|
| 150 |
|
|---|
| 151 | int interactiveT, backgroundT, lowT; /* thresholds */
|
|---|
| 152 |
|
|---|
| 153 | maxPriority = __mach_get_max_thread_priority(cT, &basePriority);
|
|---|
| 154 |
|
|---|
| 155 | if(maxPriority == -1)
|
|---|
| 156 | return -1;
|
|---|
| 157 |
|
|---|
| 158 | if (basePriority > ( (maxPriority * 2) / 3))
|
|---|
| 159 | return OBJC_THREAD_INTERACTIVE_PRIORITY;
|
|---|
| 160 |
|
|---|
| 161 | if (basePriority > ( maxPriority / 3))
|
|---|
| 162 | return OBJC_THREAD_BACKGROUND_PRIORITY;
|
|---|
|
|---|