summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Persistent_File_Allocator.cpp
blob: 24f960f74f48f3b33c1d5c8f90222ca18155f4b8 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// $Id$

#include "Persistent_File_Allocator.h"

#include <tao/debug.h>
#include "ace/OS_NS_string.h"

//#define DEBUG_LEVEL 9
#define DEBUG_LEVEL TAO_debug_level

namespace TAO_Notify
{

Persistent_Storage_Block::Persistent_Storage_Block(const size_t block_number,
  const size_t block_size)
  : block_number_(block_number)
  , no_write_(false)
  , sync_(false)
  , block_size_(block_size)
  , callback_(0)
  , allocator_owns_(true)
{
  ACE_NEW(this->data_, unsigned char[this->block_size_]);
  ACE_OS::memset(this->data_, 0, this->block_size_);

}

Persistent_Storage_Block::Persistent_Storage_Block(
  const Persistent_Storage_Block& psb)
  : block_number_(psb.block_number_)
  , no_write_(psb.no_write_)
  , sync_(psb.sync_)
  , block_size_(psb.block_size_)
  , callback_(psb.callback_)
  , allocator_owns_(psb.allocator_owns_)
{
  ACE_NEW(this->data_, unsigned char[this->block_size_]);
  ACE_OS::memcpy(this->data_, psb.data(), this->block_size_);
}

Persistent_Storage_Block::~Persistent_Storage_Block()
{
  delete [] this->data_;
  this->data_ = 0;
}

void
Persistent_Storage_Block::set_no_write()
{
  this->no_write_ = true;
  this->reassign_data(0, true);
}

bool
Persistent_Storage_Block::get_no_write()
{
  return this->no_write_;
}

void
Persistent_Storage_Block::set_sync()
{
  this->sync_ = true;
}

bool
Persistent_Storage_Block::get_sync() const
{
  return this->sync_;
}

size_t
Persistent_Storage_Block::block_number() const
{
  return this->block_number_;
}

unsigned char*
Persistent_Storage_Block::data() const
{
  return this->data_;
}

void
Persistent_Storage_Block::reassign_data(unsigned char* newptr,
  bool delete_old)
{
  if (delete_old)
  {
    delete [] this->data_;
  }
  this->data_ = newptr;
}

void
Persistent_Storage_Block::set_callback(Persistent_Callback* callback)
{
  this->callback_ = callback;
}

Persistent_Callback*
Persistent_Storage_Block::get_callback() const
{
  return this->callback_;
}

void
Persistent_Storage_Block::set_allocator_owns(bool allocator_owns)
{
  this->allocator_owns_ = allocator_owns;
}

bool
Persistent_Storage_Block::get_allocator_owns() const
{
  return this->allocator_owns_;
}

Persistent_File_Allocator::Persistent_File_Allocator()
  : pstore_()
  , terminate_thread_(false)
  , thread_active_(false)
  , wake_up_thread_(queue_lock_)
{
}

Persistent_File_Allocator::~Persistent_File_Allocator()
{
  this->shutdown_thread();
}

bool
Persistent_File_Allocator::open (const char* filename,
  const size_t block_size)
{
  bool file_opened = this->pstore_.open(filename, block_size);
  if (file_opened)
  {
    this->thread_active_ = true;
    this->thread_manager_.spawn(this->thr_func, this);
  }
  return file_opened;
}

void
Persistent_File_Allocator::shutdown()
{
  this->shutdown_thread();
}

Persistent_Storage_Block*
Persistent_File_Allocator::allocate()
{
  Persistent_Storage_Block* result = 0;
  size_t block_number = 0;
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, 0);
  if (!this->allocate_block(block_number))
  {
    //@@todo: this should never happen
    // why not.  What if the disk is full?  Oh, I see we
    // allocate non-existent blocks.  FIX this
  }
  if (DEBUG_LEVEL > 0) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Persistent_File_Allocator::allocate: %d\n"),
    ACE_static_cast (int, block_number)
    ));
  result = this->allocate_at(block_number);
  return result;
}

Persistent_Storage_Block*
Persistent_File_Allocator::allocate_at(size_t block_number)
{
  Persistent_Storage_Block* result = 0;
  this->used(block_number);
  if (DEBUG_LEVEL > 0) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Persistent_File_Allocator::allocate at : %d\n"),
    ACE_static_cast (int, block_number)
    ));
  ACE_NEW_RETURN(result, Persistent_Storage_Block(
    block_number,
    this->block_size()),
    0);
  return result;
}

Persistent_Storage_Block*
Persistent_File_Allocator::allocate_nowrite()
{
  Persistent_Storage_Block* result = 0;
  ACE_NEW_RETURN(result, Persistent_Storage_Block(~0, 0), 0);
  result->set_no_write();
  return result;
}

void
Persistent_File_Allocator::used(size_t block_number)
{
  ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->free_blocks_lock_);
  if (DEBUG_LEVEL > 0) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Persistent_File_Allocator::used: %d\n"),
    ACE_static_cast (int, block_number)
    ));
  ACE_ASSERT (!this->free_blocks_.is_set (block_number));
  this->free_blocks_.set_bit(block_number, true);
}

void
Persistent_File_Allocator::free(size_t block_number)
{
  if (DEBUG_LEVEL > 0) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Persistent_File_Allocator::free: %d\n"),
    ACE_static_cast (int, block_number)
    ));
  ACE_ASSERT (this->free_blocks_.is_set (block_number));
  this->free_block(block_number);
}

size_t
Persistent_File_Allocator::block_size() const
{
  return pstore_.block_size();
}

bool
Persistent_File_Allocator::read(Persistent_Storage_Block* psb)
{
  bool result = this->thread_active_;
  bool cached = false;
  if (result)
  {
    Persistent_Storage_Block** psbtemp = 0;
    {
      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->queue_lock_, false);
      size_t queue_size = this->block_queue_.size();
      for (size_t idx = 0; !cached && (idx < queue_size); ++idx)
      {
        // We want to start at the end of the queue and work backwards...
        size_t actual_block = (queue_size - idx) - 1;
        if (0 == this->block_queue_.get(psbtemp, actual_block))
        {
          cached = ((*psbtemp)->block_number() == psb->block_number());
        }
      }
      // this needs to be done in the guarded section
      if (cached && (0 != psbtemp))
      {
        ACE_OS::memcpy(psb->data(), (*psbtemp)->data(), this->block_size());
      }
    }
    if (!cached)
    {
      result = pstore_.read(psb->block_number(), psb->data());
    }
  }
  return result;
}

bool
Persistent_File_Allocator::write(Persistent_Storage_Block* psb)
{
  bool result = this->thread_active_;
  if (result)
  {
    Persistent_Storage_Block* ourpsb = psb;
    if (!psb->get_allocator_owns())
    {
      if (DEBUG_LEVEL) ACE_DEBUG ((LM_DEBUG,
        ACE_TEXT ("(%P|%t) Copy PSB %d\n")
        , ACE_static_cast (int, psb->block_number ())
        ));
      ACE_NEW_RETURN(ourpsb, Persistent_Storage_Block(*psb), false);
      ourpsb->set_allocator_owns(true);
    }
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->queue_lock_, false);
    if (DEBUG_LEVEL) ACE_DEBUG ((LM_DEBUG,
      ACE_TEXT ("(%P|%t) Queueing PSB to write block %d\n")
      , ACE_static_cast (int, psb->block_number ())
      ));
    result = (0 == this->block_queue_.enqueue_tail(ourpsb));
    this->wake_up_thread_.signal();
  }
  return result;
}

void
Persistent_File_Allocator::free_block(const size_t block_number)
{
  ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->free_blocks_lock_);
  ACE_ASSERT (this->free_blocks_.is_set (block_number));
  this->free_blocks_.set_bit(block_number, false);
}

bool
Persistent_File_Allocator::allocate_block(size_t& block_number)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->free_blocks_lock_, 0);
  block_number = this->free_blocks_.find_first_bit(false);
  return true;
}

ACE_THR_FUNC_RETURN
Persistent_File_Allocator::thr_func(void * arg)
{
  Persistent_File_Allocator* pfa = ACE_static_cast (Persistent_File_Allocator*,
    arg);
  pfa->run();
  return 0;
}

size_t
Persistent_File_Allocator::file_size () const
{
  return this->pstore_.size ();
}

void
Persistent_File_Allocator::shutdown_thread()
{
  if (this->thread_active_)
  {
    ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->queue_lock_);
    this->terminate_thread_ = true;
    this->wake_up_thread_.signal();
    ace_mon.release();
    this->thread_manager_.close();
    ACE_ASSERT (!this->terminate_thread_);
    ACE_ASSERT (!this->thread_active_);
  }
}

void
Persistent_File_Allocator::run()
{
  // We need this because we could be working on writing data
  // when a call to terminate comes in!
  bool do_more_work = true;
  while (do_more_work)
  {
    do_more_work = false;
    Persistent_Storage_Block * blk = 0;
    {
      ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->queue_lock_);
      while (this->block_queue_.is_empty() && !terminate_thread_)
      {
        this->wake_up_thread_.wait();
      }
      // Awkward interface to peek at head of unbounded queue
      Persistent_Storage_Block ** pblk = 0;
      if (0 == this->block_queue_.get(pblk))
      {
        do_more_work = true;
        blk = *pblk;
      }
    }
    if (0 != blk)
    {
      Persistent_Callback *callback = blk->get_callback();
      if (!blk->get_no_write())
      {
        pstore_.write(blk->block_number(), blk->data(), blk->get_sync());
      }
      {
        Persistent_Storage_Block * blk2 = 0;
        ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->queue_lock_);
        this->block_queue_.dequeue_head (blk2);
        // if this triggers, someone pushed onto the head of the queue
        // or removed the head from the queue without telling ME.
        ACE_ASSERT (blk2 == blk);
      }
      // If we own the block, then delete it.
      if (blk->get_allocator_owns())
      {
        delete blk;
        blk = 0;
      }
      if (0 != callback)
      {
        callback->persist_complete();
      }
    }
  }
  this->terminate_thread_ = false;
  this->thread_active_ = false;
}

} /* namespace TAO_Notify */

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Node<size_t>;
template class ACE_Unbounded_Stack<size_t>;
template class ACE_Node<TAO_Notify::Persistent_Storage_Block*>;
template class ACE_Unbounded_Queue<TAO_Notify::Persistent_Storage_Block*>;
template class ACE_Unbounded_Queue_Iterator<TAO_Notify::Persistent_Storage_Block*>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Node<size_t>
#pragma instantiate ACE_Unbounded_Stack<size_t>
#pragma instantiate ACE_Node<TAO_Notify::Persistent_Storage_Block*>
#pragma instantiate ACE_Unbounded_Queue<TAO_Notify::Persistent_Storage_Block*>
#pragma instantiate ACE_Unbounded_Queue_Iterator<TAO_Notify::Persistent_Storage_Block*>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */