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.

353 lines
8.4KB

  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)
  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. #endif
  93. memcpy(ch->data_buf,((char*)m_BulkSrc)+m_BulkPos,m_BulkSize-m_BulkPos);
  94. m_BulkPos=-1;
  95. }
  96. else
  97. {
  98. #ifdef CHANNEL_DEBUG
  99. cerr<<"memcpy bulk output channel: ["<<i->first<<"]"<<endl;
  100. #endif
  101. memcpy(ch->data_buf,((char*)m_BulkSrc)+m_BulkPos,ch->size);
  102. m_BulkPos+=ch->size;
  103. }
  104. ch->updated=true;
  105. }
  106. }
  107. else
  108. {
  109. // normal request transfer
  110. if (ch->requested)
  111. {
  112. #ifdef CHANNEL_DEBUG
  113. cerr<<"memcpy output channel: ["<<i->first<<"]"<<endl;
  114. #endif
  115. memcpy(ch->data_buf,ch->data,ch->size);
  116. ch->updated=true;
  117. }
  118. }
  119. } break;
  120. }
  121. }
  122. m_Command[0]=m_Command[1];
  123. // make sure the command only lasts one update
  124. m_Command[1]=0;
  125. pthread_mutex_unlock(m_Mutex);
  126. //cerr<<"audio out mutex"<<endl;
  127. //cerr<<"Update succeeded"<<endl;
  128. }
  129. else
  130. {
  131. //cerr<<"Couldn't get lock"<<endl;
  132. }
  133. #ifdef CHANNEL_DEBUG
  134. cerr<<"Ended update"<<endl;
  135. #endif
  136. }
  137. void ChannelHandler::RegisterData(const string &ID, Type t,void* pData, int size)
  138. {
  139. // probably don't need to lock here, as if get/set are called before
  140. // the channels have been set up they won't work anyway, but...
  141. //pthread_mutex_lock(m_Mutex);
  142. #ifdef CHANNEL_DEBUG
  143. cerr<<"Registering ["<<ID<<"] "<<hex<<pData<<dec<<" as "<<size<<" bytes big"<<endl;
  144. #endif
  145. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  146. if (i!=m_ChannelMap.end())
  147. {
  148. cerr<<"Channel with ID ["<<ID<<"] already exists"<<endl;
  149. }
  150. Channel *NewCh=new Channel(t);
  151. NewCh->data_buf = malloc(size);
  152. NewCh->size = size;
  153. NewCh->data = pData;
  154. NewCh->requested = false;
  155. NewCh->updated = false;
  156. memcpy(NewCh->data_buf,NewCh->data,size);
  157. m_ChannelMap[ID]=NewCh;
  158. //pthread_mutex_unlock(m_Mutex);
  159. }
  160. /////////////////////////////////////////////////////////////////////////
  161. void ChannelHandler::GetData(const string &ID, void *data)
  162. {
  163. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  164. if (i==m_ChannelMap.end())
  165. {
  166. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  167. return;
  168. }
  169. if (!data)
  170. {
  171. cerr<<"ChannelHandler: Can't copy data to uninitialised mem"<<endl;
  172. return;
  173. }
  174. pthread_mutex_lock(m_Mutex);
  175. if (i->second->type==OUTPUT || i->second->type==OUTPUT_REQUEST)
  176. {
  177. memcpy(data,i->second->data_buf,i->second->size);
  178. }
  179. else
  180. {
  181. cerr<<"ChannelHandler: Tried to Get() data registered as input"<<endl;
  182. }//cerr<<"unlock 1"<<endl;
  183. pthread_mutex_unlock(m_Mutex);
  184. }
  185. void ChannelHandler::SetData(const string &ID, void *s)
  186. {
  187. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  188. if (i==m_ChannelMap.end())
  189. {
  190. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  191. return;
  192. }
  193. //cerr<<"lock 2"<<endl;
  194. pthread_mutex_lock(m_Mutex);
  195. //cerr<<"lock 2 ok"<<endl;
  196. if (i->second->type==INPUT)
  197. {
  198. memcpy(i->second->data_buf,s,i->second->size);
  199. }
  200. else
  201. {
  202. cerr<<"ChannelHandler: Tried to Set() data registered as output"<<endl;
  203. }//cerr<<"unlock 2"<<endl;
  204. pthread_mutex_unlock(m_Mutex);
  205. }
  206. void ChannelHandler::SetCommand(char command)
  207. {
  208. pthread_mutex_lock(m_Mutex);
  209. m_Command[1]=command;
  210. pthread_mutex_unlock(m_Mutex);
  211. }
  212. void ChannelHandler::FlushChannels()
  213. {
  214. pthread_mutex_lock(m_Mutex);
  215. for(map<string, Channel*>::iterator i=m_ChannelMap.begin();
  216. i!=m_ChannelMap.end(); i++)
  217. {
  218. memcpy(i->second->data_buf,i->second->data,i->second->size);
  219. }
  220. pthread_mutex_unlock(m_Mutex);
  221. }
  222. void ChannelHandler::RequestChannelAndWait(const string &ID)
  223. {
  224. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  225. if (i==m_ChannelMap.end())
  226. {
  227. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  228. return;
  229. }
  230. if (i->second->type!=OUTPUT_REQUEST)
  231. {
  232. cerr<<"ChannelHandler: Trying to request ["<<ID<<"] which is not a requestable channel"<<endl;
  233. return;
  234. }
  235. pthread_mutex_lock(m_Mutex);
  236. i->second->requested=true;
  237. pthread_mutex_unlock(m_Mutex);
  238. bool ready=false;
  239. while (!ready)
  240. {
  241. usleep(10); // random amount of time :)
  242. pthread_mutex_lock(m_Mutex);
  243. ready=i->second->updated;
  244. pthread_mutex_unlock(m_Mutex);
  245. }
  246. pthread_mutex_lock(m_Mutex);
  247. i->second->requested=false;
  248. i->second->updated=false;
  249. pthread_mutex_unlock(m_Mutex);
  250. }
  251. void ChannelHandler::BulkTransfer(const string &ID, void *dest, int size)
  252. {
  253. map<string,Channel*>::iterator i=m_ChannelMap.find(ID);
  254. if (i==m_ChannelMap.end())
  255. {
  256. cerr<<"ChannelHandler: Channel ["<<ID<<"] does not exist"<<endl;
  257. return;
  258. }
  259. if (i->second->type!=OUTPUT_REQUEST)
  260. {
  261. cerr<<"ChannelHandler: Trying to bulk transfer on ["<<ID<<"] which is not a OUTPUT_REQUEST channel"<<endl;
  262. return;
  263. }
  264. m_BulkPos=0;
  265. m_BulkSize = size;
  266. m_BulkID = ID;
  267. int pos=0;
  268. int buffersize=i->second->size;
  269. // fill up the destination buffer
  270. while (m_BulkPos!=-1)
  271. {
  272. RequestChannelAndWait(ID);
  273. if (pos+buffersize>size)
  274. {
  275. // last copy
  276. char *tempbuf = (char*)malloc(buffersize);
  277. GetData(ID,(void*)tempbuf);
  278. memcpy(((char*)dest)+pos,(void*)tempbuf,size-pos);
  279. free(tempbuf);
  280. }
  281. else
  282. {
  283. GetData(ID,((char*)dest)+pos);
  284. }
  285. pos+=buffersize;
  286. }
  287. }
  288. void ChannelHandler::Wait()
  289. {
  290. bool current;
  291. bool last;
  292. for (int n=0; n<2; n++)
  293. {
  294. pthread_mutex_lock(m_Mutex);
  295. current=m_UpdateIndicator;
  296. last=m_UpdateIndicator;
  297. pthread_mutex_unlock(m_Mutex);
  298. while (current==last)
  299. {
  300. usleep(10);
  301. pthread_mutex_lock(m_Mutex);
  302. current=m_UpdateIndicator;
  303. pthread_mutex_unlock(m_Mutex);
  304. }
  305. }
  306. }