Wxwidgets: Fix thread memory managment
[lsnes.git] / src / plat-wxwidgets / thread.cpp
blob88d89fa45ab75744e8f314cb7f56b29ef756e982
1 #include "core/window.hpp"
3 #include <wx/thread.h>
5 struct wxw_mutex : public mutex
7 wxw_mutex() throw(std::bad_alloc);
8 ~wxw_mutex() throw();
9 void lock() throw();
10 void unlock() throw();
11 wxMutex* m;
14 wxw_mutex::wxw_mutex() throw(std::bad_alloc)
16 m = new wxMutex();
19 wxw_mutex::~wxw_mutex() throw()
21 delete m;
24 void wxw_mutex::lock() throw()
26 m->Lock();
29 void wxw_mutex::unlock() throw()
31 m->Unlock();
34 mutex& mutex::aquire() throw(std::bad_alloc)
36 return *new wxw_mutex;
39 struct wxw_condition : public condition
41 wxw_condition(mutex& m) throw(std::bad_alloc);
42 ~wxw_condition() throw();
43 bool wait(uint64_t x) throw();
44 void signal() throw();
45 wxCondition* c;
48 wxw_condition::wxw_condition(mutex& m) throw(std::bad_alloc)
49 : condition(m)
51 wxw_mutex* m2 = dynamic_cast<wxw_mutex*>(&m);
52 c = new wxCondition(*m2->m);
55 wxw_condition::~wxw_condition() throw()
57 delete c;
60 bool wxw_condition::wait(uint64_t x) throw()
62 wxCondError e = c->WaitTimeout((x + 999) / 1000);
63 return (e == wxCOND_NO_ERROR);
66 void wxw_condition::signal() throw()
68 c->Broadcast();
71 condition& condition::aquire(mutex& m) throw(std::bad_alloc)
73 return *new wxw_condition(m);
76 struct wxw_thread_id : public thread_id
78 wxw_thread_id() throw();
79 ~wxw_thread_id() throw();
80 bool is_me() throw();
81 uint32_t id;
84 wxw_thread_id::wxw_thread_id() throw()
86 id = wxThread::GetCurrentId();
89 wxw_thread_id::~wxw_thread_id() throw()
93 bool wxw_thread_id::is_me() throw()
95 return (id == wxThread::GetCurrentId());
98 thread_id& thread_id::me() throw(std::bad_alloc)
100 return *new wxw_thread_id;
103 struct wxw_thread;
105 struct wxw_thread_inner : public wxThread
107 wxw_thread_inner(wxw_thread* _up);
108 void* (*entry)(void* arg);
109 void* entry_arg;
110 wxThread::ExitCode Entry();
111 wxw_thread* up;
114 struct wxw_thread : public thread
116 wxw_thread(void* (*fn)(void* arg), void* arg) throw(std::bad_alloc, std::runtime_error);
117 ~wxw_thread() throw();
118 void _join() throw();
119 bool has_waited;
120 wxw_thread_inner* inner;
121 void notify_quit2(void* ret);
124 wxw_thread_inner::wxw_thread_inner(wxw_thread* _up)
125 : wxThread(wxTHREAD_JOINABLE)
127 up = _up;
130 void wxw_thread::notify_quit2(void* ret)
132 notify_quit(ret);
135 wxThread::ExitCode wxw_thread_inner::Entry()
137 up->notify_quit2(entry(entry_arg));
138 return 0;
141 wxw_thread::wxw_thread(void* (*fn)(void* arg), void* arg) throw(std::bad_alloc, std::runtime_error)
143 has_waited = false;
144 inner = new wxw_thread_inner(this);
145 inner->entry = fn;
146 inner->entry_arg = arg;
147 wxThreadError e = inner->Create(8 * 1024 * 1024);
148 if(e != wxTHREAD_NO_ERROR)
149 throw std::bad_alloc();
150 e = inner->Run();
151 if(e != wxTHREAD_NO_ERROR)
152 throw std::bad_alloc();
155 wxw_thread::~wxw_thread() throw()
157 if(inner) {
158 inner->Wait();
159 inner = NULL;
163 void wxw_thread::_join() throw()
165 if(inner) {
166 inner->Wait();
167 inner = NULL;
171 thread& thread::create(void* (*entrypoint)(void* arg), void* arg) throw(std::bad_alloc, std::runtime_error)
173 return *new wxw_thread(entrypoint, arg);