summaryrefslogtreecommitdiffstats
path: root/gfx/2d/JobScheduler_posix.cpp
blob: e41388f21c2a43381285f34de882a9120991c132 (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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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/. */

#include "JobScheduler.h"
#include "mozilla/gfx/Logging.h"

using namespace std;

namespace mozilla {
namespace gfx {

void* ThreadCallback(void* threadData);

class WorkerThreadPosix : public WorkerThread {
public:
  explicit WorkerThreadPosix(MultiThreadedJobQueue* aJobQueue)
  : WorkerThread(aJobQueue)
  {
    pthread_create(&mThread, nullptr, ThreadCallback, static_cast<WorkerThread*>(this));
  }

  ~WorkerThreadPosix()
  {
    pthread_join(mThread, nullptr);
  }

  virtual void SetName(const char*) override
  {
// XXX - temporarily disabled, see bug 1209039
//
//    // Call this from the thread itself because of Mac.
//#ifdef XP_MACOSX
//    pthread_setname_np(aName);
//#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
//    pthread_set_name_np(mThread, aName);
//#elif defined(__NetBSD__)
//    pthread_setname_np(mThread, "%s", (void*)aName);
//#else
//    pthread_setname_np(mThread, aName);
//#endif
  }

protected:
  pthread_t mThread;
};

void* ThreadCallback(void* threadData)
{
  WorkerThread* thread = static_cast<WorkerThread*>(threadData);
  thread->Run();
  return nullptr;
}

WorkerThread*
WorkerThread::Create(MultiThreadedJobQueue* aJobQueue)
{
  return new WorkerThreadPosix(aJobQueue);
}

MultiThreadedJobQueue::MultiThreadedJobQueue()
: mThreadsCount(0)
, mShuttingDown(false)
{}

MultiThreadedJobQueue::~MultiThreadedJobQueue()
{
  MOZ_ASSERT(mJobs.empty());
}

bool
MultiThreadedJobQueue::WaitForJob(Job*& aOutJob)
{
  return PopJob(aOutJob, BLOCKING);
}

bool
MultiThreadedJobQueue::PopJob(Job*& aOutJobs, AccessType aAccess)
{
  for (;;) {
    CriticalSectionAutoEnter lock(&mMutex);

    while (aAccess == BLOCKING && !mShuttingDown && mJobs.empty()) {
      mAvailableCondvar.Wait(&mMutex);
    }

    if (mShuttingDown) {
      return false;
    }

    if (mJobs.empty()) {
      if (aAccess == NON_BLOCKING) {
        return false;
      }
      continue;
    }

    Job* task = mJobs.front();
    MOZ_ASSERT(task);

    mJobs.pop_front();

    aOutJobs = task;
    return true;
  }
}

void
MultiThreadedJobQueue::SubmitJob(Job* aJobs)
{
  MOZ_ASSERT(aJobs);
  CriticalSectionAutoEnter lock(&mMutex);
  mJobs.push_back(aJobs);
  mAvailableCondvar.Broadcast();
}

size_t
MultiThreadedJobQueue::NumJobs()
{
  CriticalSectionAutoEnter lock(&mMutex);
  return mJobs.size();
}

bool
MultiThreadedJobQueue::IsEmpty()
{
  CriticalSectionAutoEnter lock(&mMutex);
  return mJobs.empty();
}

void
MultiThreadedJobQueue::ShutDown()
{
  CriticalSectionAutoEnter lock(&mMutex);
  mShuttingDown = true;
  while (mThreadsCount) {
    mAvailableCondvar.Broadcast();
    mShutdownCondvar.Wait(&mMutex);
  }
}

void
MultiThreadedJobQueue::RegisterThread()
{
  mThreadsCount += 1;
}

void
MultiThreadedJobQueue::UnregisterThread()
{
  CriticalSectionAutoEnter lock(&mMutex);
  mThreadsCount -= 1;
  if (mThreadsCount == 0) {
    mShutdownCondvar.Broadcast();
  }
}

EventObject::EventObject()
: mIsSet(false)
{}

EventObject::~EventObject()
{}

bool
EventObject::Peak()
{
  CriticalSectionAutoEnter lock(&mMutex);
  return mIsSet;
}

void
EventObject::Set()
{
  CriticalSectionAutoEnter lock(&mMutex);
  if (!mIsSet) {
    mIsSet = true;
    mCond.Broadcast();
  }
}

void
EventObject::Wait()
{
  CriticalSectionAutoEnter lock(&mMutex);
  if (mIsSet) {
    return;
  }
  mCond.Wait(&mMutex);
}

} // namespce
} // namespce