zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
alcThread.c
Go to the documentation of this file.
1 
21 #include "config.h"
22 
23 #include <stdlib.h>
24 
25 #include "alMain.h"
26 #include "alThunk.h"
27 
28 #define THREAD_STACK_SIZE (1*1024*1024) /* 1MB */
29 
30 #ifdef _WIN32
31 
32 typedef struct {
33  ALuint (*func)(ALvoid*);
34  ALvoid *ptr;
35  HANDLE thread;
36 } ThreadInfo;
37 
38 static DWORD CALLBACK StarterFunc(void *ptr)
39 {
40  ThreadInfo *inf = (ThreadInfo*)ptr;
41  ALint ret;
42 
43  ret = inf->func(inf->ptr);
44  ExitThread((DWORD)ret);
45 
46  return (DWORD)ret;
47 }
48 
50 {
51  DWORD dummy;
52  ThreadInfo *inf = malloc(sizeof(ThreadInfo));
53  if(!inf) return 0;
54 
55  inf->func = func;
56  inf->ptr = ptr;
57 
58  inf->thread = CreateThread(NULL, THREAD_STACK_SIZE, StarterFunc, inf, 0, &dummy);
59  if(!inf->thread)
60  {
61  free(inf);
62  return NULL;
63  }
64 
65  return inf;
66 }
67 
69 {
70  ThreadInfo *inf = thread;
71  DWORD ret = 0;
72 
73  WaitForSingleObject(inf->thread, INFINITE);
74  GetExitCodeThread(inf->thread, &ret);
75  CloseHandle(inf->thread);
76 
77  free(inf);
78 
79  return (ALuint)ret;
80 }
81 
82 #else
83 
84 #include <pthread.h>
85 
86 typedef struct {
87  ALuint (*func)(ALvoid*);
88  ALvoid *ptr;
89  ALuint ret;
90  pthread_t thread;
91 } ThreadInfo;
92 
93 static void *StarterFunc(void *ptr)
94 {
95  ThreadInfo *inf = (ThreadInfo*)ptr;
96  inf->ret = inf->func(inf->ptr);
97  return NULL;
98 }
99 
101 {
102  pthread_attr_t attr;
103  ThreadInfo *inf = malloc(sizeof(ThreadInfo));
104  if(!inf) return NULL;
105 
106  if(pthread_attr_init(&attr) != 0)
107  {
108  free(inf);
109  return NULL;
110  }
111  if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
112  {
113  pthread_attr_destroy(&attr);
114  free(inf);
115  return NULL;
116  }
117 
118  inf->func = func;
119  inf->ptr = ptr;
120  if(pthread_create(&inf->thread, &attr, StarterFunc, inf) != 0)
121  {
122  pthread_attr_destroy(&attr);
123  free(inf);
124  return NULL;
125  }
126  pthread_attr_destroy(&attr);
127 
128  return inf;
129 }
130 
132 {
133  ThreadInfo *inf = thread;
134  ALuint ret;
135 
136  pthread_join(inf->thread, NULL);
137  ret = inf->ret;
138 
139  free(inf);
140 
141  return ret;
142 }
143 
144 #endif
void ALvoid
Definition: al.h:74
#define NULL
Definition: ftobjs.h:61
static void * StarterFunc(void *ptr)
Definition: alcThread.c:93
SDL_EventEntry * free
Definition: SDL_events.c:80
int ALint
Definition: al.h:56
#define THREAD_STACK_SIZE
Definition: alcThread.c:28
GLenum func
Definition: SDL_opengl.h:5654
ret
Definition: glew_str_glx.c:2
typedef HANDLE(WINAPI *PFNWGLCREATEBUFFERREGIONARBPROC)(HDC hDC
ALuint StopThread(ALvoid *thread)
Definition: alcThread.c:131
unsigned int ALuint
Definition: al.h:59
static SDL_Thread * thread
#define malloc
Definition: SDL_malloc.c:635
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
ALvoid * StartThread(ALuint(*func)(ALvoid *), ALvoid *ptr)
Definition: alcThread.c:100