summaryrefslogtreecommitdiff
path: root/kern/sref.h
blob: e224d167f10fe79ee5fb3fb4035199f5b89a366f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (c) 2014-2018 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Scalable reference counting.
 *
 * The purpose of this module is to reduce the amount of inter-processor
 * communication usually involved with reference counting. Scalable
 * reference counters should only be used when multiprocessor scalability
 * is important because of the costs they imply (increased memory usage
 * and latencies).
 *
 * When a counter drops to 0, the no-reference function associated with it
 * is called in work context. As a result, special care must be taken if
 * using sref counters in the work module itself.
 */

#ifndef KERN_SREF_H
#define KERN_SREF_H

#include <kern/init.h>

/*
 * Scalable reference counter.
 */
struct sref_counter;

/*
 * Weak reference.
 */
struct sref_weakref;

/*
 * Type for no-reference functions.
 */
typedef void (*sref_noref_fn_t)(struct sref_counter *);

#include <kern/sref_i.h>

/*
 * Manage registration of the current processor.
 *
 * Registering tells the sref module that the current processor reports
 * periodic events. When a processor enters a state in which reporting
 * periodic events becomes irrelevant, it unregisters itself so that the
 * other registered processors don't need to wait for it to make progress.
 * For example, this is done inside the idle loop since it is obviously
 * impossible to obtain or release references while idling.
 *
 * Unregistration can fail if internal data still require processing, in
 * which case a maintenance thread is awaken and EBUSY is returned.
 *
 * Preemption must be disabled when calling these functions.
 */
void sref_register(void);
int sref_unregister(void);

/*
 * Report a periodic event (normally the periodic timer interrupt) on the
 * current processor.
 *
 * Interrupts and preemption must be disabled when calling this function.
 */
void sref_report_periodic_event(void);

/*
 * Initialize a scalable reference counter.
 *
 * The no-reference function is called (from thread context) when it is
 * certain that the true number of references is 0.
 */
void sref_counter_init(struct sref_counter *counter,
                       unsigned long init_value,
                       struct sref_weakref *weakref,
                       sref_noref_fn_t noref_fn);

/*
 * Counter operations.
 *
 * These functions may safely be called in interrupt context.
 *
 * These functions imply a compiler barrier.
 */
void sref_counter_inc(struct sref_counter *counter);
void sref_counter_dec(struct sref_counter *counter);

/*
 * Attempt to get a reference from a weak reference.
 *
 * If successful, increment the reference counter before returning it.
 * Otherwise return NULL.
 *
 * This function may safely be called in interrupt context.
 */
struct sref_counter * sref_weakref_get(struct sref_weakref *weakref);

/*
 * This init operation provides :
 *  - sref counter and weakref initialization and usage
 */
INIT_OP_DECLARE(sref_bootstrap);

#endif /* KERN_SREF_H */