From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 29 08:01:36 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 D8B3D16A41C for ; Wed, 29 Jun 2005 08:01:36 +0000 (GMT) (envelope-from NKoch@demig.de) Received: from server.absolute-media.de (server.absolute-media.de [213.239.231.9]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6357543D55 for ; Wed, 29 Jun 2005 08:01:35 +0000 (GMT) (envelope-from NKoch@demig.de) Received: from localhost (unknown [127.0.0.1]) by server.absolute-media.de (Postfix) with ESMTP id 6552A66061 for ; Wed, 29 Jun 2005 10:01:34 +0200 (CEST) Received: from server.absolute-media.de ([127.0.0.1]) by localhost (server [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 01788-01 for ; Wed, 29 Jun 2005 10:01:29 +0200 (CEST) Received: from firewall.demig (p5083937F.dip0.t-ipconnect.de [80.131.147.127]) by server.absolute-media.de (Postfix) with ESMTP id 8325280396 for ; Wed, 29 Jun 2005 10:01:29 +0200 (CEST) Received: from ws-ew-3 (ws-ew-3.w2kdemig [192.168.1.72]) by firewall.demig (8.13.4/8.13.1) with SMTP id j5T7vF4e026012 for ; Wed, 29 Jun 2005 09:57:15 +0200 (CEST) (envelope-from NKoch@demig.de) From: "Norbert Koch" To: "Freebsd-Hackers@Freebsd. Org" Date: Wed, 29 Jun 2005 09:57:11 +0200 Message-ID: <000001c57c80$27dbc6e0$4801a8c0@ws-ew-3.W2KDEMIG> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2120.0 X-Virus-Scanned: by amavisd-new X-Virus-Scanned: by amavisd-new at absolute-media.de Subject: 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 08:01:37 -0000 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? Norbert