1 /* GNU Objective C Runtime Thread Implementation
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
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>
7 This file is part of GNU CC.
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.
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
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. */
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. */
29 #include <mach/mach.h>
30 #include <mach/cthreads.h>
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 priviledges. Once lowered, it cannot be raised.
40 static int __mach_get_max_thread_priority(cthread_t t
, int *base
)
44 struct thread_sched_info info
;
45 unsigned int info_count
=THREAD_SCHED_INFO_COUNT
;
50 threadP
= cthread_thread(t
); /* get thread underlying */
52 error
=thread_info(threadP
, THREAD_SCHED_INFO
,
53 (thread_info_t
)&info
, &info_count
);
55 if (error
!= KERN_SUCCESS
)
59 *base
= info
.base_priority
;
61 return info
.max_priority
;
64 /* Backend initialization functions */
66 /* Initialize the threads subsystem. */
68 __objc_init_thread_system(void)
73 /* Close the threads subsystem. */
75 __objc_close_thread_system(void)
80 /* Backend thread functions */
82 /* Create a new thread of execution. */
84 __objc_thread_detach(void (*func
)(void *arg
), void *arg
)
86 objc_thread_t thread_id
;
87 cthread_t new_thread_handle
;
90 new_thread_handle
= cthread_fork((cthread_fn_t
)func
, arg
);
94 /* this is not terribly portable */
95 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
96 cthread_detach(new_thread_handle
);
104 /* Set the current thread's priority. */
106 __objc_thread_set_priority(int priority
)
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;
113 if (maxPriority
== -1)
118 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
119 sys_priority
= maxPriority
;
121 case OBJC_THREAD_BACKGROUND_PRIORITY
:
122 sys_priority
= (maxPriority
* 2) / 3;
124 case OBJC_THREAD_LOW_PRIORITY
:
125 sys_priority
= maxPriority
/ 3;
131 if (sys_priority
== 0)
134 /* Change the priority */
135 if (cthread_priority(cT
, sys_priority
, 0) == KERN_SUCCESS
)
141 /* Return the current thread's priority. */
143 __objc_thread_get_priority(void)
145 objc_thread_t
*t
= objc_thread_id();
146 cthread_t cT
= (cthread_t
) t
; /* see objc_thread_id() */
149 int sys_priority
= 0;
151 int interactiveT
, backgroundT
, lowT
; /* threasholds */
153 maxPriority
= __mach_get_max_thread_priority(cT
, &basePriority
);
155 if(maxPriority
== -1)
158 if (basePriority
> ( (maxPriority
* 2) / 3))
159 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
161 if (basePriority
> ( maxPriority
/ 3))
162 return OBJC_THREAD_BACKGROUND_PRIORITY
;
164 return OBJC_THREAD_LOW_PRIORITY
;
167 /* Yield our process time to another thread. */
169 __objc_thread_yield(void)
174 /* Terminate the current thread. */
176 __objc_thread_exit(void)
178 /* exit the thread */
179 cthread_exit(&__objc_thread_exit_status
);
181 /* Failed if we reached here */
185 /* Returns an integer value which uniquely describes a thread. */
187 __objc_thread_id(void)
189 cthread_t self
= cthread_self();
191 return *(objc_thread_t
*)&self
;
194 /* Sets the thread's local storage pointer. */
196 __objc_thread_set_data(void *value
)
198 cthread_set_data(cthread_self(), (any_t
) value
);
202 /* Returns the thread's local storage pointer. */
204 __objc_thread_get_data(void)
206 return (void *) cthread_data(cthread_self());
209 /* Backend mutex functions */
211 /* Allocate a mutex. */
213 __objc_mutex_allocate(objc_mutex_t mutex
)
216 mutex
->backend
= objc_malloc(sizeof(struct mutex
));
218 err
= mutex_init((mutex_t
)(mutex
->backend
));
222 objc_free(mutex
->backend
);
229 /* Deallocate a mutex. */
231 __objc_mutex_deallocate(objc_mutex_t mutex
)
233 mutex_clear((mutex_t
)(mutex
->backend
));
235 objc_free(mutex
->backend
);
236 mutex
->backend
= NULL
;
240 /* Grab a lock on a mutex. */
242 __objc_mutex_lock(objc_mutex_t mutex
)
244 mutex_lock((mutex_t
)(mutex
->backend
));
248 /* Try to grab a lock on a mutex. */
250 __objc_mutex_trylock(objc_mutex_t mutex
)
252 if (mutex_try_lock((mutex_t
)(mutex
->backend
)) == 0)
258 /* Unlock the mutex */
260 __objc_mutex_unlock(objc_mutex_t mutex
)
262 mutex_unlock((mutex_t
)(mutex
->backend
));
266 /* Backend condition mutex functions */
268 /* Allocate a condition. */
270 __objc_condition_allocate(objc_condition_t condition
)
272 condition
->backend
= objc_malloc(sizeof(struct condition
));
273 condition_init((condition_t
)(condition
->backend
));
277 /* Deallocate a condition. */
279 __objc_condition_deallocate(objc_condition_t condition
)
281 condition_clear((condition_t
)(condition
->backend
));
282 objc_free(condition
->backend
);
283 condition
->backend
= NULL
;
287 /* Wait on the condition */
289 __objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
291 condition_wait((condition_t
)(condition
->backend
),
292 (mutex_t
)(mutex
->backend
));
296 /* Wake up all threads waiting on this condition. */
298 __objc_condition_broadcast(objc_condition_t condition
)
300 condition_broadcast((condition_t
)(condition
->backend
));
304 /* Wake up one thread waiting on this condition. */
306 __objc_condition_signal(objc_condition_t condition
)
308 condition_signal((condition_t
)(condition
->backend
));