Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef WIN32
7 : #ifndef MOZILLA_GFX_TASKSCHEDULER_POSIX_H_
8 : #define MOZILLA_GFX_TASKSCHEDULER_POSIX_H_
9 :
10 : #include <string>
11 : #include <vector>
12 : #include <list>
13 : #include <pthread.h>
14 : #include <stdint.h>
15 : #include <stdio.h>
16 :
17 : #include "mozilla/RefPtr.h"
18 : #include "mozilla/DebugOnly.h"
19 : #include "mozilla/gfx/CriticalSection.h"
20 : #include "mozilla/RefCounted.h"
21 :
22 : namespace mozilla {
23 : namespace gfx {
24 :
25 : class Job;
26 : class PosixCondVar;
27 : class WorkerThread;
28 :
29 : // posix platforms only!
30 : class PosixCondVar {
31 : public:
32 0 : PosixCondVar() {
33 0 : DebugOnly<int> err = pthread_cond_init(&mCond, nullptr);
34 0 : MOZ_ASSERT(!err);
35 0 : }
36 :
37 0 : ~PosixCondVar() {
38 0 : DebugOnly<int> err = pthread_cond_destroy(&mCond);
39 0 : MOZ_ASSERT(!err);
40 0 : }
41 :
42 0 : void Wait(CriticalSection* aMutex) {
43 0 : DebugOnly<int> err = pthread_cond_wait(&mCond, &aMutex->mMutex);
44 0 : MOZ_ASSERT(!err);
45 0 : }
46 :
47 0 : void Broadcast() {
48 0 : DebugOnly<int> err = pthread_cond_broadcast(&mCond);
49 0 : MOZ_ASSERT(!err);
50 0 : }
51 :
52 : protected:
53 : pthread_cond_t mCond;
54 : };
55 :
56 :
57 : /// A simple and naive multithreaded task queue
58 : ///
59 : /// The public interface of this class must remain identical to its equivalent
60 : /// in JobScheduler_win32.h
61 : class MultiThreadedJobQueue {
62 : public:
63 : enum AccessType {
64 : BLOCKING,
65 : NON_BLOCKING
66 : };
67 :
68 : // Producer thread
69 : MultiThreadedJobQueue();
70 :
71 : // Producer thread
72 : ~MultiThreadedJobQueue();
73 :
74 : // Worker threads
75 : bool WaitForJob(Job*& aOutJob);
76 :
77 : // Any thread
78 : bool PopJob(Job*& aOutJob, AccessType aAccess);
79 :
80 : // Any threads
81 : void SubmitJob(Job* aJob);
82 :
83 : // Producer thread
84 : void ShutDown();
85 :
86 : // Any thread
87 : size_t NumJobs();
88 :
89 : // Any thread
90 : bool IsEmpty();
91 :
92 : // Producer thread
93 : void RegisterThread();
94 :
95 : // Worker threads
96 : void UnregisterThread();
97 :
98 : protected:
99 :
100 : std::list<Job*> mJobs;
101 : CriticalSection mMutex;
102 : PosixCondVar mAvailableCondvar;
103 : PosixCondVar mShutdownCondvar;
104 : int32_t mThreadsCount;
105 : bool mShuttingDown;
106 :
107 : friend class WorkerThread;
108 : };
109 :
110 : /// An object that a thread can synchronously wait on.
111 : /// Usually set by a SetEventJob.
112 0 : class EventObject : public external::AtomicRefCounted<EventObject>
113 : {
114 : public:
115 0 : MOZ_DECLARE_REFCOUNTED_TYPENAME(EventObject)
116 :
117 : EventObject();
118 :
119 : ~EventObject();
120 :
121 : /// Synchronously wait until the event is set.
122 : void Wait();
123 :
124 : /// Return true if the event is set, without blocking.
125 : bool Peak();
126 :
127 : /// Set the event.
128 : void Set();
129 :
130 : protected:
131 : CriticalSection mMutex;
132 : PosixCondVar mCond;
133 : bool mIsSet;
134 : };
135 :
136 : } // namespace
137 : } // namespace
138 :
139 : #include "JobScheduler.h"
140 :
141 : #endif
142 : #endif
|