From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 29 09:14:00 2005 Return-Path: X-Original-To: freebsd-hackers@FreeBSD.org Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4E03C16A41C for ; Wed, 29 Jun 2005 09:14:00 +0000 (GMT) (envelope-from jhb@FreeBSD.org) Received: from mail24.sea5.speakeasy.net (mail24.sea5.speakeasy.net [69.17.117.26]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2318343D53 for ; Wed, 29 Jun 2005 09:14:00 +0000 (GMT) (envelope-from jhb@FreeBSD.org) Received: (qmail 1176 invoked from network); 29 Jun 2005 09:13:59 -0000 Received: from server.baldwin.cx ([216.27.160.63]) (envelope-sender ) by mail24.sea5.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 29 Jun 2005 09:13:59 -0000 Received: from zion.baldwin.cx (zion.baldwin.cx [192.168.0.7]) (authenticated bits=0) by server.baldwin.cx (8.13.1/8.13.1) with ESMTP id j5T9DpHx007252; Wed, 29 Jun 2005 05:13:51 -0400 (EDT) (envelope-from jhb@FreeBSD.org) From: John Baldwin To: freebsd-hackers@FreeBSD.org Date: Wed, 29 Jun 2005 04:52:41 -0400 User-Agent: KMail/1.8 References: <000001c57c80$27dbc6e0$4801a8c0@ws-ew-3.W2KDEMIG> In-Reply-To: <000001c57c80$27dbc6e0$4801a8c0@ws-ew-3.W2KDEMIG> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200506290452.42480.jhb@FreeBSD.org> X-Spam-Status: No, score=-2.8 required=4.2 tests=ALL_TRUSTED autolearn=failed version=3.0.2 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on server.baldwin.cx Cc: Norbert Koch Subject: Re: Posix threads: CLOCK_REALTIME/CLOCK_MONOTONIC X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jun 2005 09:14:00 -0000 On Wednesday 29 June 2005 03:57 am, Norbert Koch wrote: > Hello. > > I am working on a multi-threaded application which > may call settimeofday() and therefore may have > serious problems with timing calculations. > > In my applications I calclulate time differences > using clock_gettime(CLOCK_MONOTONIC) under FreeBSD-5. > Under FreeBSD-4 it is a trivial kernel patch in kern_time.c > to have CLOCK_MONOTONIC, as there already is a kernel function > nanouptime(). > > int > clock_gettime(p, uap) > struct proc *p; > struct clock_gettime_args *uap; > { > struct timespec ats; > > switch (SCARG(uap, clock_id)) { > case CLOCK_REALTIME: > nanotime(&ats); > break; > > case CLOCK_MONOTONIC: > nanouptime(&ats); > break; > > default: > return (EINVAL); > }; > return (copyout(&ats, SCARG(uap, tp), sizeof(ats))); > } > > > > > Looking through the sources of the various threading libraries > I found that either gettimeofday() or clock_gettime(CLOCK_REALTIME) > is used for all calculations. > > I am not sure, what posix currently says about this > but found a chapter 'Condition variable wait clock' in [Butenhof] (p.359). > As I understand it, Posix.1j expects an implementation to > - at least for pthread_cond_timedwait() - use CLOCK_MONOTONIC by default. > They introduce a new function pair pthread_condattr_(get|set)clock() > to change pthread_cond_timedwait() to use either CLOCK_MONOTONIC or > CLOCK_REALTIME. > > >From my understanding of the threading libraries' internals, it > > should be trivial to modify them to using CLOCK_MONOTONIC only, but not > quite as trivial to implement pthread_condattr_(get|set)clock(). > > For FreeBSD-4 I already have a modified libc_r, where I call > clock_gettime(CLOCK_MONOTONIC) once in _thread_init() and set > a global variable _sched_clkid to either CLOCK_MONOTONIC or CLOCK_REALTIME > for further calls to clock_gettime(). > > Any comments/ideas/opinions? You probably want to bring this up on threads@FreeBSD.org as that is where = all=20 the guys who implement the thread libraries hang out. :) =2D-=20 John Baldwin =A0<>< =A0http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" =A0=3D =A0http://www.FreeBSD.org