1 /*******************************************************************************
2 *                         Goggles Music Manager                                *
3 ********************************************************************************
4 *           Copyright (C) 2006-2021 by Sander Jansen. All Rights Reserved      *
5 *                               ---                                            *
6 * This program is free software: you can redistribute it and/or modify         *
7 * it under the terms of the GNU General Public License as published by         *
8 * the Free Software Foundation, either version 3 of the License, or            *
9 * (at your option) any later version.                                          *
10 *                                                                              *
11 * This program is distributed in the hope that it will be useful,              *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of               *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *
14 * GNU General Public License for more details.                                 *
15 *                                                                              *
16 * You should have received a copy of the GNU General Public License            *
17 * along with this program.  If not, see http://www.gnu.org/licenses.           *
18 ********************************************************************************/
19 #ifndef GMTHREAD_H
20 #define GMTHREAD_H
21 
22 class GMWorker;
23 
24 class GMWorkerThread : public FXThread {
25 protected:
26   GMWorker * worker;
27 public:
28   FXint run();
29 protected:
30   GMWorkerThread(const GMWorkerThread&);
31   GMWorkerThread &operator=(const GMWorkerThread&);
32 public:
33   GMWorkerThread(GMWorker *w);
34   ~GMWorkerThread();
35   };
36 
37 class GMWorker : public FXObject {
38 FXDECLARE(GMWorker)
39 protected:
40   GMWorkerThread   * thread;
41   FXMessageChannel * channel;
42   volatile FXbool    processing;
43 public:
44   enum {
45     ID_THREAD_ENTER = 1,
46     ID_THREAD_LEAVE,
47     ID_LAST
48   };
49 protected:
50   GMWorker();
51   GMWorker(const GMWorker&);
52   GMWorker &operator=(const GMWorker&);
53 public:
54   GMWorker(FXApp * app);
55 
run()56   virtual FXint run() { return 0; }
57 
58   void start();
59 
60   void stop();
61 
62   FXbool send(FXSelector msg,const void* data=nullptr,FXint size=0);
63 
64   virtual ~GMWorker();
65   };
66 
67 
68 
69 
70 class GMTaskManager;
71 
72 class GMTask {
73 friend class GMTaskManager;
74 private:
75   GMTask(const GMTask&);
76   GMTask &operator=(const GMTask&);
77 protected:
78   GMTaskManager    * taskmanager;
79   FXMessageChannel * mc;
80   volatile FXbool    processing;
81 protected:
82   FXObject * target;
83   FXSelector message;
84 public:
85   GMTask(FXObject*tgt=nullptr,FXSelector sel=0);
86 
setTarget(FXObject * tgt)87   void setTarget(FXObject * tgt) { target=tgt; }
88 
setSelector(FXSelector sel)89   void setSelector(FXSelector sel) { message=sel; }
90 
91   virtual FXint run() = 0;
92 
93   virtual ~GMTask();
94   };
95 
96 typedef FXArray<GMTask*> GMTaskList;
97 
98 class GMTaskManager : public FXThread {
99 protected:
100   FXMutex           mutex;
101   FXCondition       condition_task;
102   volatile FXbool   processing;
103   FXbool            started;
104 protected:
105   FXbool wait();
106   FXbool next();
107 protected:
108   GMTask          * active;
109   FXObject*         target;
110   FXSelector        message;
111   FXMessageChannel  mc;
112   GMTaskList        tasks;
113 protected:
114   FXint run();
115 public:
116   GMTaskManager(FXObject*tgt=nullptr,FXSelector sel=0);
117 
118   void run(GMTask*);
119   void shutdown();
120   void cancelTask();
121 
122   void setStatus(const FXString &);
123 
124   virtual ~GMTaskManager();
125   };
126 
127 
128 #endif
129 
130 
131 
132