[PATCH v4 2/3] llist: add a safe version of llist_for_each_entry

From: Roger Pau Monne
Date: Thu Dec 13 2012 - 05:48:17 EST


Signed-off-by: Roger Pau Monnà <roger.pau@xxxxxxxxxx>
Cc: Huang Ying <ying.huang@xxxxxxxxx>
Cc: Konrad Rzeszutek Wilk <konrad@xxxxxxxxxx>
---
Changes since v3:
* Change n to use type *, to keep the same semantics as
list_for_each_entry_safe.
Changes since v2:
* Allow to pass a NULL node as the first entry of deleted list
entries.
---
include/linux/llist.h | 29 +++++++++++++++++++++++++++++
1 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/include/linux/llist.h b/include/linux/llist.h
index a5199f6..f551ac4 100644
--- a/include/linux/llist.h
+++ b/include/linux/llist.h
@@ -125,6 +125,35 @@ static inline void init_llist_head(struct llist_head *list)
(pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))

/**
+ * llist_for_each_entry_safe - safely iterate over some deleted entries of
+ * lock-less list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @node: the fist entry of deleted list entries.
+ * @member: the name of the llist_node with the struct.
+ *
+ * In general, some entries of the lock-less list can be traversed
+ * safely only after being removed from list, so start with an entry
+ * instead of list head.
+ *
+ * If being used on entries deleted from lock-less list directly, the
+ * traverse order is from the newest to the oldest added entry. If
+ * you want to traverse from the oldest to the newest, you must
+ * reverse the order by yourself before traversing.
+ *
+ * n is used to store a reference to the next item llist_node, so
+ * pos can be freed while iterating.
+ */
+#define llist_for_each_entry_safe(pos, n, node, member) \
+ for ((pos) = llist_entry((node), typeof(*(pos)), member), \
+ (n) = llist_entry(((&(pos)->member != NULL) ? \
+ (pos)->member.next : NULL), typeof(*(pos)), member); \
+ &(pos)->member != NULL; \
+ (pos) = n, \
+ (n) = llist_entry(((&(pos)->member != NULL) ? \
+ (pos)->member.next : NULL), typeof(*(pos)), member))
+
+/**
* llist_empty - tests whether a lock-less list is empty
* @head: the list to test
*
--
1.7.7.5 (Apple Git-26)

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/