BlackThread.h
Go to the documentation of this file.
1  /*
2 
3  ####################################################################################
4  # BlackLib Library controls Beaglebone Black's inputs and outputs. #
5  # Copyright (C) 2013-2015 by Yigit YUCE #
6  ####################################################################################
7  # #
8  # This file is part of BlackLib library. #
9  # #
10  # BlackLib library is free software: you can redistribute it and/or modify #
11  # it under the terms of the GNU Lesser General Public License as published by #
12  # the Free Software Foundation, either version 3 of the License, or #
13  # (at your option) any later version. #
14  # #
15  # BlackLib library is distributed in the hope that it will be useful, #
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of #
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
18  # GNU Lesser General Public License for more details. #
19  # #
20  # You should have received a copy of the GNU Lesser General Public License #
21  # along with this program. If not, see <http://www.gnu.org/licenses/>. #
22  # #
23  # For any comment or suggestion please contact the creator of BlackLib Library #
24  # at ygtyce@gmail.com #
25  # #
26  ####################################################################################
27 
28  */
29 
30 
31 
32 #ifndef BLACKTHREAD_H_
33 #define BLACKTHREAD_H_
34 
35 #include <cstdint>
36 #include <pthread.h>
37 #include <sched.h>
38 #include <vector>
39 #include <unistd.h>
40 
41 
42 
43 
44 
45 
46 namespace BlackLib
47 {
48  #define WAIT_THREAD_FINISH(thrd) (thrd)->waitUntilFinish();
49 
50 
51  // ########################################### BLACKTHREAD DECLARATION STARTS ############################################ //
52 
250  {
251  public:
252 
256  enum state
257  {
258  Stateless = 0,
259  Running = 1,
260  Stopped = 2,
261  Paused = 3
262  };
263 
267  enum priority
268  {
276  #ifdef SCHED_IDLE
277  PriorityIDLE = 5,
278  #endif
279  };
280 
317  BlackThread();
318 
322  virtual ~BlackThread();
323 
365 
366 
407 
449  const pthread_t getThreadId();
450 
470  static void sleep(unsigned int sec);
471 
491  static void msleep(unsigned int msec);
492 
512  static void usleep(unsigned int usec);
513 
565  static void yield();
566 
622  void run();
623 
683  void stop();
684 
745  void waitUntilFinish();
746 
801  bool isRunning();
802 
857  bool isFinished();
858 
928  void pause();
929 
930 
931 
932  private:
933 
934  bool isCreated;
935  pthread_t nativeThread;
938  std::vector<int> priorities;
994  virtual void onStopHandler();
995 
1062  virtual void onPauseHandler();
1063 
1130  virtual void onResumeHandler();
1131 
1186  virtual void onStartHandler() = 0;
1187 
1202  bool setAttribute(pthread_attr_t *attr, BlackThread::priority tp);
1203 
1211  void calculatePriorities();
1212 
1223  static void cleanUp(void *thread);
1224 
1237  static void* threadFunction(void *thread);
1238 
1239  };
1240 
1241  // ############################################ BLACKTHREAD DECLARATION ENDS ############################################# //
1242 
1243 } /* namespace BlackLib */
1244 
1245 #endif /* BLACKTHREAD_H_ */
static void yield()
Yields thread.
Definition: BlackThread.cpp:313
BlackThread::priority setPriority(BlackThread::priority tp)
Changes the priority of thread.
Definition: BlackThread.cpp:101
void pause()
Pauses thread execution.
Definition: BlackThread.cpp:257
Interface class for user specific thread class.
Definition: BlackThread.h:249
virtual void onStartHandler()=0
Thread's start handler function.
BlackThread::priority threadPriority
is used to hold the thread priority
Definition: BlackThread.h:937
bool isFinished()
Exports thread's finish state.
Definition: BlackThread.cpp:326
void run()
Runs thread.
Definition: BlackThread.cpp:217
Definition: BlackThread.h:259
Definition: BlackThread.h:261
virtual void onPauseHandler()
Thread's pause handler function.
Definition: BlackThread.cpp:272
static void * threadFunction(void *thread)
Thread's main function.
Definition: BlackThread.cpp:242
Definition: BlackThread.h:274
void stop()
Stops thread execution.
Definition: BlackThread.cpp:252
void calculatePriorities()
Calculates OS depend priorty values.
Definition: BlackThread.cpp:27
static void msleep(unsigned int msec)
Sleeps the thread for specified milisecond(s).
Definition: BlackThread.cpp:303
void waitUntilFinish()
Waits thread until it finished.
Definition: BlackThread.cpp:276
Definition: BlackThread.h:260
bool setAttribute(pthread_attr_t *attr, BlackThread::priority tp)
Sets thread's priority properties to posix type property holder.
Definition: BlackThread.cpp:47
Definition: BlackThread.h:273
virtual void onStopHandler()
Thread's stop handler function.
Definition: BlackThread.cpp:271
static void cleanUp(void *thread)
Thread's exit function.
Definition: BlackThread.cpp:284
Definition: BlackThread.h:272
BlackThread::priority getPriority()
Exports the priority of thread.
Definition: BlackThread.cpp:148
virtual void onResumeHandler()
Thread's resume handler function.
Definition: BlackThread.cpp:273
std::vector< int > priorities
is used to hold the OS based calculated priority values
Definition: BlackThread.h:938
BlackThread::state threadState
is used to hold the thread state
Definition: BlackThread.h:936
Definition: BlackThread.h:271
const pthread_t getThreadId()
Exports the thread native id.
Definition: BlackThread.cpp:292
virtual ~BlackThread()
Destructor of BlackThread class.
Definition: BlackThread.cpp:23
static void sleep(unsigned int sec)
Sleeps the thread for specified second(s).
Definition: BlackThread.cpp:298
priority
Definition: BlackThread.h:267
Definition: BlackThread.h:270
state
Definition: BlackThread.h:256
static void usleep(unsigned int usec)
Sleeps the thread for specified microsecond(s).
Definition: BlackThread.cpp:308
BlackThread()
Constructor of BlackThread class.
Definition: BlackThread.cpp:13
Definition: BlackADC.cpp:36
bool isRunning()
Exports thread's run state.
Definition: BlackThread.cpp:319
pthread_t nativeThread
is used to hold the thread's posix id
Definition: BlackThread.h:935
Definition: BlackThread.h:258
Definition: BlackThread.h:269
bool isCreated
is used to hold the thread is created or not
Definition: BlackThread.h:934