Request for review, time_pps_fetch() enhancement

Konstantin Belousov kostikbel at gmail.com
Sun Feb 10 10:37:54 UTC 2013


On Sat, Feb 09, 2013 at 02:47:06PM +0100, Jilles Tjoelker wrote:
> On Wed, Feb 06, 2013 at 05:58:30PM +0200, Konstantin Belousov wrote:
> > On Tue, Feb 05, 2013 at 09:41:38PM -0700, Ian Lepore wrote:
> > > I'd like feedback on the attached patch, which adds support to our
> > > time_pps_fetch() implementation for the blocking behaviors described in
> > > section 3.4.3 of RFC 2783.  The existing implementation can only return
> > > the most recently captured data without blocking.  These changes add the
> > > ability to block (forever or with timeout) until a new event occurs.
> 
> > > Index: sys/kern/kern_tc.c
> > > ===================================================================
> > > --- sys/kern/kern_tc.c	(revision 246337)
> > > +++ sys/kern/kern_tc.c	(working copy)
> > > @@ -1446,6 +1446,50 @@
> > >   * RFC 2783 PPS-API implementation.
> > >   */
> > >  
> > > +static int
> > > +pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
> > > +{
> > > [snip]
> > > +		aseq = pps->ppsinfo.assert_sequence;
> > > +		cseq = pps->ppsinfo.clear_sequence;
> > > +		while (aseq == pps->ppsinfo.assert_sequence &&
> > > +		    cseq == pps->ppsinfo.clear_sequence) {
> > Note that compilers are allowed to optimize these accesses even over
> > the sequential point, which is the tsleep() call. Only accesses to
> > volatile objects are forbidden to be rearranged.
> 
> > I suggest to add volatile casts to pps in the loop condition.
> 
> The memory pointed to by pps is global (other code may have a pointer to
> it); therefore, the compiler must assume that the tsleep() call (which
> invokes code in a different compilation unit) may modify it.
> 
> Because volatile does not make concurrent access by multiple threads
> defined either, adding it here only seems to slow down the code
> (potentially).
The volatile guarantees that the compiler indeed reloads the value on
read access. Conceptually, the tsleep() does not modify or even access
the checked fields, and compiler is allowed to note this by whatever
methods (LTO ?).

More, the standard says that an implementation is allowed to not evaluate
part of the expression if no side effects are produced, even by calling
a function.

I agree that for practical means, the _currently_ used compilers should
consider the tsleep() call as the sequential point. But then the volatile
qualifier cast applied for the given access would not change the code as
well.

> 
> > > +			err = tsleep(pps, PCATCH, "ppsfch", timo);
> > > +			if (err == EWOULDBLOCK && fapi->timeout.tv_sec == -1) {
> > > +				continue;
> > > +			} else if (err != 0) {
> > > +				return (err);
> > > +			}
> > > +		}
> > > +	}
> -- 
> Jilles Tjoelker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20130210/e8bc199e/attachment.sig>


More information about the freebsd-hackers mailing list