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