Develop and Download Open Source Software

Browse Subversion Repository

Contents of /common/ThreadCreator.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 279 - (show annotations) (download) (as text)
Wed Mar 12 05:28:19 2008 UTC (16 years, 2 months ago) by satofumi
File MIME type: text/x-c++src
File size: 2832 byte(s)
adjust tab index
1 /*!
2 \file
3 \brief SDL_Thread によるスレッドハンドラ
4
5 \author Satofumi KAMIMURA
6
7 $Id$
8 */
9
10 #include "ThreadCreator.h"
11 #include "SdlInit.h"
12 #include "LockGuard.h"
13 #include <SDL_thread.h>
14
15 using namespace beego;
16
17
18 /*!
19 \brief ThreadCreator の内部クラス
20 */
21 struct ThreadCreator::pImpl : private SdlInit {
22 static int function_handler(void* args);
23
24 /*!
25 \brief ThreadCreator の内部クラス
26 */
27 class ThreadInfo {
28 public:
29 SDL_mutex* mutex;
30 int (*function)(void *);
31 void* args;
32 bool pause;
33 bool quit;
34 size_t times;
35 size_t next_times;
36
37 ThreadInfo(int (*fn)(void *), void* thread_args)
38 : mutex(SDL_CreateMutex()), function(fn), args(thread_args),
39 pause(true), quit(false), times(1), next_times(0) {
40 }
41
42 ~ThreadInfo(void) {
43 SDL_DestroyMutex(mutex);
44 }
45 };
46
47 SDL_Thread* thread;
48 ThreadInfo thread_info;
49
50 pImpl(int (*fn)(void *), void* args)
51 : thread(NULL), thread_info(fn, args) {
52 }
53
54 ~pImpl(void) {
55 thread_info.quit = true;
56 if (thread) {
57 SDL_KillThread(thread);
58 }
59 }
60 };
61
62
63 /*!
64 \todo pause 変数の使い方を見直すべき
65 */
66 int ThreadCreator::pImpl::function_handler(void* args) {
67 pImpl::ThreadInfo& info = *(pImpl::ThreadInfo *)args;
68
69 int ret = 0;
70 bool pause = false;
71 bool quit = false;
72 while (! quit) {
73 if (pause) {
74 SDL_Delay(1);
75 } else {
76 ret = info.function(info.args);
77 }
78
79 LockGuard guard(info.mutex);
80 if (pause == false) {
81 ++info.times;
82 }
83
84 pause = info.pause;
85 quit = info.quit;
86 if (info.times == info.next_times) {
87 // !!! pause 変数の使い方を見直すべき
88 info.pause = true;
89 pause = true;
90 }
91 }
92 return ret;
93 }
94
95
96 ThreadCreator::ThreadCreator(int (*fn)(void *), void* args)
97 : pimpl(new pImpl(fn, args)) {
98 }
99
100
101 ThreadCreator::~ThreadCreator(void) {
102 }
103
104
105 void ThreadCreator::run(void) {
106 if (pimpl->thread == NULL) {
107 // スレッドの起動
108 pimpl->thread_info.pause = false;
109 pimpl->thread = SDL_CreateThread(pImpl::function_handler,
110 &pimpl->thread_info);
111 return;
112 }
113
114 // スレッドの再開
115 LockGuard guard(pimpl->thread_info.mutex);
116 pimpl->thread_info.pause = false;
117 }
118
119
120 void ThreadCreator::run(size_t times) {
121 LockMutex(pimpl->thread_info.mutex);
122 size_t now_times = pimpl->thread_info.times;
123 pimpl->thread_info.next_times = now_times + times;
124 UnlockMutex(pimpl->thread_info.mutex);
125
126 run();
127 }
128
129
130 void ThreadCreator::stop(void) {
131 // スレッドの停止
132 LockGuard guard(pimpl->thread_info.mutex);
133 pimpl->thread_info.pause = true;
134 }
135
136
137 void ThreadCreator::wait(int* retval) {
138 pimpl->thread_info.quit = true;
139 SDL_WaitThread(pimpl->thread, retval);
140 pimpl->thread = NULL;
141 }
142
143
144 bool ThreadCreator::isRunning(void) {
145 return ((pimpl->thread == NULL) || pimpl->thread_info.pause) ? false : true;
146 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26