/[svn]/linuxsampler/trunk/src/engines/EngineChannelFactory.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/EngineChannelFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1826 - (show annotations) (download)
Sat Jan 24 14:32:35 2009 UTC (15 years, 3 months ago) by iliev
File size: 5426 byte(s)
* fixed a crash which occurs when removing a sampler channel with
  instrument loading in progress (bug #113)

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005, 2006 Christian Schoenebeck *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
18 * MA 02111-1307 USA *
19 ***************************************************************************/
20
21 #include "EngineChannelFactory.h"
22
23 #include <strings.h>
24
25 #include "gig/EngineChannel.h"
26
27 namespace LinuxSampler {
28 class LockedChannel {
29 public:
30 const EngineChannel* pChannel;
31 bool bDestroyed;
32
33 LockedChannel(const EngineChannel* pChannel) {
34 this->pChannel = pChannel;
35 bDestroyed = false;
36 }
37 };
38
39 class LockedChannelList {
40 public:
41 void Add(const EngineChannel* pChannel) {
42 vChannelList.push_back(LockedChannel(pChannel));
43 }
44
45 bool IsDestroyed(const EngineChannel* pChannel) {
46 LockedChannel* pLockedChannel = get(pChannel);
47 if (pLockedChannel == NULL) return false;
48 return pLockedChannel->bDestroyed;
49 }
50
51 void SetDestroyed(const EngineChannel* pChannel, bool bDestroyed = true) {
52 LockedChannel* pLockedChannel = get(pChannel);
53 if (pLockedChannel == NULL) return;
54 pLockedChannel->bDestroyed = bDestroyed;
55 }
56
57 void Remove(const EngineChannel* pChannel) {
58 std::vector<LockedChannel>::iterator it = vChannelList.begin();
59 for (; it != vChannelList.end(); it++) {
60 if ((*it).pChannel == pChannel) {
61 vChannelList.erase(it);
62 return;
63 }
64 }
65 }
66
67 bool Contains(const EngineChannel* pChannel) {
68 return get(pChannel) != NULL;
69 }
70
71 private:
72 std::vector<LockedChannel> vChannelList;
73
74 LockedChannel* get(const EngineChannel* pChannel) {
75 for (int i = 0; i < vChannelList.size(); i++) {
76 if (vChannelList[i].pChannel == pChannel) {
77 return &vChannelList[i];
78 }
79 }
80
81 return NULL;
82 }
83 } lockedChannels;
84
85 Mutex EngineChannelFactory::LockedChannelsMutex;
86
87 void EngineChannelFactory::SetDeleteEnabled(const EngineChannel* pEngineChannel, bool enable) {
88 LockedChannelsMutex.Lock();
89 if (!enable) {
90 if (!lockedChannels.Contains(pEngineChannel)) lockedChannels.Add(pEngineChannel);
91 LockedChannelsMutex.Unlock();
92 } else {
93 bool b = lockedChannels.IsDestroyed(pEngineChannel);
94 lockedChannels.Remove(pEngineChannel);
95 LockedChannelsMutex.Unlock();
96
97 if (b) delete pEngineChannel;
98 }
99 }
100
101 // all currently existing engine channel instances
102 static std::set<LinuxSampler::EngineChannel*> engineChannels;
103
104 LinuxSampler::EngineChannel* EngineChannelFactory::Create(String EngineType) throw (Exception) {
105 if (!strcasecmp(EngineType.c_str(),"GigEngine") || !strcasecmp(EngineType.c_str(),"gig")) {
106 LinuxSampler::EngineChannel* pEngineChannel = new gig::EngineChannel;
107 engineChannels.insert(pEngineChannel);
108 return pEngineChannel;
109 }
110 throw Exception("Unknown engine type");
111 }
112
113 void EngineChannelFactory::Destroy(LinuxSampler::EngineChannel* pEngineChannel) {
114 pEngineChannel->RemoveAllFxSendCountListeners();
115 engineChannels.erase(pEngineChannel);
116
117 // Postpone the deletion of the specified EngineChannel if needed (bug #113)
118 LockedChannelsMutex.Lock();
119 if (lockedChannels.Contains(pEngineChannel)) {
120 lockedChannels.SetDestroyed(pEngineChannel);
121 pEngineChannel->SetSamplerChannel(NULL);
122 LockedChannelsMutex.Unlock();
123 return;
124 }
125 LockedChannelsMutex.Unlock();
126 ///////
127
128 delete pEngineChannel;
129 }
130
131 const std::set<LinuxSampler::EngineChannel*>& EngineChannelFactory::EngineChannelInstances() {
132 return engineChannels;
133 }
134
135 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC