Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 66858 invoked from network); 20 Aug 2009 18:17:53 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Aug 2009 18:17:53 -0000 Received: (qmail 63850 invoked by uid 500); 20 Aug 2009 18:18:12 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 63767 invoked by uid 500); 20 Aug 2009 18:18:11 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 63758 invoked by uid 99); 20 Aug 2009 18:18:11 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Aug 2009 18:18:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Aug 2009 18:18:10 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E4074238890A; Thu, 20 Aug 2009 18:17:49 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r806287 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Date: Thu, 20 Aug 2009 18:17:49 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090820181749.E4074238890A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mturk Date: Thu Aug 20 18:17:49 2009 New Revision: 806287 URL: http://svn.apache.org/viewvc?rev=806287&view=rev Log: Allow named mutexes Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c?rev=806287&r1=806286&r2=806287&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Thu Aug 20 18:17:49 2009 @@ -72,13 +72,27 @@ union semun ick; int rc = 0; acr_pmutex_t *m; + key_t mkey = IPC_PRIVATE; + int flags = IPC_CREAT; + if (fname) { + mkey = ftok(fname, 1); + if (mkey == (key_t)-1) { + ACR_THROW_IO_ERRNO(); + return -1; + } + flags |= IPC_EXCL; + } m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t)); if (!m) return -1; - m->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); + m->filedes = semget(mkey, 1, flags | 0600); if (m->filedes < 0) { + if (fname && errno == EEXIST) { + /* XXX: Should we throw separate exception here? + */ + } rc = ACR_GET_OS_ERROR(); goto finally; } @@ -107,8 +121,47 @@ ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname) { - ACR_SET_OS_ERROR(ACR_ENOTIMPL); - return -1; + union semun ick; + int rc = 0; + acr_pmutex_t *m; + key_t mkey; + + if (!fname) { + /* Cannot attach to unnamed mutex */ + ACR_THROW_IO_IF_ERR(ACR_EINVAL); + return -1; + } + + mkey = ftok(fname, 1); + if (mkey == (key_t)-1) { + ACR_THROW_IO_ERRNO(); + return -1; + } + m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t)); + if (!m) + return -1; + m->filedes = semget(mkey, 1, 0); + + if (m->filedes < 0) { + rc = ACR_GET_OS_ERROR(); + goto finally; + } + m->locked = 0; + +finally: + if (rc) { + if (m->filedes > 0) { + ick.val = 0; + semctl(m->filedes, 0, IPC_RMID, ick); + } + free(m); + ACR_THROW_IO_IF_ERR(rc); + return -1; + } + else { + rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup); + return rc; + } } ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)