~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/mgmsrv/SignalQueue.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef __SIGNALQUEUE_HPP_INCLUDED__
 
17
#define __SIGNALQUEUE_HPP_INCLUDED__
 
18
 
 
19
#include <NdbApiSignal.hpp>
 
20
#include <NdbMutex.h>
 
21
#include <NdbCondition.h>
 
22
#include <Vector.hpp>
 
23
 
 
24
/* XXX Look for an already existing definition */
 
25
#define DEFAULT_TIMEOUT 5000
 
26
 
 
27
class SignalQueue {
 
28
public:
 
29
  typedef void (* SignalHandler)(void *obj, int gsn, NdbApiSignal *signal);
 
30
 
 
31
  SignalQueue();
 
32
  ~SignalQueue();
 
33
 
 
34
  /**
 
35
   * Static wrapper making it possible to call receive without knowing the
 
36
   * type of the receiver
 
37
   */
 
38
  static void receive(void *me, NdbApiSignal *signal);
 
39
 
 
40
  /**
 
41
   * Enqueues a signal, and notifies any thread waiting for signals.
 
42
   */
 
43
  void receive(NdbApiSignal *signal);
 
44
 
 
45
  NdbApiSignal *waitFor(int gsn,
 
46
                        NodeId nodeid = 0,
 
47
                        Uint32 timeout = DEFAULT_TIMEOUT);
 
48
  template<class T> bool waitFor(Vector<T> &t,
 
49
                                 T **handler,
 
50
                                 NdbApiSignal **signal,
 
51
                                 Uint32 timeout = DEFAULT_TIMEOUT);
 
52
private:
 
53
  NdbMutex *m_mutex; /* Locks all data in SignalQueue */
 
54
  NdbCondition *m_cond; /* Notifies about new signal in the queue */
 
55
 
 
56
  /**
 
57
   * Returns the last recently received signal. Must be called with
 
58
   * m_mutex locked.
 
59
   * The caller takes responsibility for deleting the returned object.
 
60
   *
 
61
   * @returns NULL if failed, or a received signal
 
62
   */
 
63
  NdbApiSignal *pop();
 
64
 
 
65
  class QueueEntry {
 
66
  public:
 
67
    NdbApiSignal *signal;
 
68
    QueueEntry *next;
 
69
  };
 
70
  QueueEntry *m_signalQueueHead; /** Head of the queue.
 
71
                                  *  New entries added on the tail
 
72
                                  */
 
73
};
 
74
 
 
75
template<class T> bool
 
76
SignalQueue::waitFor(Vector<T> &t,
 
77
                     T **handler,
 
78
                     NdbApiSignal **signal,
 
79
                     Uint32 timeout) {
 
80
  Guard g(m_mutex);
 
81
 
 
82
  if(m_signalQueueHead == NULL)
 
83
    NdbCondition_WaitTimeout(m_cond, m_mutex, timeout);
 
84
 
 
85
  if(m_signalQueueHead == NULL)
 
86
    return false;
 
87
 
 
88
  for(size_t i = 0; i < t.size(); i++) {
 
89
    if(t[i].check(m_signalQueueHead->signal)) {
 
90
      * handler = &t[i];
 
91
      * signal = pop();
 
92
      return true;
 
93
    }
 
94
  }
 
95
 
 
96
  return false;
 
97
}
 
98
 
 
99
#endif /* !__SIGNALQUEUE_HPP_INCLUDED__ */