DiskWriteTask.hpp 4.14 KB
Newer Older
1
/******************************************************************************
2
 *                      DiskWriteFileTask.hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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

27
28
29
30
#include "castor/tape/tapeserver/daemon/DiskWriteTask.hpp"
#include "castor/tape/tapeserver/daemon/DataFifo.hpp"
#include "castor/tape/tapeserver/daemon/MemManager.hpp"
#include "castor/tape/tapeserver/daemon/DataConsumer.hpp"
31

David COME's avatar
David COME committed
32
33
34
35
36
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
37
/**
38
39
40
 * The DiskWriteFileTask is responsible to write a single file onto disk as part of a recall
 * session. Being a consumer of memory blocks, it inherits from the DataConsumer class. It also
 * inherits several methods from the DiskWriteTask (TODO: do we really need this base class?).
41
 */
42
class DiskWriteFileTask: public DiskWriteTask, public DataConsumer {
43
public:
44
45
46
47
48
49
50
51
52
  /**
   * Constructor
   * @param fileId: file id of the file to write to disk
   * @param blockCount: number of memory blocks that will be used
   * @param mm: memory manager of the session
   */
  DiskWriteFileTask(int fileId, int blockCount, MemoryManager& mm): m_fifo(blockCount), 
          m_blockCount(blockCount), m_fileId(fileId), 
          m_memManager(mm) { mm.addClient(&m_fifo); }
53
54
  
  /**
55
56
   * TODO: Do we need this here?
   * @return always false
57
   */
58
  virtual bool endOfWork() { return false; }
59
60
  
  /**
61
62
   * Return the numebr of files to write to disk
   * @return always 1
63
   */
64
  virtual int files() { return 1; };
65
66
  
  /**
67
   * @return the number of memory blocks to be used
68
   */
69
  virtual int blocks() { return m_blockCount; }
70
71
  
  /**
72
   * Main routine: takes each memory block in the fifo and writes it to disk
73
74
   */
  virtual void execute() {
75
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    int blockId  = 0;
    while(!m_fifo.finished()) {
      //printf("+++ In disk write file, id=%d\n",m_fileId);
      MemBlock *mb = m_fifo.popDataBlock();
      mb->m_fileid = m_fileId;
      mb->m_fileBlock = blockId++;
    }
  }
  
  /**
   * Allows client code to return a reusable memory block
   * @return the pointer to the memory block that can be reused
   */
  virtual MemBlock *getFreeBlock() { return m_fifo.getFreeBlock(); }
  
  /**
   * Function used to enqueue a new memory block holding data to be written to disk
   * @param mb: corresponding memory block
   */
  virtual void pushDataBlock(MemBlock *mb) {
    castor::tape::threading::MutexLocker ml(&m_producerProtection);
    m_fifo.pushDataBlock(mb);
  }
  
  /**
   * Function used to wait until the end of the write
   */
  virtual void waitCompletion() { volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); }
  
  /**
   * Destructor (also waiting for the end of the write operation)
   */
  virtual ~DiskWriteFileTask() { volatile castor::tape::threading::MutexLocker ml(&m_producerProtection); }
  
private:
  
  /**
   * The fifo containing the memory blocks holding data to be written to disk
   */
  DataFifo m_fifo;
  
  /**
   * Number of blocks in the fifo
   */
  int m_blockCount;
  
  /**
   * File id of the file that will be written to disk
   */
  int m_fileId;
125
126
  
  /**
127
   * Reference to the Memory Manager in use
128
   */
129
  MemoryManager & m_memManager;
130
131
  
  /**
132
   * Mutex forcing serial access to the fifo
133
   */
134
  castor::tape::threading::Mutex m_producerProtection;
135
};
David COME's avatar
David COME committed
136
137

}}}}