ThreadImplPosix.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include "ThreadImplPosix.h"
00024 
00025 #include <assert.h>
00026 #include <pthread.h>
00027 
00028 #include <icl_core/os_posix_thread.h>
00029 
00030 #include "Thread.h"
00031 
00032 namespace icl_core {
00033 namespace thread {
00034 
00035 ThreadImplPosix::ThreadImplPosix(Thread *thread, const icl_core::String& description,
00036                                  icl_core::ThreadPriority priority)
00037   : m_thread_id(0),
00038     m_thread(thread),
00039     m_priority(priority),
00040     m_description(description)
00041 { }
00042 
00043 ThreadImplPosix::~ThreadImplPosix()
00044 { }
00045 
00046 void ThreadImplPosix::cancel()
00047 {
00048   pthread_cancel(m_thread_id);
00049 }
00050 
00051 void ThreadImplPosix::join()
00052 {
00053   pthread_join(m_thread_id, NULL);
00054 }
00055 
00056 icl_core::ThreadPriority ThreadImplPosix::priority() const
00057 {
00058   struct sched_param param;
00059   int policy;
00060   int ret = pthread_getschedparam(m_thread_id, &policy, &param);
00061 
00062   if (ret == 0)
00063   {
00064     return icl_core::ThreadPriority(param.sched_priority);
00065   }
00066   else
00067   {
00068     return 0;
00069   }
00070 }
00071 
00072 bool ThreadImplPosix::setPriority(icl_core::ThreadPriority priority)
00073 {
00074   // First get the current scheduling parameters.
00075   struct sched_param param;
00076   int policy;
00077   int ret = pthread_getschedparam(m_thread_id, &policy, &param);
00078 
00079   if (ret != 0)
00080   {
00081     return false;
00082   }
00083 
00084   // Change the thread priority and set it.
00085   param.sched_priority = priority;
00086   ret = pthread_setschedparam(m_thread_id, policy, &param);
00087 
00088   return (ret == 0);
00089 }
00090 
00091 bool ThreadImplPosix::start()
00092 {
00093   if (pthread_create(&m_thread_id, NULL, ThreadImplPosix::runThread, this))
00094   {
00095     m_thread_id = 0;
00096   }
00097 
00098   return m_thread_id != 0;
00099 }
00100 
00101 icl_core::ThreadId ThreadImplPosix::threadId() const
00102 {
00103   return icl_core::ThreadId(m_thread_id);
00104 }
00105 
00106 void * ThreadImplPosix::runThread(void *arg)
00107 {
00108   ThreadImplPosix *self = static_cast<ThreadImplPosix*>(arg);
00109   self->m_thread->runThread();
00110 
00111   return NULL;
00112 }
00113 
00114 }
00115 }


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24