1 /******************************************************************************
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
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__
42 #include "portable_defns.h"
47 typedef struct node_st node_t
;
48 typedef stm_blk set_t
;
55 stm_blk
*next
[NUM_LEVELS
];
60 stm
*memory
; /* read-only */
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
);
74 r
= (r
>> 4) & ((1 << (NUM_LEVELS
-1)) - 1);
75 while ( (r
& 1) ) { l
++; r
>>= 1; }
81 * Search for first non-deleted node, N, with key >= @k at each level in @l.
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
;
95 x
= read_stm_blk(ptst
, tx
, l
);
96 for ( i
= NUM_LEVELS
- 1; i
>= 0; i
-- )
100 x_nextb
= x
->next
[i
];
101 x_next
= read_stm_blk(ptst
, tx
, x_nextb
);
102 if ( x_next
->k
>= k
) break;
107 if ( pa
) pa
[i
] = xb
;
108 if ( na
) na
[i
] = x_nextb
;
119 set_t
*set_alloc(void)
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
++ )
147 setval_t
set_update(set_t
*l
, setkey_t k
, setval_t v
, int overwrite
)
152 stm_blk
*bpreds
[NUM_LEVELS
], *bsuccs
[NUM_LEVELS
], *newb
= NULL
;
156 k
= CALLER_TO_INTERNAL_KEY(k
);
158 ptst
= critical_enter();
161 new_stm_tx(tx
, ptst
, MEMORY
);
162 x
= search_predecessors(ptst
, tx
, l
, k
, bpreds
, bsuccs
);
166 x
= write_stm_blk(ptst
, tx
, bsuccs
[0]);
176 newb
= new_stm_blk(ptst
, MEMORY
);
177 new = init_stm_blk(ptst
, MEMORY
, newb
);
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
]);
191 while ( !commit_stm_tx(ptst
, tx
) );
193 if ( (ov
!= NULL
) && (newb
!= NULL
) )
194 free_stm_blk(ptst
, MEMORY
, newb
);
202 setval_t
set_remove(set_t
*l
, setkey_t k
)
207 stm_blk
*bpreds
[NUM_LEVELS
], *bsuccs
[NUM_LEVELS
];
211 k
= CALLER_TO_INTERNAL_KEY(k
);
213 ptst
= critical_enter();
216 new_stm_tx(tx
, ptst
, MEMORY
);
217 x
= search_predecessors(ptst
, tx
, l
, k
, bpreds
, bsuccs
);
221 for ( i
= 0; i
< x
->level
; i
++ )
223 p
= write_stm_blk(ptst
, tx
, bpreds
[i
]);
224 p
->next
[i
] = x
->next
[i
];
232 while ( !commit_stm_tx(ptst
, tx
) );
235 free_stm_blk(ptst
, MEMORY
, bsuccs
[0]);
243 setval_t
set_lookup(set_t
*l
, setkey_t k
)
250 k
= CALLER_TO_INTERNAL_KEY(k
);
252 ptst
= critical_enter();
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
) );
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
));