Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
servicethread.hpp
1 //////////////////////////////
2 //
3 // File: lxs/system/servicethread.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Nov 27 2012
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __HLP_SERVICETHREAD_
15 #define __HLP_SERVICETHREAD_
16 #include "helper/tr1.h"
17 #include <deque>
18 #include <memory>
19 //#include <mutex>
20 #include <utility>
21 #include <pthread.h>
22 namespace LxS
23 {
24  namespace System
25  {
26  //!Generalized function pointer to be used in service threads (look at LxS::System::ServiceThread)
27  typedef std::tr1::function<void ()> ServiceFunction;
28  //!@brief mutex class
29  //! encapsulates pthread_mutex_t for increased portaility and for purpose of making stuff object oriented
30  class MyMutex
31  {
32  pthread_mutex_t mtx;
33  public:
34  MyMutex();
35  ~MyMutex();
36  //! lock mutex
37  void lock();
38  //! unlock a mutex
39  void unlock();
40  };
41  //!@brief class representing background thread
42  //!
43  //! threading model in LxS is formulated as set of background ("service")
44  //! threads (as opposed to main (OpenGL processing)) thread. Each thread
45  //! has queue of functions, it was ordered to execute, and it than
46  //! gradually executes them
47  //!
48  //! Any thread Background thread
49  //! | |
50  //! | | <-----------.
51  //! | | |
52  //! t->EnqueueCall()---->func_queue<----MainThreadLoop |
53  //! | ^ ^ | /
54  //! | push_back pop_front | /
55  //! \|/ execute \|/-----------'
56  //! | |
57  //!
58  //! This is designed so that any time-consuming function (which therefore
59  //! cannot be executed in main thread) can be executed in background without
60  //! creating too many threads, and with added benefit of simplified and
61  //! efficient thread management.
63  {
64  static void * ThreadLauncher(void * v);
65  MyMutex th_mutex;
66  typedef
67  std::deque< std::pair<ServiceFunction, int> >
68  fund_queue_type;
69  fund_queue_type func_queue;
70  int terminate;
71  int current_priority;
72  std::auto_ptr<pthread_t> p_thread;
73  int MainThreadLoop();
74  bool suicidal; // if true, thread will delete itself upon termination
75  public:
76  //! priority of currently executed call;
77  //! if thread is idle, 0 is returned, otherwise nonnegative number
78  int GetCurrentPriority() {return current_priority;}
79  //! default constructor
80  ServiceThread();
81  //! delete main loop object, kill thread execution;
82  int TerminateThread();
83  //! invalidate handle and terminate after execution of last call. Self-cleanup.
84 
85  int DetatchThread(); // Terminate and set suicidal
86  //! check if no calls are enqueued
87  bool QueueEmpty();
88  //! if no calls of 0 priority are enqueued. 0 is the highest possible priority
89  bool No0PriorityCalls();
90  //! more than one function ("currently executed") is enqueued
91  bool QueueMore1();
92  // erase all enqueued calls except the one that is currently being executed
93  void ClearQueue();
94  //! Enquene call for execution.. 0, default priority is the highest
95  int EnqueueCall(ServiceFunction call,int priority=0);
96  //! default destructor
98  };
99  //! commonly used thread for loading stuff - only one as these tasks are IO intensive
100  extern ServiceThread LoaderThread;
101 
102  //! thread for loading and processing audio data - can't be interrupted
103  //! by other preloading
104  extern ServiceThread AudioLoaderThread;
105  }
106 }
107 #endif