1d3811de4SShuo Chen#include "../Mutex.h"
2d3811de4SShuo Chen#include "../Thread.h"
3d3811de4SShuo Chen#include <set>
4d3811de4SShuo Chen#include <stdio.h>
5d3811de4SShuo Chen
6d3811de4SShuo Chenclass Request;
7d3811de4SShuo Chen
8d3811de4SShuo Chenclass Inventory
9d3811de4SShuo Chen{
10d3811de4SShuo Chen public:
11d3811de4SShuo Chen  void add(Request* req)
12d3811de4SShuo Chen  {
13d3811de4SShuo Chen    muduo::MutexLockGuard lock(mutex_);
14d3811de4SShuo Chen    requests_.insert(req);
15d3811de4SShuo Chen  }
16d3811de4SShuo Chen
17d3811de4SShuo Chen  void remove(Request* req) __attribute__ ((noinline))
18d3811de4SShuo Chen  {
19d3811de4SShuo Chen    muduo::MutexLockGuard lock(mutex_);
20d3811de4SShuo Chen    requests_.erase(req);
21d3811de4SShuo Chen  }
22d3811de4SShuo Chen
23d3811de4SShuo Chen  void printAll() const;
24d3811de4SShuo Chen
25d3811de4SShuo Chen private:
26d3811de4SShuo Chen  mutable muduo::MutexLock mutex_;
27d3811de4SShuo Chen  std::set<Request*> requests_;
28d3811de4SShuo Chen};
29d3811de4SShuo Chen
30d3811de4SShuo ChenInventory g_inventory;
31d3811de4SShuo Chen
32d3811de4SShuo Chenclass Request
33d3811de4SShuo Chen{
34d3811de4SShuo Chen public:
35d3811de4SShuo Chen  void process() // __attribute__ ((noinline))
36d3811de4SShuo Chen  {
37d3811de4SShuo Chen    muduo::MutexLockGuard lock(mutex_);
38d3811de4SShuo Chen    g_inventory.add(this);
39d3811de4SShuo Chen    // ...
40d3811de4SShuo Chen  }
41d3811de4SShuo Chen
42d3811de4SShuo Chen  ~Request() __attribute__ ((noinline))
43d3811de4SShuo Chen  {
44d3811de4SShuo Chen    muduo::MutexLockGuard lock(mutex_);
45d3811de4SShuo Chen    sleep(1);
46d3811de4SShuo Chen    g_inventory.remove(this);
47d3811de4SShuo Chen  }
48d3811de4SShuo Chen
49d3811de4SShuo Chen  void print() const __attribute__ ((noinline))
50d3811de4SShuo Chen  {
51d3811de4SShuo Chen    muduo::MutexLockGuard lock(mutex_);
52d3811de4SShuo Chen    // ...
53d3811de4SShuo Chen  }
54d3811de4SShuo Chen
55d3811de4SShuo Chen private:
56d3811de4SShuo Chen  mutable muduo::MutexLock mutex_;
57d3811de4SShuo Chen};
58d3811de4SShuo Chen
59d3811de4SShuo Chenvoid Inventory::printAll() const
60d3811de4SShuo Chen{
61d3811de4SShuo Chen  muduo::MutexLockGuard lock(mutex_);
62d3811de4SShuo Chen  sleep(1);
63d3811de4SShuo Chen  for (std::set<Request*>::const_iterator it = requests_.begin();
64d3811de4SShuo Chen      it != requests_.end();
65d3811de4SShuo Chen      ++it)
66d3811de4SShuo Chen  {
67d3811de4SShuo Chen    (*it)->print();
68d3811de4SShuo Chen  }
69d3811de4SShuo Chen  printf("Inventory::printAll() unlocked\n");
70d3811de4SShuo Chen}
71d3811de4SShuo Chen
722d3b4de6SShuo Chen/*
732d3b4de6SShuo Chenvoid Inventory::printAll() const
742d3b4de6SShuo Chen{
752d3b4de6SShuo Chen  std::set<Request*> requests
762d3b4de6SShuo Chen  {
772d3b4de6SShuo Chen    muduo::MutexLockGuard lock(mutex_);
782d3b4de6SShuo Chen    requests = requests_;
792d3b4de6SShuo Chen  }
802d3b4de6SShuo Chen  for (std::set<Request*>::const_iterator it = requests.begin();
812d3b4de6SShuo Chen      it != requests.end();
822d3b4de6SShuo Chen      ++it)
832d3b4de6SShuo Chen  {
842d3b4de6SShuo Chen    (*it)->print();
852d3b4de6SShuo Chen  }
862d3b4de6SShuo Chen}
872d3b4de6SShuo Chen*/
882d3b4de6SShuo Chen
89d3811de4SShuo Chenvoid threadFunc()
90d3811de4SShuo Chen{
91d3811de4SShuo Chen  Request* req = new Request;
92d3811de4SShuo Chen  req->process();
93d3811de4SShuo Chen  delete req;
94d3811de4SShuo Chen}
95d3811de4SShuo Chen
96d3811de4SShuo Chenint main()
97d3811de4SShuo Chen{
98d3811de4SShuo Chen  muduo::Thread thread(threadFunc);
99d3811de4SShuo Chen  thread.start();
1002d3b4de6SShuo Chen  usleep(500 * 1000);
101d3811de4SShuo Chen  g_inventory.printAll();
102d3811de4SShuo Chen  thread.join();
103d3811de4SShuo Chen}
104