object.c 17.6 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;
F
Frank Buss 已提交
334
#ifdef RT_DEBUG
335
    struct rt_list_node *node = RT_NULL;
F
Frank Buss 已提交
336
#endif
337
    struct rt_object_information *information;
338 339
#ifdef RT_USING_MODULE
    struct rt_dlmodule *module = dlmodule_self();
340
#endif /* RT_USING_MODULE */
341

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

F
Frank Buss 已提交
346
#ifdef RT_DEBUG
347 348
    /* check object type to avoid re-initialization */

349 350 351 352 353 354 355 356 357 358
    /* 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);
F
Frank Buss 已提交
359
        RT_ASSERT(obj != object);
360 361 362
    }
    /* leave critical */
    rt_exit_critical();
F
Frank Buss 已提交
363
#endif
364 365

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

371
    RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
372

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

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

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

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

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

406
    RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
407

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

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

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

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

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

440
    RT_DEBUG_NOT_IN_INTERRUPT;
441

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

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

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

456
    /* initialize object's parameters */
457

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

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

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

467
    RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
468

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

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

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

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

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

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

505
    RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
506

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

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

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

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

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

/**
525 526 527 528
 * @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.
529
 *
Nameless-Y's avatar
Nameless-Y 已提交
530
 * @param object is the specified object to be judged.
531
 *
532
 * @return RT_TRUE if a system object, RT_FALSE for others.
533
 */
534
rt_bool_t rt_object_is_systemobject(rt_object_t object)
535
{
536 537
    /* object check */
    RT_ASSERT(object != RT_NULL);
538

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

542
    return RT_FALSE;
543 544
}

545
/**
546 547
 * @brief This function will return the type of object without
 *        RT_Object_Class_Static flag.
548
 *
Nameless-Y's avatar
Nameless-Y 已提交
549
 * @param object is the specified object to be get type.
550 551 552 553 554 555 556 557 558 559 560
 *
 * @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;
}

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

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

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

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

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

    /* try to find object */
592
    rt_list_for_each(node, &(information->object_list))
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    {
        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;
608
}
D
dzzxzz 已提交
609

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