18 #ifdef MRPT_OS_WINDOWS 23 #include <sys/utime.h> 30 #include <sys/select.h> 41 #include <sys/types.h> 44 #include <sys/sysctl.h> 45 #include <mach/mach_init.h> 46 #include <mach/thread_act.h> 59 #ifdef MRPT_OS_WINDOWS 65 int timeLeft_ms = time_ms - (int)(tictac.
Tac()*1000);
66 while ( timeLeft_ms>0 )
68 usleep( timeLeft_ms * 1000 );
69 timeLeft_ms = time_ms - (int)(tictac.
Tac()*1000);
83 #ifdef MRPT_OS_WINDOWS 93 #ifdef MRPT_OS_WINDOWS 95 unsigned long myWindowsId;
108 #ifdef MRPT_OS_WINDOWS 110 d->myWindowsId = (
unsigned long)GetCurrentThreadId();
111 d->win_sem.release();
122 catch(std::exception &e)
124 std::cout <<
"Exception in [auxiliary_thread_launcher_LIN/WIN]!!!:\n" << e.what();
128 std::cout <<
"Untyped exception in [auxiliary_thread_launcher_LIN/WIN]!!!\n";
142 void ( *func )(
void * ),
149 auxData->ptrFunc = func;
150 auxData->param =
param;
152 #ifdef MRPT_OS_WINDOWS 156 if (h== ((HANDLE) -1))
162 threadHandle.hThread = h;
165 auxData->win_sem.waitForSignal();
166 threadHandle.idThread = auxData->myWindowsId;
168 delete auxData; auxData = NULL;
175 pthread_t newThreadId;
179 threadHandle.idThread = (
unsigned long)newThreadId;
192 if (threadHandle.
isClear())
return;
193 #ifdef MRPT_OS_WINDOWS 194 int prio = GetThreadPriority((HANDLE) threadHandle.hThread);
195 if (THREAD_PRIORITY_ERROR_RETURN==prio)
198 DWORD ret = WaitForSingleObject( (HANDLE) threadHandle.hThread , INFINITE );
199 if (ret!=WAIT_OBJECT_0)
200 cerr <<
"[mrpt::system::joinThread] Error waiting for thread completion!" << endl;
201 #elif defined(MRPT_OS_APPLE) 202 pthread_join(reinterpret_cast<pthread_t>(threadHandle.idThread), NULL);
204 pthread_join(threadHandle.idThread, NULL);
213 #ifdef MRPT_OS_WINDOWS 214 return GetCurrentThreadId();
221 #elif defined(MRPT_OS_APPLE) 222 return reinterpret_cast<unsigned long>(pthread_self());
224 return pthread_self();
234 #ifdef MRPT_OS_WINDOWS 236 h.hThread = GetCurrentThread();
237 h.idThread = GetCurrentThreadId();
238 #elif defined(MRPT_OS_APPLE) 239 h.idThread =
reinterpret_cast<long unsigned int>(pthread_self());
242 h.idThread = pthread_self();
254 #ifdef MRPT_OS_WINDOWS 256 SetThreadPriority( threadHandle.hThread, priority);
259 const pthread_t tid =
261 reinterpret_cast<pthread_t
>(threadHandle.idThread);
263 threadHandle.idThread;
267 struct sched_param
param;
269 if (0!=(ret=pthread_getschedparam(tid,&policy,&
param))) {
270 cerr <<
"[mrpt::system::changeThreadPriority] Warning: Failed call to pthread_getschedparam (error: `" << strerror(ret) <<
"`)" << endl;
275 int min_prio = sched_get_priority_min(policy), max_prio = sched_get_priority_max(policy);
276 if (min_prio<0) min_prio=1;
277 if (max_prio<0) max_prio=99;
283 case tpLower : prio=(max_prio+3*min_prio)/4;
break;
284 case tpLow : prio=(max_prio+2*min_prio)/3;
break;
285 case tpNormal: prio=(max_prio+min_prio )/2;
break;
286 case tpHigh : prio=(2*max_prio+min_prio)/3;
break;
287 case tpHigher: prio=(3*max_prio+min_prio)/4;
break;
291 param.sched_priority = prio;
292 if (0!=(ret=pthread_setschedparam(tid, policy, &
param))) {
293 cerr <<
"[mrpt::system::changeThreadPriority] Warning: Failed call to pthread_setschedparam (error: `" << strerror(ret) <<
"`)" << endl;
305 #ifdef MRPT_OS_WINDOWS 309 case ppIdle: dwPri = IDLE_PRIORITY_CLASS;
break;
310 case ppNormal: dwPri = NORMAL_PRIORITY_CLASS;
break;
311 case ppHigh: dwPri = HIGH_PRIORITY_CLASS;
break;
312 case ppVeryHigh: dwPri= REALTIME_PRIORITY_CLASS;
break;
316 SetPriorityClass( GetCurrentProcess(), dwPri );
321 case ppIdle: nice_val =+19;
break;
323 case ppHigh: nice_val =-10;
break;
329 const int ret = nice(nice_val);
330 if (ret==-1 && errno==EPERM) {
331 std::cerr <<
"[mrpt::system::changeCurrentProcessPriority] Error calling nice(): Not enough permissions.\n";
340 time_t &creationTime,
346 #ifdef MRPT_OS_WINDOWS 347 FILETIME timCreat,timExit, timKernel, timUser;
352 #if !defined(HAVE_OPENTHREAD) // defined(_MSC_VER) && (_MSC_VER<1300) 357 threadHandle = OpenThread( READ_CONTROL | THREAD_QUERY_INFORMATION,
FALSE, GetCurrentThreadId() );
358 if (!threadHandle)
THROW_EXCEPTION(
"Cannot open the thread with the given 'threadId'");
361 if (!GetThreadTimes( threadHandle , &timCreat, &timExit, &timKernel, &timUser ))
363 CloseHandle(threadHandle);
367 #if defined(HAVE_OPENTHREAD) // _MSC_VER) && (_MSC_VER>=1300) 369 CloseHandle(threadHandle);
374 t = (((
uint64_t)timCreat.dwHighDateTime) << 32) | timCreat.dwLowDateTime;
375 creationTime = (
t - 116444736000000000ULL)/10000000;
377 t = (((
uint64_t)timExit.dwHighDateTime) << 32) | timExit.dwLowDateTime;
378 exitTime = (
t - 116444736000000000ULL)/10000000;
381 int64_t t1 = (((
uint64_t)timKernel.dwHighDateTime) << 32) | timKernel.dwLowDateTime;
382 int64_t t2 = (((
uint64_t)timUser.dwHighDateTime) << 32) | timUser.dwLowDateTime;
384 cpuTime = ((double)(t1+t2)) * 100e-9;
398 #if MRPT_WORD_SIZE==64 399 pid_t
id = (
long int)syscall(186);
400 #elif MRPT_WORD_SIZE==32 401 pid_t
id = (
long int)syscall(224);
403 #error MRPT_WORD_SIZE must be 32 or 64. 408 long unsigned tms_utime=0, tms_stime=0;
409 ifstream is(
format(
"/proc/self/task/%i/stat",
id).c_str() );
416 size_t idx =
s.find(
")");
418 if (idx!=string::npos)
423 if (tokens.size()>=13)
425 sscanf(tokens[11].c_str(),
"%lu" ,&tms_utime);
426 sscanf(tokens[12].c_str(),
"%lu", &tms_stime);
432 double clockTicksPerSecond = (double)sysconf(_SC_CLK_TCK);
433 if (clockTicksPerSecond>0)
434 cpuTime = (tms_utime + tms_stime) / clockTicksPerSecond;
438 thread_basic_info
info;
439 mach_msg_type_number_t
count = THREAD_BASIC_INFO_COUNT;
440 if(thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&
info, &
count)==0)
442 double utime =
info.user_time.seconds +
info.user_time.microseconds * 1e-6;
443 double stime =
info.system_time.seconds +
info.system_time.microseconds * 1e-6;
444 cpuTime = utime + stime;
456 #ifdef MRPT_OS_WINDOWS 458 PROCESS_INFORMATION PI;
460 memset(&SI,0,
sizeof(STARTUPINFOA) );
461 SI.cb =
sizeof(STARTUPINFOA);
463 if (CreateProcessA( NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI, &PI) )
466 WaitForSingleObject( PI.hProcess, INFINITE );
472 DWORD e = GetLastError();
474 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,0,e,0,str,
sizeof(str), NULL);
477 std::cerr <<
"[launchProcess] Couldn't spawn process. Error msg: " << str << std::endl;
481 return 0== ::system(command.c_str());
488 static unsigned int ret = 0;
492 #ifdef MRPT_OS_WINDOWS 495 ret=si.dwNumberOfProcessors;
497 #elif defined(MRPT_OS_APPLE) 498 size_t len=
sizeof(int);
499 if(sysctlbyname(
"hw.logicalcpu", &ret, &
len, NULL, 0) != 0)
504 f.open(
"/proc/cpuinfo");
509 unsigned int nProc = 0;
510 while (!f.fail() && !f.eof())
513 if (!f.fail() && !f.eof())
514 if (lin.find(
"processor")!=std::string::npos)
517 ret = nProc ? nProc : 1;
528 #ifdef MRPT_OS_WINDOWS 540 if (threadHandle.isClear())
return;
542 #ifdef MRPT_OS_WINDOWS 543 TerminateThread(threadHandle.hThread, DWORD(-1));
544 #elif defined(MRPT_OS_APPLE) 545 pthread_cancel(reinterpret_cast<pthread_t>(threadHandle.idThread));
547 pthread_cancel(threadHandle.idThread);
549 threadHandle.clear();
GLuint GLuint GLsizei count
TThreadPriority
The type for cross-platform thread priorities.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS
Returns the ID of the current thread.
void BASE_IMPEXP changeCurrentProcessPriority(TProcessPriority priority)
Change the priority of the given process (it applies to all the threads, plus independent modifiers f...
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
void BASE_IMPEXP changeThreadPriority(const TThreadHandle &threadHandle, TThreadPriority priority)
Change the priority of the given thread - for Windows, see also changeCurrentProcessPriority() ...
#define THROW_EXCEPTION(msg)
TThreadHandle BASE_IMPEXP getCurrentThreadHandle() MRPT_NO_THROWS
Returns a handle to the current thread.
unsigned int BASE_IMPEXP getNumberOfProcessors()
Return the number of processors ("cores"), or 1 if it cannot be determined.
#define MRPT_NO_THROWS
C++11 noexcept: Used after member declarations.
TProcessPriority
The type for cross-platform process (application) priorities.
void Tic()
Starts the stopwatch.
bool BASE_IMPEXP launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
void BASE_IMPEXP getCurrentThreadTimes(time_t &creationTime, time_t &exitTime, double &cpuTime)
Returns the creation and exit times of the current thread and its CPU time consumed.
std::vector< std::string > vector_string
A type for passing a vector of strings.
void * auxiliary_thread_launcher_LIN(void *param)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
This class implements a high-performance stopwatch.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
bool isClear() const
Returns true if the handle is uninitialized.
GLsizei const GLchar ** string
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
This structure contains the information needed to interface the threads API on each platform: ...
double Tac()
Stops the stopwatch.
void BASE_IMPEXP terminateThread(TThreadHandle &threadHandle) MRPT_NO_THROWS
Terminate a thread, giving it no choice to delete objects, etc (use only as a last resource) ...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
TThreadHandle BASE_IMPEXP createThreadImpl(void(*func)(void *), void *param)
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
A semaphore for inter-thread synchronization.
void BASE_IMPEXP exitThread() MRPT_NO_THROWS
Explicit close of the current (running) thread.
void auxiliary_thread_launcher_WIN(void *param)
void BASE_IMPEXP joinThread(const TThreadHandle &threadHandle)
Waits until the given thread ends.