SDL_thread.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2011 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. [email protected]
  17. */
  18. #ifndef _SDL_thread_h
  19. #define _SDL_thread_h
  20. /**
  21. * \file SDL_thread.h
  22. *
  23. * Header for the SDL thread management routines.
  24. */
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. /* Thread synchronization primitives */
  28. #include "SDL_mutex.h"
  29. #include "begin_code.h"
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. /* *INDENT-OFF* */
  33. extern "C" {
  34. /* *INDENT-ON* */
  35. #endif
  36. /* The SDL thread structure, defined in SDL_thread.c */
  37. struct SDL_Thread;
  38. typedef struct SDL_Thread SDL_Thread;
  39. /* The SDL thread ID */
  40. typedef unsigned long SDL_threadID;
  41. /* The function passed to SDL_CreateThread()
  42. It is passed a void* user context parameter and returns an int.
  43. */
  44. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  45. #if defined(__WIN32__) && !defined(HAVE_LIBC)
  46. /**
  47. * \file SDL_thread.h
  48. *
  49. * We compile SDL into a DLL. This means, that it's the DLL which
  50. * creates a new thread for the calling process with the SDL_CreateThread()
  51. * API. There is a problem with this, that only the RTL of the SDL.DLL will
  52. * be initialized for those threads, and not the RTL of the calling
  53. * application!
  54. *
  55. * To solve this, we make a little hack here.
  56. *
  57. * We'll always use the caller's _beginthread() and _endthread() APIs to
  58. * start a new thread. This way, if it's the SDL.DLL which uses this API,
  59. * then the RTL of SDL.DLL will be used to create the new thread, and if it's
  60. * the application, then the RTL of the application will be used.
  61. *
  62. * So, in short:
  63. * Always use the _beginthread() and _endthread() of the calling runtime
  64. * library!
  65. */
  66. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  67. #ifndef _WIN32_WCE
  68. #include <process.h> /* This has _beginthread() and _endthread() defined! */
  69. #endif
  70. #ifdef __GNUC__
  71. typedef unsigned long (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
  72. unsigned
  73. (__stdcall *
  74. func) (void *),
  75. void *arg,
  76. unsigned,
  77. unsigned
  78. *threadID);
  79. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  80. #else
  81. typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
  82. unsigned (__stdcall *
  83. func) (void
  84. *),
  85. void *arg, unsigned,
  86. unsigned *threadID);
  87. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  88. #endif
  89. /**
  90. * Create a thread.
  91. */
  92. extern DECLSPEC SDL_Thread *SDLCALL
  93. SDL_CreateThread(SDL_ThreadFunction fn, void *data,
  94. pfnSDL_CurrentBeginThread pfnBeginThread,
  95. pfnSDL_CurrentEndThread pfnEndThread);
  96. #if defined(_WIN32_WCE)
  97. /**
  98. * Create a thread.
  99. */
  100. #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, NULL, NULL)
  101. #else
  102. /**
  103. * Create a thread.
  104. */
  105. #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthreadex, _endthreadex)
  106. #endif
  107. #else
  108. /**
  109. * Create a thread.
  110. */
  111. extern DECLSPEC SDL_Thread *SDLCALL
  112. SDL_CreateThread(SDL_ThreadFunction fn, void *data);
  113. #endif
  114. /**
  115. * Get the thread identifier for the current thread.
  116. */
  117. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  118. /**
  119. * Get the thread identifier for the specified thread.
  120. *
  121. * Equivalent to SDL_ThreadID() if the specified thread is NULL.
  122. */
  123. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  124. /**
  125. * Wait for a thread to finish.
  126. *
  127. * The return code for the thread function is placed in the area
  128. * pointed to by \c status, if \c status is not NULL.
  129. */
  130. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  131. /* Ends C function definitions when using C++ */
  132. #ifdef __cplusplus
  133. /* *INDENT-OFF* */
  134. }
  135. /* *INDENT-ON* */
  136. #endif
  137. #include "close_code.h"
  138. #endif /* _SDL_thread_h */
  139. /* vi: set ts=4 sw=4 expandtab: */