Skip to contentSkip to conversation

eal/rwlocks: Try read/write and relock write to read locks added.

PATCH In review Leonid Myravjev 2 participants 3 messages May 21, 2018 – Mar 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/rwlocks: Try read/write and relock write to read locks added.

From: Leonid Myravjev <[email protected]>

---
 lib/librte_eal/common/include/generic/rte_rwlock.h | 61 ++++++++++++++++++++++
 1 file changed, 61 insertions(+)
lib/librte_eal/common/include/generic/rte_rwlock.h +61 -0
index 899e9bc43..11212e2b8 100644
@@ -76,6 +76,30 @@ rte_rwlock_read_lock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Try take lock a read lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_read_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* write lock is held */
+	if (x < 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, x, x + 1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
+
+/**
  * Release a read lock.
  *
  * @param rwl
@@ -110,6 +134,29 @@ rte_rwlock_write_lock(rte_rwlock_t *rwl)
 					      0, -1);
 	}
 }
+/**
+ * Try take a write lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_write_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* a lock is held */
+	if (x != 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, 0, -1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
 
 /**
  * Release a write lock.
@@ -124,6 +171,20 @@ rte_rwlock_write_unlock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Relock write lock to read
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ */
+static inline void
+rte_rwlock_write_relock_read(rte_rwlock_t *rwl)
+{
+	rte_atomic32_add((rte_atomic32_t *)(intptr_t)&rwl->cnt, 2);
+}
+
+
+
+/**
  * Try to execute critical section in a hardware memory transaction, if it
  * fails or not available take a read lock
  *
Signature · 1 line
2.13.0
#1 lore raw

Hi,

Unfortunately, after 2 months, nobody reviewed this patch.

You could motivate some reviews by providing some explanations or context of use.

21/05/2018 11:08, Leonid Myravjev:

Quoted 94 lines

From: Leonid Myravjev <[email protected]>

---
 lib/librte_eal/common/include/generic/rte_rwlock.h | 61 ++++++++++++++++++++++
 1 file changed, 61 insertions(+)
lib/librte_eal/common/include/generic/rte_rwlock.h +61 -0
index 899e9bc43..11212e2b8 100644
@@ -76,6 +76,30 @@ rte_rwlock_read_lock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Try take lock a read lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_read_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* write lock is held */
+	if (x < 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, x, x + 1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
+
+/**
  * Release a read lock.
  *
  * @param rwl
@@ -110,6 +134,29 @@ rte_rwlock_write_lock(rte_rwlock_t *rwl)
 					      0, -1);
 	}
 }
+/**
+ * Try take a write lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_write_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* a lock is held */
+	if (x != 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, 0, -1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
 
 /**
  * Release a write lock.
@@ -124,6 +171,20 @@ rte_rwlock_write_unlock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Relock write lock to read
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ */
+static inline void
+rte_rwlock_write_relock_read(rte_rwlock_t *rwl)
+{
+	rte_atomic32_add((rte_atomic32_t *)(intptr_t)&rwl->cnt, 2);
+}
+
+
+
+/**
  * Try to execute critical section in a hardware memory transaction, if it
  * fails or not available take a read lock
  *
#2 lore raw

Closing this proposal.

For reference, an implementation with test has been merged 2 years ago:
https://inbox.dpdk.org/dev/[email protected]/

12/07/2018 15:55, Thomas Monjalon:

Quoted 95 lines

Hi,

Unfortunately, after 2 months, nobody reviewed this patch.

You could motivate some reviews by providing some explanations or context of use.

21/05/2018 11:08, Leonid Myravjev:

Quoted 86 lines

From: Leonid Myravjev <[email protected]>

---
 lib/librte_eal/common/include/generic/rte_rwlock.h | 61 ++++++++++++++++++++++
 1 file changed, 61 insertions(+)
lib/librte_eal/common/include/generic/rte_rwlock.h +57 -0
index 899e9bc43..11212e2b8 100644
@@ -76,6 +76,30 @@ rte_rwlock_read_lock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Try take lock a read lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_read_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* write lock is held */
+	if (x < 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, x, x + 1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
+
+/**
  * Release a read lock.
  *
  * @param rwl
@@ -110,6 +134,29 @@ rte_rwlock_write_lock(rte_rwlock_t *rwl)
 					      0, -1);
 	}
 }
+/**
+ * Try take a write lock.
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ * @return
+ *   1 if the lock is successfully taken; 0 otherwise.
+ */
+static inline int
+rte_rwlock_write_trylock(rte_rwlock_t *rwl)
+{
+	int32_t x;
+	int success = 0;
+
+	x = rwl->cnt;
+	/* a lock is held */
+	if (x != 0)
+		return 0;
+	success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt, 0, -1);
+	if (success == 0)
+		return 0;
+	return 1;
+}
 
 /**
  * Release a write lock.
@@ -124,6 +171,20 @@ rte_rwlock_write_unlock(rte_rwlock_t *rwl)
 }
 
 /**
+ * Relock write lock to read
+ *
+ * @param rwl
+ *   A pointer to a rwlock structure.
+ */
+static inline void
+rte_rwlock_write_relock_read(rte_rwlock_t *rwl)
+{
+	rte_atomic32_add((rte_atomic32_t *)(intptr_t)&rwl->cnt, 2);
+}