SST/macro
threading_pthread.h
Go to the documentation of this file.
1 //
2 // This program is free software: you can redistribute it and/or modify
3 // it under the terms of the GNU Lesser General Public License as published by
4 // the Free Software Foundation, either version 3 of the License, or
5 // (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public License
13 // along with this program. If not, see http://www.gnu.org/licenses/.
14 //
15 
16 #ifndef SSTMAC_SOFTWARE_THREADING_THREADING_PTHREAD_H_INCLUDED
17 #define SSTMAC_SOFTWARE_THREADING_THREADING_PTHREAD_H_INCLUDED
18 
20 #include <vector>
21 
22 #ifdef SSTMAC_HAVE_PTHREAD
23 #include <pthread.h>
24 #endif
25 
26 namespace sstmac {
27 namespace sw {
28 
29 #ifdef SSTMAC_HAVE_PTHREAD
30 
31 
32 struct threadcontext_t
33 {
34  pthread_t thread;
35  pthread_cond_t ready;
36  pthread_mutex_t* context_switch_lock;
37  bool started;
38  bool waiting;
39 };
40 
41 class threading_pthread : public threading_interface
42 {
43  private:
44  threadcontext_t context_;
45  int thread_id_;
46  int nthread_;
47 
48  public:
49  threading_pthread(int thread_id, int nthread);
50 
51  /// This part of context initialization is common to
52  /// init_context and start_context.
53  void
54  init_context_common(threadcontext_t &t);
55 
56  /// Initialize the context to be that for the currently running thread.
57  void
58  init_context();
59 
60  /// This tears down the context. It is only called from the scheduler's thread.
61  void
62  destroy_context();
63 
64  class threadargs
65  {
66  public:
67  void
68  (*func)(void*);
69  void *args;
70  threadcontext_t *context;
71  threadargs(void
72  (*f)(void*), void *a, threadcontext_t *c) {
73  func = f;
74  args = a;
75  context = c;
76  }
77  };
78 
79  static std::vector<pthread_mutex_t> context_switch_mutexes;
80 
81  virtual
82  threading_interface* copy() {
83  return new threading_pthread(thread_id_, nthread_);
84  }
85 
86  /// Start a new context. It does not start yet -- swap_context will start it. This is only called from the scheduler's thread.
87  void
88  start_context(int physical_thread_id, void *stack, size_t stacksize, void
89  (*func)(void*), void *args, threading_interface *yield_to);
90 
91  /// This is called when we have completed running the thread. It is
92  /// called in the from context.
93  void
94  complete_context(threading_interface *to);
95 
96  /// Swap context. The from context is always the currently running context.
97  void
98  swap_context(threading_interface *to);
99 };
100 
101 #endif
102 }
103 } // end of namespace sstmac
104 #endif
105 
#define pthread_t
int thread_id
Definition: thread_id.h:21
#define pthread_mutex_t
#define pthread_cond_t
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...