/src/unix/eio/xthread.h

http://github.com/joyent/libuv · C Header · 164 lines · 120 code · 33 blank · 11 comment · 7 complexity · 19b2da986b07922da5276a351dc77b2e MD5 · raw file

  1. #ifndef XTHREAD_H_
  2. #define XTHREAD_H_
  3. /* whether word reads are potentially non-atomic.
  4. * this is conservative, likely most arches this runs
  5. * on have atomic word read/writes.
  6. */
  7. #ifndef WORDACCESS_UNSAFE
  8. # if __i386 || __x86_64
  9. # define WORDACCESS_UNSAFE 0
  10. # else
  11. # define WORDACCESS_UNSAFE 1
  12. # endif
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. #ifdef _WIN32
  16. #include <stdio.h>//D
  17. #include <fcntl.h>
  18. #include <io.h>
  19. #include <time.h>
  20. #include <winsock2.h>
  21. #include <process.h>
  22. #include <windows.h>
  23. #include <pthread.h>
  24. #define sigset_t int
  25. #define sigfillset(a)
  26. #define pthread_sigmask(a,b,c)
  27. #define sigaddset(a,b)
  28. #define sigemptyset(s)
  29. typedef pthread_mutex_t xmutex_t;
  30. #define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
  31. #define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
  32. #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
  33. #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
  34. typedef pthread_cond_t xcond_t;
  35. #define X_COND_INIT PTHREAD_COND_INITIALIZER
  36. #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
  37. #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
  38. #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
  39. #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
  40. typedef pthread_t xthread_t;
  41. #define X_THREAD_PROC(name) void *name (void *thr_arg)
  42. #define X_THREAD_ATFORK(a,b,c)
  43. static int
  44. thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
  45. {
  46. int retval;
  47. pthread_attr_t attr;
  48. pthread_attr_init (&attr);
  49. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  50. retval = pthread_create (tid, &attr, proc, arg) == 0;
  51. pthread_attr_destroy (&attr);
  52. return retval;
  53. }
  54. #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)
  55. #define respipe_write(a,b,c) send ((a), (b), (c), 0)
  56. #define respipe_close(a) PerlSock_closesocket ((a))
  57. #else
  58. /////////////////////////////////////////////////////////////////////////////
  59. #if __linux && !defined(_GNU_SOURCE)
  60. # define _GNU_SOURCE
  61. #endif
  62. /* just in case */
  63. #define _REENTRANT 1
  64. #if __solaris
  65. # define _POSIX_PTHREAD_SEMANTICS 1
  66. /* try to bribe solaris headers into providing a current pthread API
  67. * despite environment being configured for an older version.
  68. */
  69. # define __EXTENSIONS__ 1
  70. #endif
  71. #include <unistd.h>
  72. #include <fcntl.h>
  73. #include <signal.h>
  74. #include <limits.h>
  75. #include <pthread.h>
  76. typedef pthread_mutex_t xmutex_t;
  77. #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
  78. # define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  79. # define X_MUTEX_CREATE(mutex) \
  80. do { \
  81. pthread_mutexattr_t attr; \
  82. pthread_mutexattr_init (&attr); \
  83. pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
  84. pthread_mutex_init (&(mutex), &attr); \
  85. } while (0)
  86. #else
  87. # define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
  88. # define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0)
  89. #endif
  90. #define X_LOCK(mutex) pthread_mutex_lock (&(mutex))
  91. #define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
  92. typedef pthread_cond_t xcond_t;
  93. #define X_COND_INIT PTHREAD_COND_INITIALIZER
  94. #define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0)
  95. #define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond))
  96. #define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex))
  97. #define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to))
  98. typedef pthread_t xthread_t;
  99. #define X_THREAD_PROC(name) static void *name (void *thr_arg)
  100. #define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)
  101. // the broken bsd's once more
  102. #ifndef PTHREAD_STACK_MIN
  103. # define PTHREAD_STACK_MIN 0
  104. #endif
  105. #ifndef XTHREAD_STACKSIZE
  106. # define XTHREAD_STACKSIZE sizeof (void *) * 4096
  107. #endif
  108. static int
  109. thread_create (xthread_t *tid, void *(*proc)(void *), void *arg)
  110. {
  111. int retval;
  112. sigset_t fullsigset, oldsigset;
  113. pthread_attr_t attr;
  114. pthread_attr_init (&attr);
  115. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  116. pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
  117. #ifdef PTHREAD_SCOPE_PROCESS
  118. pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
  119. #endif
  120. sigfillset (&fullsigset);
  121. pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
  122. retval = pthread_create (tid, &attr, proc, arg) == 0;
  123. pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
  124. pthread_attr_destroy (&attr);
  125. return retval;
  126. }
  127. #define respipe_read(a,b,c) read ((a), (b), (c))
  128. #define respipe_write(a,b,c) write ((a), (b), (c))
  129. #define respipe_close(a) close ((a))
  130. #endif
  131. #endif