BIND 10 trac826, updated. b0591f3dfa8f4c9a07b4f3db50b1a2ca35c1c91d src/lib/util first update

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Jun 26 17:20:50 UTC 2012


The branch, trac826 has been updated
       via  b0591f3dfa8f4c9a07b4f3db50b1a2ca35c1c91d (commit)
      from  748f816045f31e5f629f54c0d7f05403d63a5059 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b0591f3dfa8f4c9a07b4f3db50b1a2ca35c1c91d
Author: Francis Dupont <fdupont at isc.org>
Date:   Tue Jun 26 19:20:37 2012 +0200

    src/lib/util first update

-----------------------------------------------------------------------

Summary of changes:
 src/lib/util/Makefile.am                           |    5 +
 src/lib/util/buffer.h                              |   72 +++++---
 src/lib/util/encode/base_n.cc                      |    9 -
 src/lib/util/interprocess_sync.h                   |  149 +++++++++++++++++
 src/lib/util/interprocess_sync_file.cc             |  174 ++++++++++++++++++++
 src/lib/util/interprocess_sync_file.h              |   99 +++++++++++
 .../interprocess_sync_null.cc}                     |   36 ++--
 src/lib/util/interprocess_sync_null.h              |   64 +++++++
 src/lib/util/locks.h                               |   58 +------
 src/lib/util/range_utilities.h                     |   68 ++++++++
 src/lib/util/strutil.cc                            |    2 +-
 src/lib/util/time_utilities.cc                     |    2 +-
 win32build/config.h                                |    2 +
 13 files changed, 633 insertions(+), 107 deletions(-)
 create mode 100644 src/lib/util/interprocess_sync.h
 create mode 100644 src/lib/util/interprocess_sync_file.cc
 create mode 100644 src/lib/util/interprocess_sync_file.h
 copy src/lib/{nsas/nameserver_address.cc => util/interprocess_sync_null.cc} (63%)
 create mode 100644 src/lib/util/interprocess_sync_null.h
 create mode 100644 src/lib/util/range_utilities.h

-----------------------------------------------------------------------
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am
index 0b78b29..fad2465 100644
--- a/src/lib/util/Makefile.am
+++ b/src/lib/util/Makefile.am
@@ -4,6 +4,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src/lib/util -I$(top_builddir)/src/lib/util
 AM_CPPFLAGS += -I$(top_srcdir)/src/lib/exceptions -I$(top_builddir)/src/lib/exceptions
 AM_CPPFLAGS += $(BOOST_INCLUDES)
+AM_CPPFLAGS += -DLOCKFILE_DIR=\"${localstatedir}/${PACKAGE_NAME}\"
 AM_CXXFLAGS = $(B10_CXXFLAGS)
 
 lib_LTLIBRARIES = libutil.la
@@ -12,6 +13,10 @@ libutil_la_SOURCES += locks.h lru_list.h
 libutil_la_SOURCES += strutil.h strutil.cc
 libutil_la_SOURCES += buffer.h io_utilities.h
 libutil_la_SOURCES += time_utilities.h time_utilities.cc
+libutil_la_SOURCES += interprocess_sync.h
+libutil_la_SOURCES += interprocess_sync_file.h interprocess_sync_file.cc
+libutil_la_SOURCES += interprocess_sync_null.h interprocess_sync_null.cc
+libutil_la_SOURCES += range_utilities.h
 libutil_la_SOURCES += hash/sha1.h hash/sha1.cc
 libutil_la_SOURCES += encode/base16_from_binary.h
 libutil_la_SOURCES += encode/base32hex.h encode/base64.h
diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h
index b7a8e28..7e88108 100644
--- a/src/lib/util/buffer.h
+++ b/src/lib/util/buffer.h
@@ -19,8 +19,6 @@
 #include <cstring>
 #include <vector>
 
-#include <string.h>
-
 #include <stdint.h>
 
 #include <exceptions/exceptions.h>
@@ -122,10 +120,10 @@ public:
     /// an exception of class \c isc::dns::InvalidBufferPosition will be thrown.
     /// \param position The new position (offset from the beginning of the
     /// buffer).
-    void setPosition(size_t position)
-    {
-        if (position > len_)
-            isc_throw(InvalidBufferPosition, "position is too large");
+    void setPosition(size_t position) {
+        if (position > len_) {
+            throwError("position is too large");
+        }
         position_ = position;
     }
     //@}
@@ -137,10 +135,9 @@ public:
     ///
     /// If the remaining length of the buffer is smaller than 8-bit, an
     /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
-    uint8_t readUint8()
-    {
+    uint8_t readUint8() {
         if (position_ + sizeof(uint8_t) > len_) {
-            isc_throw(InvalidBufferPosition, "read beyond end of buffer");
+            throwError("read beyond end of buffer");
         }
 
         return (data_[position_++]);
@@ -150,13 +147,12 @@ public:
     ///
     /// If the remaining length of the buffer is smaller than 16-bit, an
     /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
-    uint16_t readUint16()
-    {
+    uint16_t readUint16() {
         uint16_t data;
         const uint8_t* cp;
 
         if (position_ + sizeof(data) > len_) {
-            isc_throw(InvalidBufferPosition, "read beyond end of buffer");
+            throwError("read beyond end of buffer");
         }
 
         cp = &data_[position_];
@@ -171,13 +167,12 @@ public:
     ///
     /// If the remaining length of the buffer is smaller than 32-bit, an
     /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
-    uint32_t readUint32()
-    {
+    uint32_t readUint32() {
         uint32_t data;
         const uint8_t* cp;
 
         if (position_ + sizeof(data) > len_) {
-            isc_throw(InvalidBufferPosition, "read beyond end of buffer");
+            throwError("read beyond end of buffer");
         }
 
         cp = &data_[position_];
@@ -196,18 +191,43 @@ public:
     /// If the remaining length of the buffer is smaller than the specified
     /// length, an exception of class \c isc::dns::InvalidBufferPosition will
     /// be thrown.
-    void readData(void* data, size_t len)
-    {
+    void readData(void* data, size_t len) {
         if (position_ + len > len_) {
-            isc_throw(InvalidBufferPosition, "read beyond end of buffer");
+            throwError("read beyond end of buffer");
         }
 
-        memcpy(data, &data_[position_], len);
+        std::memcpy(data, &data_[position_], len);
         position_ += len;
     }
     //@}
 
+    /// @brief Read specified number of bytes as a vector.
+    ///
+    /// If specified buffer is too short, it will be expanded
+    /// using vector::resize() method.
+    ///
+    /// @param data Reference to a buffer (data will be stored there).
+    /// @param len Size specified number of bytes to read in a vector.
+    ///
+    void readVector(std::vector<uint8_t>& data, size_t len) {
+        if (position_ + len > len_) {
+            throwError("read beyond end of buffer");
+        }
+
+        data.resize(len);
+        readData(&data[0], len);
+    }
+
 private:
+    /// \brief A common helper to throw an exception on invalid operation.
+    ///
+    /// Experiments showed that throwing from each method makes the buffer
+    /// operation slower, so we consolidate it here, and let the methods
+    /// call this.
+    static void throwError(const char* msg) {
+        isc_throw(InvalidBufferPosition, msg);
+    }
+
     size_t position_;
 
     // XXX: The following must be private, but for a short term workaround with
@@ -311,7 +331,7 @@ public:
         if (buffer_ == NULL && allocated_ != 0) {
             throw std::bad_alloc();
         }
-        memcpy(buffer_, other.buffer_, size_);
+        std::memcpy(buffer_, other.buffer_, size_);
     }
 
     /// \brief Destructor
@@ -330,7 +350,7 @@ public:
         buffer_ = newbuff;
         size_ = other.size_;
         allocated_ = other.allocated_;
-        memcpy(buffer_, other.buffer_, size_);
+        std::memcpy(buffer_, other.buffer_, size_);
         return (*this);
     }
 
@@ -358,9 +378,7 @@ public:
     /// \param pos The position in the buffer to be returned.
     uint8_t operator[](size_t pos) const
     {
-        if (pos >= size_) {
-            isc_throw(InvalidBufferPosition, "read at invalid position");
-        }
+        assert (pos < size_);
         return (buffer_[pos]);
     }
     //@}
@@ -473,7 +491,7 @@ public:
     void writeData(const void *data, size_t len)
     {
         ensureAllocated(size_ + len);
-        memcpy(buffer_ + size_, data, len);
+        std::memcpy(buffer_ + size_, data, len);
         size_ += len;
     }
     //@}
@@ -519,6 +537,6 @@ typedef boost::shared_ptr<OutputBuffer> OutputBufferPtr;
 } // namespace isc
 #endif  // __BUFFER_H
 
-// Local Variables: 
+// Local Variables:
 // mode: c++
-// End: 
+// End:
diff --git a/src/lib/util/encode/base_n.cc b/src/lib/util/encode/base_n.cc
index d9507e4..0026a0b 100644
--- a/src/lib/util/encode/base_n.cc
+++ b/src/lib/util/encode/base_n.cc
@@ -18,11 +18,6 @@
 #include <string>
 #include <vector>
 
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable: 4512)
-#endif
-
 #include <boost/archive/iterators/base64_from_binary.hpp>
 #include <boost/archive/iterators/binary_from_base64.hpp>
 #include <boost/archive/iterators/transform_width.hpp>
@@ -424,7 +419,3 @@ decodeHex(const string& input, vector<uint8_t>& result) {
 } // namespace encode
 } // namespace util
 } // namespace isc
-
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
diff --git a/src/lib/util/interprocess_sync.h b/src/lib/util/interprocess_sync.h
new file mode 100644
index 0000000..e4fa7af
--- /dev/null
+++ b/src/lib/util/interprocess_sync.h
@@ -0,0 +1,149 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __INTERPROCESS_SYNC_H__
+#define __INTERPROCESS_SYNC_H__
+
+#include <string>
+
+namespace isc {
+namespace util {
+
+class InterprocessSyncLocker; // forward declaration
+
+/// \brief Interprocess Sync Class
+///
+/// This class specifies an interface for mutual exclusion among
+/// co-operating processes. This is an abstract class and a real
+/// implementation such as InterprocessSyncFile should be used
+/// in code. Usage is as follows:
+///
+/// 1. Client instantiates a sync object of an implementation (such as
+/// InterprocessSyncFile).
+/// 2. Client then creates an automatic (stack) object of
+/// InterprocessSyncLocker around the sync object. Such an object
+/// destroys itself and releases any acquired lock when it goes out of extent.
+/// 3. Client calls lock() method on the InterprocessSyncLocker.
+/// 4. Client performs task that needs mutual exclusion.
+/// 5. Client frees lock with unlock(), or simply returns from the basic
+/// block which forms the scope for the InterprocessSyncLocker.
+///
+/// NOTE: All implementations of InterprocessSync should keep the
+/// is_locked_ member variable updated whenever their
+/// lock()/tryLock()/unlock() implementations are called.
+class InterprocessSync {
+  // InterprocessSyncLocker is the only code outside this class that
+  // should be allowed to call the lock(), tryLock() and unlock()
+  // methods.
+  friend class InterprocessSyncLocker;
+
+public:
+    /// \brief Constructor
+    ///
+    /// Creates an interprocess synchronization object
+    ///
+    /// \param task_name Name of the synchronization task. This has to be
+    /// identical among the various processes that need to be
+    /// synchronized for the same task.
+    InterprocessSync(const std::string& task_name) :
+        task_name_(task_name), is_locked_(false)
+    {}
+
+    /// \brief Destructor
+    virtual ~InterprocessSync() {}
+
+protected:
+    /// \brief Acquire the lock (blocks if something else has acquired a
+    /// lock on the same task name)
+    ///
+    /// \return Returns true if the lock was acquired, false otherwise.
+    virtual bool lock() = 0;
+
+    /// \brief Try to acquire a lock (doesn't block)
+    ///
+    /// \return Returns true if the lock was acquired, false otherwise.
+    virtual bool tryLock() = 0;
+
+    /// \brief Release the lock
+    ///
+    /// \return Returns true if the lock was released, false otherwise.
+    virtual bool unlock() = 0;
+
+    const std::string task_name_; ///< The task name
+    bool is_locked_;              ///< Is the lock taken?
+};
+
+/// \brief Interprocess Sync Locker Class
+///
+/// This class is used for making automatic stack objects to manage
+/// locks that are released automatically when the block is exited
+/// (RAII). It is meant to be used along with InterprocessSync objects. See
+/// the description of InterprocessSync.
+class InterprocessSyncLocker {
+public:
+    /// \brief Constructor
+    ///
+    /// Creates a lock manager around a interprocess synchronization object
+    ///
+    /// \param sync The sync object which has to be locked/unlocked by
+    /// this locker object.
+    InterprocessSyncLocker(InterprocessSync& sync) :
+        sync_(sync)
+    {}
+
+    /// \brief Destructor
+    ~InterprocessSyncLocker() {
+        if (isLocked())
+            unlock();
+    }
+
+    /// \brief Acquire the lock (blocks if something else has acquired a
+    /// lock on the same task name)
+    ///
+    /// \return Returns true if the lock was acquired, false otherwise.
+    bool lock() {
+        return (sync_.lock());
+    }
+
+    /// \brief Try to acquire a lock (doesn't block)
+    ///
+    /// \return Returns true if a new lock could be acquired, false
+    ///         otherwise.
+    bool tryLock() {
+        return (sync_.tryLock());
+    }
+
+    /// \brief Check if the lock is taken
+    ///
+    /// \return Returns true if a lock is currently acquired, false
+    ///         otherwise.
+    bool isLocked() const {
+        return (sync_.is_locked_);
+    }
+
+    /// \brief Release the lock
+    ///
+    /// \return Returns true if the lock was released, false otherwise.
+    bool unlock() {
+        return (sync_.unlock());
+    }
+
+protected:
+    InterprocessSync& sync_; ///< Ref to underlying sync object
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __INTERPROCESS_SYNC_H__
diff --git a/src/lib/util/interprocess_sync_file.cc b/src/lib/util/interprocess_sync_file.cc
new file mode 100644
index 0000000..58c0d5c
--- /dev/null
+++ b/src/lib/util/interprocess_sync_file.cc
@@ -0,0 +1,174 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include "interprocess_sync_file.h"
+
+#include <string>
+
+#include <stdlib.h>
+#include <string.h>
+#ifndef _WIN32
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#endif
+
+namespace isc {
+namespace util {
+
+InterprocessSyncFile::~InterprocessSyncFile() {
+#ifdef _WIN32
+    if (fd_ != INVALID_HANDLE_VALUE) {
+        // This will also release any applied locks.
+        CloseHandle(fd_);
+        // The lockfile will continue to exist, and we must not delete
+        // it.
+    }
+#else
+    if (fd_ != -1) {
+        // This will also release any applied locks.
+        close(fd_);
+        // The lockfile will continue to exist, and we must not delete
+        // it.
+    }
+#endif
+}
+
+bool
+InterprocessSyncFile::do_lock(int cmd, short l_type) {
+    // Open lock file only when necessary (i.e., here). This is so that
+    // if a default InterprocessSync object is replaced with another
+    // implementation, it doesn't attempt any opens.
+    if (fd_ == -1) {
+        std::string lockfile_path = LOCKFILE_DIR;
+
+        const char* const env = getenv("B10_FROM_BUILD");
+        if (env != NULL) {
+            lockfile_path = env;
+        }
+
+        const char* const env2 = getenv("B10_FROM_BUILD_LOCALSTATEDIR");
+        if (env2 != NULL) {
+            lockfile_path = env2;
+        }
+
+        const char* const env3 = getenv("B10_LOCKFILE_DIR_FROM_BUILD");
+        if (env3 != NULL) {
+            lockfile_path = env3;
+        }
+
+        lockfile_path += "/" + task_name_ + "_lockfile";
+
+        // Open the lockfile in the constructor so it doesn't do the access
+        // checks every time a message is logged.
+#ifdef _WIN32
+        fd_ = CreateFile(lockfile_path.c_str(),
+                         GENERIC_READ | GENERIC_WRITE,
+                         FILE_SHARE_READ | FILE_SHARE_WRITE,
+                         NULL,
+                         OPEN_ALWAYS,
+                         FILE_ATTRIBUTE_NORMAL,
+                         NULL);
+        if (fd_ == INVALID_HANDLE_VALUE) {
+            isc_throw(InterprocessSyncFileError,
+                      "Unable to use interprocess sync lockfile: " +
+                      lockfile_path);
+        }
+#else
+        const mode_t mode = umask(0111);
+        fd_ = open(lockfile_path.c_str(), O_CREAT | O_RDWR, 0660);
+        umask(mode);
+
+        if (fd_ == -1) {
+            isc_throw(InterprocessSyncFileError,
+                      "Unable to use interprocess sync lockfile: " +
+                      lockfile_path);
+        }
+#endif
+    }
+
+#ifdef _WIN32
+#define F_SETLK 0
+#define F_SETLKW 1
+#define F_UNLCK 0
+#define F_WRLCK 1
+
+    if (l_type == F_UNLCK)
+        return (UnlockFile(fd_, 0, 0, 1, 0));
+    OVERLAPPED o;
+    memset(&o, 0, sizeof (o));
+    if (cmd == F_SETLK)
+        return (LockFileEx(fd_, LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &o));
+    else
+        return (LockFileEx(fd_, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &o));
+#else
+    struct flock lock;
+
+    memset(&lock, 0, sizeof (lock));
+    lock.l_type = l_type;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = 0;
+    lock.l_len = 1;
+
+    return (fcntl(fd_, cmd, &lock) == 0);
+#endif
+}
+
+bool
+InterprocessSyncFile::lock() {
+    if (is_locked_) {
+        return (true);
+    }
+
+    if (do_lock(F_SETLKW, F_WRLCK)) {
+        is_locked_ = true;
+        return (true);
+    }
+
+    return (false);
+}
+
+bool
+InterprocessSyncFile::tryLock() {
+    if (is_locked_) {
+        return (true);
+    }
+
+    if (do_lock(F_SETLK, F_WRLCK)) {
+        is_locked_ = true;
+        return (true);
+    }
+
+    return (false);
+}
+
+bool
+InterprocessSyncFile::unlock() {
+    if (!is_locked_) {
+        return (true);
+    }
+
+    if (do_lock(F_SETLKW, F_UNLCK)) {
+        is_locked_ = false;
+        return (true);
+    }
+
+    return (false);
+}
+
+} // namespace util
+} // namespace isc
diff --git a/src/lib/util/interprocess_sync_file.h b/src/lib/util/interprocess_sync_file.h
new file mode 100644
index 0000000..654ec0d
--- /dev/null
+++ b/src/lib/util/interprocess_sync_file.h
@@ -0,0 +1,99 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __INTERPROCESS_SYNC_FILE_H__
+#define __INTERPROCESS_SYNC_FILE_H__
+
+#include <util/interprocess_sync.h>
+#include <exceptions/exceptions.h>
+
+namespace isc {
+namespace util {
+
+/// \brief InterprocessSyncFileError
+///
+/// Exception that is thrown if it's not possible to open the
+/// lock file.
+class InterprocessSyncFileError : public Exception {
+public:
+    InterprocessSyncFileError(const char* file, size_t line,
+                              const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
+/// \brief File-based Interprocess Sync Class
+///
+/// This class specifies a concrete implementation for a file-based
+/// interprocess synchronization mechanism. Please see the
+/// InterprocessSync class documentation for usage.
+///
+/// An InterprocessSyncFileError exception may be thrown if there is an
+/// issue opening the lock file.
+///
+/// Lock files are created typically in the local state directory
+/// (var). They are typically named like "<task_name>_lockfile".
+/// This implementation opens lock files lazily (only when
+/// necessary). It also leaves the lock files lying around as multiple
+/// processes may have locks on them.
+class InterprocessSyncFile : public InterprocessSync {
+public:
+    /// \brief Constructor
+    ///
+    /// Creates a file-based interprocess synchronization object
+    ///
+    /// \param name Name of the synchronization task. This has to be
+    /// identical among the various processes that need to be
+    /// synchronized for the same task.
+    InterprocessSyncFile(const std::string& task_name) :
+#ifdef _WIN32
+        InterprocessSync(task_name), fd_(INVALID_HANDLE_VALUE)
+#else
+        InterprocessSync(task_name), fd_(-1)
+#endif
+    {}
+
+    /// \brief Destructor
+    virtual ~InterprocessSyncFile();
+
+protected:
+    /// \brief Acquire the lock (blocks if something else has acquired a
+    /// lock on the same task name)
+    ///
+    /// \return Returns true if the lock was acquired, false otherwise.
+    bool lock();
+
+    /// \brief Try to acquire a lock (doesn't block)
+    ///
+    /// \return Returns true if the lock was acquired, false otherwise.
+    bool tryLock();
+
+    /// \brief Release the lock
+    ///
+    /// \return Returns true if the lock was released, false otherwise.
+    bool unlock();
+
+private:
+    bool do_lock(int cmd, short l_type);
+
+#ifdef _WIN32
+    HANDLE fd_; ///< The handle for the open file
+#else
+    int fd_; ///< The descriptor for the open file
+#endif
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __INTERPROCESS_SYNC_FILE_H__
diff --git a/src/lib/util/interprocess_sync_null.cc b/src/lib/util/interprocess_sync_null.cc
new file mode 100644
index 0000000..5355d57
--- /dev/null
+++ b/src/lib/util/interprocess_sync_null.cc
@@ -0,0 +1,42 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include "interprocess_sync_null.h"
+
+namespace isc {
+namespace util {
+
+InterprocessSyncNull::~InterprocessSyncNull() {
+}
+
+bool
+InterprocessSyncNull::lock() {
+    is_locked_ = true;
+    return (true);
+}
+
+bool
+InterprocessSyncNull::tryLock() {
+    is_locked_ = true;
+    return (true);
+}
+
+bool
+InterprocessSyncNull::unlock() {
+    is_locked_ = false;
+    return (true);
+}
+
+} // namespace util
+} // namespace isc
diff --git a/src/lib/util/interprocess_sync_null.h b/src/lib/util/interprocess_sync_null.h
new file mode 100644
index 0000000..6ac0322
--- /dev/null
+++ b/src/lib/util/interprocess_sync_null.h
@@ -0,0 +1,64 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __INTERPROCESS_SYNC_NULL_H__
+#define __INTERPROCESS_SYNC_NULL_H__
+
+#include <util/interprocess_sync.h>
+
+namespace isc {
+namespace util {
+
+/// \brief Null Interprocess Sync Class
+///
+/// This class specifies a concrete implementation for a null (no effect)
+/// interprocess synchronization mechanism. Please see the
+/// InterprocessSync class documentation for usage.
+class InterprocessSyncNull : public InterprocessSync {
+public:
+    /// \brief Constructor
+    ///
+    /// Creates a null interprocess synchronization object
+    ///
+    /// \param name Name of the synchronization task. This has to be
+    /// identical among the various processes that need to be
+    /// synchronized for the same task.
+    InterprocessSyncNull(const std::string& task_name) :
+        InterprocessSync(task_name)
+    {}
+
+    /// \brief Destructor
+    virtual ~InterprocessSyncNull();
+
+protected:
+    /// \brief Acquire the lock (never blocks)
+    ///
+    /// \return Always returns true
+    bool lock();
+
+    /// \brief Try to acquire a lock (doesn't block)
+    ///
+    /// \return Always returns true
+    bool tryLock();
+
+    /// \brief Release the lock
+    ///
+    /// \return Always returns true
+    bool unlock();
+};
+
+} // namespace util
+} // namespace isc
+
+#endif // __INTERPROCESS_SYNC_NULL_H__
diff --git a/src/lib/util/locks.h b/src/lib/util/locks.h
index 971c413..da9e9cd 100644
--- a/src/lib/util/locks.h
+++ b/src/lib/util/locks.h
@@ -15,13 +15,9 @@
 /// This file (right now) provides dummy locks
 /// It also contains code to use boost/threads locks:
 ///
-/// if USE_BOOST_THREADS is defined, we typedef the relevant classes
-/// and derive from the relevant templates so our dummy locks are
-/// replaced by the boost locks (--enable-boost-threads)
 ///
-/// If USE_BOOST_THREADS is NOT defined, all locks are dummy classes
-/// that don't actually do anything. At this moment, only the very
-/// minimal set of methods that we actually use is defined.
+/// All locks are dummy classes that don't actually do anything. At this moment,
+/// only the very minimal set of methods that we actually use is defined.
 ///
 /// Note that we need to include <config.h> in our .cc files for that
 /// to be set. we might want to enfore this at compile time with a check
@@ -30,8 +26,6 @@
 #ifndef __LOCKS_
 #define __LOCKS_
 
-#ifndef USE_BOOST_THREADS
-
 namespace isc {
 namespace util {
 namespace locks {
@@ -64,52 +58,4 @@ public:
 } // namespace util
 } // namespace isc
 
-#else // USE_BOOST_THREADS
-
-// Workaround for a problem with boost and sunstudio 5.10
-// There is a version check in there that appears wrong,
-// which makes including boost/thread.hpp fail
-// This will probably be fixed in a future version of boost,
-// in which case this part can be removed then
-#ifdef NEED_SUNPRO_WORKAROUND
-#if defined(__SUNPRO_CC) && __SUNPRO_CC == 0x5100
-#undef __SUNPRO_CC
-#define __SUNPRO_CC 0x5090
-#endif
-#endif // NEED_SUNPRO_WORKAROUND
-
-#include <boost/thread.hpp>
-#include <boost/interprocess/sync/sharable_lock.hpp>
-#include <boost/interprocess/sync/scoped_lock.hpp>
-#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
-#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
-
-namespace isc {
-namespace util {
-namespace locks {
-
-typedef boost::mutex mutex;
-typedef boost::interprocess::interprocess_upgradable_mutex upgradable_mutex;
-typedef boost::interprocess::interprocess_recursive_mutex recursive_mutex;
-
-template <typename T>
-struct sharable_lock : public boost::interprocess::sharable_lock<T> {
-public:
-    sharable_lock(T&  mtype) : boost::interprocess::sharable_lock<T>(mtype) {}
-};
-
-
-template <class T>
-struct scoped_lock : public boost::interprocess::scoped_lock<T> {
-public:
-    scoped_lock(T& mtype) : boost::interprocess::scoped_lock<T>(mtype) { }
-};
-
-} // namespace locks
-} // namespace util
-} // namespace isc
-
-
-#endif // USE_BOOST_THREADS
-
 #endif // __LOCKS_
diff --git a/src/lib/util/range_utilities.h b/src/lib/util/range_utilities.h
new file mode 100644
index 0000000..3f8b971
--- /dev/null
+++ b/src/lib/util/range_utilities.h
@@ -0,0 +1,68 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __RANGE_UTIL_H_
+#define __RANGE_UTIL_H_ 1
+
+#include <stdlib.h>
+#include <algorithm>
+
+// This header contains useful methods for conduction operations on
+// a range of container elements. Currently the collection is limited,
+// but it is expected to grow.
+
+namespace isc {
+namespace util {
+
+/// @brief Checks if specified range in a container contains only zeros
+///
+/// @param begin beginning of the range
+/// @param end end of the range
+///
+/// @return true if there are only zeroes, false otherwise
+template <typename Iterator>
+bool
+isRangeZero(Iterator begin, Iterator end) {
+    return (std::find_if(begin, end,
+                         std::bind1st(std::not_equal_to<int>(), 0))
+            == end);
+}
+
+/// @brief Fill in specified range with a random data.
+///
+/// Make sure that random number generator is initialized properly. Otherwise you
+/// will get a the same pseudo-random sequence after every start of your process.
+/// Calling srand() is enough. This method uses default rand(), which is usually
+/// a LCG pseudo-random number generator, so it is not suitable for security
+/// purposes. Please get a decent PRNG implementation, like mersene twister, if
+/// you are doing anything related with security.
+///
+/// PRNG initialization is left out of this function on purpose. It may be
+/// initialized to specific value on purpose, e.g. to repeat exactly the same
+/// sequence in a test.
+///
+/// @param begin
+/// @param end
+template <typename Iterator>
+void
+fillRandom(Iterator begin, Iterator end) {
+    for (Iterator x = begin; x != end; ++x) {
+        *x = random();
+    }
+}
+
+} // end of isc::util namespace
+} // end of isc namespace
+
+#endif  // __PKTINFO_UTIL_H_
diff --git a/src/lib/util/strutil.cc b/src/lib/util/strutil.cc
index e0fc1d9..89edcc9 100644
--- a/src/lib/util/strutil.cc
+++ b/src/lib/util/strutil.cc
@@ -120,7 +120,7 @@ format(const std::string& format, const std::vector<std::string>& args) {
     // Iterate through replacing all tokens
     result = format;
     size_t tokenpos = 0;    // Position of last token replaced
-    unsigned int i = 0;     // Index into argument array
+    std::vector<std::string>::size_type i = 0; // Index into argument array
 
     while ((i < args.size()) && (tokenpos != string::npos)) {
         tokenpos = result.find(flag, tokenpos);
diff --git a/src/lib/util/time_utilities.cc b/src/lib/util/time_utilities.cc
index 4ea37c9..1551249 100644
--- a/src/lib/util/time_utilities.cc
+++ b/src/lib/util/time_utilities.cc
@@ -177,7 +177,7 @@ uint64_t
 timeFromText64(const string& time_txt) {
     // Confirm the source only consists digits.  sscanf() allows some
     // minor exceptions.
-    for (unsigned int i = 0; i < time_txt.length(); ++i) {
+    for (string::size_type i = 0; i < time_txt.length(); ++i) {
         if (!isdigit(time_txt.at(i))) {
             isc_throw(InvalidTime, "Couldn't convert non-numeric time value: "
                       << time_txt);
diff --git a/win32build/config.h b/win32build/config.h
index 83ea23f..44989b8 100644
--- a/win32build/config.h
+++ b/win32build/config.h
@@ -74,3 +74,5 @@ typedef int ssize_t;
 #ifdef _RESTORE_WSA_
 #undef _WINSOCKAPI_
 #endif
+
+#pragma warning(disable: 4512)



More information about the bind10-changes mailing list