MDAT  1.0
 All Classes Files Functions Variables Typedefs Pages
thread.h
Go to the documentation of this file.
1 
9 /* Copyright (C) 2013 Eric Larson and Rochelle Palting
10  elarson@seattleu.edu
11 
12  This file is part of the MDAT infrastructure.
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 #ifndef THREAD_H
29 #define THREAD_H
30 
31 #include <string>
32 #include <vector>
33 #include <deque>
34 #include <pthread.h>
35 using namespace std;
36 
62 {
63  public:
64 
79  void init(int numThreads, bool useInteractive, bool useSeed, unsigned int seed);
80 
91  void startThread(int id, string property);
92 
99  bool finishThread();
100 
111  void invokeScheduler(int location = -1);
112 
121  void markWaitingOnLock(int id, string name);
122 
131  void markWaitingOnSem(int id, string name);
132 
146  int wakeupThread(deque<int> &waiting);
147 
155  void updateSection(string section);
156 
162  int getCurrThreadId() const;
163 
169  string getThreadProperty(int id) const;
170 
176  string getThreadTable() const;
177 
178  private:
179 
180  /* Thread state */
181  enum ThreadState {
182  UNKNOWN, // Status is unknown / unitialized
183  READY, // Ready to run but not currently running
184  WAITING_LOCK, // Waiting for a lock
185  WAITING_SEM, // Waiting for a semaphore
186  RUNNING, // Currently running
187  COMPLETED // Thread has commpleted execution
188  };
189 
190  /* Thread data type */
191  struct Thread {
192  pthread_t threadId; // thread id
193  string property; // thread property
194  ThreadState status; // status of a thread
195  int location; // current location in program
196  string section; // current section in program
197  string waitingOn; // name of lock or semaphore thread is waiting on
198  };
199 
200  int threadCount; // number of threads
201  vector<Thread> threadArray; // array of threads
202  int numAddedThreads; // number of added threads
203  bool allThreadsAdded; // set to true when all threads have been added
204  pthread_mutex_t initLock; // lock to prevent race conditions at startup
205  int numThreadsCompleted; // number of finished threads
206  bool interactiveMode; // set if interactive mode is used
207  deque<int> ready; // ready queue
208  int initialThread; // initial thread to start running
209  int currThreadId; // thread that is currently running
210 
211  /* Pauses thread until a signal is received to wake it up. */
212  void pauseThread(int id);
213 
214  /* Blocks the SIGUSR1 signal so it is not received prematurely before
215  * sigwait is called.
216  */
217  void blockSignal();
218 
219  /* Selects a thread from the queue based on the scheduler mode. */
220  int selectThread(deque<int> &queue, string message);
221 
222  /* Returns state of a thread in a string. */
223  string getThreadState(ThreadState state) const;
224 };
225 
229 extern ThreadManager threads;
230 
231 #endif
This is a personal WEB site developed and maintained by an individual and not by Seattle University. The content and link(s) provided on this site do not represent or reflect the view(s) of Seattle University. The individual who authored this site is solely responsible for the site's content. This site and its author are subject to applicable University policies including the Computer Acceptable Use Policy (www.seattleu.edu/policies).