xref: /freebsd/sys/contrib/ck/include/ck_hp.h (revision 72143e89bb430c3b1406b399b810806904f6c882)
1 /*
2  * Copyright 2010-2015 Samy Al Bahra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef CK_HP_H
28 #define CK_HP_H
29 
30 #include <ck_cc.h>
31 #include <ck_md.h>
32 #include <ck_pr.h>
33 #include <ck_stack.h>
34 
35 #ifndef CK_HP_CACHE
36 #define CK_HP_CACHE 512
37 #endif
38 
39 struct ck_hp_hazard;
40 typedef void (*ck_hp_destructor_t)(void *);
41 
42 struct ck_hp {
43 	ck_stack_t subscribers;
44 	unsigned int n_subscribers;
45 	unsigned int n_free;
46 	unsigned int threshold;
47 	unsigned int degree;
48 	ck_hp_destructor_t destroy;
49 };
50 typedef struct ck_hp ck_hp_t;
51 
52 struct ck_hp_hazard {
53 	void *pointer;
54 	void *data;
55 	ck_stack_entry_t pending_entry;
56 };
57 typedef struct ck_hp_hazard ck_hp_hazard_t;
58 
59 enum {
60 	CK_HP_USED = 0,
61 	CK_HP_FREE = 1
62 };
63 
64 struct ck_hp_record {
65 	int state;
66 	void **pointers;
67 	void *cache[CK_HP_CACHE];
68 	struct ck_hp *global;
69 	ck_stack_t pending;
70 	unsigned int n_pending;
71 	ck_stack_entry_t global_entry;
72 	unsigned int n_peak;
73 	uint64_t n_reclamations;
74 } CK_CC_CACHELINE;
75 typedef struct ck_hp_record ck_hp_record_t;
76 
77 CK_CC_INLINE static void
78 ck_hp_set(struct ck_hp_record *record, unsigned int i, void *pointer)
79 {
80 
81 	ck_pr_store_ptr(&record->pointers[i], pointer);
82 	return;
83 }
84 
85 CK_CC_INLINE static void
86 ck_hp_set_fence(struct ck_hp_record *record, unsigned int i, void *pointer)
87 {
88 
89 #ifdef CK_MD_TSO
90 	ck_pr_fas_ptr(&record->pointers[i], pointer);
91 #else
92 	ck_pr_store_ptr(&record->pointers[i], pointer);
93 	ck_pr_fence_memory();
94 #endif
95 
96 	return;
97 }
98 
99 CK_CC_INLINE static void
100 ck_hp_clear(struct ck_hp_record *record)
101 {
102 	void **pointers = record->pointers;
103 	unsigned int i;
104 
105 	for (i = 0; i < record->global->degree; i++)
106 		*pointers++ = NULL;
107 
108 	return;
109 }
110 
111 void ck_hp_init(ck_hp_t *, unsigned int, unsigned int, ck_hp_destructor_t);
112 void ck_hp_set_threshold(ck_hp_t *, unsigned int);
113 void ck_hp_register(ck_hp_t *, ck_hp_record_t *, void **);
114 void ck_hp_unregister(ck_hp_record_t *);
115 ck_hp_record_t *ck_hp_recycle(ck_hp_t *);
116 void ck_hp_reclaim(ck_hp_record_t *);
117 void ck_hp_free(ck_hp_record_t *, ck_hp_hazard_t *, void *, void *);
118 void ck_hp_retire(ck_hp_record_t *, ck_hp_hazard_t *, void *, void *);
119 void ck_hp_purge(ck_hp_record_t *);
120 
121 #endif /* CK_HP_H */
122