Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / rpool.h
blobb4b3028ff612c8cf5590b4fe90953e86999c8aa6
1 /*
2 WDL - rpool.h
3 Copyright (C) 2006 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 This file defines a template for a class that stores a list of objects, and allows the caller
23 to periodically get an object, do something with it, and add it back into the pool.
25 When the caller does this, it can set ownership of the object, and an expiration for that ownership.
28 The PTYPE1 and PTYPE2entries for the template are there to store additional information (for use with poollist.h)
31 This is pretty esoteric. But we use it for some things.
36 #ifndef _WDL_RPOOL_H_
37 #define _WDL_RPOOL_H_
39 // resource pool (time based)
41 #include "ptrlist.h"
42 #include "mutex.h"
44 class WDL_ResourcePool_ResInfo // include in class RTYPE as WDL_ResourcePool_ResInfo m_rpoolinfo;
46 public:
47 WDL_ResourcePool_ResInfo(){ m_owneduntil=0; m_ownerptr=0; next=0; }
48 ~WDL_ResourcePool_ResInfo() {}
50 unsigned int m_owneduntil;
51 void *m_ownerptr;
53 void *next;
54 } WDL_FIXALIGN;
57 template<class RTYPE, class EXTRAINFOTYPE> class WDL_ResourcePool
59 public:
60 WDL_ResourcePool(char *identstr)
62 WDL_POOLLIST_refcnt=0;
63 WDL_POOLLIST_identstr=identstr;
64 m_rlist=NULL;
65 extraInfo=0;
66 m_hadres=false;
68 ~WDL_ResourcePool()
70 while (m_rlist)
72 RTYPE *tp=m_rlist;
73 m_rlist=(RTYPE *)m_rlist->m_rpoolinfo.next;
74 delete tp;
76 delete extraInfo;
78 void Clear()
80 m_mutex.Enter();
81 while (m_rlist)
83 RTYPE *tp=m_rlist;
84 m_rlist=(RTYPE *)m_rlist->m_rpoolinfo.next;
85 delete tp;
87 m_hadres=false;
88 m_mutex.Leave();
90 bool HasResources()
92 return m_hadres;
95 void AddResource(RTYPE *item, void *own, unsigned int until)
97 item->m_rpoolinfo.m_ownerptr = own;
98 item->m_rpoolinfo.m_owneduntil = until;
100 m_mutex.Enter();
101 item->m_rpoolinfo.next = m_rlist;
102 m_rlist = item;
103 m_hadres=true;
104 m_mutex.Leave();
107 void ReleaseResources(void *own)
109 m_mutex.Enter();
110 RTYPE *ent=m_rlist;
111 while (ent)
113 if (ent->m_rpoolinfo.m_ownerptr == own)
115 ent->m_rpoolinfo.m_ownerptr = 0;
116 ent->m_rpoolinfo.m_owneduntil=0;
118 ent=(RTYPE *)ent->m_rpoolinfo.next;
120 m_mutex.Leave();
123 RTYPE *GetResource(void *own, unsigned int now)
125 m_mutex.Enter();
126 RTYPE *ent=m_rlist, *lastent=NULL, *bestent=NULL, *bestlastent=NULL;
127 bool bestnoown=false;
128 while (ent)
130 if (ent->m_rpoolinfo.m_ownerptr == own)
132 if (lastent) lastent->m_rpoolinfo.next = ent->m_rpoolinfo.next;
133 else m_rlist = (RTYPE *)ent->m_rpoolinfo.next;
134 m_mutex.Leave();
135 return ent;
138 if (!bestnoown && (!ent->m_rpoolinfo.m_ownerptr || ent->m_rpoolinfo.m_owneduntil < now))
140 bestent=ent;
141 bestlastent=lastent;
142 if (!ent->m_rpoolinfo.m_ownerptr || !ent->m_rpoolinfo.m_owneduntil) bestnoown=true;
144 lastent=ent;
145 ent=(RTYPE *)ent->m_rpoolinfo.next;
148 if (bestent)
150 if (bestlastent) bestlastent->m_rpoolinfo.next = bestent->m_rpoolinfo.next;
151 else m_rlist = (RTYPE *)bestent->m_rpoolinfo.next;
154 m_mutex.Leave();
155 return bestent;
158 int WDL_POOLLIST_refcnt;
159 char *WDL_POOLLIST_identstr;
160 bool m_hadres;
162 EXTRAINFOTYPE *extraInfo;
164 RTYPE *PeekList()
166 return m_rlist;
168 void LockList()
170 m_mutex.Enter();
172 void UnlockList()
174 m_mutex.Leave();
177 private:
179 WDL_Mutex m_mutex;
180 RTYPE *m_rlist;
182 } WDL_FIXALIGN;
186 #endif