Linux对I/O端口资源的管理(1)

Linux对I/O端口资源的管理(1)》摘要: int request_resource(struct resource *root, struct resource *new) { struct resource *conflict; write_lock(resource_lock); conflict = __request_resource(root, new); write_unlock(re…

int request_resource(struct resource *root, struct resource *new)

{

struct resource *conflict;

write_lock(&resource_lock);

conflict = __request_resource(root, new);

write_unlock(&resource_lock);

return conflict ? -EBUSY : 0;

}

对上述函数的NOTE如下:

①资源锁resource_lock对所有资源树进行读写保护,任何代码段在访问某一颗资源树之前都必须先持有该锁。其定义如下(kernel/Resource.c):

static rwlock_t resource_lock = RW_LOCK_UNLOCKED;

②可以看出,函数实际上是通过调用内部静态函数__request_resource()来完成实际的资源分配工作。如果该函数返回非空指针,则表示有资源冲突;否则,返回NULL就表示分配成功。

③最后,如果conflict指针为NULL,则request_resource()函数返回返回值0,表示成功;否则返回-EBUSY表示想要分配的资源已被占用。

函数__request_resource()完成实际的资源分配工作。如果参数new所描述的资源中的一部分或全部已经被其它节点所占用,则函数返回与new相冲突的resource结构的指针。否则就返回NULL。该函数的源代码如下

(kernel/Resource.c):

/* Return the conflict entry if you can't request it */

static struct resource * __request_resource

(struct resource *root, struct resource *new)

{

unsigned long start = new->start;

unsigned long end = new->end;

struct resource *tmp, **p;

if (end < start)

return root;

if (start < root->start)

return root;

if (end > root->end)

return root;

p = &root->child;

for (;;) {

tmp = *p;

if (!tmp || tmp->start > end) {

new->sibling = tmp;

*p = new;

new->parent = root;

return NULL;

}

p = &tmp->sibling;

if (tmp->end < start)

你的位置:电脑故障网 >> 操作系统 >> Linux/Unix >> Linux对I/O端口资源的管理(1)