SheafSystem  0.0.0.0
thread.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class THREAD
19 
20 #ifndef THREAD_H
21 #define THREAD_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifdef _PTHREADS
28 #include "SheafSystem/pthread.h"
29 
30 #ifndef STD_UNISTD_H
31 #include "SheafSystem/std_unistd.h"
32 #endif
33 
34 #ifndef SINGLY_LINKED_LIST_H
35 #include "SheafSystem/singly_linked_list.h"
36 #endif
37 
38 namespace sheaf
39 {
40 
41 class runnable;
42 
47 class SHEAF_DLL_SPEC thread
48 {
49 
50 private:
51 
52  // data:
53 
54  bool _is_alive;
55  pthread_t pthread;
56  runnable *_runnable;
57  static singly_linked_list<thread *> *_thread_list; // $$ISSUE. destroying a thread or querying for is_alive requires
58  // a linear search through the list. For small numbers of threads
59  // this is probably ok, but for large numbers a hash table would be
60  // better.
61 
62  // actions:
63 
64  static void cleanup(thread *This);
65  static void *static_run(thread *This);
66 
67 public:
68 
69  // queries:
70 
72  static int active_count();
73 
75  singly_linked_list<thread *> *thread_list();
76 
80  bool invariant() const;
81 
85  static bool is_alive(thread *other);
86 
87  // actions:
88 
90  static thread *current_thread();
91 
95  void join();
96 
100  virtual void run();
101 
105  void sleep(unsigned int seconds);
106 
111  void start();
112 
116  void stop();
117 
118  // constructors:
119 
123  thread();
124 
128  thread(runnable *xrunnable);
129 
133  ~thread();
134 };
135 
136 } // namespace sheaf
137 
138 #endif // _PTHREADS
139 
140 #endif // ifndef THREAD_H
Namespace for the sheaves component of the sheaf system.