object.c 17.7 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-14     Bernard      the first version
 * 2006-04-21     Bernard      change the scheduler lock to interrupt lock
 * 2006-05-18     Bernard      fix the object init bug
 * 2006-08-03     Bernard      add hook support
 * 2007-01-28     Bernard      rename RT_OBJECT_Class_Static to RT_Object_Class_Static
qiuyiuestc's avatar
qiuyiuestc 已提交
13
 * 2010-10-26     yi.qiu       add module support in rt_object_allocate and rt_object_free
14
 * 2017-12-10     Bernard      Add object_info enum.
15
 * 2018-01-25     Bernard      Fix the object find issue when enable MODULE.
16
 * 2022-01-07     Gabriel      Moving __on_rt_xxxxx_hook to object.c
17 18 19 20 21
 */

#include <rtthread.h>
#include <rthw.h>

22 23
#ifdef RT_USING_MODULE
#include <dlmodule.h>
24
#endif /* RT_USING_MODULE */
25

26
/*
mysterywolf's avatar
mysterywolf 已提交
27
 * define object_info for the number of _object_container items.
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
 */
enum rt_object_info_type
{
    RT_Object_Info_Thread = 0,                         /**< The object is a thread. */
#ifdef RT_USING_SEMAPHORE
    RT_Object_Info_Semaphore,                          /**< The object is a semaphore. */
#endif
#ifdef RT_USING_MUTEX
    RT_Object_Info_Mutex,                              /**< The object is a mutex. */
#endif
#ifdef RT_USING_EVENT
    RT_Object_Info_Event,                              /**< The object is a event. */
#endif
#ifdef RT_USING_MAILBOX
    RT_Object_Info_MailBox,                            /**< The object is a mail box. */
#endif
#ifdef RT_USING_MESSAGEQUEUE
    RT_Object_Info_MessageQueue,                       /**< The object is a message queue. */
#endif
#ifdef RT_USING_MEMHEAP
    RT_Object_Info_MemHeap,                            /**< The object is a memory heap */
#endif
#ifdef RT_USING_MEMPOOL
    RT_Object_Info_MemPool,                            /**< The object is a memory pool. */
#endif
#ifdef RT_USING_DEVICE
    RT_Object_Info_Device,                             /**< The object is a device */
#endif
    RT_Object_Info_Timer,                              /**< The object is a timer. */
#ifdef RT_USING_MODULE
    RT_Object_Info_Module,                             /**< The object is a module. */
59 60 61
#endif
#ifdef RT_USING_HEAP
    RT_Object_Info_Memory,                            /**< The object is a memory. */
62 63 64 65
#endif
    RT_Object_Info_Unknown,                            /**< The object is unknown. */
};

66
#define _OBJ_CONTAINER_LIST_INIT(c)     \
mysterywolf's avatar
mysterywolf 已提交
67 68 69
    {&(_object_container[c].object_list), &(_object_container[c].object_list)}

static struct rt_object_information _object_container[RT_Object_Info_Unknown] =
70
{
71
    /* initialize object container - thread */
72
    {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
73
#ifdef RT_USING_SEMAPHORE
74
    /* initialize object container - semaphore */
75
    {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
76 77
#endif
#ifdef RT_USING_MUTEX
78
    /* initialize object container - mutex */
79
    {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
80 81
#endif
#ifdef RT_USING_EVENT
82
    /* initialize object container - event */
83
    {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
84 85
#endif
#ifdef RT_USING_MAILBOX
86
    /* initialize object container - mailbox */
87
    {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
88 89
#endif
#ifdef RT_USING_MESSAGEQUEUE
90
    /* initialize object container - message queue */
91
    {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
92
#endif
93
#ifdef RT_USING_MEMHEAP
94
    /* initialize object container - memory heap */
95
    {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
96
#endif
97
#ifdef RT_USING_MEMPOOL
98
    /* initialize object container - memory pool */
99
    {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
100 101
#endif
#ifdef RT_USING_DEVICE
102
    /* initialize object container - device */
103
    {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
104
#endif
105
    /* initialize object container - timer */
106
    {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
107
#ifdef RT_USING_MODULE
108
    /* initialize object container - module */
109
    {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
110
#endif
111 112 113 114
#ifdef RT_USING_HEAP
    /* initialize object container - small memory */
    {RT_Object_Class_Memory, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Memory), sizeof(struct rt_memory)},
#endif
115
};
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#ifndef __on_rt_object_attach_hook
    #define __on_rt_object_attach_hook(obj)         __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
#endif
#ifndef __on_rt_object_detach_hook
    #define __on_rt_object_detach_hook(obj)         __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
#endif
#ifndef __on_rt_object_trytake_hook
    #define __on_rt_object_trytake_hook(parent)     __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
#endif
#ifndef __on_rt_object_take_hook
    #define __on_rt_object_take_hook(parent)        __ON_HOOK_ARGS(rt_object_take_hook, (parent))
#endif
#ifndef __on_rt_object_put_hook
    #define __on_rt_object_put_hook(parent)         __ON_HOOK_ARGS(rt_object_put_hook, (parent))
#endif

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
D
dzzxzz 已提交
134 135 136 137 138
static void (*rt_object_attach_hook)(struct rt_object *object);
static void (*rt_object_detach_hook)(struct rt_object *object);
void (*rt_object_trytake_hook)(struct rt_object *object);
void (*rt_object_take_hook)(struct rt_object *object);
void (*rt_object_put_hook)(struct rt_object *object);
139 140 141 142

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
143

D
dogandog 已提交
144
/**@{*/
145 146

/**
147 148
 * @brief This function will set a hook function, which will be invoked when object
 *        attaches to kernel object system.
B
bernard.xiong 已提交
149
 *
Nameless-Y's avatar
Nameless-Y 已提交
150
 * @param hook is the hook function.
151
 */
D
dzzxzz 已提交
152
void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
153
{
154
    rt_object_attach_hook = hook;
155 156 157
}

/**
158 159
 * @brief This function will set a hook function, which will be invoked when object
 *        detaches from kernel object system.
B
bernard.xiong 已提交
160
 *
Nameless-Y's avatar
Nameless-Y 已提交
161
 * @param hook is the hook function
162
 */
D
dzzxzz 已提交
163
void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
164
{
165
    rt_object_detach_hook = hook;
166 167 168
}

/**
169 170
 * @brief This function will set a hook function, which will be invoked when object
 *        is taken from kernel object system.
B
bernard.xiong 已提交
171
 *
172 173 174 175 176 177
 *        The object is taken means:
 *            semaphore - semaphore is taken by thread
 *            mutex - mutex is taken by thread
 *            event - event is received by thread
 *            mailbox - mail is received by thread
 *            message queue - message is received by thread
B
bernard.xiong 已提交
178
 *
Nameless-Y's avatar
Nameless-Y 已提交
179
 * @param hook is the hook function.
180
 */
D
dzzxzz 已提交
181
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
182
{
183
    rt_object_trytake_hook = hook;
184 185 186
}

/**
187 188
 * @brief This function will set a hook function, which will be invoked when object
 *        have been taken from kernel object system.
B
bernard.xiong 已提交
189
 *
190 191 192 193 194 195 196
 *        The object have been taken means:
 *            semaphore - semaphore have been taken by thread
 *            mutex - mutex have been taken by thread
 *            event - event have been received by thread
 *            mailbox - mail have been received by thread
 *            message queue - message have been received by thread
 *            timer - timer is started
B
bernard.xiong 已提交
197
 *
198
 * @param hook the hook function.
199
 */
D
dzzxzz 已提交
200
void rt_object_take_sethook(void (*hook)(struct rt_object *object))
201
{
202
    rt_object_take_hook = hook;
203 204 205
}

/**
206 207
 * @brief This function will set a hook function, which will be invoked when object
 *        is put to kernel object system.
B
bernard.xiong 已提交
208
 *
Nameless-Y's avatar
Nameless-Y 已提交
209
 * @param hook is the hook function
210
 */
D
dzzxzz 已提交
211
void rt_object_put_sethook(void (*hook)(struct rt_object *object))
212
{
213
    rt_object_put_hook = hook;
214 215
}

D
dogandog 已提交
216
/**@}*/
217
#endif /* RT_USING_HOOK */
218 219 220 221

/**
 * @addtogroup KernelObject
 */
D
dzzxzz 已提交
222

D
dogandog 已提交
223
/**@{*/
224

225
/**
226
 * @brief This function will return the specified type of object information.
B
Bernard Xiong 已提交
227
 *
Nameless-Y's avatar
Nameless-Y 已提交
228
 * @param type is the type of object, which can be
229 230
 *             RT_Object_Class_Thread/Semaphore/Mutex... etc
 *
231 232
 * @return the object type information or RT_NULL
 */
233 234
struct rt_object_information *
rt_object_get_information(enum rt_object_class_type type)
235
{
236
    int index;
237

238
    for (index = 0; index < RT_Object_Info_Unknown; index ++)
mysterywolf's avatar
mysterywolf 已提交
239
        if (_object_container[index].type == type) return &_object_container[index];
240

241
    return RT_NULL;
242
}
243
RTM_EXPORT(rt_object_get_information);
244

245
/**
246
 * @brief This function will return the length of object list in object container.
247
 *
Nameless-Y's avatar
Nameless-Y 已提交
248
 * @param type is the type of object, which can be
249
 *             RT_Object_Class_Thread/Semaphore/Mutex... etc
250
 *
251 252 253 254 255
 * @return the length of object list
 */
int rt_object_get_length(enum rt_object_class_type type)
{
    int count = 0;
256
    rt_base_t level;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    struct rt_list_node *node = RT_NULL;
    struct rt_object_information *information = RT_NULL;

    information = rt_object_get_information((enum rt_object_class_type)type);
    if (information == RT_NULL) return 0;

    level = rt_hw_interrupt_disable();
    /* get the count of objects */
    rt_list_for_each(node, &(information->object_list))
    {
        count ++;
    }
    rt_hw_interrupt_enable(level);

    return count;
}
RTM_EXPORT(rt_object_get_length);

/**
276 277
 * @brief This function will copy the object pointer of the specified type,
 *        with the maximum size specified by maxlen.
278
 *
Nameless-Y's avatar
Nameless-Y 已提交
279
 * @param type is the type of object, which can be
280
 *             RT_Object_Class_Thread/Semaphore/Mutex... etc
281
 *
Nameless-Y's avatar
Nameless-Y 已提交
282
 * @param pointers is the pointer will be saved to.
283
 *
Nameless-Y's avatar
Nameless-Y 已提交
284
 * @param maxlen is the maximum number of pointers can be saved.
285
 *
Nameless-Y's avatar
Nameless-Y 已提交
286
 * @return the copied number of object pointers.
287 288 289 290
 */
int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
{
    int index = 0;
291
    rt_base_t level;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

    struct rt_object *object;
    struct rt_list_node *node = RT_NULL;
    struct rt_object_information *information = RT_NULL;

    if (maxlen <= 0) return 0;

    information = rt_object_get_information((enum rt_object_class_type)type);
    if (information == RT_NULL) return 0;

    level = rt_hw_interrupt_disable();
    /* retrieve pointer of object */
    rt_list_for_each(node, &(information->object_list))
    {
        object = rt_list_entry(node, struct rt_object, list);

        pointers[index] = object;
        index ++;
310 311

        if (index >= maxlen) break;
312 313 314 315 316 317 318
    }
    rt_hw_interrupt_enable(level);

    return index;
}
RTM_EXPORT(rt_object_get_pointers);

319
/**
320 321
 * @brief This function will initialize an object and add it to object system
 *        management.
322
 *
Nameless-Y's avatar
Nameless-Y 已提交
323
 * @param object is the specified object to be initialized.
324
 *
Nameless-Y's avatar
Nameless-Y 已提交
325
 * @param type is the object type.
326
 *
Nameless-Y's avatar
Nameless-Y 已提交
327
 * @param name is the object name. In system, the object's name must be unique.
328
 */
329 330 331
void rt_object_init(struct rt_object         *object,
                    enum rt_object_class_type type,
                    const char               *name)
332
{
333
    rt_base_t level;
334
    struct rt_list_node *node = RT_NULL;
335
    struct rt_object_information *information;
336 337
#ifdef RT_USING_MODULE
    struct rt_dlmodule *module = dlmodule_self();
338
#endif /* RT_USING_MODULE */
339

340
    /* get object information */
341 342
    information = rt_object_get_information(type);
    RT_ASSERT(information != RT_NULL);
343

344 345
    /* check object type to avoid re-initialization */

346 347 348 349 350 351 352 353 354 355
    /* enter critical */
    rt_enter_critical();
    /* try to find object */
    for (node  = information->object_list.next;
            node != &(information->object_list);
            node  = node->next)
    {
        struct rt_object *obj;

        obj = rt_list_entry(node, struct rt_object, list);
356 357 358 359
        if (obj) /* skip warning when disable debug */
        {
            RT_ASSERT(obj != object);
        }
360 361 362 363 364
    }
    /* leave critical */
    rt_exit_critical();

    /* initialize object's parameters */
365 366 367 368
    /* set object type to static */
    object->type = type | RT_Object_Class_Static;
    /* copy name */
    rt_strncpy(object->name, name, RT_NAME_MAX);
369

370
    RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
371

372
    /* lock interrupt */
373
    level = rt_hw_interrupt_disable();
374

375 376 377 378 379 380 381
#ifdef RT_USING_MODULE
    if (module)
    {
        rt_list_insert_after(&(module->object_list), &(object->list));
        object->module_id = (void *)module;
    }
    else
382
#endif /* RT_USING_MODULE */
383 384 385 386
    {
        /* insert object into information object list */
        rt_list_insert_after(&(information->object_list), &(object->list));
    }
387

388
    /* unlock interrupt */
389
    rt_hw_interrupt_enable(level);
390 391 392
}

/**
393 394
 * @brief This function will detach a static object from object system,
 *        and the memory of static object is not freed.
395 396 397 398 399
 *
 * @param object the specified object to be detached.
 */
void rt_object_detach(rt_object_t object)
{
400
    rt_base_t level;
401

402 403
    /* object check */
    RT_ASSERT(object != RT_NULL);
404

405
    RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
406

407 408 409
    /* reset object type */
    object->type = 0;

410
    /* lock interrupt */
411
    level = rt_hw_interrupt_disable();
412

413 414
    /* remove from old list */
    rt_list_remove(&(object->list));
415

416
    /* unlock interrupt */
417
    rt_hw_interrupt_enable(level);
418 419 420 421
}

#ifdef RT_USING_HEAP
/**
422
 * @brief This function will allocate an object from object system.
423
 *
Nameless-Y's avatar
Nameless-Y 已提交
424
 * @param type is the type of object.
425
 *
Nameless-Y's avatar
Nameless-Y 已提交
426
 * @param name is the object name. In system, the object's name must be unique.
427 428 429
 *
 * @return object
 */
D
dzzxzz 已提交
430
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
431
{
432
    struct rt_object *object;
433
    rt_base_t level;
434
    struct rt_object_information *information;
435 436
#ifdef RT_USING_MODULE
    struct rt_dlmodule *module = dlmodule_self();
437
#endif /* RT_USING_MODULE */
438

439
    RT_DEBUG_NOT_IN_INTERRUPT;
440

441
    /* get object information */
442 443
    information = rt_object_get_information(type);
    RT_ASSERT(information != RT_NULL);
444

B
Bernard Xiong 已提交
445
    object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
446 447 448 449 450
    if (object == RT_NULL)
    {
        /* no memory can be allocated */
        return RT_NULL;
    }
B
Bernard Xiong 已提交
451

452 453 454
    /* clean memory data of object */
    rt_memset(object, 0x0, information->object_size);

455
    /* initialize object's parameters */
456

457 458
    /* set object type */
    object->type = type;
459

460 461
    /* set object flag */
    object->flag = 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
462

463 464
    /* copy name */
    rt_strncpy(object->name, name, RT_NAME_MAX);
465

466
    RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
467

468
    /* lock interrupt */
469
    level = rt_hw_interrupt_disable();
470

471 472 473 474 475 476 477
#ifdef RT_USING_MODULE
    if (module)
    {
        rt_list_insert_after(&(module->object_list), &(object->list));
        object->module_id = (void *)module;
    }
    else
478
#endif /* RT_USING_MODULE */
479 480 481 482
    {
        /* insert object into information object list */
        rt_list_insert_after(&(information->object_list), &(object->list));
    }
483

484
    /* unlock interrupt */
485
    rt_hw_interrupt_enable(level);
486

487 488
    /* return object */
    return object;
489 490 491
}

/**
492
 * @brief This function will delete an object and release object memory.
493
 *
Nameless-Y's avatar
Nameless-Y 已提交
494
 * @param object is the specified object to be deleted.
495 496 497
 */
void rt_object_delete(rt_object_t object)
{
498
    rt_base_t level;
499

500 501 502
    /* object check */
    RT_ASSERT(object != RT_NULL);
    RT_ASSERT(!(object->type & RT_Object_Class_Static));
503

504
    RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
505

506
    /* reset object type */
507
    object->type = RT_Object_Class_Null;
508

509
    /* lock interrupt */
510
    level = rt_hw_interrupt_disable();
511

512 513
    /* remove from old list */
    rt_list_remove(&(object->list));
514

515
    /* unlock interrupt */
516
    rt_hw_interrupt_enable(level);
517

518 519
    /* free the memory of object */
    RT_KERNEL_FREE(object);
520
}
521
#endif /* RT_USING_HEAP */
522 523

/**
524 525 526 527
 * @brief This function will judge the object is system object or not.
 *
 * @note  Normally, the system object is a static object and the type
 *        of object set to RT_Object_Class_Static.
528
 *
Nameless-Y's avatar
Nameless-Y 已提交
529
 * @param object is the specified object to be judged.
530
 *
531
 * @return RT_TRUE if a system object, RT_FALSE for others.
532
 */
533
rt_bool_t rt_object_is_systemobject(rt_object_t object)
534
{
535 536
    /* object check */
    RT_ASSERT(object != RT_NULL);
537

538 539
    if (object->type & RT_Object_Class_Static)
        return RT_TRUE;
540

541
    return RT_FALSE;
542 543
}

544
/**
545 546
 * @brief This function will return the type of object without
 *        RT_Object_Class_Static flag.
547
 *
Nameless-Y's avatar
Nameless-Y 已提交
548
 * @param object is the specified object to be get type.
549 550 551 552 553 554 555 556 557 558 559
 *
 * @return the type of object.
 */
rt_uint8_t rt_object_get_type(rt_object_t object)
{
    /* object check */
    RT_ASSERT(object != RT_NULL);

    return object->type & ~RT_Object_Class_Static;
}

560
/**
561 562
 * @brief This function will find specified name object from object
 *        container.
563
 *
Nameless-Y's avatar
Nameless-Y 已提交
564
 * @param name is the specified name of object.
565
 *
Nameless-Y's avatar
Nameless-Y 已提交
566
 * @param type is the type of object
567 568 569 570
 *
 * @return the found object or RT_NULL if there is no this object
 * in object container.
 *
B
bernard.xiong@gmail.com 已提交
571
 * @note this function shall not be invoked in interrupt status.
572
 */
D
dzzxzz 已提交
573
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
574
{
B
Bernard Xiong 已提交
575 576
    struct rt_object *object = RT_NULL;
    struct rt_list_node *node = RT_NULL;
577
    struct rt_object_information *information = RT_NULL;
578

579 580
    information = rt_object_get_information((enum rt_object_class_type)type);

581
    /* parameter check */
582
    if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
583 584

    /* which is invoke in interrupt status */
585 586
    RT_DEBUG_NOT_IN_INTERRUPT;

587 588 589 590
    /* enter critical */
    rt_enter_critical();

    /* try to find object */
591
    rt_list_for_each(node, &(information->object_list))
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    {
        object = rt_list_entry(node, struct rt_object, list);
        if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
        {
            /* leave critical */
            rt_exit_critical();

            return object;
        }
    }

    /* leave critical */
    rt_exit_critical();

    return RT_NULL;
607
}
D
dzzxzz 已提交
608

D
dogandog 已提交
609
/**@}*/