2 Copyright (C) 1998-99 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <glibmm/threads.h>
28 #include "pbd/libpbd_visibility.h"
29 #include "pbd/ringbuffer.h"
31 /** A pool of data items that can be allocated, read from and written to
32 * without system memory allocation or locking.
37 Pool (std::string name, unsigned long item_size, unsigned long nitems);
40 virtual void *alloc ();
41 virtual void release (void *);
43 std::string name() const { return _name; }
44 guint available() const { return free_list.read_space(); }
45 guint used() const { return free_list.bufsize() - available(); }
46 guint total() const { return free_list.bufsize(); }
49 RingBuffer<void*> free_list; ///< a list of pointers to free items within block
53 void *block; ///< data storage area
55 unsigned long max_usage;
59 class LIBPBD_API SingleAllocMultiReleasePool : public Pool
62 SingleAllocMultiReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
63 ~SingleAllocMultiReleasePool ();
65 virtual void *alloc ();
66 virtual void release (void *);
69 Glib::Threads::Mutex m_lock;
73 class LIBPBD_API MultiAllocSingleReleasePool : public Pool
76 MultiAllocSingleReleasePool (std::string name, unsigned long item_size, unsigned long nitems);
77 ~MultiAllocSingleReleasePool ();
79 virtual void *alloc ();
80 virtual void release (void *);
83 Glib::Threads::Mutex m_lock;
86 class LIBPBD_API PerThreadPool;
88 /** Management of a per-thread pool of data that is allocated by one thread and
89 * freed by one other thread. Not safe for use when there is more than 1
90 * reader and 1 writer.
92 * This is basically a wrapper around a thread-local storage instance of a
93 * ringbuffer, made safe for use in the case where multiple threads allocate
94 * from the ringbuffer and a single thread "frees" the allocations.
96 * Rather than using locks, each thread has its own ringbuffer (and associated
97 * data), and so it calls alloc(), passes a pointer to the result of the alloc
98 * to another thread, which later calls push() to "free" it.
100 class LIBPBD_API CrossThreadPool : public Pool
103 CrossThreadPool (std::string n, unsigned long isize, unsigned long nitems, PerThreadPool *);
108 PerThreadPool* parent () const {
113 guint pending_size() const { return pending.read_space(); }
115 void flush_pending ();
116 void flush_pending_with_ev (void*);
119 RingBuffer<void*> pending;
120 PerThreadPool* _parent;
123 /** A class to manage per-thread pools of memory. One object of this class is instantiated,
124 * and then it is used to create per-thread pools for 1 or more threads as required.
126 class LIBPBD_API PerThreadPool
131 const Glib::Threads::Private<CrossThreadPool>& key() const { return _key; }
133 void create_per_thread_pool (std::string name, unsigned long item_size, unsigned long nitems);
134 CrossThreadPool* per_thread_pool (bool must_exist = true);
135 bool has_per_thread_pool ();
136 void set_trash (RingBuffer<CrossThreadPool*>* t);
137 void add_to_trash (CrossThreadPool *);
140 Glib::Threads::Private<CrossThreadPool> _key;
143 /** mutex to protect either changes to the _trash variable, or writes to the RingBuffer */
144 Glib::Threads::Mutex _trash_mutex;
145 RingBuffer<CrossThreadPool*>* _trash;
148 #endif // __qm_pool_h__