1 /*
2 * pte_tkAssocDestroy.c
3 *
4 * Description:
5 * This translation unit implements routines which are private to
6 * the implementation and may be used throughout it.
7 *
8 * --------------------------------------------------------------------------
9 *
10 * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
11 * Copyright(C) 2008 Jason Schmidlapp
12 *
13 * Contact Email: jschmidlapp@users.sourceforge.net
14 *
15 *
16 * Based upon Pthreads-win32 - POSIX Threads Library for Win32
17 * Copyright(C) 1998 John E. Bossom
18 * Copyright(C) 1999,2005 Pthreads-win32 contributors
19 *
20 * Contact Email: rpj@callisto.canberra.edu.au
21 *
22 * The original list of contributors to the Pthreads-win32 project
23 * is contained in the file CONTRIBUTORS.ptw32 included with the
24 * source code distribution. The list can also be seen at the
25 * following World Wide Web location:
26 * http://sources.redhat.com/pthreads-win32/contributors.html
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
37 *
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library in the file COPYING.LIB;
40 * if not, write to the Free Software Foundation, Inc.,
41 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42 */
43
44 #include <stdlib.h>
45
46 #include "pthread.h"
47 #include "implement.h"
48
49
50 void
pte_tkAssocDestroy(ThreadKeyAssoc * assoc)51 pte_tkAssocDestroy (ThreadKeyAssoc * assoc)
52 /*
53 * -------------------------------------------------------------------
54 * This routine releases all resources for the given ThreadKeyAssoc
55 * once it is no longer being referenced
56 * ie) either the key or thread has stopped referencing it.
57 *
58 * Parameters:
59 * assoc
60 * an instance of ThreadKeyAssoc.
61 * Returns:
62 * N/A
63 * -------------------------------------------------------------------
64 */
65 {
66
67 /*
68 * Both key->keyLock and thread->threadLock are locked on
69 * entry to this routine.
70 */
71 if (assoc != NULL)
72 {
73 ThreadKeyAssoc * prev, * next;
74
75 /* Remove assoc from thread's keys chain */
76 prev = assoc->prevKey;
77 next = assoc->nextKey;
78 if (prev != NULL)
79 {
80 prev->nextKey = next;
81 }
82 if (next != NULL)
83 {
84 next->prevKey = prev;
85 }
86
87 if (assoc->thread->keys == assoc)
88 {
89 /* We're at the head of the thread's keys chain */
90 assoc->thread->keys = next;
91 }
92 if (assoc->thread->nextAssoc == assoc)
93 {
94 /*
95 * Thread is exiting and we're deleting the assoc to be processed next.
96 * Hand thread the assoc after this one.
97 */
98 assoc->thread->nextAssoc = next;
99 }
100
101 /* Remove assoc from key's threads chain */
102 prev = assoc->prevThread;
103 next = assoc->nextThread;
104 if (prev != NULL)
105 {
106 prev->nextThread = next;
107 }
108 if (next != NULL)
109 {
110 next->prevThread = prev;
111 }
112
113 if (assoc->key->threads == assoc)
114 {
115 /* We're at the head of the key's threads chain */
116 assoc->key->threads = next;
117 }
118
119 free (assoc);
120 }
121
122 } /* pte_tkAssocDestroy */
123