ScummVM API documentation
thread.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef PSP_THREAD_H
23 #define PSP_THREAD_H
24 
25 #include <pspthreadman.h>
26 #include "common/mutex.h"
27 
28 // class to inherit for creating threads
30 protected:
31  int _threadId;
32  virtual void threadFunction() = 0; // this function will be called when the thread starts
33 public:
34  PspThreadable() : _threadId(-1) {} // constructor
35  virtual ~PspThreadable() {} // destructor
36  static int __threadCallback(SceSize, void *__this); // used to get called by sceKernelStartThread() Don't override
37  bool threadCreateAndStart(const char *threadName, int priority, int stackSize, bool useVfpu = false);
38 };
39 
40 // class for thread utils
41 class PspThread {
42 public:
43  // static functions
44  static void delayMillis(uint32 ms); // delay the current thread
45  static void delayMicros(uint32 us);
46 };
47 
48 class PspSemaphore {
49 private:
50  uint32 _handle;
51 public:
52  PspSemaphore(int initialValue, int maxValue=255);
53  ~PspSemaphore();
54  bool take() { return takeWithTimeOut(0); }
55  bool takeWithTimeOut(uint32 timeOut);
56  bool give(int num=1);
57  bool pollForValue(int value); // check for a certain value
58  int numOfWaitingThreads();
59  int getValue();
60 };
61 
63 private:
64  PspSemaphore _semaphore;
65  int _recursiveCount;
66  int _ownerId;
67 public:
68  PspMutex(bool initialValue) : _semaphore(initialValue ? 1 : 0, 255), _recursiveCount(0), _ownerId(0) {} // initial, max value
69  bool lock();
70  bool unlock();
71  bool poll() { return _semaphore.pollForValue(1); }
72  int numOfWaitingThreads() { return _semaphore.numOfWaitingThreads(); }
73  bool getValue() { return (bool)_semaphore.getValue(); }
74 };
75 
76 class PspCondition {
77 private:
78  PspMutex _mutex;
79  int _waitingThreads;
80  int _signaledThreads;
81  PspSemaphore _waitSem;
82  PspSemaphore _doneSem;
83 public:
84  PspCondition() : _mutex(true), _waitingThreads(0), _signaledThreads(0),
85  _waitSem(0), _doneSem(0) {}
86  void wait(PspMutex &externalMutex);
87  void releaseAll();
88 };
89 
90 enum ThreadPriority {
91  PRIORITY_MAIN_THREAD = 36,
92  PRIORITY_TIMER_THREAD = 30,
93  PRIORITY_AUDIO_THREAD = 25, // must be higher than timer or we get stuttering
94  PRIORITY_POWER_THREAD = 20, // quite a light thread
95  PRIORITY_DISPLAY_THREAD = 17 // very light thread for callbacks only
96 };
97 
98 enum StackSizes {
99  STACK_DEFAULT = 4 * 1024,
100  STACK_AUDIO_THREAD = 16 * 1024,
101  STACK_TIMER_THREAD = 32 * 1024,
102  STACK_DISPLAY_THREAD = 2 * 1024,
103  STACK_POWER_THREAD = 4 * 1024
104 };
105 
106 #endif /* PSP_THREADS_H */
Definition: thread.h:62
Definition: thread.h:41
Definition: thread.h:29
Definition: mutex.h:40
Definition: thread.h:76
Definition: thread.h:48