Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / mcas / ptst.c
blobfbcec4b5292f38ba55a03828e3ccdbfab3062aff
1 /******************************************************************************
2 * ptst.c
4 * Per-thread state management. Essentially the state management parts
5 * of MB's garbage-collection code have been pulled out and placed here,
6 * for the use of other utility routines.
8 * Copyright (c) 2002-2003, K A Fraser
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are
12 met:
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution. Neither the name of the Keir Fraser
20 * nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific
22 * prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "portable_defns.h"
41 #include "ptst.h"
44 pthread_key_t ptst_key;
45 ptst_t *ptst_list;
47 static unsigned int next_id;
49 ptst_t *critical_enter(void)
51 ptst_t *ptst, *next, *new_next;
52 unsigned int id, oid;
54 ptst = (ptst_t *)pthread_getspecific(ptst_key);
55 if ( ptst == NULL )
57 for ( ptst = ptst_first(); ptst != NULL; ptst = ptst_next(ptst) )
59 if ( (ptst->count == 0) && (CASIO(&ptst->count, 0, 1) == 0) )
61 break;
65 if ( ptst == NULL )
67 ptst = ALIGNED_ALLOC(sizeof(*ptst));
68 if ( ptst == NULL ) exit(1);
69 memset(ptst, 0, sizeof(*ptst));
70 ptst->gc = gc_init();
71 rand_init(ptst);
72 ptst->count = 1;
73 id = next_id;
74 while ( (oid = CASIO(&next_id, id, id+1)) != id ) id = oid;
75 ptst->id = id;
76 new_next = ptst_list;
77 do {
78 ptst->next = next = new_next;
79 WMB_NEAR_CAS();
81 while ( (new_next = CASPO(&ptst_list, next, ptst)) != next );
84 pthread_setspecific(ptst_key, ptst);
87 gc_enter(ptst);
88 return(ptst);
92 static void ptst_destructor(ptst_t *ptst)
94 ptst->count = 0;
98 void _init_ptst_subsystem(void)
100 ptst_list = NULL;
101 next_id = 0;
102 WMB();
103 if ( pthread_key_create(&ptst_key, (void (*)(void *))ptst_destructor) )
105 exit(1);