os0thread.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. The interface to the operating system
  3. process and thread control primitives
  4. (c) 1995 Innobase Oy
  5. Created 9/8/1995 Heikki Tuuri
  6. *******************************************************/
  7. #include "os0thread.h"
  8. #ifdef UNIV_NONINL
  9. #include "os0thread.ic"
  10. #endif
  11. #ifdef __WIN__
  12. #include <windows.h>
  13. #endif
  14. /*********************************************************************
  15. Returns the thread identifier of current thread. */
  16. os_thread_id_t
  17. os_thread_get_curr_id(void)
  18. /*=======================*/
  19. {
  20. #ifdef __WIN__
  21. return(GetCurrentThreadId());
  22. #else
  23. pthread_t    pthr;
  24. pthr = pthread_self();
  25. /* TODO: in the future we have to change os_thread_id
  26.    to pthread_t; the following cast may work in a wrong way on some
  27.    systems if pthread_t is a struct; this is just a quick fix
  28.    for HP-UX to eliminate a compiler warning */
  29. return(*(os_thread_id_t*)((void*) (&pthr)));
  30. #endif
  31. }
  32. /********************************************************************
  33. Creates a new thread of execution. The execution starts from
  34. the function given. The start function takes a void* parameter
  35. and returns an ulint. */
  36. os_thread_t
  37. os_thread_create(
  38. /*=============*/
  39. /* out: handle to the thread */
  40. #ifndef __WIN__
  41.  os_posix_f_t            start_f,
  42. #else
  43. ulint (*start_f)(void*), /* in: pointer to function
  44. from which to start */
  45. #endif
  46. void* arg, /* in: argument to start
  47. function */
  48. os_thread_id_t* thread_id) /* out: id of created
  49. thread */
  50. {
  51. #ifdef __WIN__
  52. os_thread_t thread;
  53. thread = CreateThread(NULL, /* no security attributes */
  54. 0, /* default size stack */
  55. (LPTHREAD_START_ROUTINE)start_f,
  56. arg,
  57. 0, /* thread runs immediately */
  58. thread_id);
  59. ut_a(thread);
  60. return(thread);
  61. #else
  62. int ret;
  63. os_thread_t pthread;
  64. pthread_attr_t  attr;
  65.         pthread_attr_init(&attr);
  66. ret = pthread_create(&pthread, NULL, start_f, arg);
  67. pthread_attr_destroy(&attr);
  68. return(pthread);
  69. #endif
  70. }
  71. /*********************************************************************
  72. Returns handle to the current thread. */
  73. os_thread_t
  74. os_thread_get_curr(void)
  75. /*=======================*/
  76. {
  77. #ifdef __WIN__
  78. return(GetCurrentThread());
  79. #else
  80. return(pthread_self());
  81. #endif
  82. }
  83. /*********************************************************************
  84. Converts a thread id to a ulint. */
  85. ulint
  86. os_thread_conv_id_to_ulint(
  87. /*=======================*/
  88. /* out: converted to ulint */
  89. os_thread_id_t id) /* in: thread id */
  90. {
  91. return((ulint)id);
  92. }
  93. /*********************************************************************
  94. Advises the os to give up remainder of the thread's time slice. */
  95. void
  96. os_thread_yield(void)
  97. /*=================*/
  98. {
  99. #ifdef __WIN__
  100. Sleep(0);
  101. #else
  102. os_thread_sleep(0);
  103. #endif
  104. }
  105. /*********************************************************************
  106. The thread sleeps at least the time given in microseconds. */
  107. void
  108. os_thread_sleep(
  109. /*============*/
  110. ulint tm) /* in: time in microseconds */
  111. {
  112. #ifdef __WIN__
  113. Sleep(tm / 1000);
  114. #else
  115. struct timeval t;
  116. t.tv_sec = 0;
  117. t.tv_usec = tm;
  118. select(0, NULL, NULL, NULL, &t);
  119. #endif
  120. }
  121. /**********************************************************************
  122. Sets a thread priority. */
  123. void
  124. os_thread_set_priority(
  125. /*===================*/
  126. os_thread_t handle, /* in: OS handle to the thread */
  127. ulint pri) /* in: priority */
  128. {
  129. #ifdef __WIN__
  130. int os_pri;
  131. if (pri == OS_THREAD_PRIORITY_BACKGROUND) {
  132. os_pri = THREAD_PRIORITY_BELOW_NORMAL;
  133. } else if (pri == OS_THREAD_PRIORITY_NORMAL) {
  134. os_pri = THREAD_PRIORITY_NORMAL;
  135. } else if (pri == OS_THREAD_PRIORITY_ABOVE_NORMAL) {
  136. os_pri = THREAD_PRIORITY_HIGHEST;
  137. } else {
  138. ut_error;
  139. }
  140. ut_a(SetThreadPriority(handle, os_pri));
  141. #else
  142. UT_NOT_USED(handle);
  143. UT_NOT_USED(pri);
  144. #endif
  145. }
  146. /**********************************************************************
  147. Gets a thread priority. */
  148. ulint
  149. os_thread_get_priority(
  150. /*===================*/
  151. /* out: priority */
  152. os_thread_t handle) /* in: OS handle to the thread */
  153. {
  154. #ifdef __WIN__
  155. int os_pri;
  156. ulint pri;
  157. os_pri = GetThreadPriority(handle);
  158. if (os_pri == THREAD_PRIORITY_BELOW_NORMAL) {
  159. pri = OS_THREAD_PRIORITY_BACKGROUND;
  160. } else if (os_pri == THREAD_PRIORITY_NORMAL) {
  161. pri = OS_THREAD_PRIORITY_NORMAL;
  162. } else if (os_pri == THREAD_PRIORITY_HIGHEST) {
  163. pri = OS_THREAD_PRIORITY_ABOVE_NORMAL;
  164. } else {
  165. ut_error;
  166. }
  167. return(pri);
  168. #else
  169. return(0);
  170. #endif
  171. }
  172. /**********************************************************************
  173. Gets the last operating system error code for the calling thread. */
  174. ulint
  175. os_thread_get_last_error(void)
  176. /*==========================*/
  177. {
  178. #ifdef __WIN__
  179. return(GetLastError());
  180. #else
  181. return(0);
  182. #endif
  183. }