[dpdk-dev] [PATCH 1/2] rwlock: introduce 'try' semantics for RD and WR locking

Ananyev, Konstantin konstantin.ananyev at intel.com
Wed Dec 19 17:27:42 CET 2018



> > > + * try to take a write lock.
> > > + *
> > > + * @param rwl
> > > + *   A pointer to a rwlock structure.
> > > + * @return
> > > + *   - zero if the lock is successfully taken
> > > + *   - -EBUSY if lock could not be acquired for writing because
> > > + *     it was already locked for reading or writing
> > > + */
> > > +static inline __rte_experimental int
> > > +rte_rwlock_write_trylock(rte_rwlock_t *rwl) {
> > > +	int32_t x;
> > > +	int success = 0;
> > > +
> > > +	while (success == 0) {
> (Apologies for not thinking through all my comments earlier)
> I am wondering if the 'while' loop is required for the write lock.
> 
> > > +		x = rwl->cnt;
> > > +		/* a lock is held */
> > > +		if (x != 0)
> > > +			return -EBUSY;
> > > +		success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt,
> > > +					      0, (uint32_t)-1);
> This might fail as the lock was taken (either reader or writer). We should return from here as it already indicates that the lock is not
> available for the writer to take. Otherwise, there is a possibility that the while loop will run for multiple iterations. I would think, from the
> user's expectation, it might not be correct.

Good point  - it will also save us extra read in case of failure.
Will change in v2.
Konstantin

> 
> In the read_trylock, the while loop should be fine because the write lock itself is not held. The failure could be because some other reader
> incremented the counter before we did. i.e. the reader lock itself may be available to take in the next iteration.
> 
> > > +	}
> > > +	return 0;
> > > +}
> > > +
>


More information about the dev mailing list