xref: /relibc/pthreads-emb/pte_tkAssocCreate.c (revision 062c5bc4dfeed2c1bed58ed4810dd27adb32c68d)
1*062c5bc4SJason Schmidlapp /*
2*062c5bc4SJason Schmidlapp  * pte_tkAssocCreate.c
3*062c5bc4SJason Schmidlapp  *
4*062c5bc4SJason Schmidlapp  * Description:
5*062c5bc4SJason Schmidlapp  * This translation unit implements routines which are private to
6*062c5bc4SJason Schmidlapp  * the implementation and may be used throughout it.
7*062c5bc4SJason Schmidlapp  *
8*062c5bc4SJason Schmidlapp  * --------------------------------------------------------------------------
9*062c5bc4SJason Schmidlapp  *
10*062c5bc4SJason Schmidlapp  *      Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
11*062c5bc4SJason Schmidlapp  *      Copyright(C) 2008 Jason Schmidlapp
12*062c5bc4SJason Schmidlapp  *
13*062c5bc4SJason Schmidlapp  *      Contact Email: jschmidlapp@users.sourceforge.net
14*062c5bc4SJason Schmidlapp  *
15*062c5bc4SJason Schmidlapp  *
16*062c5bc4SJason Schmidlapp  *      Based upon Pthreads-win32 - POSIX Threads Library for Win32
17*062c5bc4SJason Schmidlapp  *      Copyright(C) 1998 John E. Bossom
18*062c5bc4SJason Schmidlapp  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
19*062c5bc4SJason Schmidlapp  *
20*062c5bc4SJason Schmidlapp  *      Contact Email: rpj@callisto.canberra.edu.au
21*062c5bc4SJason Schmidlapp  *
22*062c5bc4SJason Schmidlapp  *      The original list of contributors to the Pthreads-win32 project
23*062c5bc4SJason Schmidlapp  *      is contained in the file CONTRIBUTORS.ptw32 included with the
24*062c5bc4SJason Schmidlapp  *      source code distribution. The list can also be seen at the
25*062c5bc4SJason Schmidlapp  *      following World Wide Web location:
26*062c5bc4SJason Schmidlapp  *      http://sources.redhat.com/pthreads-win32/contributors.html
27*062c5bc4SJason Schmidlapp  *
28*062c5bc4SJason Schmidlapp  *      This library is free software; you can redistribute it and/or
29*062c5bc4SJason Schmidlapp  *      modify it under the terms of the GNU Lesser General Public
30*062c5bc4SJason Schmidlapp  *      License as published by the Free Software Foundation; either
31*062c5bc4SJason Schmidlapp  *      version 2 of the License, or (at your option) any later version.
32*062c5bc4SJason Schmidlapp  *
33*062c5bc4SJason Schmidlapp  *      This library is distributed in the hope that it will be useful,
34*062c5bc4SJason Schmidlapp  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
35*062c5bc4SJason Schmidlapp  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36*062c5bc4SJason Schmidlapp  *      Lesser General Public License for more details.
37*062c5bc4SJason Schmidlapp  *
38*062c5bc4SJason Schmidlapp  *      You should have received a copy of the GNU Lesser General Public
39*062c5bc4SJason Schmidlapp  *      License along with this library in the file COPYING.LIB;
40*062c5bc4SJason Schmidlapp  *      if not, write to the Free Software Foundation, Inc.,
41*062c5bc4SJason Schmidlapp  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42*062c5bc4SJason Schmidlapp  */
43*062c5bc4SJason Schmidlapp 
44*062c5bc4SJason Schmidlapp #include <stdlib.h>
45*062c5bc4SJason Schmidlapp 
46*062c5bc4SJason Schmidlapp #include "pthread.h"
47*062c5bc4SJason Schmidlapp #include "implement.h"
48*062c5bc4SJason Schmidlapp 
49*062c5bc4SJason Schmidlapp 
50*062c5bc4SJason Schmidlapp int
pte_tkAssocCreate(pte_thread_t * sp,pthread_key_t key)51*062c5bc4SJason Schmidlapp pte_tkAssocCreate (pte_thread_t * sp, pthread_key_t key)
52*062c5bc4SJason Schmidlapp /*
53*062c5bc4SJason Schmidlapp  * -------------------------------------------------------------------
54*062c5bc4SJason Schmidlapp  * This routine creates an association that
55*062c5bc4SJason Schmidlapp  * is unique for the given (thread,key) combination.The association
56*062c5bc4SJason Schmidlapp  * is referenced by both the thread and the key.
57*062c5bc4SJason Schmidlapp  * This association allows us to determine what keys the
58*062c5bc4SJason Schmidlapp  * current thread references and what threads a given key
59*062c5bc4SJason Schmidlapp  * references.
60*062c5bc4SJason Schmidlapp  * See the detailed description
61*062c5bc4SJason Schmidlapp  * at the beginning of this file for further details.
62*062c5bc4SJason Schmidlapp  *
63*062c5bc4SJason Schmidlapp  * Notes:
64*062c5bc4SJason Schmidlapp  *      1)      New associations are pushed to the beginning of the
65*062c5bc4SJason Schmidlapp  *              chain so that the internal pte_selfThreadKey association
66*062c5bc4SJason Schmidlapp  *              is always last, thus allowing selfThreadExit to
67*062c5bc4SJason Schmidlapp  *              be implicitly called last by pthread_exit.
68*062c5bc4SJason Schmidlapp  *      2)
69*062c5bc4SJason Schmidlapp  *
70*062c5bc4SJason Schmidlapp  * Parameters:
71*062c5bc4SJason Schmidlapp  *              thread
72*062c5bc4SJason Schmidlapp  *                      current running thread.
73*062c5bc4SJason Schmidlapp  *              key
74*062c5bc4SJason Schmidlapp  *                      key on which to create an association.
75*062c5bc4SJason Schmidlapp  * Returns:
76*062c5bc4SJason Schmidlapp  *       0              - if successful,
77*062c5bc4SJason Schmidlapp  *       ENOMEM         - not enough memory to create assoc or other object
78*062c5bc4SJason Schmidlapp  *       EINVAL         - an internal error occurred
79*062c5bc4SJason Schmidlapp  *       ENOSYS         - an internal error occurred
80*062c5bc4SJason Schmidlapp  * -------------------------------------------------------------------
81*062c5bc4SJason Schmidlapp  */
82*062c5bc4SJason Schmidlapp {
83*062c5bc4SJason Schmidlapp   ThreadKeyAssoc *assoc;
84*062c5bc4SJason Schmidlapp 
85*062c5bc4SJason Schmidlapp   /*
86*062c5bc4SJason Schmidlapp    * Have to create an association and add it
87*062c5bc4SJason Schmidlapp    * to both the key and the thread.
88*062c5bc4SJason Schmidlapp    *
89*062c5bc4SJason Schmidlapp    * Both key->keyLock and thread->threadLock are locked on
90*062c5bc4SJason Schmidlapp    * entry to this routine.
91*062c5bc4SJason Schmidlapp    */
92*062c5bc4SJason Schmidlapp   assoc = (ThreadKeyAssoc *) calloc (1, sizeof (*assoc));
93*062c5bc4SJason Schmidlapp 
94*062c5bc4SJason Schmidlapp   if (assoc == NULL)
95*062c5bc4SJason Schmidlapp     {
96*062c5bc4SJason Schmidlapp       return ENOMEM;
97*062c5bc4SJason Schmidlapp     }
98*062c5bc4SJason Schmidlapp 
99*062c5bc4SJason Schmidlapp   assoc->thread = sp;
100*062c5bc4SJason Schmidlapp   assoc->key = key;
101*062c5bc4SJason Schmidlapp 
102*062c5bc4SJason Schmidlapp   /*
103*062c5bc4SJason Schmidlapp    * Register assoc with key
104*062c5bc4SJason Schmidlapp    */
105*062c5bc4SJason Schmidlapp   assoc->prevThread = NULL;
106*062c5bc4SJason Schmidlapp   assoc->nextThread = (ThreadKeyAssoc *) key->threads;
107*062c5bc4SJason Schmidlapp   if (assoc->nextThread != NULL)
108*062c5bc4SJason Schmidlapp     {
109*062c5bc4SJason Schmidlapp       assoc->nextThread->prevThread = assoc;
110*062c5bc4SJason Schmidlapp     }
111*062c5bc4SJason Schmidlapp   key->threads = (void *) assoc;
112*062c5bc4SJason Schmidlapp 
113*062c5bc4SJason Schmidlapp   /*
114*062c5bc4SJason Schmidlapp    * Register assoc with thread
115*062c5bc4SJason Schmidlapp    */
116*062c5bc4SJason Schmidlapp   assoc->prevKey = NULL;
117*062c5bc4SJason Schmidlapp   assoc->nextKey = (ThreadKeyAssoc *) sp->keys;
118*062c5bc4SJason Schmidlapp   if (assoc->nextKey != NULL)
119*062c5bc4SJason Schmidlapp     {
120*062c5bc4SJason Schmidlapp       assoc->nextKey->prevKey = assoc;
121*062c5bc4SJason Schmidlapp     }
122*062c5bc4SJason Schmidlapp   sp->keys = (void *) assoc;
123*062c5bc4SJason Schmidlapp 
124*062c5bc4SJason Schmidlapp   return (0);
125*062c5bc4SJason Schmidlapp 
126*062c5bc4SJason Schmidlapp }				/* pte_tkAssocCreate */
127