Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / mcas / skip_stm.c
blob63bcca8174b0f416422d0e3a802cd7f30c9bf835
1 /******************************************************************************
2 * skip_stm.c
4 * Skip lists, allowing concurrent update by use of the STM abstraction.
6 * Copyright (c) 2003, K A Fraser
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
13 * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution. Neither the name of the Keir Fraser
19 * nor the names of its contributors may be used to endorse or
20 * promote products derived from this software without specific
21 * prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #define __SET_IMPLEMENTATION__
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <assert.h>
42 #include "portable_defns.h"
43 #include "gc.h"
44 #include "stm.h"
45 #include "set.h"
47 typedef struct node_st node_t;
48 typedef stm_blk set_t;
50 struct node_st
52 int level;
53 setkey_t k;
54 setval_t v;
55 stm_blk *next[NUM_LEVELS];
58 static struct {
59 CACHE_PAD(0);
60 stm *memory; /* read-only */
61 CACHE_PAD(2);
62 } shared;
64 #define MEMORY (shared.memory)
67 * Random level generator. Drop-off rate is 0.5 per level.
68 * Returns value 1 <= level <= NUM_LEVELS.
70 static int get_level(ptst_t *ptst)
72 unsigned long r = rand_next(ptst);
73 int l = 1;
74 r = (r >> 4) & ((1 << (NUM_LEVELS-1)) - 1);
75 while ( (r & 1) ) { l++; r >>= 1; }
76 return l;
81 * Search for first non-deleted node, N, with key >= @k at each level in @l.
82 * RETURN VALUES:
83 * Array @pa: @pa[i] is non-deleted predecessor of N at level i
84 * Array @na: @na[i] is N itself, which should be pointed at by @pa[i]
85 * MAIN RETURN VALUE: same as @na[0], direct pointer open for reading.
87 static node_t *search_predecessors(
88 ptst_t *ptst, stm_tx *tx, set_t *l, setkey_t k, stm_blk **pa, stm_blk **na)
90 stm_blk *xb, *x_nextb;
91 node_t *x, *x_next;
92 int i;
94 xb = l;
95 x = read_stm_blk(ptst, tx, l);
96 for ( i = NUM_LEVELS - 1; i >= 0; i-- )
98 for ( ; ; )
100 x_nextb = x->next[i];
101 x_next = read_stm_blk(ptst, tx, x_nextb);
102 if ( x_next->k >= k ) break;
103 xb = x_nextb;
104 x = x_next;
107 if ( pa ) pa[i] = xb;
108 if ( na ) na[i] = x_nextb;
111 return x_next;
116 * PUBLIC FUNCTIONS
119 set_t *set_alloc(void)
121 ptst_t *ptst;
122 stm_blk *hb, *tb;
123 node_t *h, *t;
124 int i;
126 ptst = critical_enter();
128 tb = new_stm_blk(ptst, MEMORY);
129 t = init_stm_blk(ptst, MEMORY, tb);
130 memset(t, 0, sizeof(*t));
131 t->k = SENTINEL_KEYMAX;
133 hb = new_stm_blk(ptst, MEMORY);
134 h = init_stm_blk(ptst, MEMORY, hb);
135 memset(h, 0, sizeof(*h));
136 h->k = SENTINEL_KEYMIN;
137 h->level = NUM_LEVELS;
138 for ( i = 0; i < NUM_LEVELS; i++ )
139 h->next[i] = tb;
141 critical_exit(ptst);
143 return hb;
147 setval_t set_update(set_t *l, setkey_t k, setval_t v, int overwrite)
149 ptst_t *ptst;
150 stm_tx *tx;
151 setval_t ov;
152 stm_blk *bpreds[NUM_LEVELS], *bsuccs[NUM_LEVELS], *newb = NULL;
153 node_t *x, *p, *new;
154 int i;
156 k = CALLER_TO_INTERNAL_KEY(k);
158 ptst = critical_enter();
160 do {
161 new_stm_tx(tx, ptst, MEMORY);
162 x = search_predecessors(ptst, tx, l, k, bpreds, bsuccs);
164 if ( x->k == k )
166 x = write_stm_blk(ptst, tx, bsuccs[0]);
167 ov = x->v;
168 x->v = v;
170 else
172 ov = NULL;
174 if ( newb == NULL )
176 newb = new_stm_blk(ptst, MEMORY);
177 new = init_stm_blk(ptst, MEMORY, newb);
178 new->k = k;
179 new->v = v;
180 new->level = get_level(ptst);
183 for ( i = 0; i < new->level; i++ )
185 new->next[i] = bsuccs[i];
186 p = write_stm_blk(ptst, tx, bpreds[i]);
187 p->next[i] = newb;
191 while ( !commit_stm_tx(ptst, tx) );
193 if ( (ov != NULL) && (newb != NULL) )
194 free_stm_blk(ptst, MEMORY, newb);
196 critical_exit(ptst);
198 return ov;
202 setval_t set_remove(set_t *l, setkey_t k)
204 setval_t v;
205 ptst_t *ptst;
206 stm_tx *tx;
207 stm_blk *bpreds[NUM_LEVELS], *bsuccs[NUM_LEVELS];
208 node_t *p, *x;
209 int i;
211 k = CALLER_TO_INTERNAL_KEY(k);
213 ptst = critical_enter();
215 do {
216 new_stm_tx(tx, ptst, MEMORY);
217 x = search_predecessors(ptst, tx, l, k, bpreds, bsuccs);
218 if ( x->k == k )
220 v = x->v;
221 for ( i = 0; i < x->level; i++ )
223 p = write_stm_blk(ptst, tx, bpreds[i]);
224 p->next[i] = x->next[i];
227 else
229 v = NULL;
232 while ( !commit_stm_tx(ptst, tx) );
234 if ( v != NULL )
235 free_stm_blk(ptst, MEMORY, bsuccs[0]);
237 critical_exit(ptst);
239 return v;
243 setval_t set_lookup(set_t *l, setkey_t k)
245 setval_t v;
246 ptst_t *ptst;
247 stm_tx *tx;
248 node_t *x;
250 k = CALLER_TO_INTERNAL_KEY(k);
252 ptst = critical_enter();
254 do {
255 new_stm_tx(tx, ptst, MEMORY);
256 x = search_predecessors(ptst, tx, l, k, NULL, NULL);
257 v = (x->k == k) ? x->v : NULL;
259 while ( !commit_stm_tx(ptst, tx) );
261 critical_exit(ptst);
263 return v;
267 void _init_set_subsystem(void)
269 ptst_t *ptst = critical_enter();
270 _init_stm_subsystem(0);
271 MEMORY = new_stm(ptst, sizeof(node_t));
272 critical_exit(ptst);