You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
8.6KB

  1. /* SpiralSound
  2. * Copyleft (C) 2002 David Griffiths <dave@pawfal.org>
  3. *
  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.
  8. *
  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.
  13. *
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "ChannelHandler.h"
  19. #include <unistd.h>
  20. using namespace std;
  21. //#define CHANNEL_DEBUG
  22. ChannelHandler::ChannelHandler() :
  23. m_UpdateIndicator(false)
  24. {
  25. m_Mutex = new pthread_mutex_t;
  26. m_Command[0]=0;
  27. m_Command[1]=0;
  28. m_BulkSrc=NULL;
  29. m_BulkSize=0;
  30. m_BulkPos=-1;
  31. pthread_mutex_init(m_Mutex,NULL);
  32. }
  33. ChannelHandler::~ChannelHandler()
  34. {
  35. for(map<string,Channel*>::iterator i=m_ChannelMap.begin();
  36. i!=m_ChannelMap.end(); i++)
  37. {
  38. free(i->second->data_buf);
  39. delete i->second;
  40. }
  41. pthread_mutex_destroy(m_Mutex);
  42. delete m_Mutex;
  43. }
  44. ///////////////////////////////////////////////////////////////
  45. void ChannelHandler::UpdateDataNow()
  46. {
  47. #ifdef CHANNEL_DEBUG
  48. cerr<<"Started update"<<endl;
  49. #endif
  50. // make sure the command is cleared even if
  51. // we can't get a lock on the data
  52. m_Command[0]=0;
  53. if (pthread_mutex_trylock(m_Mutex))
  54. {
  55. #ifdef CHANNEL_DEBUG
  56. cerr<<"Got lock"<<endl;
  57. #endif
  58. m_UpdateIndicator=!m_UpdateIndicator;
  59. for(map<string, Channel*>::iterator i=m_ChannelMap.begin();
  60. i!=m_ChannelMap.end(); i++)
  61. {
  62. Channel *ch = i->second;
  63. switch (ch->type)
  64. {
  65. case INPUT :
  66. {
  67. #ifdef CHANNEL_DEBUG
  68. cerr<<"memcpy input channel: ["<<i->first<<"]"<<endl;
  69. #endif
  70. memcpy(ch->data,ch->data_buf,ch->size);
  71. } break;
  72. case OUTPUT :
  73. {
  74. #ifdef CHANNEL_DEBUG
  75. cerr<<"memcpy output channel: ["<<i->first<<"]"<<endl;
  76. #endif
  77. memcpy(ch->data_buf,ch->data,ch->size);
  78. } break;
  79. // one off request type
  80. case OUTPUT_REQUEST :
  81. {
  82. if (m_BulkID==i->first && ch->requested)
  83. {
  84. if (m_BulkPos!=-1)
  85. {
  86. // doing a bulk transfer
  87. if (m_BulkPos+ch->size>m_BulkSize)
  88. {
  89. // last transfer
  90. #ifdef CHANNEL_DEBUG
  91. cerr<<"memcpy (last) bulk output channel: ["<<i->first<<"]"<<endl;
  92. cerr<<"pos:"<<m_BulkPos<<" size:"<<m_BulkSize<<" chsize:"<<ch->size<<endl;
  93. #endif
  94. memcpy(ch->data_buf,((char*)m_BulkSrc)+m_BulkPos,m_BulkSize-m_BulkPos);
  95. m_BulkPos=-1;
  96. }
  97. else
  98. {
  99. #ifdef CHANNEL_DEBUG
  100. cerr<<"memcpy bulk output channel: ["<<i->first<<"]"<<endl;
  101. cerr<<"pos:"<<m_BulkPos<<" size:"<<m_BulkSize<<endl;
  102. #endif
  103. memcpy(ch->data_buf,((char*)m_BulkSrc)+m_BulkPos,ch->size);
  104. m_BulkPos+=ch->size;
  105. }
  106. ch->updated=true;
  107. ch->requested=false;
  108. }
  109. }
  110. else
  111. {
  112. // normal request transfer
  113. if (ch->requested)
  114. {
  115. #ifdef CHANNEL_DEBUG
  116. cerr<<"memcpy output channel: ["<<i->first<<"]"<<endl;
  117. #endif
  118. memcpy(ch->data_buf,ch->data,ch->size);
  119. ch->updated=true;
  120. ch->requested=false;
  121. }
  122. }
  123. } break;
  124. }
  125. }
  126. m_Command[0]=m_Command[1];
  127. // make sure the command only lasts one update
  128. m_Command[1]=0;
  129. pthread_mutex_unlock(m_Mutex);
  130. //cerr<<"audio out mutex"<<endl;
  131. //cerr<<"Update succeeded"<<endl;
  132. }
  133. else
  134. {
  135. //cerr<<"Couldn't get lock"<<endl;
  136. }
  137. #ifdef CHANNEL_DEBUG
  138. cerr<<"Ended update"<<endl;
  139. #endif
  140. }
  141. void ChannelHandler::RegisterData(const string &ID, Type t,void* pData, int size)
  142. {
  143. // probably don't need to lock here, as if get/set are called before
  144. // the channels have been set up they won't work anyway, but...
  145. //pthread_mutex_lock(m_Mutex);
  146. #ifdef CHANNEL_DEBUG
  147. cerr<<"Registering ["<<ID<<"] "<<hex<<pData<<dec<<" as "<<size<<" bytes big"<<endl;
  148. #endif
  149. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  150. if (i!=m_ChannelMap.end())
  151. {
  152. cerr<<"Channel with ID ["<<ID<<"] already exists"<<endl;
  153. }
  154. Channel *NewCh=new Channel(t);
  155. NewCh->data_buf = malloc(size);
  156. NewCh->size = size;
  157. NewCh->data = pData;
  158. NewCh->requested = false;
  159. NewCh->updated = false;
  160. memcpy(NewCh->data_buf,NewCh->data,size);
  161. m_ChannelMap[ID]=NewCh;
  162. //pthread_mutex_unlock(m_Mutex);
  163. }
  164. /////////////////////////////////////////////////////////////////////////
  165. void ChannelHandler::GetData(const string &ID, void *data)
  166. {
  167. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  168. if (i==m_ChannelMap.end())
  169. {
  170. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  171. return;
  172. }
  173. if (!data)
  174. {
  175. cerr<<"ChannelHandler: Can't copy data to uninitialised mem"<<endl;
  176. return;
  177. }
  178. pthread_mutex_lock(m_Mutex);
  179. if (i->second->type==OUTPUT || i->second->type==OUTPUT_REQUEST)
  180. {
  181. memcpy(data,i->second->data_buf,i->second->size);
  182. }
  183. else
  184. {
  185. cerr<<"ChannelHandler: Tried to Get() data registered as input"<<endl;
  186. }//cerr<<"unlock 1"<<endl;
  187. pthread_mutex_unlock(m_Mutex);
  188. }
  189. void ChannelHandler::SetData(const string &ID, void *s)
  190. {
  191. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  192. if (i==m_ChannelMap.end())
  193. {
  194. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  195. return;
  196. }
  197. //cerr<<"lock 2"<<endl;
  198. pthread_mutex_lock(m_Mutex);
  199. //cerr<<"lock 2 ok"<<endl;
  200. if (i->second->type==INPUT)
  201. {
  202. memcpy(i->second->data_buf,s,i->second->size);
  203. }
  204. else
  205. {
  206. cerr<<"ChannelHandler: Tried to Set() data registered as output"<<endl;
  207. }//cerr<<"unlock 2"<<endl;
  208. pthread_mutex_unlock(m_Mutex);
  209. }
  210. void ChannelHandler::SetCommand(char command)
  211. {
  212. pthread_mutex_lock(m_Mutex);
  213. m_Command[1]=command;
  214. pthread_mutex_unlock(m_Mutex);
  215. }
  216. void ChannelHandler::FlushChannels()
  217. {
  218. pthread_mutex_lock(m_Mutex);
  219. for(map<string, Channel*>::iterator i=m_ChannelMap.begin();
  220. i!=m_ChannelMap.end(); i++)
  221. {
  222. memcpy(i->second->data_buf,i->second->data,i->second->size);
  223. }
  224. pthread_mutex_unlock(m_Mutex);
  225. }
  226. void ChannelHandler::RequestChannelAndWait(const string &ID)
  227. {
  228. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  229. if (i==m_ChannelMap.end())
  230. {
  231. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  232. return;
  233. }
  234. if (i->second->type!=OUTPUT_REQUEST)
  235. {
  236. cerr<<"ChannelHandler: Trying to request ["<<ID<<"] which is not a requestable channel"<<endl;
  237. return;
  238. }
  239. pthread_mutex_lock(m_Mutex);
  240. i->second->requested=true;
  241. pthread_mutex_unlock(m_Mutex);
  242. bool ready=false;
  243. while (!ready)
  244. {
  245. usleep(10); // random amount of time :)
  246. pthread_mutex_lock(m_Mutex);
  247. ready=i->second->updated;
  248. pthread_mutex_unlock(m_Mutex);
  249. }
  250. pthread_mutex_lock(m_Mutex);
  251. i->second->requested=false;
  252. i->second->updated=false;
  253. pthread_mutex_unlock(m_Mutex);
  254. }
  255. void ChannelHandler::BulkTransfer(const string &ID, void *dest, int size)
  256. {
  257. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  258. if (i==m_ChannelMap.end())
  259. {
  260. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  261. return;
  262. }
  263. if (i->second->type!=OUTPUT_REQUEST)
  264. {
  265. cerr<<"ChannelHandler: Trying to bulk transfer on ["<<ID<<"] which is not a OUTPUT_REQUEST channel"<<endl;
  266. return;
  267. }
  268. m_BulkPos=0;
  269. m_BulkSize = size;
  270. m_BulkID = ID;
  271. int pos=0;
  272. int buffersize=i->second->size;
  273. // fill up the destination buffer
  274. while (m_BulkPos!=-1)
  275. {
  276. RequestChannelAndWait(ID);
  277. if (pos+buffersize>size)
  278. {
  279. // last copy
  280. char *tempbuf = (char*)malloc(buffersize);
  281. GetData(ID,(void*)tempbuf);
  282. memcpy(((char*)dest)+pos,(void*)tempbuf,size-pos);
  283. free(tempbuf);
  284. }
  285. else
  286. {
  287. GetData(ID,((char*)dest)+pos);
  288. }
  289. pos+=buffersize;
  290. }
  291. }
  292. void ChannelHandler::Wait()
  293. {
  294. bool current;
  295. bool last;
  296. for (int n=0; n<2; n++)
  297. {
  298. pthread_mutex_lock(m_Mutex);
  299. current=m_UpdateIndicator;
  300. last=m_UpdateIndicator;
  301. pthread_mutex_unlock(m_Mutex);
  302. while (current==last)
  303. {
  304. usleep(10);
  305. pthread_mutex_lock(m_Mutex);
  306. current=m_UpdateIndicator;
  307. pthread_mutex_unlock(m_Mutex);
  308. }
  309. }
  310. }