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

信息检索与抽取

开发平台:

Unix_Linux

  1. /* GNU Objective C Runtime Thread Interface for POSIX compliant threads
  2.    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
  3.    Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
  4.    Modified for Linux/Pthreads by Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
  5. This file is part of GNU CC.
  6. GNU CC is free software; you can redistribute it and/or modify it under the
  7. terms of the GNU General Public License as published by the Free Software
  8. Foundation; either version 2, or (at your option) any later version.
  9. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  12. details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA.  */
  17. /* As a special exception, if you link this library with files compiled with
  18.    GCC to produce an executable, this does not cause the resulting executable
  19.    to be covered by the GNU General Public License. This exception does not
  20.    however invalidate any other reasons why the executable file might be
  21.    covered by the GNU General Public License.  */
  22. #include <objc/thr.h>
  23. #include "runtime.h"
  24. #include <pthread.h>
  25. /* Key structure for maintaining thread specific storage */
  26. static pthread_key_t _objc_thread_storage;
  27. /* Backend initialization functions */
  28. /* Initialize the threads subsystem. */
  29. int
  30. __objc_init_thread_system(void)
  31. {
  32.   /* Initialize the thread storage key */
  33.   return pthread_key_create(&_objc_thread_storage, NULL);
  34. }
  35. /* Close the threads subsystem. */
  36. int
  37. __objc_close_thread_system(void)
  38. {
  39.   return 0;
  40. }
  41. /* Backend thread functions */
  42. /* Create a new thread of execution. */
  43. objc_thread_t
  44. __objc_thread_detach(void (*func)(void *arg), void *arg)
  45. {
  46.   objc_thread_t thread_id;
  47.   pthread_t new_thread_handle;
  48.   if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
  49.       thread_id = *(objc_thread_t *)&new_thread_handle;
  50.   else
  51.     thread_id = NULL;
  52.   
  53.   return thread_id;
  54. }
  55. /* Set the current thread's priority. */
  56. int
  57. __objc_thread_set_priority(int priority)
  58. {
  59.   /* Not implemented yet */
  60.   return -1;
  61. }
  62. /* Return the current thread's priority. */
  63. int
  64. __objc_thread_get_priority(void)
  65. {
  66.   /* Not implemented yet */
  67.   return -1;
  68. }
  69. /* Yield our process time to another thread. */
  70. void
  71. __objc_thread_yield(void)
  72. {
  73.   sched_yield();
  74. }
  75. /* Terminate the current thread. */
  76. int
  77. __objc_thread_exit(void)
  78. {
  79.   /* exit the thread */
  80.   pthread_exit(&__objc_thread_exit_status);
  81.   /* Failed if we reached here */
  82.   return -1;
  83. }
  84. /* Returns an integer value which uniquely describes a thread. */
  85. objc_thread_t
  86. __objc_thread_id(void)
  87. {
  88.   pthread_t self = pthread_self();
  89.   return *(objc_thread_t *)&self;
  90. }
  91. /* Sets the thread's local storage pointer. */
  92. int
  93. __objc_thread_set_data(void *value)
  94. {
  95.   return pthread_setspecific(_objc_thread_storage, value);
  96. }
  97. /* Returns the thread's local storage pointer. */
  98. void *
  99. __objc_thread_get_data(void)
  100. {
  101.   return pthread_getspecific(_objc_thread_storage);
  102. }
  103. /* Backend mutex functions */
  104. /* Allocate a mutex. */
  105. int
  106. __objc_mutex_allocate(objc_mutex_t mutex)
  107. {
  108.   mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
  109.   if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
  110.     {
  111.       objc_free(mutex->backend);
  112.       mutex->backend = NULL;
  113.       return -1;
  114.     }
  115.   return 0;
  116. }
  117. /* Deallocate a mutex. */
  118. int
  119. __objc_mutex_deallocate(objc_mutex_t mutex)
  120. {
  121.   if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
  122.     return -1;
  123.   objc_free(mutex->backend);
  124.   mutex->backend = NULL;
  125.   return 0;
  126. }
  127. /* Grab a lock on a mutex. */
  128. int
  129. __objc_mutex_lock(objc_mutex_t mutex)
  130. {
  131.   return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
  132. }
  133. /* Try to grab a lock on a mutex. */
  134. int
  135. __objc_mutex_trylock(objc_mutex_t mutex)
  136. {
  137.   return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
  138. }
  139. /* Unlock the mutex */
  140. int
  141. __objc_mutex_unlock(objc_mutex_t mutex)
  142. {
  143.   return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
  144. }
  145. /* Backend condition mutex functions */
  146. /* Allocate a condition. */
  147. int
  148. __objc_condition_allocate(objc_condition_t condition)
  149. {
  150.   condition->backend = objc_malloc(sizeof(pthread_cond_t));
  151.   if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
  152.     {
  153.       objc_free(condition->backend);
  154.       condition->backend = NULL;
  155.       return -1;
  156.     }
  157.   return 0;
  158. }
  159. /* Deallocate a condition. */
  160. int
  161. __objc_condition_deallocate(objc_condition_t condition)
  162. {
  163.   if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
  164.     return -1;
  165.   objc_free(condition->backend);
  166.   condition->backend = NULL;
  167.   return 0;
  168. }
  169. /* Wait on the condition */
  170. int
  171. __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
  172. {
  173.   return pthread_cond_wait((pthread_cond_t *)condition->backend,
  174.    (pthread_mutex_t *)mutex->backend);
  175. }
  176. /* Wake up all threads waiting on this condition. */
  177. int
  178. __objc_condition_broadcast(objc_condition_t condition)
  179. {
  180.   return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
  181. }
  182. /* Wake up one thread waiting on this condition. */
  183. int
  184. __objc_condition_signal(objc_condition_t condition)
  185. {
  186.   return pthread_cond_signal((pthread_cond_t *)condition->backend);
  187. }
  188. /* End of File */