Using semaphore in scull - from ldd3

来源:互联网 发布:战争之王 知乎 编辑:程序博客网 时间:2024/06/08 07:34

struct scull_dev {
     struct scull_qset *data; /* Pointer to first quantum set */
     int quantum; /* the current quantum size */
     int qset; /* the current array size */
     unsigned long size; /* amount of data stored here */
     unsigned int access_key; /* used by sculluid and scullpriv */
     struct semaphore sem; /* mutual exclusion semaphore */
     struct cdev cdev; /* Char device structure */
};

 

    each virtual scull device uses a semaphore seperately.

 

semaphore has to be intialized before the other section of scull device initialization.

 

for (i = 0; i < scull_nr_devs; i++) {
scull_devices[i].quantum = scull_quantum;
scull_devices[i].qset = scull_qset;
init_MUTEX(&scull_devices[i].sem);
scull_setup_cdev(&scull_devices[i], i);
}

scull_write starts from the following:

 

if (down_interruptible(&dev->sem))
return -ERESTARTSYS;

and release semaphore as follow(whether it can successfully carry out its other task):

 

out:
up(&dev->sem);
return retval;

        if some mistake ocurrs during scull_write operating, use : "goto out" to release semaphore correctly.

 

原创粉丝点击