MemManager.hpp 4.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/******************************************************************************
 *                      MemManager.hpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#pragma once

#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
#include "castor/tape/tapeserver/daemon/MemManagerClient.hpp"
29
30
#include "castor/tape/tapeserver/threading/BlockingQueue.hpp"
#include "castor/tape/tapeserver/threading/Threading.hpp"
31
32
33
#include "castor/exception/Exception.hpp"
#include <iostream>

34
35
36
37
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
38
39
40
41
/**
 * The memory manager is responsible for allocating memory blocks and distributing
 * the free ones around to any class in need.
 */
42
class MemoryManager: private castor::tape::threading::Thread {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public:
  
  /**
   * Constructor
   * @param numberOfBlocks: number of blocks to allocate
   * @param blockSize: size of each block
   */
  MemoryManager(const size_t numberOfBlocks, const size_t blockSize) throw(castor::exception::Exception) : m_totalNumberOfBlocks(numberOfBlocks) {
    for (size_t i = 0; i < numberOfBlocks; i++) {
      m_freeBlocks.push(new MemBlock(i, blockSize));
    }
  }
  
  /**
   * Are all sheep back to the farm?
   * @return 
   */
  bool areBlocksAllBack() throw() {
    return m_totalNumberOfBlocks==m_freeBlocks.size();
  }
  
  /**
   * Start serving clients (in the dedicated thread)
   */
  void startThreads() throw(castor::exception::Exception) {
68
    castor::tape::threading::Thread::start();
69
70
71
72
73
74
  }
  
  /**
   * Waiting for clients to finish (in the dedicated thread)
   */
  void waitThreads() throw(castor::exception::Exception) {
75
    castor::tape::threading::Thread::wait();
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  }
  
  /**
   * Adds a new client in need for free memory blocks
   * @param c: the new client
   */
  void addClient(MemoryManagerClient* c) throw(castor::exception::Exception) {
    m_clientQueue.push(c);
  }
  
  /**
   * Takes back a block which has been released by one of the clients
   * @param mb: the pointer to the block
   */
  void releaseBlock(MemBlock *mb) throw(castor::exception::Exception) {
    mb->reset();
    m_freeBlocks.push(mb);
  }
  
  /**
   * Function used to specify that there are no more clients for this memory manager.
   * See the definition of endOfClients below.
   */
  void finish() throw(castor::exception::Exception) {
    addClient(new endOfClients());
  }

  /**
   * Destructor
   */
  ~MemoryManager() throw() {
    // Make sure the thread is finished: this should be done by the caller,
    // who should have called waitThreads.
109
    // castor::tape::threading::Thread::wait();
110
111
112
113
    // we expect to be called after all users are finished. Just "free"
    // the memory blocks we still have.
    try {
      while(true) {
114
        delete m_freeBlocks.tryPop();
115
116
      }
    }
117
    catch (castor::tape::threading::noMore) {
118
119
120
121
      //done
    } 
  }
  
122
123
124
  MemBlock* getFreeBlock(){
    return m_freeBlocks.pop();
  }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
private:
  
  /**
   * Special token used to say that there are no more clients for this memory manager
   */
  class endOfClients: public MemoryManagerClient {
    virtual bool endOfWork() throw() {
      return true;
    }
  };
  
  /**
   * Total number of allocated memory blocks
   */
  size_t m_totalNumberOfBlocks;
  
  /**
   * Container for the free blocks
   */
144
  castor::tape::threading::BlockingQueue<MemBlock *> m_freeBlocks;
145
146
147
148
149
  
  /**
   * The client queue: we will feed them as soon as blocks
   * become free. This is done in a dedicated thread.
   */
150
   castor::tape::threading::BlockingQueue<MemoryManagerClient *> m_clientQueue;
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

  /**
   * Thread routine: pops a client and provides him blocks until he is happy!
   */
  void run() throw(castor::exception::Exception) {
    while(true) {
      MemoryManagerClient* c = m_clientQueue.pop();
    
      /* This is an unfortunate special case: WE allocate the end block
       and WE have to delete it. */
      if (c->endOfWork()) {
        delete c;
        return;
      };
      
      /* Spin on the the client. We rely on the fact that he will want
       at least one block (which is the case currently) */
      while (c->provideBlock(m_freeBlocks.pop()));
    }
  }
  
};
173
174
175
176
177
178

}
}
}
}