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