Skip to content

Commit

Permalink
add ProcessInfo::threadStat() and BlockingQueue::put(T&& x).
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed Apr 9, 2014
1 parent 1e242a3 commit e29c815
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions muduo/base/BlockingQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class BlockingQueue : boost::noncopyable
// http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
void put(T&& x)
{
MutexLockGuard lock(mutex_);
queue_.push_back(std::move(x));
notEmpty_.notify();
}
// FIXME: emplace()
#endif

T take()
{
MutexLockGuard lock(mutex_);
Expand All @@ -44,7 +54,11 @@ class BlockingQueue : boost::noncopyable
notEmpty_.wait();
}
assert(!queue_.empty());
#ifdef __GXX_EXPERIMENTAL_CXX0X__
T front(std::move(queue_.front()));
#else
T front(queue_.front());
#endif
queue_.pop_front();
return front;
}
Expand Down
10 changes: 10 additions & 0 deletions muduo/base/ProcessInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//

#include <muduo/base/ProcessInfo.h>
#include <muduo/base/CurrentThread.h>
#include <muduo/base/FileUtil.h>

#include <algorithm>
Expand Down Expand Up @@ -171,6 +172,15 @@ string ProcessInfo::procStat()
return result;
}

string ProcessInfo::threadStat()
{
char buf[64];
snprintf(buf, sizeof buf, "/proc/self/task/%d/stat", CurrentThread::tid());
string result;
FileUtil::readFile(buf, 65536, &result);
return result;
}

string ProcessInfo::exePath()
{
string result;
Expand Down
3 changes: 3 additions & 0 deletions muduo/base/ProcessInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace ProcessInfo
/// read /proc/self/stat
string procStat();

/// read /proc/self/task/tid/stat
string threadStat();

/// readlink /proc/self/exe
string exePath();

Expand Down
17 changes: 17 additions & 0 deletions muduo/base/tests/BlockingQueue_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <memory>
#include <string>
#include <stdio.h>
#include <unistd.h>
Expand Down Expand Up @@ -76,12 +77,28 @@ class Test
boost::ptr_vector<muduo::Thread> threads_;
};

void testMove()
{
#ifdef __GXX_EXPERIMENTAL_CXX0X__
muduo::BlockingQueue<std::unique_ptr<int>> queue;
queue.put(std::unique_ptr<int>(new int(42)));
std::unique_ptr<int> x = queue.take();
printf("took %d\n", *x);
*x = 123;
queue.put(std::move(x));
std::unique_ptr<int> y = queue.take();
printf("took %d\n", *y);
#endif
}

int main()
{
printf("pid=%d, tid=%d\n", ::getpid(), muduo::CurrentThread::tid());
Test t(5);
t.run(100);
t.joinAll();

testMove();

printf("number of created threads %d\n", muduo::Thread::numCreated());
}
4 changes: 4 additions & 0 deletions muduo/base/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ add_executable(atomic_unittest Atomic_unittest.cc)
add_executable(blockingqueue_test BlockingQueue_test.cc)
target_link_libraries(blockingqueue_test muduo_base)

add_executable(blockingqueue_cpp11_test BlockingQueue_test.cc)
target_link_libraries(blockingqueue_cpp11_test muduo_base_cpp11)
set_target_properties(blockingqueue_cpp11_test PROPERTIES COMPILE_FLAGS "-std=c++0x")

add_executable(blockingqueue_bench BlockingQueue_bench.cc)
target_link_libraries(blockingqueue_bench muduo_base)

Expand Down

0 comments on commit e29c815

Please sign in to comment.