summaryrefslogtreecommitdiffstats
path: root/dom/workers/Queue.h
blob: c7a99158bf637ca152854ba9182f3c44b6c84f5f (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_workers_queue_h__
#define mozilla_dom_workers_queue_h__

#include "Workers.h"

#include "mozilla/Mutex.h"
#include "nsTArray.h"

BEGIN_WORKERS_NAMESPACE

template <typename T, int TCount>
struct StorageWithTArray
{
  typedef AutoTArray<T, TCount> StorageType;

  static void Reverse(StorageType& aStorage)
  {
    uint32_t length = aStorage.Length();
    for (uint32_t index = 0; index < length / 2; index++) {
      uint32_t reverseIndex = length - 1 - index;

      T t1 = aStorage.ElementAt(index);
      T t2 = aStorage.ElementAt(reverseIndex);

      aStorage.ReplaceElementsAt(index, 1, t2);
      aStorage.ReplaceElementsAt(reverseIndex, 1, t1);
    }
  }

  static bool IsEmpty(const StorageType& aStorage)
  {
    return !!aStorage.IsEmpty();
  }

  static bool Push(StorageType& aStorage, const T& aEntry)
  {
    return !!aStorage.AppendElement(aEntry);
  }

  static bool Pop(StorageType& aStorage, T& aEntry)
  {
    if (IsEmpty(aStorage)) {
      return false;
    }

    uint32_t index = aStorage.Length() - 1;
    aEntry = aStorage.ElementAt(index);
    aStorage.RemoveElementAt(index);
    return true;
  }

  static void Clear(StorageType& aStorage)
  {
    aStorage.Clear();
  }

  static void Compact(StorageType& aStorage)
  {
    aStorage.Compact();
  }
};

class LockingWithMutex
{
  mozilla::Mutex mMutex;

protected:
  LockingWithMutex()
  : mMutex("LockingWithMutex::mMutex")
  { }

  void Lock()
  {
    mMutex.Lock();
  }

  void Unlock()
  {
    mMutex.Unlock();
  }

  class AutoLock
  {
    LockingWithMutex& mHost;

  public:
    explicit AutoLock(LockingWithMutex& aHost)
    : mHost(aHost)
    {
      mHost.Lock();
    }

    ~AutoLock()
    {
      mHost.Unlock();
    }
  };

  friend class AutoLock;
};

class NoLocking
{
protected:
  void Lock()
  { }

  void Unlock()
  { }

  class AutoLock
  {
  public:
    explicit AutoLock(NoLocking& aHost)
    { }

    ~AutoLock()
    { }
  };
};

template <typename T,
          int TCount = 256,
          class LockingPolicy = NoLocking,
          class StoragePolicy = StorageWithTArray<T, TCount % 2 ?
                                                     TCount / 2 + 1 :
                                                     TCount / 2> >
class Queue : public LockingPolicy
{
  typedef typename StoragePolicy::StorageType StorageType;
  typedef typename LockingPolicy::AutoLock AutoLock;

  StorageType mStorage1;
  StorageType mStorage2;

  StorageType* mFront;
  StorageType* mBack;

public:
  Queue()
  : mFront(&mStorage1), mBack(&mStorage2)
  { }

  bool IsEmpty()
  {
    AutoLock lock(*this);
    return StoragePolicy::IsEmpty(*mFront) &&
           StoragePolicy::IsEmpty(*mBack);
  }

  bool Push(const T& aEntry)
  {
    AutoLock lock(*this);
    return StoragePolicy::Push(*mBack, aEntry);
  }

  bool Pop(T& aEntry)
  {
    AutoLock lock(*this);
    if (StoragePolicy::IsEmpty(*mFront)) {
      StoragePolicy::Compact(*mFront);
      StoragePolicy::Reverse(*mBack);
      StorageType* tmp = mFront;
      mFront = mBack;
      mBack = tmp;
    }
    return StoragePolicy::Pop(*mFront, aEntry);
  }

  void Clear()
  {
    AutoLock lock(*this);
    StoragePolicy::Clear(*mFront);
    StoragePolicy::Clear(*mBack);
  }

  // XXX Do we need this?
  void Lock()
  {
    LockingPolicy::Lock();
  }

  // XXX Do we need this?
  void Unlock()
  {
    LockingPolicy::Unlock();
  }

private:
  // Queue is not copyable.
  Queue(const Queue&);
  Queue & operator=(const Queue&);
};

END_WORKERS_NAMESPACE

#endif /* mozilla_dom_workers_queue_h__ */