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

信息检索与抽取

开发平台:

Unix_Linux

  1. /* GNU Objective C Runtime Thread Interface - Win32 Implementation
  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 <objc/thr.h>
  22. #include "runtime.h"
  23. #ifndef __OBJC__
  24. #define __OBJC__
  25. #endif
  26. #include <windows.h>
  27. /* Key structure for maintaining thread specific storage */
  28. static DWORD __objc_data_tls = (DWORD)-1;
  29. /* Backend initialization functions */
  30. /* Initialize the threads subsystem. */
  31. int
  32. __objc_init_thread_system(void)
  33. {
  34.   /* Initialize the thread storage key */
  35.   if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
  36.     return 0;
  37.   else
  38.     return -1;
  39. }
  40. /* Close the threads subsystem. */
  41. int
  42. __objc_close_thread_system(void)
  43. {
  44.   if (__objc_data_tls != (DWORD)-1)
  45.     TlsFree(__objc_data_tls);
  46.   return 0;
  47. }
  48. /* Backend thread functions */
  49. /* Create a new thread of execution. */
  50. objc_thread_t
  51. __objc_thread_detach(void (*func)(void *arg), void *arg)
  52. {
  53.   DWORD thread_id = 0;
  54.   HANDLE win32_handle;
  55.   if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
  56.                                    arg, 0, &thread_id)))
  57.     thread_id = 0;
  58.   
  59.   return (objc_thread_t)thread_id;
  60. }
  61. /* Set the current thread's priority. */
  62. int
  63. __objc_thread_set_priority(int priority)
  64. {
  65.   int sys_priority = 0;
  66.   switch (priority)
  67.     {
  68.     case OBJC_THREAD_INTERACTIVE_PRIORITY:
  69.       sys_priority = THREAD_PRIORITY_NORMAL;
  70.       break;
  71.     default:
  72.     case OBJC_THREAD_BACKGROUND_PRIORITY:
  73.       sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
  74.       break;
  75.     case OBJC_THREAD_LOW_PRIORITY:
  76.       sys_priority = THREAD_PRIORITY_LOWEST;
  77.       break;
  78.     }
  79.   /* Change priority */
  80.   if (SetThreadPriority(GetCurrentThread(), sys_priority))
  81.     return 0;
  82.   else
  83.     return -1;
  84. }
  85. /* Return the current thread's priority. */
  86. int
  87. __objc_thread_get_priority(void)
  88. {
  89.   int sys_priority;
  90.   sys_priority = GetThreadPriority(GetCurrentThread());
  91.   
  92.   switch (sys_priority)
  93.     {
  94.     case THREAD_PRIORITY_HIGHEST:
  95.     case THREAD_PRIORITY_TIME_CRITICAL:
  96.     case THREAD_PRIORITY_ABOVE_NORMAL:
  97.     case THREAD_PRIORITY_NORMAL:
  98.       return OBJC_THREAD_INTERACTIVE_PRIORITY;
  99.     default:
  100.     case THREAD_PRIORITY_BELOW_NORMAL:
  101.       return OBJC_THREAD_BACKGROUND_PRIORITY;
  102.     
  103.     case THREAD_PRIORITY_IDLE:
  104.     case THREAD_PRIORITY_LOWEST:
  105.       return OBJC_THREAD_LOW_PRIORITY;
  106.     }
  107.   /* Couldn't get priority. */
  108.   return -1;
  109. }
  110. /* Yield our process time to another thread. */
  111. void
  112. __objc_thread_yield(void)
  113. {
  114.   Sleep(0);
  115. }
  116. /* Terminate the current thread. */
  117. int
  118. __objc_thread_exit(void)
  119. {
  120.   /* exit the thread */
  121.   ExitThread(__objc_thread_exit_status);
  122.   /* Failed if we reached here */
  123.   return -1;
  124. }
  125. /* Returns an integer value which uniquely describes a thread. */
  126. objc_thread_t
  127. __objc_thread_id(void)
  128. {
  129.   return (objc_thread_t)GetCurrentThreadId();
  130. }
  131. /* Sets the thread's local storage pointer. */
  132. int
  133. __objc_thread_set_data(void *value)
  134. {
  135.   if (TlsSetValue(__objc_data_tls, value))
  136.     return 0;
  137.   else
  138.     return -1;
  139. }
  140. /* Returns the thread's local storage pointer. */
  141. void *
  142. __objc_thread_get_data(void)
  143. {
  144.   return TlsGetValue(__objc_data_tls);          /* Return thread data.      */
  145. }
  146. /* Backend mutex functions */
  147. /* Allocate a mutex. */
  148. int
  149. __objc_mutex_allocate(objc_mutex_t mutex)
  150. {
  151.   if ((mutex->backend = (void *)CreateMutex(NULL, 0, NULL)) == NULL)
  152.     return -1;
  153.   else
  154.     return 0;
  155. }
  156. /* Deallocate a mutex. */
  157. int
  158. __objc_mutex_deallocate(objc_mutex_t mutex)
  159. {
  160.   CloseHandle((HANDLE)(mutex->backend));
  161.   return 0;
  162. }
  163. /* Grab a lock on a mutex. */
  164. int
  165. __objc_mutex_lock(objc_mutex_t mutex)
  166. {
  167.   int status;
  168.   status = WaitForSingleObject((HANDLE)(mutex->backend), INFINITE);
  169.   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
  170.     return -1;
  171.   else
  172.     return 0;
  173. }
  174. /* Try to grab a lock on a mutex. */
  175. int
  176. __objc_mutex_trylock(objc_mutex_t mutex)
  177. {
  178.   int status;
  179.   status = WaitForSingleObject((HANDLE)(mutex->backend), 0);
  180.   if (status != WAIT_OBJECT_0 && status != WAIT_ABANDONED)
  181.     return -1;
  182.   else
  183.     return 0;
  184. }
  185. /* Unlock the mutex */
  186. int
  187. __objc_mutex_unlock(objc_mutex_t mutex)
  188. {
  189.   if (ReleaseMutex((HANDLE)(mutex->backend)) == 0)
  190.     return -1;
  191.   else
  192.     return 0;
  193. }
  194. /* Backend condition mutex functions */
  195. /* Allocate a condition. */
  196. int
  197. __objc_condition_allocate(objc_condition_t condition)
  198. {
  199.   /* Unimplemented. */
  200.   return -1;
  201. }
  202. /* Deallocate a condition. */
  203. int
  204. __objc_condition_deallocate(objc_condition_t condition)
  205. {
  206.   /* Unimplemented. */
  207.   return -1;
  208. }
  209. /* Wait on the condition */
  210. int
  211. __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
  212. {
  213.   /* Unimplemented. */
  214.   return -1;
  215. }
  216. /* Wake up all threads waiting on this condition. */
  217. int
  218. __objc_condition_broadcast(objc_condition_t condition)
  219. {
  220.   /* Unimplemented. */
  221.   return -1;
  222. }
  223. /* Wake up one thread waiting on this condition. */
  224. int
  225. __objc_condition_signal(objc_condition_t condition)
  226. {
  227.   /* Unimplemented. */
  228.   return -1;
  229. }
  230. /* End of File */