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

信息检索与抽取

开发平台:

Unix_Linux

  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.    Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
  5.       
  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 along with
  15. GNU CC; see the file COPYING.  If not, write to the Free Software
  16. 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 <objc/thr.h>
  24. #include "runtime.h"
  25. #include <thread.h>
  26. #include <synch.h>
  27. #include <errno.h>
  28. /* Key structure for maintaining thread specific storage */
  29. static thread_key_t     __objc_thread_data_key;
  30. /* Backend initialization functions */
  31. /* Initialize the threads subsystem. */
  32. int
  33. __objc_init_thread_system(void)
  34. {
  35.   /* Initialize the thread storage key */
  36.   if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
  37.     return 0;
  38.   else
  39.     return -1;
  40. }
  41. /* Close the threads subsystem. */
  42. int
  43. __objc_close_thread_system(void)
  44. {
  45.   return 0;
  46. }
  47. /* Backend thread functions */
  48. /* Create a new thread of execution. */
  49. objc_thread_t
  50. __objc_thread_detach(void (*func)(void *arg), void *arg)
  51. {
  52.   objc_thread_t thread_id;
  53.   thread_t new_thread_id = 0;
  54.   if (thr_create(NULL, 0, (void *)func, arg,
  55.                  THR_DETACHED | THR_NEW_LWP,
  56.                  &new_thread_id) == 0)
  57.     thread_id = *(objc_thread_t *)&new_thread_id;
  58.   else
  59.     thread_id = NULL;
  60.   
  61.   return thread_id;
  62. }
  63. /* Set the current thread's priority. */
  64. int
  65. __objc_thread_set_priority(int priority)
  66. {
  67.   int sys_priority = 0;
  68.   switch (priority)
  69.     {
  70.     case OBJC_THREAD_INTERACTIVE_PRIORITY:
  71.       sys_priority = 300;
  72.       break;
  73.     default:
  74.     case OBJC_THREAD_BACKGROUND_PRIORITY:
  75.       sys_priority = 200;
  76.       break;
  77.     case OBJC_THREAD_LOW_PRIORITY:
  78.       sys_priority = 1000;
  79.       break;
  80.     }
  81.   /* Change priority */
  82.   if (thr_setprio(thr_self(), sys_priority) == 0)
  83.     return 0;
  84.   else
  85.     return -1;
  86. }
  87. /* Return the current thread's priority. */
  88. int
  89. __objc_thread_get_priority(void)
  90. {
  91.   int sys_priority;
  92.                                                    
  93.   if (thr_getprio(thr_self(), &sys_priority) == 0)
  94.     {
  95.       if (sys_priority >= 250)
  96. return OBJC_THREAD_INTERACTIVE_PRIORITY;
  97.       else if (sys_priority >= 150)
  98. return OBJC_THREAD_BACKGROUND_PRIORITY;
  99.       return OBJC_THREAD_LOW_PRIORITY;
  100.     }
  101.   /* Couldn't get priority. */
  102.   return -1;
  103. }
  104. /* Yield our process time to another thread. */
  105. void
  106. __objc_thread_yield(void)
  107. {
  108.   thr_yield();
  109. }
  110. /* Terminate the current thread. */
  111. int
  112. __objc_thread_exit(void)
  113. {
  114.   /* exit the thread */
  115.   thr_exit(&__objc_thread_exit_status);
  116.   /* Failed if we reached here */
  117.   return -1;
  118. }
  119. /* Returns an integer value which uniquely describes a thread. */
  120. objc_thread_t
  121. __objc_thread_id(void)
  122. {
  123.   return (objc_thread_t)thr_self();
  124. }
  125. /* Sets the thread's local storage pointer. */
  126. int
  127. __objc_thread_set_data(void *value)
  128. {
  129.   if (thr_setspecific(__objc_thread_data_key, value) == 0)
  130.     return 0;
  131.   else
  132.     return -1;
  133. }
  134. /* Returns the thread's local storage pointer. */
  135. void *
  136. __objc_thread_get_data(void)
  137. {
  138.   void *value = NULL;
  139.   if (thr_getspecific(__objc_thread_data_key, &value) == 0)
  140.     return value;
  141.   return NULL;
  142. }
  143. /* Backend mutex functions */
  144. /* Allocate a mutex. */
  145. int
  146. __objc_mutex_allocate(objc_mutex_t mutex)
  147. {
  148.   if (mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
  149.     return -1;
  150.   else
  151.     return 0;
  152. }
  153. /* Deallocate a mutex. */
  154. int
  155. __objc_mutex_deallocate(objc_mutex_t mutex)
  156. {
  157.   mutex_destroy((mutex_t *)(&(mutex->backend)));
  158.   return 0;
  159. }
  160. /* Grab a lock on a mutex. */
  161. int
  162. __objc_mutex_lock(objc_mutex_t mutex)
  163. {
  164.   if (mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
  165.     return -1;
  166.   else
  167.     return 0;
  168. }
  169. /* Try to grab a lock on a mutex. */
  170. int
  171. __objc_mutex_trylock(objc_mutex_t mutex)
  172. {
  173.   if (mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
  174.     return -1;
  175.   else
  176.     return 0;
  177. }
  178. /* Unlock the mutex */
  179. int
  180. __objc_mutex_unlock(objc_mutex_t mutex)
  181. {
  182.   if (mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
  183.     return -1;
  184.   else
  185.     return 0;
  186. }
  187. /* Backend condition mutex functions */
  188. /* Allocate a condition. */
  189. int
  190. __objc_condition_allocate(objc_condition_t condition)
  191. {
  192.   return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD, NULL);
  193. }
  194. /* Deallocate a condition. */
  195. int
  196. __objc_condition_deallocate(objc_condition_t condition)
  197. {
  198.   return cond_destroy((cond_t *)(&(condition->backend)));
  199. }
  200. /* Wait on the condition */
  201. int
  202. __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
  203. {
  204.   return cond_wait((cond_t *)(&(condition->backend)),
  205.    (mutex_t *)(&(mutex->backend)));
  206. }
  207. /* Wake up all threads waiting on this condition. */
  208. int
  209. __objc_condition_broadcast(objc_condition_t condition)
  210. {
  211.   return cond_broadcast((cond_t *)(&(condition->backend)));
  212. }
  213. /* Wake up one thread waiting on this condition. */
  214. int
  215. __objc_condition_signal(objc_condition_t condition)
  216. {
  217.   return cond_signal((cond_t *)(&(condition->backend)));
  218. }
  219. /* End of File */