SDL_thread.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 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. slouken@libsdl.org
  17. */
  18. #ifndef _SDL_thread_h
  19. #define _SDL_thread_h
  20. /** @file SDL_thread.h
  21. * Header for the SDL thread management routines
  22. *
  23. * @note These are independent of the other SDL 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. extern "C" {
  33. #endif
  34. /** The SDL thread structure, defined in SDL_thread.c */
  35. struct SDL_Thread;
  36. typedef struct SDL_Thread SDL_Thread;
  37. /** Create a thread */
  38. #if ((defined(__WIN32__) && !defined(HAVE_LIBC)) || defined(__OS2__)) && !defined(__SYMBIAN32__)
  39. /**
  40. * We compile SDL into a DLL on OS/2. This means, that it's the DLL which
  41. * creates a new thread for the calling process with the SDL_CreateThread()
  42. * API. There is a problem with this, that only the RTL of the SDL.DLL will
  43. * be initialized for those threads, and not the RTL of the calling application!
  44. * To solve this, we make a little hack here.
  45. * We'll always use the caller's _beginthread() and _endthread() APIs to
  46. * start a new thread. This way, if it's the SDL.DLL which uses this API,
  47. * then the RTL of SDL.DLL will be used to create the new thread, and if it's
  48. * the application, then the RTL of the application will be used.
  49. * So, in short:
  50. * Always use the _beginthread() and _endthread() of the calling runtime library!
  51. */
  52. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  53. #ifndef _WIN32_WCE
  54. #include <process.h> /* This has _beginthread() and _endthread() defined! */
  55. #endif
  56. #ifdef __OS2__
  57. typedef int (*pfnSDL_CurrentBeginThread)(void (*func)(void *), void *, unsigned, void *arg);
  58. typedef void (*pfnSDL_CurrentEndThread)(void);
  59. #else
  60. typedef uintptr_t (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
  61. unsigned (__stdcall *func)(void *), void *arg,
  62. unsigned, unsigned *threadID);
  63. typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
  64. #endif
  65. extern DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread);
  66. #ifdef __OS2__
  67. #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthread, _endthread)
  68. #elif defined(_WIN32_WCE)
  69. #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, NULL, NULL)
  70. #else
  71. #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthreadex, _endthreadex)
  72. #endif
  73. #else
  74. extern DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data);
  75. #endif
  76. /** Get the 32-bit thread identifier for the current thread */
  77. extern DECLSPEC Uint32 SDLCALL SDL_ThreadID(void);
  78. /** Get the 32-bit thread identifier for the specified thread,
  79. * equivalent to SDL_ThreadID() if the specified thread is NULL.
  80. */
  81. extern DECLSPEC Uint32 SDLCALL SDL_GetThreadID(SDL_Thread *thread);
  82. /** Wait for a thread to finish.
  83. * The return code for the thread function is placed in the area
  84. * pointed to by 'status', if 'status' is not NULL.
  85. */
  86. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread *thread, int *status);
  87. /** Forcefully kill a thread without worrying about its state */
  88. extern DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread *thread);
  89. /* Ends C function definitions when using C++ */
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #include "close_code.h"
  94. #endif /* _SDL_thread_h */