Lattice Microbes 2.5
This is for whole cell modeling
Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1/*
2 * University of Illinois Open Source License
3 * Copyright 2008-2018 Luthey-Schulten Group,
4 * All rights reserved.
5 *
6 * Developed by: Luthey-Schulten Group
7 * University of Illinois at Urbana-Champaign
8 * http://www.scs.uiuc.edu/~schulten
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the Software), to deal with
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to
15 * do so, subject to the following conditions:
16 *
17 * - Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimers.
19 *
20 * - Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimers in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * - Neither the names of the Luthey-Schulten Group, University of Illinois at
25 * Urbana-Champaign, nor the names of its contributors may be used to endorse or
26 * promote products derived from this Software without specific prior written
27 * permission.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS WITH THE SOFTWARE.
36 *
37 * Author(s): Elijah Roberts
38 */
39
40#ifndef LM_THREAD_THREAD_H_
41#define LM_THREAD_THREAD_H_
42
43#include <cstdlib>
44#include <pthread.h>
45#include "core/Exceptions.h"
46
47namespace lm {
48namespace thread {
49
53{
54public:
55 PthreadException(int error, const char * file, const int line) : Exception("Error in pthread library", error, file, line) {}
56};
57
60class Thread
61{
62public:
64 Thread();
66 virtual ~Thread();
67
69 virtual void start();
71 virtual void stop();
73 virtual void wake()=0;
76 virtual pthread_t getId() {return threadId;}
79 virtual void setAffinity(int cpuNumber);
80
81protected:
82 virtual int run()=0;
83
84private:
85 static void * start_thread(void * obj);
86
87protected:
88 pthread_mutex_t controlMutex; // thread locking mechanism
89 pthread_t threadId; // thread ID
90 volatile bool running; // thread status
91 int cpuNumber; // which cpu thread is running on
92};
93
94}
95}
96
97
100#define PTHREAD_EXCEPTION_CHECK(pthread_call) {int _pthread_ret_=pthread_call; if (_pthread_ret_ != 0) throw lm::thread::PthreadException(_pthread_ret_,__FILE__,__LINE__);}
101#define PTHREAD_EXCEPTION_CHECK_NOTHROW(pthread_call) {int _pthread_ret_=pthread_call; if (_pthread_ret_ != 0) {fprintf(stderr, "%s:%d: Pthread error, returned %d\n", __FILE__, __LINE__, _pthread_ret_); abort();}}
102
103#endif
Exception(const char *message="")
Create an Exception.
Definition Exceptions.h:62
PthreadException(int error, const char *file, const int line)
Definition Thread.h:55
volatile bool running
Definition Thread.h:90
virtual void stop()
Joins the thread with the parent waiting if necessary.
Definition Thread.cpp:123
virtual int run()=0
Thread()
Creates a pthread locking mechanism and initializes "Thread".
Definition Thread.cpp:48
virtual void wake()=0
Wakes a sleeping thead.
pthread_mutex_t controlMutex
Definition Thread.h:88
virtual ~Thread()
Destory the Thread.
Definition Thread.cpp:59
virtual pthread_t getId()
Returns the pthread based id for the Thread.
Definition Thread.h:76
virtual void start()
If no thread exists, creates a new thread and begins execution.
Definition Thread.cpp:87
pthread_t threadId
Definition Thread.h:89
virtual void setAffinity(int cpuNumber)
Binds the thread to a CPU core.
Definition Thread.cpp:64
int cpuNumber
Definition Thread.h:91
Definition Thread.cpp:46
Definition Capsule.cpp:46