Fawkes API  Fawkes Development Version
thread_list.h
1 
2 /***************************************************************************
3  * thread_list.h - Thread list
4  *
5  * Created: Tue Oct 31 18:04:31 2006
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_THREADING_THREAD_LIST_H_
25 #define __CORE_THREADING_THREAD_LIST_H_
26 
27 #include <core/exception.h>
28 #include <core/threading/thread.h>
29 #include <core/threading/thread_initializer.h>
30 #include <core/threading/thread_finalizer.h>
31 #include <core/utils/lock_list.h>
32 
33 #include <utility>
34 #include <string>
35 
36 namespace fawkes {
37 
38 
39 class ThreadList;
40 class Mutex;
41 class Barrier;
42 class InterruptibleBarrier;
43 
45 {
46  public:
47  ThreadListSealedException(const char *operation);
48 };
49 
51 {
52  public:
53  ThreadListNotSealedException(const char *format, ...);
54 };
55 
56 
57 class ThreadList : private LockList<Thread *>
58 {
59  public:
60  ThreadList(const char *tlname = "");
61  ThreadList(bool maintain_barrier, const char *tlname = "");
62  ThreadList(const ThreadList &tl);
63  ~ThreadList();
64 
65  const char * name();
66  void set_name(const char *format, ...);
67 
68  void seal();
69  bool sealed();
70 
71  void init(ThreadInitializer *initializer, ThreadFinalizer *finalizer);
72  bool prepare_finalize(ThreadFinalizer *finalizer);
73  void finalize(ThreadFinalizer *finalizer);
74  void cancel_finalize();
75  void set_prepfin_hold(bool hold);
76 
77  void wakeup();
78  void wakeup(Barrier *barrier);
79  void wakeup_unlocked();
80  void wakeup_unlocked(Barrier *barrier);
81  void wakeup_and_wait(unsigned int timeout_sec = 0,
82  unsigned int timeout_nanosec = 0);
83  void start();
84  void stop();
85  void cancel();
86  void join();
87 
88  void try_recover(std::list<std::string> &recovered_threads);
89  void set_maintain_barrier(bool maintain_barrier);
90 
91  void force_stop(ThreadFinalizer *finalizer);
92 
93  void push_front(Thread *thread);
94  void push_front_locked(Thread *thread);
95  void push_back(Thread *thread);
96  void push_back_locked(Thread *thread);
97  void clear();
98  void pop_back();
99  void pop_front();
100  ThreadList::iterator erase(iterator pos);
101 
102  void remove(Thread *thread);
103  void remove_locked(Thread *thread);
104 
115 
116  ThreadList & operator= (const ThreadList & tl);
117 
118 
119  private:
120  void notify_of_failed_init();
121  void update_barrier();
122 
123  private:
124  char *__name;
125  bool __sealed;
126  Mutex *__finalize_mutex;
127  InterruptibleBarrier *__wnw_barrier;
128 
129  std::list<std::pair<InterruptibleBarrier *, ThreadList> > __wnw_bad_barriers;
130  std::list<std::pair<InterruptibleBarrier *, ThreadList> >::iterator __wnw_bbit;
131 };
132 
133 
134 } // end namespace fawkes
135 
136 #endif
ThreadListSealedException(const char *operation)
Constructor.
Definition: thread_list.cpp:53
Fawkes library namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Thread list sealed exception.
Definition: thread_list.h:44
List of threads.
Definition: thread_list.h:57
Base class for exceptions in Fawkes.
Definition: exception.h:36
Thread initializer interface.
List with a lock.
Definition: thread.h:40
Thread list not sealed exception.
Definition: thread_list.h:50
Exception & operator=(const Exception &exc)
Assign an Exception.
Definition: exception.cpp:519
Mutex mutual exclusion lock.
Definition: mutex.h:32
A barrier is a synchronization tool which blocks until a given number of threads have reached the bar...
Definition: barrier.h:32
Thread finalizer interface.