|
From: <stu...@us...> - 2008-10-20 17:08:22
|
Revision: 5556
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=5556&view=rev
Author: stuglaser
Date: 2008-10-20 17:08:12 +0000 (Mon, 20 Oct 2008)
Log Message:
-----------
Mechanism control now refuses to create a controller when one with the same name already exists.
Modified Paths:
--------------
pkg/trunk/mechanism/mechanism_control/src/mechanism_control.cpp
Added Paths:
-----------
pkg/trunk/util/misc_utils/include/misc_utils/mutex_guard.h
Modified: pkg/trunk/mechanism/mechanism_control/src/mechanism_control.cpp
===================================================================
--- pkg/trunk/mechanism/mechanism_control/src/mechanism_control.cpp 2008-10-20 17:07:54 UTC (rev 5555)
+++ pkg/trunk/mechanism/mechanism_control/src/mechanism_control.cpp 2008-10-20 17:08:12 UTC (rev 5556)
@@ -30,6 +30,7 @@
#include "mechanism_control/mechanism_control.h"
#include "rosthread/member_thread.h"
+#include "misc_utils/mutex_guard.h"
using namespace mechanism;
@@ -112,28 +113,22 @@
bool MechanismControl::addController(controller::Controller *c, const std::string &name)
{
- //Add controller to list of controllers in realtime-safe manner;
- controllers_lock_.lock(); //This lock is only to prevent us from other non-realtime threads. The realtime thread may be spinning through the list of controllers while we are in here, so we need to keep that list always in a valid state. This is why we fully allocate and set up the controller before adding it into the list of active controllers.
+ misc_utils::MutexGuard guard(&controllers_lock_);
- bool spot_found = false;
+ if (getControllerByName(name))
+ return false;
+
for (int i = 0; i < MAX_NUM_CONTROLLERS; i++)
{
if (controllers_[i] == NULL)
{
- spot_found = true;
controllers_[i] = c;
controller_names_[i] = name;
- break;
+ return true;
}
}
- controllers_lock_.unlock();
- if (!spot_found)
- {
- return false;
- }
-
- return true;
+ return false;
}
Added: pkg/trunk/util/misc_utils/include/misc_utils/mutex_guard.h
===================================================================
--- pkg/trunk/util/misc_utils/include/misc_utils/mutex_guard.h (rev 0)
+++ pkg/trunk/util/misc_utils/include/misc_utils/mutex_guard.h 2008-10-20 17:08:12 UTC (rev 5556)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2008, Willow Garage, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Willow Garage, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Author: Stuart Glaser
+ */
+#ifndef MUTEX_GUARD_H
+#define MUTEX_GUARD_H
+
+#include "rosthread/mutex.h"
+
+namespace misc_utils {
+
+class MutexGuard
+{
+public:
+ MutexGuard(ros::thread::mutex *mutex)
+ {
+ assert(mutex);
+ m = mutex;
+ m->lock();
+ }
+
+ ~MutexGuard()
+ {
+ m->unlock();
+ }
+
+private:
+ ros::thread::mutex *m;
+};
+
+}
+
+#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|