1/* $NetBSD: psref.h,v 1.1 2016/04/09 06:21:16 riastradh Exp $ */
2
3/*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _SYS_PSREF_H
33#define _SYS_PSREF_H
34
35#include <sys/types.h>
36#include <sys/queue.h>
37
38struct cpu_info;
39struct lwp;
40
41struct psref;
42struct psref_class;
43struct psref_target;
44
45/*
46 * struct psref_target
47 *
48 * Bookkeeping for an object to which users can acquire passive
49 * references. This is compact so that it can easily be embedded
50 * into many multitudes of objects, e.g. IP packet flows.
51 *
52 * prt_draining is false on initialization, and may be written
53 * only once, to make it true, when someone has prevented new
54 * references from being created and wants to drain the target in
55 * order to destroy it.
56 */
57struct psref_target {
58 struct psref_class *prt_class;
59 bool prt_draining;
60};
61
62/*
63 * struct psref
64 *
65 * Bookkeeping for a single passive reference. There should only
66 * be a few of these per CPU in the system at once, no matter how
67 * many targets are stored, so these are a bit larger than struct
68 * psref_target. The contents of struct psref may be read and
69 * written only on the local CPU.
70 */
71struct psref {
72 LIST_ENTRY(psref) psref_entry;
73 const struct psref_target *psref_target;
74 struct lwp *psref_lwp;
75 struct cpu_info *psref_cpu;
76};
77
78struct psref_class *
79 psref_class_create(const char *, int);
80void psref_class_destroy(struct psref_class *);
81
82void psref_target_init(struct psref_target *, struct psref_class *);
83void psref_target_destroy(struct psref_target *, struct psref_class *);
84
85void psref_acquire(struct psref *, const struct psref_target *,
86 struct psref_class *);
87void psref_release(struct psref *, const struct psref_target *,
88 struct psref_class *);
89void psref_copy(struct psref *, const struct psref *,
90 struct psref_class *);
91
92/* For use only in assertions. */
93bool psref_held(const struct psref_target *, struct psref_class *);
94
95#endif /* _SYS_PSREF_H */
96