* src/threads/threadlist.cpp (ThreadList::get_thread_by_index): Fixed
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Mon, 8 Sep 2008 15:54:44 +0000 (17:54 +0200)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Mon, 8 Sep 2008 15:54:44 +0000 (17:54 +0200)
a bug that this function always returned the last index if the index
was not found.  Additionally it uses now find_if.

src/threads/threadlist.cpp

index b791fcdde75988d7604e58892a0b016148f07915..6a5309315e812e858c5c72324629241e4e689225 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <stdint.h>
 
+#include <algorithm>
+
 #include "threads/mutex.hpp"
 #include "threads/threadlist.hpp"
 #include "threads/thread.hpp"
@@ -175,24 +177,36 @@ int32_t ThreadList::get_number_of_non_daemon_threads(void)
  *
  * @return thread object
  */
+
+class foo : public std::binary_function<threadobject*, int32_t, bool> {
+public:
+       bool operator() (const threadobject* t, const int32_t index) const
+       {
+               return (t->index == index);
+       }
+};
+
 threadobject* ThreadList::get_thread_by_index(int32_t index)
 {
-       threadobject* t = NULL;
-
        lock();
 
-       for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
-               t = *it;
+       List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(foo(), index));
 
-               if (t->state == THREAD_STATE_NEW)
-                       continue;
+       // No thread found.
+       if (it == _active_thread_list.end()) {
+               unlock();
+               return NULL;
+       }
+
+       threadobject* t = *it;
 
-               if (t->index == index)
-                       break;
+       // The thread found is in state new.
+       if (t->state == THREAD_STATE_NEW) {
+               unlock();
+               return NULL;
        }
 
        unlock();
-
        return t;
 }