1 #ifndef THREAD_CABINET_CLASS
2 #define THREAD_CABINET_CLASS
4 /*****************************************************************************\
6 * Name : thread_cabinet *
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2000-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
21 #include <basis/mutex.h>
22 #include <structures/roller.h>
23 #include <structures/set.h>
24 #include <structures/unique_id.h>
30 //! Manages a collection of threads.
32 The thread cabinet allows one to corral a bunch of threads in one place
33 and treat them as a group if necessary.
40 virtual ~thread_cabinet();
42 DEFINE_CLASS_NAME("thread_cabinet");
44 structures::unique_int add_thread(ethread *to_add, bool start_it, void *parm);
45 //!< adds a thread to be managed by the thread_cabinet.
46 /*!< the thread cabinet takes over responsibility for the thread "to_add".
47 if "start_it" is true, then the thread is started. otherwise it is
48 left in whatever state it was in. the "parm" is passed to the thread's
51 bool zap_thread(const structures::unique_int &to_whack);
52 //!< removes the thread with the id "to_whack".
53 /*!< if it's found and stopped and removed, true is returned. note that
54 if the thread is found, then this will wait until the thread exits before
57 bool cancel_thread(const structures::unique_int &to_cancel);
58 //!< shuts down the thread "to_cancel" as quickly as possible.
59 /*!< this calls the cancel() method on the thread "to_cancel", which tells
60 the thread to stop as soon as possible. once it has stopped, the
61 clean_debris() method will throw it and other stopped threads out. */
63 int threads() const; //!< number of threads being managed here.
65 structures::int_set thread_ids() const;
66 //!< returns the identifiers of all threads managed by this object.
68 bool any_running() const;
69 //!< returns true if any threads are currently running.
71 void start_all(void *pointer);
72 //!< cranks up any threads that are not already running.
73 /*!< the "pointer" will be provided to any threads that are started. */
76 //!< signals to all threads that they should exit as soon as possible.
77 /*!< this does not actually wait for them to exit. */
80 //!< makes all of the threads quit.
81 /*!< they are cleaned up after they have stopped running also. any
82 attempts to add threads while this method is operating will be rejected. */
84 ethread *get_thread(int index);
85 //!< this returns the thread at "index" in our list.
86 /*!< note that this is not safe to use if other threads could be removing
87 threads from the cabinet or calling clean_debris(). */
90 //!< clean out threads that have finished.
91 /*!< note that if threads were added to the list without starting them,
92 then these get cleaned out also. */
95 basis::mutex *_lock; //!< synchronizes our thread list.
96 thread_amorph *_threads; //!< the list of threads we're managing.
97 structures::int_roller *_next_id; //!< the id to be assigned to the next thread.
98 int _no_additions; //!< true if we're not allowed to add threads currently.