Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / thread / detachthread.c
blobdaf731e1947eefe56fc55df96baf11de07e9b1d1
1 /*
2 * thread.library - threading and synchronisation primitives
4 * Copyright © 2007 Robert Norris
6 * This program is free software; you can redistribute it and/or modify it
7 * under the same terms as AROS itself.
8 */
10 #include "thread_intern.h"
12 #include <proto/exec.h>
13 #include <assert.h>
15 /*****************************************************************************
17 NAME */
18 AROS_LH1(BOOL, DetachThread,
20 /* SYNOPSIS */
21 AROS_LHA(uint32_t, thread_id, D0),
23 /* LOCATION */
24 struct ThreadBase *, ThreadBase, 8, Thread)
26 /* FUNCTION
27 Detaches a thread from the parent process.
29 INPUTS
30 thread_id - ID of thread to detach.
32 RESULT
33 TRUE if the thread was detached, FALSE if the thread was already
34 detached or another error occured.
36 NOTES
37 You cannot detach a thread that is already detached.
39 Once detached, the thread is no longer accessible from any other
40 thread.
42 EXAMPLE
43 DetachThread(id);
45 BUGS
46 Currently this doesn't really do anything other than make it so you
47 can't call WaitThread() on the thread. Threads can't truly be detached
48 from the parent process since they run in the same address space, and
49 so when the process exits the program code and all its other resources
50 a freed.
52 thread.library protects against this by waiting for all threads to
53 complete (detached or not) before allowing the main process to exit.
55 Detached threads can't be truly implemented until a thread task and its
56 allocated resources can exist independently of the process that created
57 it.
59 SEE ALSO
60 CreateThread(), CurrentThread(), WaitThread(), WaitAllThreads()
62 INTERNALS
64 *****************************************************************************/
66 AROS_LIBFUNC_INIT
68 assert(thread_id);
70 /* get thread data */
71 struct _Thread *thread = _getthreadbyid(thread_id, ThreadBase);
72 if (thread == NULL)
73 return FALSE;
75 /* mark it detached */
76 ObtainSemaphore(&thread->lock);
77 thread->detached = TRUE;
78 ReleaseSemaphore(&thread->lock);
80 return TRUE;
82 AROS_LIBFUNC_EXIT
83 } /* DetachThread */