priority_win.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Platform Check
00010 *****************************************************************************/
00011 
00012 #include <ecl/config/ecl.hpp>
00013 #if defined(ECL_IS_WIN32)
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include <iostream>
00020 #include <ecl/errors/handlers.hpp>
00021 #include "../../include/ecl/threads/priority_win.hpp"
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace ecl {
00028 
00029 /*****************************************************************************
00030 ** Using
00031 *****************************************************************************/
00032 
00033 using std::ostringstream;
00034 
00035 /*****************************************************************************
00036 ** Implementation [Process]
00037 *****************************************************************************/
00038 
00039 bool set_priority(Priority priority_level) ecl_debug_throw_decl(StandardException)
00040 {
00041     if (priority_level >= RealTimePriority1) {
00042         return threads::set_real_time_priority(0, priority_level);
00043     }
00044 
00045     BOOL bResult = FALSE;
00046     HANDLE hThread = GetCurrentThread();
00047 
00048     if (!hThread) {
00049         ecl_debug_throw(threads::throwPriorityException(LOC));
00050         return false;
00051     }
00052 
00053     switch (priority_level) {
00054         case CriticalPriority:
00055                 bResult = SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
00056                 break;
00057         case HighPriority:
00058                 bResult = SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
00059                 break;
00060         case NormalPriority:
00061                 bResult = SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
00062                 break;
00063         case LowPriority:
00064                 bResult = SetThreadPriority(hThread, THREAD_PRIORITY_BELOW_NORMAL);
00065                 break;
00066         case BackgroundPriority:
00067                 bResult = SetThreadPriority(hThread, THREAD_PRIORITY_IDLE);
00068                 break;
00069         default:
00070                 break;
00071     }
00072 
00073     if (!bResult) {
00074         ecl_debug_throw(threads::throwPriorityException(LOC));
00075         return false;
00076     }
00077 
00078     return true;
00079 }
00080 
00081 Priority get_priority() ecl_debug_throw_decl(StandardException)
00082 {
00083     HANDLE hThread = GetCurrentThread();
00084 
00085     if (!hThread) {
00086         ecl_debug_throw(threads::throwPriorityException(LOC));
00087         return UnknownPriority;
00088     }
00089 
00090     DWORD dwPriority = GetThreadPriority(hThread);
00091 
00092     switch (dwPriority) {
00093     case THREAD_PRIORITY_TIME_CRITICAL:
00094         return RealTimePriority1; // representative of real-time priority group
00095     case THREAD_PRIORITY_HIGHEST:
00096         return CriticalPriority;
00097     case THREAD_PRIORITY_ABOVE_NORMAL:
00098         return HighPriority;
00099     case THREAD_PRIORITY_NORMAL:
00100         return NormalPriority;
00101     case THREAD_PRIORITY_BELOW_NORMAL:
00102         return LowPriority;
00103     case THREAD_PRIORITY_IDLE:
00104         return BackgroundPriority;
00105     default:
00106         break;
00107     }
00108 
00109     return UnknownPriority;
00110 }
00111 
00112 /*****************************************************************************
00113 ** Implementation [Debugging]
00114 *****************************************************************************/
00115 
00116 std::string print_priority_diagnostics() ecl_debug_throw_decl(StandardException) {
00117 
00118     ostringstream ostream;
00119 
00120     ostream << "\n";
00121     ostream << "***********************************************************\n";
00122     ostream << "*                 Priority Statistics\n";
00123     ostream << "***********************************************************\n";
00124     ostream << "\n";
00125     switch ( get_priority() ) {
00126         case ( BackgroundPriority ) : {
00127             ostream << "Priority...................................Background\n";
00128             break;
00129         }
00130         case ( LowPriority ) : {
00131             ostream << "Priority...................................Low\n";
00132             break;
00133         }
00134         case ( NormalPriority ) : {
00135             ostream << "Priority...................................Normal\n";
00136             break;
00137         }
00138         case ( HighPriority ) : {
00139             ostream << "Priority...................................High\n";
00140             break;
00141         }
00142         case ( CriticalPriority ) : {
00143             ostream << "Priority...................................Critical\n";
00144             break;
00145         }
00146         case ( RealTimePriority4 ) : {
00147             ostream << "Priority...................................RealTime4\n";
00148             break;
00149         }
00150         case ( RealTimePriority3 ) : {
00151             ostream << "Priority...................................RealTime3\n";
00152             break;
00153         }
00154         case ( RealTimePriority2 ) : {
00155             ostream << "Priority...................................RealTime2\n";
00156             break;
00157         }
00158         case ( RealTimePriority1 ) : {
00159             ostream << "Priority...................................RealTime1\n";
00160             break;
00161         }
00162         case ( DefaultPriority ) : {
00163             ostream << "Priority...................................Default (Inherited)\n";
00164             break;
00165         }
00166         case ( UnknownPriority ) : {
00167             ostream << "Priority...................................Unknown\n";
00168             break;
00169         }
00170     }
00171     return ostream.str();
00172 }
00173 
00174 
00175 /*****************************************************************************
00176 ** Hidden Implementations
00177 *****************************************************************************/
00178 
00179 namespace threads {
00180 
00181 bool set_real_time_priority(int policy,int priority_level) ecl_debug_throw_decl(StandardException) {
00182         // policy is ignored
00183 
00184     HANDLE hThread = GetCurrentThread();
00185 
00186     if (!hThread) {
00187         ecl_debug_throw(threads::throwPriorityException(LOC));
00188         return false;
00189     }
00190 
00191         BOOL bResult = FALSE;
00192 
00193     if (priority_level >= RealTimePriority1) {
00194         bResult = SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
00195     }
00196 
00197     return bResult != FALSE;
00198 }
00199 } // namespace threads
00200 }; // namespace ecl
00201 
00202 #endif /* ECL_IS_WIN32 */


ecl_threads
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:37