klist.h 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *	klist.h - Some generic list helpers, extending struct list_head a bit.
 *
 *	Implementations are found in lib/klist.c
 *
 *
 *	Copyright (C) 2005 Patrick Mochel
 *
 *	This file is rleased under the GPL v2.
 */

12 13 14
#ifndef _LINUX_KLIST_H
#define _LINUX_KLIST_H

15 16 17 18 19
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/kref.h>
#include <linux/list.h>

20
struct klist_node;
21 22 23
struct klist {
	spinlock_t		k_lock;
	struct list_head	k_list;
24 25
	void			(*get)(struct klist_node *);
	void			(*put)(struct klist_node *);
26 27
};

28 29 30 31 32 33 34 35
#define KLIST_INIT(_name, _get, _put)					\
	{ .k_lock	= __SPIN_LOCK_UNLOCKED(_name.k_lock),		\
	  .k_list	= LIST_HEAD_INIT(_name.k_list),			\
	  .get		= _get,						\
	  .put		= _put, }

#define DEFINE_KLIST(_name, _get, _put)					\
	struct klist _name = KLIST_INIT(_name, _get, _put)
36

37
extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
38
		       void (*put)(struct klist_node *));
39 40

struct klist_node {
41
	struct klist		*n_klist;
42 43 44 45 46
	struct list_head	n_node;
	struct kref		n_ref;
	struct completion	n_removed;
};

47 48
extern void klist_add_tail(struct klist_node *n, struct klist *k);
extern void klist_add_head(struct klist_node *n, struct klist *k);
49 50
extern void klist_add_after(struct klist_node *n, struct klist_node *pos);
extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
51

52 53
extern void klist_del(struct klist_node *n);
extern void klist_remove(struct klist_node *n);
54

55
extern int klist_node_attached(struct klist_node *n);
56

57 58

struct klist_iter {
59 60 61
	struct klist		*i_klist;
	struct list_head	*i_head;
	struct klist_node	*i_cur;
62 63 64
};


65 66 67 68 69
extern void klist_iter_init(struct klist *k, struct klist_iter *i);
extern void klist_iter_init_node(struct klist *k, struct klist_iter *i,
				 struct klist_node *n);
extern void klist_iter_exit(struct klist_iter *i);
extern struct klist_node *klist_next(struct klist_iter *i);
70

71
#endif