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.
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.
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
31 #define THREAD_STACK_SIZE (1*1024*1024) /* 1MB */
35 #define WIN32_LEAN_AND_MEAN
38 typedef struct althread_info
{
39 ALuint (*func
)(ALvoid
*);
44 static DWORD CALLBACK
StarterFunc(void *ptr
)
46 althread_info
*inf
= (althread_info
*)ptr
;
49 ret
= inf
->func(inf
->ptr
);
50 ExitThread((DWORD
)ret
);
56 ALboolean
StartThread(althread_t
*thread
, ALuint (*func
)(ALvoid
*), ALvoid
*ptr
)
61 info
= malloc(sizeof(*info
));
62 if(!info
) return AL_FALSE
;
67 info
->hdl
= CreateThread(NULL
, THREAD_STACK_SIZE
, StarterFunc
, info
, 0, &dummy
);
78 ALuint
StopThread(althread_t thread
)
82 WaitForSingleObject(thread
->hdl
, INFINITE
);
83 GetExitCodeThread(thread
->hdl
, &ret
);
84 CloseHandle(thread
->hdl
);
92 void SetThreadName(const char *name
)
95 #define MS_VC_EXCEPTION 0x406D1388
97 DWORD dwType
; // Must be 0x1000.
98 LPCSTR szName
; // Pointer to name (in user addr space).
99 DWORD dwThreadID
; // Thread ID (-1=caller thread).
100 DWORD dwFlags
; // Reserved for future use, must be zero.
102 info
.dwType
= 0x1000;
104 info
.dwThreadID
= -1;
108 RaiseException(MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(DWORD
), (ULONG_PTR
*)&info
);
110 __except(EXCEPTION_CONTINUE_EXECUTION
) {
112 #undef MS_VC_EXCEPTION
114 TRACE("Can't set thread %04lx name to \"%s\"\n", GetCurrentThreadId(), name
);
122 typedef struct althread_info
{
123 ALuint (*func
)(ALvoid
*);
129 static void *StarterFunc(void *ptr
)
131 althread_info
*inf
= (althread_info
*)ptr
;
132 inf
->ret
= inf
->func(inf
->ptr
);
137 ALboolean
StartThread(althread_t
*thread
, ALuint (*func
)(ALvoid
*), ALvoid
*ptr
)
142 info
= malloc(sizeof(*info
));
143 if(!info
) return AL_FALSE
;
145 if(pthread_attr_init(&attr
) != 0)
150 if(pthread_attr_setstacksize(&attr
, THREAD_STACK_SIZE
) != 0)
152 pthread_attr_destroy(&attr
);
159 if(pthread_create(&info
->hdl
, &attr
, StarterFunc
, info
) != 0)
161 pthread_attr_destroy(&attr
);
165 pthread_attr_destroy(&attr
);
171 ALuint
StopThread(althread_t thread
)
175 pthread_join(thread
->hdl
, NULL
);
184 void SetThreadName(const char *name
)
186 #if defined(HAVE_PTHREAD_SETNAME_NP)
187 #if defined(__GNUC__)
188 if(pthread_setname_np(pthread_self(), name
) != 0)
189 #elif defined(__APPLE__)
190 if(pthread_setname_np(name
) != 0)
192 WARN("Failed to set thread name to \"%s\": %s\n", name
, strerror(errno
));
193 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
194 pthread_set_name_np(pthread_self(), name
);
196 TRACE("Can't set thread name to \"%s\"\n", name
);