alcThread.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "alThunk.h"
  24. #ifdef _WIN32
  25. typedef struct {
  26. ALuint (*func)(ALvoid*);
  27. ALvoid *ptr;
  28. HANDLE thread;
  29. } ThreadInfo;
  30. static DWORD CALLBACK StarterFunc(void *ptr)
  31. {
  32. ThreadInfo *inf = (ThreadInfo*)ptr;
  33. ALint ret;
  34. ret = inf->func(inf->ptr);
  35. ExitThread((DWORD)ret);
  36. return (DWORD)ret;
  37. }
  38. ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
  39. {
  40. DWORD dummy;
  41. ThreadInfo *inf = malloc(sizeof(ThreadInfo));
  42. if(!inf) return 0;
  43. inf->func = func;
  44. inf->ptr = ptr;
  45. inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, &dummy);
  46. if(!inf->thread)
  47. {
  48. free(inf);
  49. return NULL;
  50. }
  51. return inf;
  52. }
  53. ALuint StopThread(ALvoid *thread)
  54. {
  55. ThreadInfo *inf = thread;
  56. DWORD ret = 0;
  57. WaitForSingleObject(inf->thread, INFINITE);
  58. GetExitCodeThread(inf->thread, &ret);
  59. CloseHandle(inf->thread);
  60. free(inf);
  61. return (ALuint)ret;
  62. }
  63. #else
  64. #include <pthread.h>
  65. typedef struct {
  66. ALuint (*func)(ALvoid*);
  67. ALvoid *ptr;
  68. ALuint ret;
  69. pthread_t thread;
  70. } ThreadInfo;
  71. static void *StarterFunc(void *ptr)
  72. {
  73. ThreadInfo *inf = (ThreadInfo*)ptr;
  74. inf->ret = inf->func(inf->ptr);
  75. return NULL;
  76. }
  77. ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
  78. {
  79. ThreadInfo *inf = malloc(sizeof(ThreadInfo));
  80. if(!inf) return NULL;
  81. inf->func = func;
  82. inf->ptr = ptr;
  83. if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0)
  84. {
  85. free(inf);
  86. return NULL;
  87. }
  88. return inf;
  89. }
  90. ALuint StopThread(ALvoid *thread)
  91. {
  92. ThreadInfo *inf = thread;
  93. ALuint ret;
  94. pthread_join(inf->thread, NULL);
  95. ret = inf->ret;
  96. free(inf);
  97. return ret;
  98. }
  99. #endif