XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
object.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <xwos/lib/object.h>
15#include <xwos/osal/irq.h>
16#include <xwos/osal/skd.h>
17
18static __xwlib_code
20
21static __xwlib_code
22xwer_t xwos_object_release_refaop_tst(const void * ov, void * arg);
23
24static __xwlib_code
25void xwos_object_release_refaop_op(void * nv, const void * ov, void * arg);
26
27#define XWOS_OBJTIK_CHUNK ((xwsq_t)1 << 20U) /* 1M */
28#define XWOS_OBJTIK_CHUNK_MSK (XWOS_OBJTIK_CHUNK - 1U)
29
33static __xwlib_data
35
39static __xwlib_data
41
47{
48 xwsq_t i;
49
50 xwos_objtik_dispatcher = (CPUCFG_CPU_NUM * XWOS_OBJTIK_CHUNK);
51 for (i = 0; i < CPUCFG_CPU_NUM; i++) {
53 }
54}
55
65static __xwlib_code
67{
68 xwid_t cpu;
69 atomic_xwsq_t * objtik;
70 xwsq_t curr;
71 xwsq_t max;
72 xwsq_t ov;
73 xwer_t rc;
74
76 objtik = &xwos_objtik[cpu];
77 do {
78 curr = xwaop_load(xwsq_t, objtik, xwaop_mo_relaxed);
79 max = (curr & (~XWOS_OBJTIK_CHUNK_MSK)) + XWOS_OBJTIK_CHUNK_MSK;
80 rc = xwaop_tlt_then_add(xwsq_t, objtik, max, 1, NULL, &ov);
81 if (rc < 0) {
82 xwreg_t cpuirq;
83 xwsq_t chunk;
84
85 xwos_cpuirq_save_lc(&cpuirq);
86 curr = xwaop_load(xwsq_t, objtik, xwaop_mo_relaxed);
87 if (curr == max) {
88 ov = curr;
90 XWOS_OBJTIK_CHUNK, NULL, &chunk);
91 xwaop_store(xwsq_t, objtik, xwaop_mo_relaxed, chunk);
93 break;
94 } else {
96 }
97 }
98 } while (curr > max);
99 return ov;
100}
101
108{
109 obj->refcnt = 0;
110 obj->gcfunc = NULL;
111 obj->tik = 0;
112}
113
120{
121 obj->gcfunc = NULL;
122 obj->tik = 0;
123 obj->magic = 0;
124}
125
136{
137 xwer_t rc;
138
139 rc = xwaop_teq_then_add(xwsq_t, &obj->refcnt, 0, 1, NULL, NULL);
140 if (XWOK == rc) {
141 obj->tik = xwos_objtik_get();
142 obj->magic = XWOS_OBJ_MAGIC;
143 obj->gcfunc = gcfunc;
144 } else {
145 rc = -EOBJACTIVE;
146 }
147 return rc;
148}
149
156void xwos_object_setgc(struct xwos_object * obj, xwobj_gc_f gcfunc)
157{
158 if (XWOS_OBJ_MAGIC == obj->magic) {
159 obj->gcfunc = gcfunc;
160 }
161}
162
163static __xwlib_code
164xwer_t xwos_object_acquire_refaop_tst(const void * ov, void * arg)
165{
166 const xwsq_t * o;
167 xwobj_d * objd;
168 xwer_t rc;
169
170 o = (const xwsq_t *)ov;
171 objd = arg;
172 if ((XWOS_OBJ_MAGIC != objd->obj->magic) ||
173 (objd->tik != objd->obj->tik)) {
174 rc = -EACCES;
175 } else if (*o < 1) {
176 rc = -EOBJDEAD;
177 } else {
178 rc = XWOK;
179 }
180 return rc;
181}
182
183static __xwlib_code
184void xwos_object_acquire_refaop_op(void * nv, const void * ov, void * arg)
185{
186 xwsq_t * n;
187 const xwsq_t * o;
188
189 XWOS_UNUSED(arg);
190 n = (xwsq_t *)nv;
191 o = (const xwsq_t *)ov;
192 *n = *o + 1;
193}
194
207{
208 xwobj_d objd = {obj, tik};
209 xwer_t rc;
210
211 if ((NULL == obj) || ((xwsq_t)0 == tik)) {
212 rc = -ENILOBJD;
213 } else {
214 rc = xwaop_tst_then_op(xwsq_t, &obj->refcnt,
217 NULL, NULL);
218 }
219 return rc;
220}
221
222static __xwlib_code
223xwer_t xwos_object_release_refaop_tst(const void * ov, void * arg)
224{
225 const xwsq_t * o;
226 xwobj_d * objd;
227 xwer_t rc;
228
229 o = (const xwsq_t *)ov;
230 objd = arg;
231 if ((XWOS_OBJ_MAGIC != objd->obj->magic) ||
232 (objd->tik != objd->obj->tik)) {
233 rc = -EACCES;
234 } else if (*o < 1) {
235 rc = -EOBJDEAD;
236 } else {
237 rc = XWOK;
238 }
239 return rc;
240}
241
242static __xwlib_code
243void xwos_object_release_refaop_op(void * nv, const void * ov, void * arg)
244{
245 xwsq_t * n;
246 const xwsq_t * o;
247
248 XWOS_UNUSED(arg);
249 n = (xwsq_t *)nv;
250 o = (const xwsq_t *)ov;
251 *n = *o - 1;
252}
253
266{
267 xwobj_d objd = {obj, tik};
268 xwsq_t nv;
269 xwer_t rc;
270
271 if ((NULL == obj) || ((xwsq_t)0 == tik)) {
272 rc = -ENILOBJD;
273 } else {
274 rc = xwaop_tst_then_op(xwsq_t, &obj->refcnt,
277 &nv, NULL);
278 if (XWOK == rc) {
279 if ((xwsq_t)0 == nv) {
280 if (NULL != obj->gcfunc) {
281 rc = obj->gcfunc(obj);
282 }
283 }
284 }
285 }
286 return rc;
287}
288
298{
299 xwer_t rc;
300
301 rc = xwaop_tge_then_add(xwsq_t, &obj->refcnt, 1, 1, NULL, NULL);
302 if (rc < 0) {
303 rc = -EOBJDEAD;
304 }
305 return rc;
306}
307
317{
318 xwer_t rc;
319 xwsq_t nv;
320
321 rc = xwaop_tgt_then_sub(xwsq_t, &obj->refcnt,
322 0, 1,
323 &nv, NULL);
324 if (XWOK == rc) {
325 if ((xwsq_t)0 == nv) {
326 if (NULL != obj->gcfunc) {
327 rc = obj->gcfunc(obj);
328 }
329 }
330 } else {
331 rc = -EOBJDEAD;
332 }
333 return rc;
334}
335
345{
346 xwer_t rc;
347
348 rc = xwaop_tgt_then_sub(xwsq_t, &obj->refcnt,
349 0, 1,
350 NULL, NULL);
351 if (rc < 0) {
352 rc = -EOBJDEAD;
353 }
354 return rc;
355}
356
364{
366}
static void xwos_cpuirq_save_lc(xwreg_t *cpuirq)
XWOS API:保存然后关闭本地CPU的中断开关
Definition irq.h:220
static void xwos_cpuirq_restore_lc(xwreg_t cpuirq)
XWOS API:恢复本地CPU的中断开关
Definition irq.h:206
#define __xwcc_alignl1cache
Definition compiler.h:123
#define __xwlib_data
Definition compiler.h:207
#define __xwlib_code
Definition compiler.h:199
#define XWOK
No error
Definition errno.h:182
#define ENILOBJD
Nil object descriptor
Definition errno.h:207
#define EACCES
Permission denied
Definition errno.h:43
#define EOBJACTIVE
Object active
Definition errno.h:209
#define EOBJDEAD
Object dead
Definition errno.h:208
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
unsigned long xwid_t
Definition type.h:481
__xwcc_atomic xwsq_t atomic_xwsq_t
Definition type.h:448
unsigned long xwsq_t
Definition type.h:445
xwptr_t xwreg_t
Definition type.h:409
@ xwaop_mo_relaxed
Definition type.h:628
#define xwaop_tst_then_op(type, a, tf, tfarg, of, ofarg, nv, ov)
对原子变量进行原子操作:读取-测试-修改-回写
Definition xwaop.h:1328
#define xwaop_store(type, a, memorder, v)
对原子变量进行原子操作:存储
Definition xwaop.h:56
#define xwaop_tge_then_add(type, a, t, v, nv, ov)
对原子变量进行原子操作:读取-测试“大于等于”-相加-回写
Definition xwaop.h:430
#define xwaop_tgt_then_sub(type, a, t, v, nv, ov)
对原子变量进行原子操作:读取-测试“大于”-相减-回写
Definition xwaop.h:673
#define xwaop_load(type, a, memorder)
对原子变量进行原子操作:加载
Definition xwaop.h:45
#define xwaop_tlt_then_add(type, a, t, v, nv, ov)
对原子变量进行原子操作:读取-测试“小于”-相加-回写
Definition xwaop.h:455
#define xwaop_teq_then_add(type, a, t, v, nv, ov)
对原子变量进行原子操作:读取-测试“相等”-相加-回写
Definition xwaop.h:355
#define xwaop_add(type, a, v, nv, ov)
对原子变量进行原子操作:读取-相加-回写
Definition xwaop.h:330
void xwos_object_setgc(struct xwos_object *obj, xwobj_gc_f gcfunc)
设置XWOS对象的垃圾回收函数
Definition object.c:156
xwer_t xwos_object_rawput(struct xwos_object *obj)
减少对象的引用计数,不触发垃圾回收函数
Definition object.c:344
xwer_t(* xwobj_gc_f)(struct xwos_object *)
垃圾回收函数指针类型
Definition object.h:42
xwer_t xwos_object_grab(struct xwos_object *obj)
增加对象的引用计数
Definition object.c:297
xwer_t xwos_object_activate(struct xwos_object *obj, xwobj_gc_f gcfunc)
激活一个XWOS对象(将其引用计数初始化位1)
Definition object.c:135
void xwos_object_construct(struct xwos_object *obj)
XWOS对象的构造函数
Definition object.c:107
xwsq_t xwos_object_get_refcnt(struct xwos_object *obj)
获取XWOS对象的引用计数
Definition object.c:363
void xwos_object_destruct(struct xwos_object *obj)
XWOS对象的析构函数
Definition object.c:119
xwer_t xwos_object_release(struct xwos_object *obj, xwsq_t tik)
检查对象的标签并减少引用计数
Definition object.c:265
void xwos_objtik_init(void)
初始化对象标签分配器
Definition object.c:46
#define XWOS_OBJ_MAGIC
Definition object.h:35
xwer_t xwos_object_acquire(struct xwos_object *obj, xwsq_t tik)
检查对象的标签并增加引用计数
Definition object.c:206
xwer_t xwos_object_put(struct xwos_object *obj)
减少对象的引用计数
Definition object.c:316
static xwid_t xwos_skd_get_cpuid_lc(void)
XWOS API:获取当前CPU的ID
Definition skd.h:190
#define XWOS_UNUSED(x)
Definition standard.h:66
操作系统抽象层:异常与中断
操作系统抽象层:调度器
XWOS对象描述符
Definition object.h:63
struct xwos_object * obj
Definition object.h:64
xwsq_t tik
Definition object.h:65
XWOS对象
Definition object.h:53
atomic_xwsq_t refcnt
Definition object.h:56
xwsq_t magic
Definition object.h:55
xwsq_t tik
Definition object.h:54
xwobj_gc_f gcfunc
Definition object.h:57
#define XWOS_OBJTIK_CHUNK_MSK
Definition object.c:28
static xwsq_t xwos_objtik_get(void)
从对象标签分配器获取一个标签
Definition object.c:66
#define XWOS_OBJTIK_CHUNK
Definition object.c:27
static xwer_t xwos_object_acquire_refaop_tst(const void *ov, void *arg)
Definition object.c:164
static atomic_xwsq_t xwos_objtik[(1U)]
每CPU的对象标签分配器
Definition object.c:40
static void xwos_object_release_refaop_op(void *nv, const void *ov, void *arg)
Definition object.c:243
static xwer_t xwos_object_release_refaop_tst(const void *ov, void *arg)
Definition object.c:223
static void xwos_object_acquire_refaop_op(void *nv, const void *ov, void *arg)
Definition object.c:184
static atomic_xwsq_t xwos_objtik_dispatcher
对象标签分配器,每次分配 XWOS_OBJTIK_CHUNK 个ID给CPU
Definition object.c:34
XWOS通用库:XWOS的对象
XWOS的标准头文件