You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. threads.h
  3. Portable threads and locks API
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2010, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under one of the following licenses:
  7. GPL, the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. gSOAP public license.
  10. The contents of this file are subject to the gSOAP Public License Version 1.3
  11. (the "License"); you may not use this file except in compliance with the
  12. License. You may obtain a copy of the License at
  13. http://www.cs.fsu.edu/~engelen/soaplicense.html
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the License.
  17. The Initial Developer of the Original Code is Robert A. van Engelen.
  18. Copyright (C) 2000-2010, Robert van Engelen, Genivia Inc., All Rights Reserved.
  19. --------------------------------------------------------------------------------
  20. GPL license.
  21. This program is free software; you can redistribute it and/or modify it under
  22. the terms of the GNU General Public License as published by the Free Software
  23. Foundation; either version 2 of the License, or (at your option) any later
  24. version.
  25. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  26. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  27. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License along with
  29. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  30. Place, Suite 330, Boston, MA 02111-1307 USA
  31. Author contact information:
  32. engelen@genivia.com / engelen@acm.org
  33. This program is released under the GPL with the additional exemption that
  34. compiling, linking, and/or using OpenSSL is allowed.
  35. --------------------------------------------------------------------------------
  36. A commercial use license is available from Genivia, Inc., contact@genivia.com
  37. --------------------------------------------------------------------------------
  38. */
  39. /**
  40. @page threads Portable threads and locking support
  41. The threads.h and threads.c code define the following portable API:
  42. - THREAD_TYPE portable thread type
  43. - THREAD_ID returns current thread ID of type THREAD_TYPE*
  44. - THREAD_CREATE(t,f,a) start a thread (THREAD_TYPE*)t that calls f(a)
  45. - THREAD_DETACH(t) detach thread (THREAD_TYPE*)t
  46. - THREAD_JOIN(t) wait to join (THREAD_TYPE*)t
  47. - THREAD_EXIT exit the current thread
  48. - THREAD_CANCEL(t) kill a thread, dangerous, use only in extreme cases!
  49. - MUTEX_TYPE portable mutex type
  50. - MUTEX_INITIALIZER global initializer value for static locks
  51. - MUTEX_SETUP(m) setup lock (MUTEX_TYPE*)m
  52. - MUTEX_CLEANUP(m) cleanup lock (MUTEX_TYPE*)m
  53. - MUTEX_LOCK(m) acquire lock (MUTEX_TYPE*)m
  54. - MUTEX_UNLOCK(m) release lock (MUTEX_TYPE*)m
  55. - COND_TYPE portable condition variable type
  56. - COND_SETUP(c) setup condition variable (COND_TYPE*)c
  57. - COND_CLEANUP(c) cleanup condition variable (COND_TYPE*)c
  58. - COND_SIGNAL(c) signal condition variable (COND_TYPE*)c
  59. - COND_WAIT(c,m) wait on variable (COND_TYPE*)c in mutex (MUTEX_TYPE*)m
  60. */
  61. #ifndef THREADS_H
  62. #define THREADS_H
  63. #ifndef WIN32
  64. # include <unistd.h> /* defines _POSIX_THREADS if pthreads are available */
  65. #else
  66. # define ssize_t int
  67. # include <io.h>
  68. # include <sys/types.h>
  69. # include <process.h>
  70. # include <windows.h>
  71. #endif
  72. #if defined(_POSIX_THREADS) || defined(_SC_THREADS)
  73. # include <pthread.h>
  74. #endif
  75. /******************************************************************************\
  76. *
  77. * Threads
  78. *
  79. \******************************************************************************/
  80. #if defined(WIN32)
  81. # define THREAD_TYPE HANDLE
  82. # define THREAD_ID GetCurrentThreadId()
  83. # define THREAD_CREATE(x,y,z) *(x) = (HANDLE)_beginthread((y), 8*4096, (z))
  84. # define THREAD_DETACH(x)
  85. # define THREAD_JOIN(x) WaitForSingleObject((x), INFINITE)
  86. # define THREAD_EXIT _endthread()
  87. # define THREAD_CANCEL(x) TerminateThread(x, 0)
  88. # define MUTEX_TYPE HANDLE
  89. # define MUTEX_INITIALIZER NULL
  90. # define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
  91. # define MUTEX_CLEANUP(x) (CloseHandle(x) == 0)
  92. # define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
  93. # define MUTEX_UNLOCK(x) (ReleaseMutex(x) == 0)
  94. # define COND_SETUP(x) emulate_pthread_cond_init(&(x))
  95. # define COND_CLEANUP(x) emulate_pthread_cond_destroy(&(x))
  96. # define COND_SIGNAL(x) emulate_pthread_cond_signal(&(x))
  97. # define COND_WAIT(x,y) emulate_pthread_cond_wait(&(x), &(y))
  98. typedef struct
  99. { UINT waiters_count_;
  100. CRITICAL_SECTION waiters_count_lock_;
  101. HANDLE signal_event_;
  102. } COND_TYPE;
  103. #ifdef __cplusplus
  104. extern "C" {
  105. #endif
  106. int emulate_pthread_mutex_lock(volatile MUTEX_TYPE*);
  107. int emulate_pthread_cond_init(COND_TYPE*);
  108. int emulate_pthread_cond_destroy(COND_TYPE*);
  109. int emulate_pthread_cond_signal(COND_TYPE*);
  110. int emulate_pthread_cond_wait(COND_TYPE*, MUTEX_TYPE*);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #elif defined(_POSIX_THREADS) || defined(_SC_THREADS)
  115. # define THREAD_TYPE pthread_t
  116. # define THREAD_ID pthread_self()
  117. # define THREAD_CREATE(x,y,z) pthread_create((x), NULL, (y), (z))
  118. # define THREAD_DETACH(x) pthread_detach((x))
  119. # define THREAD_JOIN(x) pthread_join((x), NULL)
  120. # define THREAD_EXIT pthread_exit(NULL)
  121. # define THREAD_CANCEL(x) pthread_cancel(x)
  122. # define MUTEX_TYPE pthread_mutex_t
  123. # define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  124. # define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
  125. # define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
  126. #if 0 /* 1: DEBUG MUTEX */
  127. # define MUTEX_LOCK(x) (fprintf(stderr, "! LOCK %p %s:%d\n", &x, __FILE__, __LINE__), pthread_mutex_lock(&(x)))
  128. # define MUTEX_UNLOCK(x) (fprintf(stderr, "! UNLOCK %p %s:%d\n", &x, __FILE__, __LINE__), pthread_mutex_unlock(&(x)))
  129. #else
  130. # define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
  131. # define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
  132. #endif
  133. # define COND_TYPE pthread_cond_t
  134. # define COND_SETUP(x) pthread_cond_init(&(x), NULL)
  135. # define COND_CLEANUP(x) pthread_cond_destroy(&(x))
  136. # define COND_SIGNAL(x) pthread_cond_signal(&(x))
  137. # define COND_WAIT(x,y) pthread_cond_wait(&(x), &(y))
  138. #else
  139. # error "No POSIX threads detected: we need thread and mutex operations. See for example OpenSSL /threads/th-lock.c on how to implement mutex on your platform"
  140. #endif
  141. #endif