blob: 8ea98db223e567db691efb28fe1153d60d2f6d78 [file] [log] [blame]
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08001/*
2 * klist.h - Some generic list helpers, extending struct list_head a bit.
3 *
4 * Implementations are found in lib/klist.c
5 *
6 *
7 * Copyright (C) 2005 Patrick Mochel
8 *
9 * This file is rleased under the GPL v2.
10 */
11
James Bottomleyd856f1e32005-08-19 09:14:01 -040012#ifndef _LINUX_KLIST_H
13#define _LINUX_KLIST_H
14
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080015#include <linux/spinlock.h>
16#include <linux/completion.h>
17#include <linux/kref.h>
18#include <linux/list.h>
19
James Bottomley34bb61f2005-09-06 16:56:51 -070020struct klist_node;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080021struct klist {
22 spinlock_t k_lock;
23 struct list_head k_list;
James Bottomley34bb61f2005-09-06 16:56:51 -070024 void (*get)(struct klist_node *);
25 void (*put)(struct klist_node *);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080026};
27
Tejun Heo1da43e42008-04-26 03:16:04 +090028#define KLIST_INIT(_name, _get, _put) \
29 { .k_lock = __SPIN_LOCK_UNLOCKED(_name.k_lock), \
30 .k_list = LIST_HEAD_INIT(_name.k_list), \
31 .get = _get, \
32 .put = _put, }
33
34#define DEFINE_KLIST(_name, _get, _put) \
35 struct klist _name = KLIST_INIT(_name, _get, _put)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080036
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070037extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
James Bottomley34bb61f2005-09-06 16:56:51 -070038 void (*put)(struct klist_node *));
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080039
40struct klist_node {
Tejun Heoa1ed5b02008-08-25 19:50:16 +020041 void *n_klist; /* never access directly */
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080042 struct list_head n_node;
43 struct kref n_ref;
44 struct completion n_removed;
45};
46
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070047extern void klist_add_tail(struct klist_node *n, struct klist *k);
48extern void klist_add_head(struct klist_node *n, struct klist *k);
Tejun Heo93dd4002008-04-22 18:58:46 +090049extern void klist_add_after(struct klist_node *n, struct klist_node *pos);
50extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080051
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070052extern void klist_del(struct klist_node *n);
53extern void klist_remove(struct klist_node *n);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080054
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070055extern int klist_node_attached(struct klist_node *n);
mochel@digitalimplant.org8b0c2502005-03-24 12:58:57 -080056
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080057
58struct klist_iter {
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070059 struct klist *i_klist;
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070060 struct klist_node *i_cur;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080061};
62
63
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070064extern void klist_iter_init(struct klist *k, struct klist_iter *i);
65extern void klist_iter_init_node(struct klist *k, struct klist_iter *i,
66 struct klist_node *n);
67extern void klist_iter_exit(struct klist_iter *i);
68extern struct klist_node *klist_next(struct klist_iter *i);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080069
James Bottomleyd856f1e32005-08-19 09:14:01 -040070#endif