thr-mach.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:7k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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