xref: /openbmc/linux/include/linux/klist.h (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*e311edbcSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
29a19fea4Smochel@digitalimplant.org /*
39a19fea4Smochel@digitalimplant.org  *	klist.h - Some generic list helpers, extending struct list_head a bit.
49a19fea4Smochel@digitalimplant.org  *
59a19fea4Smochel@digitalimplant.org  *	Implementations are found in lib/klist.c
69a19fea4Smochel@digitalimplant.org  *
79a19fea4Smochel@digitalimplant.org  *	Copyright (C) 2005 Patrick Mochel
89a19fea4Smochel@digitalimplant.org  */
99a19fea4Smochel@digitalimplant.org 
10d856f1e3SJames Bottomley #ifndef _LINUX_KLIST_H
11d856f1e3SJames Bottomley #define _LINUX_KLIST_H
12d856f1e3SJames Bottomley 
139a19fea4Smochel@digitalimplant.org #include <linux/spinlock.h>
149a19fea4Smochel@digitalimplant.org #include <linux/kref.h>
159a19fea4Smochel@digitalimplant.org #include <linux/list.h>
169a19fea4Smochel@digitalimplant.org 
1734bb61f9SJames Bottomley struct klist_node;
189a19fea4Smochel@digitalimplant.org struct klist {
199a19fea4Smochel@digitalimplant.org 	spinlock_t		k_lock;
209a19fea4Smochel@digitalimplant.org 	struct list_head	k_list;
2134bb61f9SJames Bottomley 	void			(*get)(struct klist_node *);
2234bb61f9SJames Bottomley 	void			(*put)(struct klist_node *);
23795abaf1SDavid Miller } __attribute__ ((aligned (sizeof(void *))));
249a19fea4Smochel@digitalimplant.org 
251da43e4aSTejun Heo #define KLIST_INIT(_name, _get, _put)					\
261da43e4aSTejun Heo 	{ .k_lock	= __SPIN_LOCK_UNLOCKED(_name.k_lock),		\
271da43e4aSTejun Heo 	  .k_list	= LIST_HEAD_INIT(_name.k_list),			\
281da43e4aSTejun Heo 	  .get		= _get,						\
291da43e4aSTejun Heo 	  .put		= _put, }
301da43e4aSTejun Heo 
311da43e4aSTejun Heo #define DEFINE_KLIST(_name, _get, _put)					\
321da43e4aSTejun Heo 	struct klist _name = KLIST_INIT(_name, _get, _put)
339a19fea4Smochel@digitalimplant.org 
3434bb61f9SJames Bottomley extern void klist_init(struct klist *k, void (*get)(struct klist_node *),
3534bb61f9SJames Bottomley 		       void (*put)(struct klist_node *));
369a19fea4Smochel@digitalimplant.org 
379a19fea4Smochel@digitalimplant.org struct klist_node {
38a1ed5b0cSTejun Heo 	void			*n_klist;	/* never access directly */
399a19fea4Smochel@digitalimplant.org 	struct list_head	n_node;
409a19fea4Smochel@digitalimplant.org 	struct kref		n_ref;
419a19fea4Smochel@digitalimplant.org };
429a19fea4Smochel@digitalimplant.org 
43d856f1e3SJames Bottomley extern void klist_add_tail(struct klist_node *n, struct klist *k);
44d856f1e3SJames Bottomley extern void klist_add_head(struct klist_node *n, struct klist *k);
450f9859caSKen Helias extern void klist_add_behind(struct klist_node *n, struct klist_node *pos);
4693dd4001STejun Heo extern void klist_add_before(struct klist_node *n, struct klist_node *pos);
479a19fea4Smochel@digitalimplant.org 
489a19fea4Smochel@digitalimplant.org extern void klist_del(struct klist_node *n);
499a19fea4Smochel@digitalimplant.org extern void klist_remove(struct klist_node *n);
509a19fea4Smochel@digitalimplant.org 
518b0c250bSmochel@digitalimplant.org extern int klist_node_attached(struct klist_node *n);
528b0c250bSmochel@digitalimplant.org 
539a19fea4Smochel@digitalimplant.org 
549a19fea4Smochel@digitalimplant.org struct klist_iter {
559a19fea4Smochel@digitalimplant.org 	struct klist		*i_klist;
569a19fea4Smochel@digitalimplant.org 	struct klist_node	*i_cur;
579a19fea4Smochel@digitalimplant.org };
589a19fea4Smochel@digitalimplant.org 
599a19fea4Smochel@digitalimplant.org 
609a19fea4Smochel@digitalimplant.org extern void klist_iter_init(struct klist *k, struct klist_iter *i);
619a19fea4Smochel@digitalimplant.org extern void klist_iter_init_node(struct klist *k, struct klist_iter *i,
629a19fea4Smochel@digitalimplant.org 				 struct klist_node *n);
639a19fea4Smochel@digitalimplant.org extern void klist_iter_exit(struct klist_iter *i);
642e0fed7fSAndy Shevchenko extern struct klist_node *klist_prev(struct klist_iter *i);
659a19fea4Smochel@digitalimplant.org extern struct klist_node *klist_next(struct klist_iter *i);
669a19fea4Smochel@digitalimplant.org 
67d856f1e3SJames Bottomley #endif
68