Author: mturk
Date: Thu Aug 6 09:47:03 2009
New Revision: 801580
URL: http://svn.apache.org/viewvc?rev=801580&view=rev
Log:
Add Darwin (fcntl) mutex
Added:
commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c (with
props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.in
commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=801580&r1=801579&r2=801580&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Thu Aug 6
09:47:03 2009
@@ -142,6 +142,7 @@
$(SRCDIR)/os/unix/uuid.$(OBJ) \
$(SRCDIR)/os/unix/uutils.$(OBJ) \
$(SRCDIR)/os/darwin/platform.$(OBJ) \
+ $(SRCDIR)/os/darwin/pmutex.$(OBJ) \
$(SRCDIR)/os/darwin/pgroup.$(OBJ) \
$(SRCDIR)/os/darwin/puser.$(OBJ) \
$(SRCDIR)/os/darwin/os.$(OBJ)
Added: commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c?rev=801580&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c Thu Aug 6
09:47:03 2009
@@ -0,0 +1,223 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+#include "acr_string.h"
+#include "acr_descriptor.h"
+#include "acr_file.h"
+#include "acr_procmutex.h"
+
+#include <sys/ipc.h>
+#include <sys/file.h>
+
+struct acr_pmutex_t {
+ int filedes;
+ int locked;
+ int anon;
+ char *fname;
+};
+
+static int mutex_cleanup(void *mutex, int type, unsigned int flags)
+{
+ if (type != ACR_DT_MUTEX) {
+ int rc = 0;
+ acr_pmutex_t *m = (acr_pmutex_t *)mutex;
+ if (m->filedes > 0 && m->locked) {
+ struct flock op;
+
+ op.l_whence = SEEK_SET; /* from current point */
+ op.l_start = 0; /* -"- */
+ op.l_len = 0; /* until end of file */
+ op.l_type = F_UNLCK; /* unlock */
+ op.l_pid = 0; /* pid not actually interesting */
+ do {
+ rc = fcntl(m->filedes, F_SETLKW, &op);
+ } while (rc < 0 && errno == EINTR);
+
+ }
+ if (m->filedes > 0)
+ close(m->filedes);
+ free(m->fname);
+ free(m);
+ return rc;
+ }
+ return ACR_EINVAL;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname)
+{
+ int rc = 0;
+ acr_pmutex_t *m;
+
+ m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+ if (!m)
+ return -1;
+ if (!fname) {
+ m->anon = 1;
+ fname = "/tmp/apmXXXXXX";
+ }
+ m->fname = ACR_StrdupA(_E, THROW_FMARK, fname);
+ if (!m->fname) {
+ rc = ACR_GET_OS_ERROR();
+ free(m);
+ ACR_SET_OS_ERROR(rc);
+ return -1;
+ }
+ if (m->anon)
+ m->filedes = mkstemp(m->fname);
+ else
+ m->filedes = open(m->fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
+
+ if (m->filedes < 0) {
+ rc = ACR_GET_OS_ERROR();
+ goto cleanup;
+ }
+ m->locked = 0;
+ unlink(m->fname);
+
+cleanup:
+ if (rc) {
+ free(m->fname);
+ free(m);
+ if (!IS_INVALID_HANDLE(_E)) {
+ if (rc == EACCES)
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+ else
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, rc);
+ }
+ ACR_SET_OS_ERROR(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)
+{
+ int rc;
+ struct flock op;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
+
+ if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
+ ACR_SET_OS_ERROR(ACR_EINVAL);
+ return -1;
+ }
+
+ op.l_whence = SEEK_SET; /* from current point */
+ op.l_start = 0; /* -"- */
+ op.l_len = 0; /* until end of file */
+ op.l_type = F_WRLCK; /* set exclusive/write lock */
+ op.l_pid = 0; /* pid not actually interesting */
+
+
+ do {
+ rc = fcntl(m->filedes, F_SETLKW, &op);
+ } while (rc < 0 && errno == EINTR);
+
+ if (rc < 0)
+ return ACR_GET_OS_ERROR();
+ m->locked = 1;
+
+ return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
+{
+ int rc;
+ struct flock op;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
+
+ if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EINVAL;
+ }
+ op.l_whence = SEEK_SET; /* from current point */
+ op.l_start = 0; /* -"- */
+ op.l_len = 0; /* until end of file */
+ op.l_type = F_WRLCK; /* set exclusive/write lock */
+ op.l_pid = 0; /* pid not actually interesting */
+
+ do {
+ rc = fcntl(m->filedes, F_SETLK, &op);
+ } while (rc < 0 && errno == EINTR);
+
+ if (rc < 0) {
+ if (errno == EAGAIN) {
+ return ACR_EBUSY;
+ }
+ }
+ else
+ m->locked = 1;
+
+ return ACR_SUCCESS;;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
+{
+ int rc;
+ struct flock op;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
+
+ if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EINVAL;
+ }
+
+ m->locked = 0;
+ op.l_whence = SEEK_SET; /* from current point */
+ op.l_start = 0; /* -"- */
+ op.l_len = 0; /* until end of file */
+ op.l_type = F_UNLCK; /* unlock */
+ op.l_pid = 0; /* pid not actually interesting */
+ do {
+ rc = fcntl(m->filedes, F_SETLKW, &op);
+ } while (rc < 0 && errno == EINTR);
+
+ if (rc < 0)
+ return ACR_GET_OS_ERROR();
+ else
+ return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
+ acr_uid_t uid, acr_uid_t gid)
+{
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
+
+ if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EINVAL;
+ }
+ if (!(perms & ACR_FPROT_GSETID))
+ gid = -1;
+ if (fchown(m->filedes, uid, gid) < 0) {
+ return ACR_GET_OS_ERROR();
+ }
+ else
+ return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *_E, int mutex)
+{
+
+ /* Close will call the cleanup function
+ */
+ return acr_ioh_close(mutex);
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/native/os/darwin/pmutex.c
------------------------------------------------------------------------------
svn:eol-style = native
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=801580&r1=801579&r2=801580&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 6
09:47:03 2009
@@ -36,7 +36,6 @@
struct acr_pmutex_t {
int filedes;
int locked;
- char *fname;
};
static int mutex_cleanup(void *mutex, int type, unsigned int flags)
@@ -48,7 +47,6 @@
ick.val = 0;
semctl(m->filedes, 0, IPC_RMID, ick);
}
- free(m->fname);
free(m);
return ACR_SUCCESS;
}
@@ -64,14 +62,6 @@
m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
if (!m)
return -1;
- m->fname = ACR_StrdupA(_E, THROW_FMARK, fname);
- if (!m->fname) {
- rc = ACR_GET_OS_ERROR();
- free(m);
- ACR_SET_OS_ERROR(rc);
- return -1;
- }
-
m->filedes = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
if (m->filedes < 0) {
@@ -91,7 +81,6 @@
ick.val = 0;
semctl(m->filedes, 0, IPC_RMID, ick);
}
- free(m->fname);
free(m);
if (!IS_INVALID_HANDLE(_E)) {
if (rc == EACCES)