]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/include/can_queue.h
Added support for fasync system call and replacement of spinXXX by can_spinXXX
[lincan.git] / lincan / include / can_queue.h
index 5951186166a81f846d9e5f6f6c25b6ce9d39f82c..2fc422f4f38d080a294da19df4f7a752d9b7d2fa 100644 (file)
@@ -51,7 +51,7 @@ struct canque_fifo_t {
        struct canque_slot_t **tail;    /* points to NULL pointer for chaining */
        struct canque_slot_t *flist;    /* points the first entry in the free list */
        struct canque_slot_t *entry;    /* points to first allocated entry */
-       spinlock_t fifo_lock;   /* spin_lock_irqsave / spin_lock_irqrestore */
+       can_spinlock_t fifo_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
 };
 
 #define CAN_FIFOF_DESTROY_b    15
@@ -102,21 +102,21 @@ struct canque_fifo_t {
 static inline
 int canque_fifo_get_inslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp, int cmd)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        struct canque_slot_t *slot;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        /* get the first free slot slot from flist */
        if(!(slot=fifo->flist)) {
                canque_fifo_set_fl(fifo,OVERRUN);
                canque_fifo_set_fl(fifo,FULL);
-               spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+               can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
                *slotp=NULL;
                return -1;
        }
        /* adjust free slot list */
        if(!(fifo->flist=slot->next))
                canque_fifo_set_fl(fifo,FULL);
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
        *slotp=slot;
        slot->slot_flags=cmd&CAN_SLOTF_CMD;
        return 1;
@@ -134,9 +134,9 @@ static inline
 int canque_fifo_put_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
 {
        int ret;
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        slot->next=NULL;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        if(*fifo->tail) printk(KERN_CRIT "canque_fifo_put_inslot: fifo->tail != NULL\n");
        *fifo->tail=slot;
        fifo->tail=&slot->next;
@@ -145,7 +145,7 @@ int canque_fifo_put_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slo
          ret=CAN_FIFOF_EMPTY;  /* Fifo has been empty before put */
        if(canque_fifo_test_and_clear_fl(fifo,INACTIVE))
          ret=CAN_FIFOF_INACTIVE; /* Fifo has been empty before put */
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
        return ret;
 }
 
@@ -160,13 +160,13 @@ static inline
 int canque_fifo_abort_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
 {
        int ret=0;
-       unsigned long flags;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_irqflags_t flags;
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        slot->next=fifo->flist;
        fifo->flist=slot;
        if(canque_fifo_test_and_clear_fl(fifo,FULL))
                ret=CAN_FIFOF_FULL;
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
        return ret;
 }
 
@@ -184,19 +184,19 @@ int canque_fifo_abort_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *s
 static inline
 int canque_fifo_test_outslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        int cmd;
        struct canque_slot_t *slot;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        if(!(slot=fifo->head)){;
                canque_fifo_set_fl(fifo,EMPTY);
-               spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+               can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
                *slotp=NULL;
                return -1;
        }
        if(!(fifo->head=slot->next))
                fifo->tail=&fifo->head;
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
 
        *slotp=slot;
        cmd=slot->slot_flags;
@@ -218,8 +218,8 @@ static inline
 int canque_fifo_free_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
 {
        int ret=0;
-       unsigned long flags;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_irqflags_t flags;
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        slot->next=fifo->flist;
        fifo->flist=slot;
        if(canque_fifo_test_and_clear_fl(fifo,FULL))
@@ -228,7 +228,7 @@ int canque_fifo_free_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *s
                canque_fifo_set_fl(fifo,EMPTY);
                ret|=CAN_FIFOF_EMPTY;
        }
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
        return ret;
 }
 
@@ -242,12 +242,12 @@ int canque_fifo_free_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *s
 static inline
 int canque_fifo_again_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
 {
-       unsigned long flags;
-       spin_lock_irqsave(&fifo->fifo_lock, flags);
+       can_spin_irqflags_t flags;
+       can_spin_lock_irqsave(&fifo->fifo_lock, flags);
        if(!(slot->next=fifo->head))
                fifo->tail=&slot->next;
        fifo->head=slot;
-       spin_unlock_irqrestore(&fifo->fifo_lock, flags);
+       can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
        return 1;
 }
 
@@ -269,10 +269,13 @@ struct chip_t;
  * @filtid: the possible CAN message identifiers filter.
  * @filtmask: the filter mask, the comparison considers only
  *     @filtid bits corresponding to set bits in the @filtmask field.
- * @inpeers: the lists of all peer FIFOs connected by their
+ * @inpeers: the lists of all peers FIFOs connected by their
  *     input side (@inends) to the same terminal (@struct canque_ends_t).
- * @outpeers: the lists of all peer FIFOs connected by their
+ * @outpeers: the lists of all peers FIFOs connected by their
  *     output side (@outends) to the same terminal (@struct canque_ends_t).
+ * @activepeers: the lists of peers FIFOs connected by their
+ *     output side (@outends) to the same terminal (@struct canque_ends_t)
+ *     with same priority and active state.
  * @inends: the pointer to the FIFO input side terminal (@struct canque_ends_t).
  * @outends: the pointer to the FIFO output side terminal (@struct canque_ends_t).
  * @edge_used: the atomic usage counter, mainly used for safe destruction of the edge.
@@ -289,6 +292,7 @@ struct canque_edge_t {
        unsigned long filtmask;
        struct list_head inpeers;
        struct list_head outpeers;
+       struct list_head activepeers;
        struct canque_ends_t *inends;
        struct canque_ends_t *outends;
        atomic_t edge_used;
@@ -328,7 +332,8 @@ struct canque_ends_t {
        struct list_head active[CANQUEUE_PRIO_NR];
        struct list_head idle;
        struct list_head inlist;
-       spinlock_t ends_lock;   /* spin_lock_irqsave / spin_lock_irqrestore */
+       struct list_head outlist;
+       can_spinlock_t ends_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
        void (*notify)(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what);
        void *context;
        union {
@@ -336,6 +341,9 @@ struct canque_ends_t {
                        wait_queue_head_t readq;
                        wait_queue_head_t writeq;
                        wait_queue_head_t emptyq;
+                   #ifdef CAN_ENABLE_KERN_FASYNC
+                       struct fasync_struct *fasync;
+                   #endif /*CAN_ENABLE_KERN_FASYNC*/
                } fileinfo;
                struct {
                        wait_queue_head_t daemonq;
@@ -410,23 +418,23 @@ void canque_notify_bothends(struct canque_edge_t *qedge, int what)
 static inline
 void canque_activate_edge(struct canque_ends_t *inends, struct canque_edge_t *qedge)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        struct canque_ends_t *outends;
        if(qedge->edge_prio>=CANQUEUE_PRIO_NR)
                qedge->edge_prio=CANQUEUE_PRIO_NR-1;
-       spin_lock_irqsave(&inends->ends_lock, flags);
+       can_spin_lock_irqsave(&inends->ends_lock, flags);
        if((outends=qedge->outends)){
-               spin_lock(&outends->ends_lock);
-               spin_lock(&qedge->fifo.fifo_lock);
+               can_spin_lock(&outends->ends_lock);
+               can_spin_lock(&qedge->fifo.fifo_lock);
                if(!canque_fifo_test_fl(&qedge->fifo,EMPTY)){
-                       list_del(&qedge->outpeers);
-                       list_add_tail(&qedge->outpeers,&outends->active[qedge->edge_prio]);
+                       list_del(&qedge->activepeers);
+                       list_add_tail(&qedge->activepeers,&outends->active[qedge->edge_prio]);
                }
-               spin_unlock(&qedge->fifo.fifo_lock);
-               spin_unlock(&outends->ends_lock);
+               can_spin_unlock(&qedge->fifo.fifo_lock);
+               can_spin_unlock(&outends->ends_lock);
 
        }
-       spin_unlock_irqrestore(&inends->ends_lock, flags);
+       can_spin_unlock_irqrestore(&inends->ends_lock, flags);
 }
 
 /**
@@ -479,6 +487,13 @@ int canqueue_connect_edge(struct canque_edge_t *qedge, struct canque_ends_t *ine
 
 int canqueue_ends_init_gen(struct canque_ends_t *qends);
 
+void canqueue_block_inlist(struct canque_ends_t *qends);
+
+void canqueue_block_outlist(struct canque_ends_t *qends);
+
+int canqueue_ends_kill_inlist(struct canque_ends_t *qends, int send_rest);
+
+int canqueue_ends_kill_outlist(struct canque_ends_t *qends);
 
 /* edge reference and traversal functions */
 
@@ -493,34 +508,34 @@ void canque_edge_incref(struct canque_edge_t *edge)
 static inline
 void canque_edge_decref(struct canque_edge_t *edge)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        struct canque_ends_t *inends=edge->inends;
        struct canque_ends_t *outends=edge->outends;
        int dead_fl;
        
-       spin_lock_irqsave(&inends->ends_lock, flags);
-       spin_lock(&outends->ends_lock);
+       can_spin_lock_irqsave(&inends->ends_lock, flags);
+       can_spin_lock(&outends->ends_lock);
        if(atomic_dec_and_test(&edge->edge_used)) {
                dead_fl=canque_fifo_test_and_set_fl(&edge->fifo,DEAD);
                /*This should not be there, but it cannot be outside of the lock :-(*/
                canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
-               spin_unlock(&outends->ends_lock);
-               spin_unlock_irqrestore(&inends->ends_lock, flags);
+               can_spin_unlock(&outends->ends_lock);
+               can_spin_unlock_irqrestore(&inends->ends_lock, flags);
                canque_edge_do_dead(edge, dead_fl);
        } else {
-               spin_unlock(&outends->ends_lock);
-               spin_unlock_irqrestore(&inends->ends_lock, flags);
+               can_spin_unlock(&outends->ends_lock);
+               can_spin_unlock_irqrestore(&inends->ends_lock, flags);
        }
 }
 
 static inline
 struct canque_edge_t *canque_first_inedge(struct canque_ends_t *qends)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        struct list_head *entry;
        struct canque_edge_t *edge;
        
-       spin_lock_irqsave(&qends->ends_lock, flags);
+       can_spin_lock_irqsave(&qends->ends_lock, flags);
        entry=qends->inlist.next;
     skip_dead:
        if(entry != &qends->inlist) {
@@ -533,7 +548,7 @@ struct canque_edge_t *canque_first_inedge(struct canque_ends_t *qends)
        } else {
                edge=NULL;
        }
-       spin_unlock_irqrestore(&qends->ends_lock, flags);
+       can_spin_unlock_irqrestore(&qends->ends_lock, flags);
        return edge;
 }
 
@@ -541,11 +556,11 @@ struct canque_edge_t *canque_first_inedge(struct canque_ends_t *qends)
 static inline
 struct canque_edge_t *canque_next_inedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
 {
-       unsigned long flags;
+       can_spin_irqflags_t flags;
        struct list_head *entry;
        struct canque_edge_t *next;
        
-       spin_lock_irqsave(&qends->ends_lock, flags);
+       can_spin_lock_irqsave(&qends->ends_lock, flags);
        entry=edge->inpeers.next;
     skip_dead:
        if(entry != &qends->inlist) {
@@ -558,7 +573,7 @@ struct canque_edge_t *canque_next_inedge(struct canque_ends_t *qends, struct can
        } else {
                next=NULL;
        }
-       spin_unlock_irqrestore(&qends->ends_lock, flags);
+       can_spin_unlock_irqrestore(&qends->ends_lock, flags);
        canque_edge_decref(edge);
        return next;
 }
@@ -566,6 +581,59 @@ struct canque_edge_t *canque_next_inedge(struct canque_ends_t *qends, struct can
 #define canque_for_each_inedge(qends, edge) \
            for(edge=canque_first_inedge(qends);edge;edge=canque_next_inedge(qends, edge))
 
+static inline
+struct canque_edge_t *canque_first_outedge(struct canque_ends_t *qends)
+{
+       can_spin_irqflags_t flags;
+       struct list_head *entry;
+       struct canque_edge_t *edge;
+       
+       can_spin_lock_irqsave(&qends->ends_lock, flags);
+       entry=qends->outlist.next;
+    skip_dead:
+       if(entry != &qends->outlist) {
+               edge=list_entry(entry,struct canque_edge_t,outpeers);
+               if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
+                       entry=entry->next;
+                       goto skip_dead;
+               }
+               canque_edge_incref(edge);
+       } else {
+               edge=NULL;
+       }
+       can_spin_unlock_irqrestore(&qends->ends_lock, flags);
+       return edge;
+}
+
+
+static inline
+struct canque_edge_t *canque_next_outedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
+{
+       can_spin_irqflags_t flags;
+       struct list_head *entry;
+       struct canque_edge_t *next;
+       
+       can_spin_lock_irqsave(&qends->ends_lock, flags);
+       entry=edge->outpeers.next;
+    skip_dead:
+       if(entry != &qends->outlist) {
+               next=list_entry(entry,struct canque_edge_t,outpeers);
+               if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
+                       entry=entry->next;
+                       goto skip_dead;
+               }
+               canque_edge_incref(next);
+       } else {
+               next=NULL;
+       }
+       can_spin_unlock_irqrestore(&qends->ends_lock, flags);
+       canque_edge_decref(edge);
+       return next;
+}
+
+#define canque_for_each_outedge(qends, edge) \
+           for(edge=canque_first_outedge(qends);edge;edge=canque_next_outedge(qends, edge))
+
 /* Linux kernel specific functions */
 
 struct canque_edge_t *canque_new_edge_kern(int slotsnr);