Added SDL_DetachThread() API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
diff --git a/include/SDL_thread.h b/include/SDL_thread.h
index f248c3d..42df3d6 100644
--- a/include/SDL_thread.h
+++ b/include/SDL_thread.h
@@ -165,14 +165,54 @@ extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
/**
- * Wait for a thread to finish.
+ * Wait for a thread to finish. Threads that haven't been detached will
+ * remain (as a "zombie") until this function cleans them up. Not doing so
+ * is a resource leak.
+ *
+ * Once a thread has been cleaned up through this function, the SDL_Thread
+ * that references it becomes invalid and should not be referenced again.
+ * As such, only one thread may call SDL_WaitThread() on another.
*
* The return code for the thread function is placed in the area
* pointed to by \c status, if \c status is not NULL.
+ *
+ * You may not wait on a thread that has been used in a call to
+ * SDL_DetachThread(). Use either that function or this one, but not
+ * both, or behavior is undefined.
+ *
+ * It is safe to pass NULL to this function; it is a no-op.
*/
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
/**
+ * A thread may be "detached" to signify that it should not remain until
+ * another thread has called SDL_WaitThread() on it. Detaching a thread
+ * is useful for long-running threads that nothing needs to synchronize
+ * with or further manage. When a detached thread is done, it simply
+ * goes away.
+ *
+ * There is no way to recover the return code of a detached thread. If you
+ * need this, don't detach the thread and instead use SDL_WaitThread().
+ *
+ * Once a thread is detached, you should usually assume the SDL_Thread isn't
+ * safe to reference again, as it will become invalid immediately upon
+ * the detached thread's exit, instead of remaining until someone has called
+ * SDL_WaitThread() to finally clean it up. As such, don't detach the same
+ * thread more than once.
+ *
+ * If a thread has already exited when passed to SDL_DetachThread(), it will
+ * stop waiting for a call to SDL_WaitThread() and clean up immediately.
+ * It is not safe to detach a thread that might be used with SDL_WaitThread().
+ *
+ * You may not call SDL_WaitThread() on a thread that has been detached.
+ * Use either that function or this one, but not both, or behavior is
+ * undefined.
+ *
+ * It is safe to pass NULL to this function; it is a no-op.
+ */
+extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
+
+/**
* \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
*
* \return The newly created thread local storage identifier, or 0 on error
diff --git a/src/thread/SDL_systhread.h b/src/thread/SDL_systhread.h
index 36898f3..3c8ba89 100644
--- a/src/thread/SDL_systhread.h
+++ b/src/thread/SDL_systhread.h
@@ -51,6 +51,9 @@ extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
*/
extern void SDL_SYS_WaitThread(SDL_Thread * thread);
+/* Mark thread as cleaned up as soon as it exits, without joining. */
+extern void SDL_SYS_DetachThread(SDL_Thread * thread);
+
/* Get the thread local storage for this thread */
extern SDL_TLSData *SDL_SYS_GetTLSData();
diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c
index 4b070da..362e75a 100644
--- a/src/thread/SDL_thread.c
+++ b/src/thread/SDL_thread.c
@@ -22,6 +22,7 @@
/* System independent thread management routines for SDL */
+#include "SDL_assert.h"
#include "SDL_thread.h"
#include "SDL_thread_c.h"
#include "SDL_systhread.h"
@@ -265,13 +266,14 @@ SDL_RunThread(void *data)
thread_args *args = (thread_args *) data;
int (SDLCALL * userfunc) (void *) = args->func;
void *userdata = args->data;
- int *statusloc = &args->info->status;
+ SDL_Thread *thread = args->info;
+ int *statusloc = &thread->status;
/* Perform any system-dependent setup - this function may not fail */
- SDL_SYS_SetupThread(args->info->name);
+ SDL_SYS_SetupThread(thread->name);
/* Get the thread id */
- args->info->threadid = SDL_ThreadID();
+ thread->threadid = SDL_ThreadID();
/* Wake up the parent thread */
SDL_SemPost(args->wait);
@@ -281,6 +283,17 @@ SDL_RunThread(void *data)
/* Clean up thread-local storage */
SDL_TLSCleanup();
+
+ /* Mark us as ready to be joined (or detached) */
+ if (!SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_ZOMBIE)) {
+ /* Clean up if something already detached us. */
+ if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_CLEANED)) {
+ if (thread->name) {
+ SDL_free(thread->name);
+ }
+ SDL_free(thread);
+ }
+ }
}
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
@@ -306,8 +319,9 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
SDL_OutOfMemory();
return (NULL);
}
- SDL_memset(thread, 0, (sizeof *thread));
+ SDL_zerop(thread);
thread->status = -1;
+ SDL_AtomicSet(&thread->state, SDL_THREAD_STATE_ALIVE);
/* Set up the arguments for the thread */
if (name != NULL) {
@@ -410,4 +424,27 @@ SDL_WaitThread(SDL_Thread * thread, int *status)
}
}
+void
+SDL_DetachThread(SDL_Thread * thread)
+{
+ if (!thread) {
+ return;
+ }
+
+ /* Grab dibs if the state is alive+joinable. */
+ if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_DETACHED)) {
+ SDL_SYS_DetachThread(thread);
+ } else {
+ /* all other states are pretty final, see where we landed. */
+ const int state = SDL_AtomicGet(&thread->state);
+ if ((state == SDL_THREAD_STATE_DETACHED) || (state == SDL_THREAD_STATE_CLEANED)) {
+ return; /* already detached (you shouldn't call this twice!) */
+ } else if (state == SDL_THREAD_STATE_ZOMBIE) {
+ SDL_WaitThread(thread, NULL); /* already done, clean it up. */
+ } else {
+ SDL_assert(0 && "Unexpected thread state");
+ }
+ }
+}
+
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h
index 4fe9254..8d76a5f 100644
--- a/src/thread/SDL_thread_c.h
+++ b/src/thread/SDL_thread_c.h
@@ -40,12 +40,21 @@
#endif
#include "../SDL_error_c.h"
+typedef enum SDL_ThreadState
+{
+ SDL_THREAD_STATE_ALIVE,
+ SDL_THREAD_STATE_DETACHED,
+ SDL_THREAD_STATE_ZOMBIE,
+ SDL_THREAD_STATE_CLEANED,
+} SDL_ThreadState;
+
/* This is the system-independent thread info structure */
struct SDL_Thread
{
SDL_threadID threadid;
SYS_ThreadHandle handle;
int status;
+ SDL_atomic_t state; /* SDL_THREAD_STATE_* */
SDL_error errbuf;
char *name;
void *data;
diff --git a/src/thread/generic/SDL_systhread.c b/src/thread/generic/SDL_systhread.c
index 139f8ac..ab7f4ba 100644
--- a/src/thread/generic/SDL_systhread.c
+++ b/src/thread/generic/SDL_systhread.c
@@ -62,4 +62,10 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
return;
}
+void
+SDL_SYS_DetachThread(SDL_Thread * thread)
+{
+ return;
+}
+
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/thread/psp/SDL_systhread.c b/src/thread/psp/SDL_systhread.c
index 05a2341..269defe 100644
--- a/src/thread/psp/SDL_systhread.c
+++ b/src/thread/psp/SDL_systhread.c
@@ -77,6 +77,12 @@ void SDL_SYS_WaitThread(SDL_Thread *thread)
sceKernelDeleteThread(thread->handle);
}
+void SDL_SYS_DetachThread(SDL_Thread *thread)
+{
+ /* !!! FIXME: is this correct? */
+ sceKernelDeleteThread(thread->handle);
+}
+
void SDL_SYS_KillThread(SDL_Thread *thread)
{
sceKernelTerminateDeleteThread(thread->handle);
diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c
index f9eaef2..4c23478 100644
--- a/src/thread/pthread/SDL_systhread.c
+++ b/src/thread/pthread/SDL_systhread.c
@@ -213,4 +213,10 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
pthread_join(thread->handle, 0);
}
+void
+SDL_SYS_DetachThread(SDL_Thread * thread)
+{
+ pthread_detach(thread->handle);
+}
+
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c
index 06e23ab..aa2db2e 100644
--- a/src/thread/windows/SDL_systhread.c
+++ b/src/thread/windows/SDL_systhread.c
@@ -234,6 +234,12 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
CloseHandle(thread->handle);
}
+void
+SDL_SYS_DetachThread(SDL_Thread * thread)
+{
+ CloseHandle(thread->handle);
+}
+
#endif /* SDL_THREAD_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */