From owner-freebsd-hackers@FreeBSD.ORG Sat Jul 12 23:38:39 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B2C231065671 for ; Sat, 12 Jul 2008 23:38:38 +0000 (UTC) (envelope-from ioplex@gmail.com) Received: from ik-out-1112.google.com (ik-out-1112.google.com [66.249.90.183]) by mx1.freebsd.org (Postfix) with ESMTP id 49E9E8FC21 for ; Sat, 12 Jul 2008 23:38:37 +0000 (UTC) (envelope-from ioplex@gmail.com) Received: by ik-out-1112.google.com with SMTP id c30so2267855ika.3 for ; Sat, 12 Jul 2008 16:38:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=Pb3lQHUWjU/j2WMQ7vGRyagyBJdzkCqvAYZKROaOmQk=; b=UaApnvro3V6MeknkDVbhVM8dpzfilUW0VaHDJyowoMt81dQOiGdPMi54RPIaP6QsOb 014EAHpwFZVXEwBoaFSiIiaBTcwo07lbWY69IIm/7W9lnsAyK/Z9qlzw9Hxs9nzcPDc0 EFyVdeHC0Temp+EdvwDo4eZMLARX0tSUsxRmM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=nSv1/jynggJeisrukdA9a6CEUsYKSLrmgIoTVF92CKTR+/W92GCcPv1y9wXtNao8Ji u6IPCxzQCPEWg/wRMwg1nyRiLybqjO4IhjVvX897Z1xVMmrFl/nlVn8Nxy9jCPdCfywR 1Oin/ieIsllBfJ1q+YhJq+YWsryh/6ZtuN/IM= Received: by 10.210.67.20 with SMTP id p20mr7757662eba.66.1215904286057; Sat, 12 Jul 2008 16:11:26 -0700 (PDT) Received: by 10.210.139.1 with HTTP; Sat, 12 Jul 2008 16:11:26 -0700 (PDT) Message-ID: <78c6bd860807121611w4f6ab44brbebfffea9929682a@mail.gmail.com> Date: Sat, 12 Jul 2008 19:11:26 -0400 From: "Michael B Allen" To: freebsd-hackers MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: Pls sanity check my semtimedop(2) implementation 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: Sat, 12 Jul 2008 23:38:39 -0000 Hi, Below is a semtimedop(2) implementation that I'm using for FreeBSD. I was hoping someone could look it over and tell me if they think the implementation is sound. The code seems to work ok but when stressing the FreeBSD build of my app I have managed to provoke errors related to concurrency (usually when a SIGALRM goes off). The Linux build works flawlessesly so I'm wondering about this one critical function that is different. Do you think it would make any difference if I used ITIMER_VIRTUAL / SIGVTALRM instead of ITIMER_REAL / SIGALRM? Or perhaps I should be using a different implementation entirely? Mike int _semtimedop(int semid, struct sembuf *array, size_t nops, struct timespec *_timeout) { struct timeval timeout, before, after; struct itimerval value, ovalue; struct sigaction sa, osa; int ret; if (_timeout) { timeout.tv_sec = _timeout->tv_sec; timeout.tv_usec = _timeout->tv_nsec / 1000; if (gettimeofday(&before, NULL) == -1) { return -1; } memset(&value, 0, sizeof value); value.it_value = timeout; memset(&sa, 0, sizeof sa); /* signal_print writes the signal info to a log file */ sa.sa_sigaction = signal_print; sa.sa_flags = SA_SIGINFO; sigemptyset(&sa.sa_mask); sigaction(SIGALRM, &sa, &osa); if (setitimer(ITIMER_REAL, &value, &ovalue) == -1) { sigaction(SIGALRM, &osa, NULL); return -1; } } ret = semop(semid, array, nops); if (_timeout) { sigaction(SIGALRM, &osa, NULL); if (setitimer(ITIMER_REAL, &ovalue, NULL) == -1) { return -1; } } if (ret == -1) { if (_timeout) { struct timeval elapsed; if (gettimeofday(&after, NULL) == -1) { return -1; } _timeval_diff(&after, &before, &elapsed); if (timercmp(&elapsed, &timeout, >=)) errno = EAGAIN; } return -1; } return 0; }