You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2008/06/25 18:54:23 UTC

svn commit: r671604 - in /incubator/qpid/trunk/qpid/cpp: examples/direct/ examples/fanout/ examples/pub-sub/ examples/request-response/ examples/xml-exchange/ src/ src/qpid/ src/qpid/broker/ src/qpid/cluster/ src/qpid/sys/posix/

Author: aconway
Date: Wed Jun 25 09:54:22 2008
New Revision: 671604

URL: http://svn.apache.org/viewvc?rev=671604&view=rev
Log:
 - use flock to lock data dir rather than a lock file.
 - removed troublesome global constructor in Mutex initialization.

Added:
    incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp   (with props)
Modified:
    incubator/qpid/trunk/qpid/cpp/examples/direct/   (props changed)
    incubator/qpid/trunk/qpid/cpp/examples/fanout/   (props changed)
    incubator/qpid/trunk/qpid/cpp/examples/pub-sub/   (props changed)
    incubator/qpid/trunk/qpid/cpp/examples/request-response/   (props changed)
    incubator/qpid/trunk/qpid/cpp/examples/xml-exchange/   (props changed)
    incubator/qpid/trunk/qpid/cpp/src/Makefile.am
    incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Daemon.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/cluster/Cpg.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.h

Propchange: incubator/qpid/trunk/qpid/cpp/examples/direct/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun 25 09:54:22 2008
@@ -2,3 +2,4 @@
 direct_producer
 listener
 Makefile.in
+Makefile

Propchange: incubator/qpid/trunk/qpid/cpp/examples/fanout/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun 25 09:54:22 2008
@@ -2,3 +2,4 @@
 fanout_producer
 listener
 Makefile.in
+Makefile

Propchange: incubator/qpid/trunk/qpid/cpp/examples/pub-sub/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun 25 09:54:22 2008
@@ -2,3 +2,4 @@
 topic_listener
 topic_publisher
 Makefile.in
+Makefile

Propchange: incubator/qpid/trunk/qpid/cpp/examples/request-response/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun 25 09:54:22 2008
@@ -1,3 +1,4 @@
 client
 server
 Makefile.in
+Makefile

Propchange: incubator/qpid/trunk/qpid/cpp/examples/xml-exchange/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Jun 25 09:54:22 2008
@@ -2,3 +2,4 @@
 listener
 xml_producer
 Makefile.in
+Makefile

Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Wed Jun 25 09:54:22 2008
@@ -76,7 +76,8 @@
   qpid/sys/posix/AsynchIO.cpp \
   qpid/sys/posix/Time.cpp \
   qpid/sys/posix/Thread.cpp \
-  qpid/sys/posix/Shlib.cpp
+  qpid/sys/posix/Shlib.cpp \
+  qpid/sys/posix/Mutex.cpp
 
 posix_plat_hdr = \
   qpid/sys/posix/check.h \

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp Wed Jun 25 09:54:22 2008
@@ -23,6 +23,7 @@
 #include "qpid/log/Statement.h"
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/file.h>
 #include <fcntl.h>
 #include <cerrno>
 
@@ -30,7 +31,8 @@
 
 DataDir::DataDir (std::string path) :
     enabled (!path.empty ()),
-    dirPath (path)
+    dirPath (path),
+    dirFd(-1)
 {
     if (!enabled)
     {
@@ -40,7 +42,6 @@
 
     const  char *cpath = dirPath.c_str ();
     struct stat  s;
-
     if (::stat(cpath, &s)) {
         if (errno == ENOENT) {
             if (::mkdir(cpath, 0755))
@@ -49,34 +50,23 @@
         else
             throw Exception ("Data directory not found: " + path);
     }
-
-    std::string lockFile (path);
-    lockFile = lockFile + "/lock";
-    int fd = ::open (lockFile.c_str (), O_CREAT | O_EXCL,
-                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-    if (fd == -1)
-    {
-        if (errno == EEXIST)
-            throw Exception ("Data directory is locked by another process: " + path);
-        if (errno == EACCES)
-            throw Exception ("Insufficient privileges for data directory: " + path);
-        throw Exception(
-            QPID_MSG("Error locking " << lockFile << ": " << strError(errno)));
+    int dirFd = ::open(path.c_str(), 0);
+    if (dirFd == -1)
+        throw Exception(QPID_MSG("Can't open data directory: " << dirPath << ": " << strError(errno)));
+    int result = ::flock(dirFd, LOCK_EX | LOCK_NB);
+    if (result != 0) {
+        if (errno == EWOULDBLOCK)
+            throw Exception(QPID_MSG("Data directory locked by another process: " << path));
+        throw  Exception(QPID_MSG("Cannot lock data directory: " << strError(errno)));
     }
-
     QPID_LOG (info, "Locked data directory: " << dirPath);
 }
 
-DataDir::~DataDir ()
-{
-    if (dirPath.empty ())
-        return;
-
-    std::string lockFile (dirPath);
-    lockFile = lockFile + "/lock";
-
-    ::unlink (lockFile.c_str ());
-    QPID_LOG (info, "Unlocked data directory: " << dirPath);
+DataDir::~DataDir () {
+    if (dirFd != -1) {
+        ::close(dirFd);         // Closing the fd unlocks the directory.
+        QPID_LOG (info, "Unlocked data directory: " << dirPath);
+    }
 }
 
 } // namespace qpid

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.h?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.h Wed Jun 25 09:54:22 2008
@@ -32,6 +32,7 @@
 {
     const bool        enabled;
     const std::string dirPath;
+    int dirFd;
 
   public:
 

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Daemon.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Daemon.h?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Daemon.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/Daemon.h Wed Jun 25 09:54:22 2008
@@ -72,6 +72,7 @@
 
     pid_t pid;
     int pipeFds[2];
+    int lockFileFd;
     std::string lockFile;
     std::string pidDir;
 };

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/cluster/Cpg.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/cluster/Cpg.h?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/cluster/Cpg.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/cluster/Cpg.h Wed Jun 25 09:54:22 2008
@@ -149,19 +149,19 @@
     }
 
     static void globalDeliver(
-        cpg_handle_t /*handle*/,
+        cpg_handle_t handle,
         struct cpg_name *group,
-        uint32_t /*nodeid*/,
-        uint32_t /*pid*/,
-        void* /*msg*/,
-        int /*msg_len*/);
+        uint32_t nodeid,
+        uint32_t pid,
+        void* msg,
+        int msg_len);
 
     static void globalConfigChange(
-        cpg_handle_t /*handle*/,
-        struct cpg_name */*group*/,
-        struct cpg_address */*members*/, int /*nMembers*/,
-        struct cpg_address */*left*/, int /*nLeft*/,
-        struct cpg_address */*joined*/, int /*nJoined*/
+        cpg_handle_t handle,
+        struct cpg_name *group,
+        struct cpg_address *members, int nMembers,
+        struct cpg_address *left, int nLeft,
+        struct cpg_address *joined, int nJoined
     );
 
     static Handles handles;

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp?rev=671604&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp Wed Jun 25 09:54:22 2008
@@ -0,0 +1,46 @@
+/*
+ *
+ * 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 "qpid/sys/Mutex.h"
+
+namespace qpid {
+namespace sys {
+
+/**
+ * Initialise a recursive mutex attr for use in creating mutexes later
+ * (we use pthread_once to make sure it is initialised exactly once)
+ */
+
+namespace {
+pthread_once_t  onceControl = PTHREAD_ONCE_INIT;
+pthread_mutexattr_t mutexattr;
+	
+void initMutexattr()  {
+    pthread_mutexattr_init(&mutexattr);
+    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
+}
+}
+
+const pthread_mutexattr_t* Mutex::getAttribute() {
+    pthread_once(&onceControl, initMutexattr);
+    return &mutexattr;
+}
+
+}} // namespace qpid::sys

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.cpp
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.h?rev=671604&r1=671603&r2=671604&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Mutex.h Wed Jun 25 09:54:22 2008
@@ -34,6 +34,7 @@
  */
 class Mutex : private boost::noncopyable {
     friend class Condition;
+    static const pthread_mutexattr_t* getAttribute();
 
 public:
     typedef ::qpid::sys::ScopedLock<Mutex> ScopedLock;
@@ -74,32 +75,6 @@
 
 
 /**
- * Initialise a recursive mutex attr for use in creating mutexes later
- * (we use pthread_once to make sure it is initialised exactly once)
- */
-namespace {
-	pthread_once_t  onceControl = PTHREAD_ONCE_INIT;
-	pthread_mutexattr_t mutexattr;
-	
-	void initMutexattr()  {
-		pthread_mutexattr_init(&mutexattr);
-		pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
-	}
-
-	struct RecursiveMutexattr {
-		RecursiveMutexattr() {
-			pthread_once(&onceControl, initMutexattr);
-		}
-		
-		operator const pthread_mutexattr_t*() const {
-			return &mutexattr;
-		}
-	};
-  
-	RecursiveMutexattr recursiveMutexattr;
-}
-
-/**
  * PODMutex is a POD, can be static-initialized with
  * PODMutex m = QPID_PODMUTEX_INITIALIZER
  */
@@ -130,7 +105,7 @@
 }
 
 Mutex::Mutex() {
-    QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_init(&mutex, recursiveMutexattr));
+    QPID_POSIX_ASSERT_THROW_IF(pthread_mutex_init(&mutex, getAttribute()));
 }
 
 Mutex::~Mutex(){