Changeset 34448
- Timestamp:
- 07/16/09 22:32:46 (4 years ago)
- Location:
- lang/objective-cplusplus/i3/trunk
- Files:
-
- 12 modified
-
am/mil_test.am (modified) (1 diff)
-
src/mil/include/mil/Debug.h (modified) (1 diff)
-
src/mil/include/mil/ModuleCommon.h (modified) (1 diff)
-
src/mil/include/mil/Serial.h (modified) (3 diffs)
-
src/mil/include/mil/Thread.h (modified) (2 diffs)
-
src/mil/include/mil/gui-windows/WindowProcedureRedirector.h (modified) (8 diffs)
-
src/mil/include/mil/os-unix/Debug.h (modified) (1 diff)
-
src/mil/include/mil/os-unix/Thread.h (modified) (1 diff)
-
src/mil/include/mil/os-windows/Debug.h (modified) (1 diff)
-
src/mil/include/mil/os-windows/Thread.h (modified) (1 diff)
-
src/mil/src/Test8.cpp (modified) (4 diffs)
-
src/mil/src/Test9.cpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/objective-cplusplus/i3/trunk/am/mil_test.am
r34440 r34448 7 7 src/mil/src/TestMain.cpp \ 8 8 src/mil/src/StaticData.cpp \ 9 src/mil/src/Test1.cpp \10 9 src/mil/src/Test2.cpp \ 11 10 src/mil/src/Test3.cpp \ -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/Debug.h
r34447 r34448 37 37 if (will_abort) { 38 38 abort(); 39 throw std::runtime_error("halt"); 39 40 } 40 41 } -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/ModuleCommon.h
r34447 r34448 216 216 } 217 217 void flush() { 218 for ( int i = 0; i < _countof(data); i++) {218 for (size_t i = 0; i < _countof(data); i++) { 219 219 #ifdef MIL_GUI_WINDOWS 220 220 data[i].hWndCache = NULL; -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/Serial.h
r34447 r34448 1 1 #pragma once 2 2 #include "Mil.h" 3 #include "Mutex.h" 3 4 4 5 /** … … 12 13 }; 13 14 private: 14 Mutex &mutex;15 Mutex mutex; 15 16 int max; 16 17 int serial; … … 41 42 } 42 43 43 voidrelease(int v) {44 bool release(int v) { 44 45 synchronized (mutex) { 45 46 try { 46 47 list.push_back(v); 47 48 } catch (std::exception&) { 49 return false; 48 50 } 49 51 } 52 return true; 50 53 } 51 54 }; -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/Thread.h
r34447 r34448 203 203 Thread() : thread_id(thread_id_serial.retain()), is_exited(true) { 204 204 if (thread_id == Serial::INVALID_VALUE) { 205 throw runtime_error("Can't retain thread_id");205 halt << "can't retain thread id"; 206 206 } 207 207 threads[thread_id].store(reinterpret_cast<void*>(this)); … … 211 211 this->join(); 212 212 threads[thread_id].store(NULL); 213 try { 214 thread_id_serial.release(thread_id); 215 } catch (std::exception&) { 213 if (!thread_id_serial.release(thread_id)) { 216 214 halt << "can't release thread id"; 217 215 } 218 216 } 219 217 }; 220 221 class SynchronizeHelper : public boost::noncopyable {222 Mutex& mutex;223 bool once;224 public:225 bool evaluation() {226 if (once) {227 return false;228 }229 once = true;230 return true;231 }232 233 SynchronizeHelper(Mutex& mutex) : mutex(mutex), once(false) {234 mutex.lock();235 }236 237 ~SynchronizeHelper() {238 mutex.unlock();239 }240 };241 242 #define synchronized(mutex) \243 for(mil::SynchronizeHelper synchronize_helper_instance__##mutex (mutex); (synchronize_helper_instance__##mutex ).evaluation();)244 218 } 245 219 -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/gui-windows/WindowProcedureRedirector.h
r34447 r34448 114 114 #endif 115 115 116 #define MIL_STATIC_WINDOWPROCEDURE_MAX 5116 #define MIL_STATIC_WINDOWPROCEDURE_MAX 10 117 117 namespace mil { 118 118 template <typename T, LRESULT (T::* WindowProcedure)(HWND, UINT, WPARAM, LPARAM)> … … 120 120 public: 121 121 static Serial serial; 122 WindowProcedureRedirector(T& receiver) : error(false), index(0), hWnd(NULL) { //, dynamic(false), dynamic_redirector(NULL) 123 try { 124 index = serial.get(); 125 } catch (std::exception&) { 122 WindowProcedureRedirector(T& receiver) : error(false), index(serial.retain()), hWnd(NULL) { //, dynamic(false), dynamic_redirector(NULL) 123 if (index == Serial::INVALID_VALUE) { 126 124 DebugBreak(); 127 125 error = true; … … 129 127 } 130 128 targets[index] = &receiver; 131 } 132 133 WNDPROC get() { 129 BOOST_STATIC_ASSERT(_countof(dispatchTable) == MIL_STATIC_WINDOWPROCEDURE_MAX); 130 } 131 132 WNDPROC get() const { 134 133 if (error) { 135 134 DebugBreak(); … … 139 138 } 140 139 141 bool set( HWND hWnd_) {140 bool set(const HWND hWnd_) { 142 141 if (error) { 143 142 DebugBreak(); … … 170 169 BOOST_STATIC_ASSERT(sizeof(LONG_PTR) == sizeof(targets[index])); 171 170 172 try { 173 serial.release(index); 174 } catch (std::exception&) { 171 if (!serial.release(index)) { 175 172 return; 176 173 } … … 178 175 private: 179 176 bool error; 180 unsignedint index;177 const int index; 181 178 HWND hWnd; 182 183 static unsigned int index_seq;184 static LONG_PTR index_stack_mutex;185 static std::stack<unsigned int> index_stack;186 179 static T* targets[MIL_STATIC_WINDOWPROCEDURE_MAX]; 187 180 const static WNDPROC dispatchTable[MIL_STATIC_WINDOWPROCEDURE_MAX]; 188 189 struct Guard {190 LONG_PTR* mutex;191 Guard(LONG_PTR* mutex) : mutex(mutex) {192 while (InterlockedExchangePointer(mutex, 0)) {193 Sleep(10);194 }195 }196 ~Guard() {197 InterlockedExchangePointer(mutex, 1);198 }199 };200 181 201 182 template <int Index> … … 214 195 // dispatchTable 215 196 template <typename T, LRESULT (T::* WindowProcedure)(HWND, UINT, WPARAM, LPARAM)> 216 const WNDPROC WindowProcedureRedirector<T, WindowProcedure>::dispatchTable[ MIL_STATIC_WINDOWPROCEDURE_MAX] = {197 const WNDPROC WindowProcedureRedirector<T, WindowProcedure>::dispatchTable[] = { 217 198 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<0>, 218 199 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<1>, … … 220 201 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<3>, 221 202 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<4>, 203 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<5>, 204 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<6>, 205 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<7>, 206 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<8>, 207 WindowProcedureRedirector<T, WindowProcedure>::template dispatch<9>, 222 208 //#define WPR_TARGET_CLASS_ WindowProcedureRedirector<T, WindowProcedure>::template dispatch 223 209 //#define WPR_MAKE_TEMPLATE_(MAX,INDEX,TEMPLATE) TEMPLATE<INDEX>, -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/os-unix/Debug.h
r34447 r34448 31 31 *((volatile int*)0) = -1; 32 32 cerr << "SEGV suppressed !" << endl; 33 //abort(); 33 abort(); 34 throw std::runtime_error("halt"); 34 35 } 35 36 } -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/os-unix/Thread.h
r34447 r34448 1 #pragma once2 #include "../Atomic.h"3 #include "../Mil.h"4 #include <pthread.h>5 #include <unistd.h>6 #include <sched.h>7 8 /**9 * POSIX10 */11 12 namespace mil {13 14 class Mutex : public boost::noncopyable {15 pthread_mutex_t mutex;16 public:17 Mutex() {18 pthread_mutex_init(&mutex, NULL);19 }20 21 void lock() {22 pthread_mutex_lock(&mutex);23 }24 25 void unlock() {26 pthread_mutex_unlock(&mutex);27 }28 29 ~Mutex() {30 pthread_mutex_destroy(&mutex);31 }32 };33 34 class Conditional {35 pthread_cond_t cond;36 pthread_mutex_t mutex;37 public:38 Conditional() {39 pthread_mutex_init(&mutex, NULL);40 pthread_mutex_lock(&mutex);41 pthread_cond_init(&cond, NULL);42 }43 44 ~Conditional() {45 signal();46 pthread_cond_destroy(&cond);47 pthread_mutex_destroy(&mutex);48 }49 50 void wait() {51 pthread_cond_wait(&cond, &mutex);52 }53 54 void signal() {55 pthread_cond_signal(&cond);56 }57 };58 }59 60 61 62 63 64 -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/os-windows/Debug.h
r34447 r34448 185 185 abort(); 186 186 #endif 187 throw std::runtime_error("halt"); 187 188 } 188 189 } -
lang/objective-cplusplus/i3/trunk/src/mil/include/mil/os-windows/Thread.h
r34447 r34448 1 #pragma once2 3 #include "../Mil.h"4 5 namespace mil {6 class Mutex {7 CRITICAL_SECTION cs;8 public:9 Mutex() {10 InitializeCriticalSection(&cs);11 }12 13 void lock() {14 EnterCriticalSection(&cs);15 }16 17 void unlock() {18 LeaveCriticalSection(&cs);19 }20 21 ~Mutex() {22 DeleteCriticalSection(&cs);23 }24 };25 class Conditional {26 HANDLE event;27 public:28 Conditional() {29 event = CreateEvent(NULL, TRUE, FALSE, NULL);30 }31 32 ~Conditional() {33 signal();34 CloseHandle(event);35 }36 37 void wait() {38 WaitForSingleObject(event, INFINITE);39 }40 41 void signal() {42 SetEvent(event);43 }44 };45 } -
lang/objective-cplusplus/i3/trunk/src/mil/src/Test8.cpp
r34447 r34448 24 24 struct FooThread : Thread<FooThread, DefaultThread> { 25 25 void run() { 26 while (spin.load()); 26 while (spin.load()) { 27 } 27 28 28 29 for (int i = 0; i < 30000; i++) { … … 36 37 struct BarThread : Thread<BarThread, DefaultThread> { 37 38 void run() { 38 while (spin.load()); 39 while (spin.load()) { 40 } 39 41 40 42 for (int i = 0; i < 50000; i++) { … … 48 50 struct FooThread2 : Thread<FooThread2, DefaultThread> { 49 51 void run() { 50 while (spin.load()); 52 while (spin.load()) { 53 } 51 54 52 55 for (int i = 0; i < 23456; i++) { 53 while (!spin2.exchange(false)); 56 while (!spin2.exchange(false)) { 57 } 54 58 global++; 55 59 spin2.store(true); … … 60 64 struct BarThread2 : Thread<BarThread2, DefaultThread> { 61 65 void run() { 62 while (spin.load()); 66 while (spin.load()) { 67 } 63 68 64 69 for (int i = 0; i < 100000; i++) { 65 while (!spin2.exchange(false)); 70 while (!spin2.exchange(false)) { 71 } 66 72 global++; 67 73 spin2.store(true); -
lang/objective-cplusplus/i3/trunk/src/mil/src/Test9.cpp
r34447 r34448 27 27 void createUI() { 28 28 setWindow(create_window()); 29 while (spin.load()); 29 while (spin.load()) { 30 } 30 31 } 31 32 … … 45 46 void run() { 46 47 setWindow(create_window()); 47 while (spin.load()); 48 while (spin.load()) { 49 } 48 50 49 51 for (int i = 0; i < 500; i++) { … … 62 64 void run() { 63 65 setWindow(create_window()); 64 while (spin.load()); 66 while (spin.load()) { 67 } 65 68 66 69 for (int i = 0; i < 500; i++) { … … 78 81 void run() { 79 82 setWindow(create_window()); 80 while (spin.load()); 83 while (spin.load()) { 84 } 81 85 82 86 for (int i = 0; i < 500; i++) {
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)