Whamcloud - gitweb
LU-1346 libcfs: cleanup libcfs atomic primitives
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_hash.h
index ceaa570..5e7c9de 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -28,6 +26,8 @@
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -133,8 +133,8 @@ struct cfs_hash_lock_ops;
 struct cfs_hash_hlist_ops;
 
 typedef union {
-        cfs_rwlock_t                rw;             /**< rwlock */
-        cfs_spinlock_t              spin;           /**< spinlock */
+       rwlock_t                rw;             /**< rwlock */
+       spinlock_t              spin;           /**< spinlock */
 } cfs_hash_lock_t;
 
 /**
@@ -149,12 +149,12 @@ typedef union {
  * - some extra bytes (caller can require it while creating hash)
  */
 typedef struct cfs_hash_bucket {
-        cfs_hash_lock_t             hsb_lock;       /**< bucket lock */
-        __u32                       hsb_count;      /**< current entries */
-        __u32                       hsb_version;    /**< change version */
-        unsigned int                hsb_index;      /**< index of bucket */
-        int                         hsb_depmax;     /**< max depth on bucket */
-        char                        hsb_head[0];    /**< hash-head array */
+       cfs_hash_lock_t         hsb_lock;       /**< bucket lock */
+       __u32                   hsb_count;      /**< current entries */
+       __u32                   hsb_version;    /**< change version */
+       unsigned int            hsb_index;      /**< index of bucket */
+       int                     hsb_depmax;     /**< max depth on bucket */
+       long                    hsb_head[0];    /**< hash-head array */
 } cfs_hash_bucket_t;
 
 /**
@@ -272,12 +272,12 @@ typedef struct cfs_hash {
         /** hash list operations */
         struct cfs_hash_hlist_ops  *hs_hops;
         /** hash buckets-table */
-        cfs_hash_bucket_t         **hs_buckets;
-        /** total number of items on this hash-table */
-        cfs_atomic_t                hs_count;
-        /** hash flags, see cfs_hash_tag for detail */
-        __u16                       hs_flags;
-        /** # of extra-bytes for bucket, for user saving extended attributes */
+       cfs_hash_bucket_t         **hs_buckets;
+       /** total number of items on this hash-table */
+       atomic_t                hs_count;
+       /** hash flags, see cfs_hash_tag for detail */
+       __u16                       hs_flags;
+       /** # of extra-bytes for bucket, for user saving extended attributes */
         __u16                       hs_extra_bytes;
         /** wants to iterate */
         __u8                        hs_iterating;
@@ -301,15 +301,15 @@ typedef struct cfs_hash {
         __u32                       hs_rehash_count;
         /** # of iterators (caller of cfs_hash_for_each_*) */
         __u32                       hs_iterators;
-        /** rehash workitem */
-        cfs_workitem_t              hs_rehash_wi;
-        /** refcount on this hash table */
-        cfs_atomic_t                hs_refcount;
-        /** rehash buckets-table */
-        cfs_hash_bucket_t         **hs_rehash_buckets;
+       /** rehash workitem */
+       cfs_workitem_t              hs_rehash_wi;
+       /** refcount on this hash table */
+       atomic_t                    hs_refcount;
+       /** rehash buckets-table */
+       cfs_hash_bucket_t         **hs_rehash_buckets;
 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
         /** serialize debug members */
-        cfs_spinlock_t              hs_dep_lock;
+       spinlock_t                      hs_dep_lock;
         /** max depth */
         unsigned int                hs_dep_max;
         /** id of the deepest bucket */
@@ -356,7 +356,10 @@ typedef struct cfs_hash_ops {
         void *   (*hs_key)(cfs_hlist_node_t *hnode);
         /** copy key from @hnode to @key */
         void     (*hs_keycpy)(cfs_hlist_node_t *hnode, void *key);
-        /** compare @key with key of @hnode */
+       /**
+        *  compare @key with key of @hnode
+        *  returns 1 on a match
+        */
         int      (*hs_keycmp)(const void *key, cfs_hlist_node_t *hnode);
         /** return object address of @hnode, i.e: container_of(...hnode) */
         void *   (*hs_object)(cfs_hlist_node_t *hnode);
@@ -580,10 +583,10 @@ static inline void cfs_hash_unlock(cfs_hash_t *hs, int excl)
 }
 
 static inline int cfs_hash_dec_and_lock(cfs_hash_t *hs,
-                                        cfs_atomic_t *condition)
+                                       atomic_t *condition)
 {
-        LASSERT(cfs_hash_with_no_bktlock(hs));
-        return cfs_atomic_dec_and_lock(condition, &hs->hs_lock.spin);
+       LASSERT(cfs_hash_with_no_bktlock(hs));
+       return atomic_dec_and_lock(condition, &hs->hs_lock.spin);
 }
 
 static inline void cfs_hash_bd_lock(cfs_hash_t *hs,
@@ -670,11 +673,10 @@ void cfs_hash_bd_move_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd_old,
                              cfs_hash_bd_t *bd_new, cfs_hlist_node_t *hnode);
 
 static inline int cfs_hash_bd_dec_and_lock(cfs_hash_t *hs, cfs_hash_bd_t *bd,
-                                           cfs_atomic_t *condition)
+                                          atomic_t *condition)
 {
-        LASSERT(cfs_hash_with_spin_bktlock(hs));
-        return cfs_atomic_dec_and_lock(condition,
-                                       &bd->bd_bucket->hsb_lock.spin);
+       LASSERT(cfs_hash_with_spin_bktlock(hs));
+       return atomic_dec_and_lock(condition, &bd->bd_bucket->hsb_lock.spin);
 }
 
 static inline cfs_hlist_head_t *cfs_hash_bd_hhead(cfs_hash_t *hs,
@@ -685,6 +687,8 @@ static inline cfs_hlist_head_t *cfs_hash_bd_hhead(cfs_hash_t *hs,
 
 cfs_hlist_node_t *cfs_hash_bd_lookup_locked(cfs_hash_t *hs,
                                             cfs_hash_bd_t *bd, const void *key);
+cfs_hlist_node_t *cfs_hash_bd_peek_locked(cfs_hash_t *hs,
+                                         cfs_hash_bd_t *bd, const void *key);
 cfs_hlist_node_t *cfs_hash_bd_findadd_locked(cfs_hash_t *hs,
                                              cfs_hash_bd_t *bd, const void *key,
                                              cfs_hlist_node_t *hnode,
@@ -826,8 +830,8 @@ static inline int __cfs_hash_theta_frac(int theta)
 
 static inline int __cfs_hash_theta(cfs_hash_t *hs)
 {
-        return (cfs_atomic_read(&hs->hs_count) <<
-                CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
+       return (atomic_read(&hs->hs_count) <<
+               CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
 }
 
 static inline void __cfs_hash_set_theta(cfs_hash_t *hs, int min, int max)