/src/rtpproxy/src/rtpp_wref.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2026 Sippy Software, Inc., http://www.sippysoft.com |
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 | | |
28 | | #include <sched.h> |
29 | | #include <stdatomic.h> |
30 | | #include <stddef.h> |
31 | | #include <stdlib.h> |
32 | | |
33 | | #include "config.h" |
34 | | |
35 | | #include "rtpp_types.h" |
36 | | #include "rtpp_debug.h" |
37 | | #include "rtpp_mallocs.h" |
38 | | #include "rtpp_codeptr.h" |
39 | | #include "rtpp_refcnt.h" |
40 | | #include "rtpp_wref.h" |
41 | | #include "rtpp_wref_fin.h" |
42 | | |
43 | | struct rtpp_wref_priv { |
44 | | struct rtpp_wref pub; |
45 | | struct rtpp_wref_target target; |
46 | | _Atomic(int) cnt; |
47 | | }; |
48 | | |
49 | | static void rtpp_wref_dtor(struct rtpp_wref_priv *); |
50 | | static int rtpp_wref_setref(struct rtpp_wref *, struct rtpp_refcnt *, void *); |
51 | | static void rtpp_wref_inval(struct rtpp_wref *); |
52 | | static const struct rtpp_wref_target *rtpp_wref_getref(struct rtpp_wref *); |
53 | | |
54 | | DEFINE_SMETHODS(rtpp_wref, |
55 | | .setref = &rtpp_wref_setref, |
56 | | .getref = &rtpp_wref_getref, |
57 | | ); |
58 | | |
59 | | struct rtpp_wref * |
60 | | rtpp_wref_ctor(void) |
61 | 0 | { |
62 | 0 | struct rtpp_wref_priv *pvt; |
63 | |
|
64 | 0 | pvt = rtpp_rzmalloc(sizeof(*pvt), PVT_RCOFFS(pvt)); |
65 | 0 | if (pvt == NULL) { |
66 | 0 | return (NULL); |
67 | 0 | } |
68 | 0 | atomic_init(&pvt->cnt, 0); |
69 | 0 | RTPP_DBGCODE() { |
70 | 0 | PUBINST_FININIT(&pvt->pub, pvt, rtpp_wref_dtor); |
71 | 0 | } |
72 | 0 | return (&pvt->pub); |
73 | 0 | } |
74 | | |
75 | | static void |
76 | | rtpp_wref_dtor(struct rtpp_wref_priv *pvt) |
77 | 0 | { |
78 | 0 |
|
79 | 0 | rtpp_wref_fin(&pvt->pub); |
80 | 0 | RTPP_DBG_ASSERT(pvt->target.rco == NULL); |
81 | 0 | RTPP_DBG_ASSERT(pvt->target.obj == NULL); |
82 | 0 | RTPP_DBG_ASSERT(atomic_load_explicit(&pvt->cnt, memory_order_relaxed) == 0); |
83 | 0 | } |
84 | | |
85 | | static int |
86 | | rtpp_wref_setref(struct rtpp_wref *pub, struct rtpp_refcnt *target, void *obj) |
87 | 0 | { |
88 | 0 | struct rtpp_wref_priv *pvt; |
89 | 0 | int rval; |
90 | |
|
91 | 0 | PUB2PVT(pub, pvt); |
92 | 0 | RTPP_DBG_ASSERT(pvt->target.rco == NULL); |
93 | 0 | RTPP_DBG_ASSERT(atomic_load_explicit(&pvt->cnt, memory_order_relaxed) == 0); |
94 | 0 | rval = CALL_SMETHOD(target, attach, (rtpp_refcnt_dtor_t)&rtpp_wref_inval, |
95 | 0 | &pvt->pub); |
96 | 0 | if (rval != 0) { |
97 | 0 | return (-1); |
98 | 0 | } |
99 | 0 | pvt->target = (struct rtpp_wref_target){.rco = target, .obj = obj}; |
100 | | /* |
101 | | * Keep the weak reference object alive until the target invalidates it. |
102 | | */ |
103 | 0 | RTPP_OBJ_INCREF(&pvt->pub); |
104 | 0 | atomic_store_explicit(&pvt->cnt, 1, memory_order_release); |
105 | 0 | return (0); |
106 | 0 | } |
107 | | |
108 | | static void |
109 | | rtpp_wref_inval(struct rtpp_wref *pub) |
110 | 0 | { |
111 | 0 | struct rtpp_wref_priv *pvt; |
112 | 0 | int expected, nspins = 0; |
113 | |
|
114 | 0 | PUB2PVT(pub, pvt); |
115 | 0 | expected = 1; |
116 | 0 | while (!atomic_compare_exchange_weak_explicit(&pvt->cnt, &expected, 0, |
117 | 0 | memory_order_acq_rel, memory_order_acquire)) { |
118 | 0 | RTPP_DBG_ASSERT(expected > 1); |
119 | 0 | expected = 1; |
120 | 0 | if (nspins == 40) |
121 | 0 | sched_yield(); |
122 | 0 | else |
123 | 0 | nspins += 1; |
124 | 0 | } |
125 | 0 | RTPP_DBGCODE() { |
126 | 0 | pvt->target = (struct rtpp_wref_target){}; |
127 | 0 | } |
128 | 0 | RTPP_OBJ_DECREF(pub); |
129 | 0 | } |
130 | | |
131 | | static const struct rtpp_wref_target * |
132 | | rtpp_wref_getref(struct rtpp_wref *pub) |
133 | 0 | { |
134 | 0 | struct rtpp_wref_priv *pvt; |
135 | 0 | int oldcnt; |
136 | |
|
137 | 0 | PUB2PVT(pub, pvt); |
138 | 0 | oldcnt = atomic_fetch_add_explicit(&pvt->cnt, 1, memory_order_acquire); |
139 | 0 | if (oldcnt == 0) { |
140 | 0 | atomic_fetch_sub_explicit(&pvt->cnt, 1, memory_order_relaxed); |
141 | 0 | return (NULL); |
142 | 0 | } |
143 | 0 | RTPP_DBG_ASSERT(oldcnt >= 1); |
144 | 0 | RTPP_DBG_ASSERT(pvt->target.rco != NULL); |
145 | 0 | if (CALL_SMETHOD(pvt->target.rco, tryincref) == 0) { |
146 | 0 | atomic_fetch_sub_explicit(&pvt->cnt, 1, memory_order_relaxed); |
147 | 0 | return (&pvt->target); |
148 | 0 | } |
149 | 0 | atomic_fetch_sub_explicit(&pvt->cnt, 1, memory_order_relaxed); |
150 | | return (NULL); |
151 | 0 | } |