object.c 11.7 KB
Newer Older
1 2 3
/*
 * File      : object.c
 * This file is part of RT-Thread RTOS
B
bernard.xiong 已提交
4
 * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
5 6 7
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
B
bernard.xiong 已提交
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16
 *
 * 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 已提交
17
 * 2010-10-26     yi.qiu       add module support in rt_object_allocate and rt_object_free
18 19 20 21 22 23 24
 */

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

#include "kservice.h"

25 26 27 28
#define _OBJ_CONTAINER_LIST_INIT(c) 	\
	{&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
{
B
bernard.xiong@gmail.com 已提交
29
	/* initialize object container - thread */
30 31
	{RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
#ifdef RT_USING_SEMAPHORE
B
bernard.xiong@gmail.com 已提交
32
	/* initialize object container - semaphore */
33 34 35
	{RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
#endif
#ifdef RT_USING_MUTEX
B
bernard.xiong@gmail.com 已提交
36
	/* initialize object container - mutex */
37 38 39
	{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},
#endif
#ifdef RT_USING_EVENT
B
bernard.xiong@gmail.com 已提交
40
	/* initialize object container - event */
41 42 43
	{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
#endif
#ifdef RT_USING_MAILBOX
B
bernard.xiong@gmail.com 已提交
44
	/* initialize object container - mailbox */
45 46 47
	{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},
#endif
#ifdef RT_USING_MESSAGEQUEUE
B
bernard.xiong@gmail.com 已提交
48
	/* initialize object container - message queue */
49 50 51
	{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},
#endif
#ifdef RT_USING_MEMPOOL
B
bernard.xiong@gmail.com 已提交
52
	/* initialize object container - memory pool */
53 54 55
	{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
#endif
#ifdef RT_USING_DEVICE
B
bernard.xiong@gmail.com 已提交
56
	/* initialize object container - device */
57 58
	{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
#endif
B
bernard.xiong@gmail.com 已提交
59
	/* initialize object container - timer */
60 61
	{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
B
bernard.xiong@gmail.com 已提交
62
	/* initialize object container - module */
63 64 65
	{RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
#endif
};
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

#ifdef RT_USING_HOOK
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);

/**
 * @addtogroup Hook
 */
/*@{*/

/**
 * This function will set a hook function, which will be invoked when object
 * attaches to kernel object system.
B
bernard.xiong 已提交
82
 *
83 84 85 86
 * @param hook the hook function
 */
void rt_object_attach_sethook(void (*hook)(struct rt_object* object))
{
87 88 89 90 91
	register rt_base_t temp;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

92
	rt_object_attach_hook = hook;
93 94 95

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);
96 97 98 99 100
}

/**
 * This function will set a hook function, which will be invoked when object
 * detaches from kernel object system.
B
bernard.xiong 已提交
101
 *
102 103 104 105
 * @param hook the hook function
 */
void rt_object_detach_sethook(void (*hook)(struct rt_object* object))
{
106 107 108 109 110
	register rt_base_t temp;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

111
	rt_object_detach_hook = hook;
112 113 114

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);
115 116 117 118 119
}

/**
 * This function will set a hook function, which will be invoked when object
 * is taken from kernel object system.
B
bernard.xiong 已提交
120
 *
B
bernard.xiong@gmail.com 已提交
121
 * The object is taken means:
122 123
 * semaphore - semaphore is taken by thread
 * mutex - mutex is taken by thread
B
bernard.xiong 已提交
124
 * event - event is received by thread
125 126
 * mailbox - mail is received by thread
 * message queue - message is received by thread
B
bernard.xiong 已提交
127
 *
128 129 130 131
 * @param hook the hook function
 */
void rt_object_trytake_sethook(void (*hook)(struct rt_object* object))
{
132 133 134 135 136
	register rt_base_t temp;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

137
	rt_object_trytake_hook = hook;
138 139 140

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);
141 142 143 144 145
}

/**
 * This function will set a hook function, which will be invoked when object
 * have been taken from kernel object system.
B
bernard.xiong 已提交
146
 *
B
bernard.xiong@gmail.com 已提交
147
 * The object have been taken means:
148 149
 * semaphore - semaphore have been taken by thread
 * mutex - mutex have been taken by thread
B
bernard.xiong 已提交
150
 * event - event have been received by thread
151 152 153
 * mailbox - mail have been received by thread
 * message queue - message have been received by thread
 * timer - timer is started
B
bernard.xiong 已提交
154
 *
155 156 157 158
 * @param hook the hook function
 */
void rt_object_take_sethook(void (*hook)(struct rt_object* object))
{
159 160 161 162 163
	register rt_base_t temp;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

164
	rt_object_take_hook = hook;
165 166 167

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);
168 169 170 171 172
}

/**
 * This function will set a hook function, which will be invoked when object
 * is put to kernel object system.
B
bernard.xiong 已提交
173
 *
174 175 176 177
 * @param hook the hook function
 */
void rt_object_put_sethook(void (*hook)(struct rt_object* object))
{
178 179 180 181 182
	register rt_base_t temp;

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

183
	rt_object_put_hook = hook;
184 185 186

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);
187 188 189 190 191 192 193 194
}

/*@}*/
#endif

/**
 * @ingroup SystemInit
 *
B
bernard.xiong@gmail.com 已提交
195
 * This function will initialize system object management.
196
 *
B
bernard.xiong@gmail.com 已提交
197 198
 * @deprecated since 0.3.0, this function does not need to be invoked
 * in the system initialization.
199 200 201 202 203 204 205 206 207 208 209
 */
void rt_system_object_init(void)
{
}

/**
 * @addtogroup KernelObject
 */
/*@{*/

/**
B
bernard.xiong@gmail.com 已提交
210
 * This function will initialize an object and add it to object system management.
211 212 213
 *
 * @param object the specified object to be initialized.
 * @param type the object type.
B
bernard.xiong@gmail.com 已提交
214
 * @param name the object name. In system, the object's name must be unique.
215 216 217 218 219 220
 */
void rt_object_init(struct rt_object* object, enum rt_object_class_type type, const char* name)
{
	register rt_base_t temp;
	struct rt_object_information* information;

qiuyiuestc's avatar
qiuyiuestc 已提交
221 222
#ifdef RT_USING_MODULE
	/* get module object information */
qiuyiuestc's avatar
qiuyiuestc 已提交
223 224
	information = (rt_module_self() != RT_NULL) ? 
		&rt_module_self()->module_object[type] : &rt_object_container[type];
qiuyiuestc's avatar
qiuyiuestc 已提交
225
#else
226 227
	/* get object information */
	information = &rt_object_container[type];
qiuyiuestc's avatar
qiuyiuestc 已提交
228
#endif
229

B
bernard.xiong@gmail.com 已提交
230
	/* initialize object's parameters */
231 232 233 234 235 236 237 238 239 240 241

	/* set object type to static */
	object->type = type | RT_Object_Class_Static;

	/* copy name */
	for (temp = 0; temp < RT_NAME_MAX; temp ++)
	{
		object->name[temp] = name[temp];
	}

#ifdef RT_USING_HOOK
242
	RT_OBJECT_HOOK_CALL2(rt_object_attach_hook,object);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#endif

	/* lock interrupt */
	temp = rt_hw_interrupt_disable();

	/* insert object into information object list */
	rt_list_insert_after(&(information->object_list), &(object->list));

	/* unlock interrupt */
	rt_hw_interrupt_enable(temp);
}

/**
 * This function will detach a static object from object system,
 * and the memory of static object is not freed.
 *
 * @param object the specified object to be detached.
 */
void rt_object_detach(rt_object_t object)
{
	register rt_base_t temp;

	/* object check */
	RT_ASSERT(object != RT_NULL);

#ifdef RT_USING_HOOK
269
	RT_OBJECT_HOOK_CALL2(rt_object_detach_hook,object);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
#endif

	/* lock interrupt */
	temp = rt_hw_interrupt_disable();

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

	/* unlock interrupt */
	rt_hw_interrupt_enable(temp);
}

#ifdef RT_USING_HEAP
/**
 * This function will allocate an object from object system
 *
 * @param type the type of object
 * @param name the object name. In system, the object's name must be unique.
 *
 * @return object
 */
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
{
	struct rt_object* object;
	register rt_base_t temp;
	struct rt_object_information* information;

297 298
	RT_DEBUG_NOT_REENT

qiuyiuestc's avatar
qiuyiuestc 已提交
299
#ifdef RT_USING_MODULE
qiuyiuestc's avatar
qiuyiuestc 已提交
300 301
	/* get module object information, module object should be managed by kernel object container */
	information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ? 
qiuyiuestc's avatar
qiuyiuestc 已提交
302
		&rt_module_self()->module_object[type] : &rt_object_container[type];
qiuyiuestc's avatar
qiuyiuestc 已提交
303
#else
304
	/* get object information */
qiuyiuestc's avatar
qiuyiuestc 已提交
305
	information = &rt_object_container[type];
qiuyiuestc's avatar
qiuyiuestc 已提交
306
#endif
307 308 309 310 311 312 313

	object = (struct rt_object*)rt_malloc(information->object_size);
	if (object == RT_NULL)
	{
		/* no memory can be allocated */
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
314
	
B
bernard.xiong@gmail.com 已提交
315
	/* initialize object's parameters */
316 317 318 319

	/* set object type */
	object->type = type;

qiuyiuestc's avatar
qiuyiuestc 已提交
320 321 322 323 324 325 326
	/* set object flag */
	object->flag = 0;

#ifdef RT_USING_MODULE
	if(rt_module_self() != RT_NULL)
	{
		object->flag |= RT_OBJECT_FLAG_MODULE;
qiuyiuestc's avatar
qiuyiuestc 已提交
327 328
	}
	object->module_id = (void*)rt_module_self();
qiuyiuestc's avatar
qiuyiuestc 已提交
329 330
#endif

331 332 333 334 335 336 337
	/* copy name */
	for (temp = 0; temp < RT_NAME_MAX; temp ++)
	{
		object->name[temp] = name[temp];
	}

#ifdef RT_USING_HOOK
338
	RT_OBJECT_HOOK_CALL2(rt_object_attach_hook,object);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
#endif

	/* lock interrupt */
	temp = rt_hw_interrupt_disable();

	/* insert object into information object list */
	rt_list_insert_after(&(information->object_list), &(object->list));

	/* unlock interrupt */
	rt_hw_interrupt_enable(temp);

	/* return object */
	return object;
}

/**
 * This function will delete an object and release object memory.
 *
 * @param object the specified object to be deleted.
 */
void rt_object_delete(rt_object_t object)
{
	register rt_base_t temp;

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

#ifdef RT_USING_HOOK
368
	RT_OBJECT_HOOK_CALL2(rt_object_detach_hook,object);
369 370 371 372 373 374 375 376 377 378 379
#endif

	/* lock interrupt */
	temp = rt_hw_interrupt_disable();

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

	/* unlock interrupt */
	rt_hw_interrupt_enable(temp);

qiuyiuestc's avatar
qiuyiuestc 已提交
380 381 382 383 384 385
#ifdef RT_USING_MODULE
	if(object->flag & RT_OBJECT_FLAG_MODULE) 
		rt_module_free((rt_module_t)object->module_id, object);
	else
#endif

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	/* free the memory of object */
	rt_free(object);
}
#endif

/**
 * This function will judge the object is system object or not.
 * Normally, the system object is a static object and the type
 * of object set to RT_Object_Class_Static.
 *
 * @param object the specified object to be judged.
 *
 * @return RT_EOK if a system object, RT_ERROR for others.
 */
rt_err_t rt_object_is_systemobject(rt_object_t object)
{
	/* object check */
	RT_ASSERT(object != RT_NULL);

	if (object->type & RT_Object_Class_Static) return RT_EOK;

	return -RT_ERROR;
}

410 411 412 413 414 415 416 417 418 419
/**
 * This function will find specified name object from object
 * container.
 *
 * @param name the specified name of object.
 * @param type the type of object
 *
 * @return the found object or RT_NULL if there is no this object
 * in object container.
 *
B
bernard.xiong@gmail.com 已提交
420
 * @note this function shall not be invoked in interrupt status.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
 */
rt_object_t rt_object_find(const char* name, rt_uint8_t type)
{
	struct rt_object* object;
	struct rt_list_node* node;
	struct rt_object_information *information;
	extern volatile rt_uint8_t rt_interrupt_nest;

	/* parameter check */
	if ((name == RT_NULL) ||
		(type > RT_Object_Class_Unknown))
		return RT_NULL;

	/* which is invoke in interrupt status */
	if (rt_interrupt_nest != 0)
		RT_ASSERT(0);

	/* enter critical */
	rt_enter_critical();

	/* try to find object */
	information = &rt_object_container[type];
	for (node = information->object_list.next; node != &(information->object_list); node = node->next)
	{
		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 (rt_object_t)object;
		}
	}

	/* leave critical */
	rt_exit_critical();

	return RT_NULL;
}
460
/*@}*/