jack2 codebase
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.

316 lines
9.4KB

  1. /*
  2. Copyright (C) 2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackLockedEngine__
  16. #define __JackLockedEngine__
  17. #include "JackEngine.h"
  18. #include "JackMutex.h"
  19. #include "JackTools.h"
  20. #include "JackException.h"
  21. namespace Jack
  22. {
  23. #define TRY_CALL \
  24. try { \
  25. /*
  26. See : http://groups.google.com/group/comp.programming.threads/browse_thread/thread/652bcf186fbbf697/f63757846514e5e5
  27. catch (...) {
  28. // Assuming thread cancellation, must rethrow
  29. throw;
  30. }
  31. */
  32. #define CATCH_EXCEPTION_RETURN \
  33. } catch(std::bad_alloc& e) { \
  34. jack_error("Memory allocation error..."); \
  35. return -1; \
  36. } catch(JackTemporaryException& e) { \
  37. jack_error("JackTemporaryException : now quits..."); \
  38. JackTools::KillServer(); \
  39. return -1; \
  40. } catch (...) { \
  41. jack_error("Unknown error..."); \
  42. throw; \
  43. } \
  44. #define CATCH_EXCEPTION \
  45. } catch(std::bad_alloc& e) { \
  46. jack_error("Memory allocation error..."); \
  47. } catch (...) { \
  48. jack_error("Unknown error..."); \
  49. throw; \
  50. } \
  51. /*!
  52. \brief Locked Engine, access to methods is serialized using a mutex.
  53. */
  54. class SERVER_EXPORT JackLockedEngine : public JackLockAble
  55. {
  56. private:
  57. JackEngine fEngine;
  58. public:
  59. JackLockedEngine(JackGraphManager* manager, JackSynchro* table, JackEngineControl* controler):
  60. fEngine(manager, table, controler)
  61. {}
  62. ~JackLockedEngine()
  63. {}
  64. int Open()
  65. {
  66. // No lock needed
  67. TRY_CALL
  68. return fEngine.Open();
  69. CATCH_EXCEPTION_RETURN
  70. }
  71. int Close()
  72. {
  73. // No lock needed
  74. TRY_CALL
  75. return fEngine.Close();
  76. CATCH_EXCEPTION_RETURN
  77. }
  78. // Client management
  79. int ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
  80. {
  81. TRY_CALL
  82. JackLock lock(this);
  83. return fEngine.ClientCheck(name, name_res, protocol, options, status);
  84. CATCH_EXCEPTION_RETURN
  85. }
  86. int ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
  87. {
  88. TRY_CALL
  89. JackLock lock(this);
  90. return fEngine.ClientExternalOpen(name, pid, ref, shared_engine, shared_client, shared_graph_manager);
  91. CATCH_EXCEPTION_RETURN
  92. }
  93. int ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
  94. {
  95. TRY_CALL
  96. JackLock lock(this);
  97. return fEngine.ClientInternalOpen(name, ref, shared_engine, shared_manager, client, wait);
  98. CATCH_EXCEPTION_RETURN
  99. }
  100. int ClientExternalClose(int refnum)
  101. {
  102. TRY_CALL
  103. JackLock lock(this);
  104. return fEngine.ClientExternalClose(refnum);
  105. CATCH_EXCEPTION_RETURN
  106. }
  107. int ClientInternalClose(int refnum, bool wait)
  108. {
  109. TRY_CALL
  110. JackLock lock(this);
  111. return fEngine.ClientInternalClose(refnum, wait);
  112. CATCH_EXCEPTION_RETURN
  113. }
  114. int ClientActivate(int refnum, bool is_real_time)
  115. {
  116. TRY_CALL
  117. JackLock lock(this);
  118. return fEngine.ClientActivate(refnum, is_real_time);
  119. CATCH_EXCEPTION_RETURN
  120. }
  121. int ClientDeactivate(int refnum)
  122. {
  123. TRY_CALL
  124. JackLock lock(this);
  125. return fEngine.ClientDeactivate(refnum);
  126. CATCH_EXCEPTION_RETURN
  127. }
  128. // Internal client management
  129. int GetInternalClientName(int int_ref, char* name_res)
  130. {
  131. TRY_CALL
  132. JackLock lock(this);
  133. return fEngine.GetInternalClientName(int_ref, name_res);
  134. CATCH_EXCEPTION_RETURN
  135. }
  136. int InternalClientHandle(const char* client_name, int* status, int* int_ref)
  137. {
  138. TRY_CALL
  139. JackLock lock(this);
  140. return fEngine.InternalClientHandle(client_name, status, int_ref);
  141. CATCH_EXCEPTION_RETURN
  142. }
  143. int InternalClientUnload(int refnum, int* status)
  144. {
  145. TRY_CALL
  146. JackLock lock(this);
  147. return fEngine.InternalClientUnload(refnum, status);
  148. CATCH_EXCEPTION_RETURN
  149. }
  150. // Port management
  151. int PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port)
  152. {
  153. TRY_CALL
  154. JackLock lock(this);
  155. return fEngine.PortRegister(refnum, name, type, flags, buffer_size, port);
  156. CATCH_EXCEPTION_RETURN
  157. }
  158. int PortUnRegister(int refnum, jack_port_id_t port)
  159. {
  160. TRY_CALL
  161. JackLock lock(this);
  162. return fEngine.PortUnRegister(refnum, port);
  163. CATCH_EXCEPTION_RETURN
  164. }
  165. int PortConnect(int refnum, const char* src, const char* dst)
  166. {
  167. TRY_CALL
  168. JackLock lock(this);
  169. return fEngine.PortConnect(refnum, src, dst);
  170. CATCH_EXCEPTION_RETURN
  171. }
  172. int PortDisconnect(int refnum, const char* src, const char* dst)
  173. {
  174. TRY_CALL
  175. JackLock lock(this);
  176. return fEngine.PortDisconnect(refnum, src, dst);
  177. CATCH_EXCEPTION_RETURN
  178. }
  179. int PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  180. {
  181. TRY_CALL
  182. JackLock lock(this);
  183. return fEngine.PortConnect(refnum, src, dst);
  184. CATCH_EXCEPTION_RETURN
  185. }
  186. int PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
  187. {
  188. TRY_CALL
  189. JackLock lock(this);
  190. return fEngine.PortDisconnect(refnum, src, dst);
  191. CATCH_EXCEPTION_RETURN
  192. }
  193. int PortRename(int refnum, jack_port_id_t port, const char* name)
  194. {
  195. TRY_CALL
  196. JackLock lock(this);
  197. return fEngine.PortRename(refnum, port, name);
  198. CATCH_EXCEPTION_RETURN
  199. }
  200. // Graph
  201. bool Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
  202. {
  203. // RT : no lock
  204. return fEngine.Process(cur_cycle_begin, prev_cycle_end);
  205. }
  206. // Notifications
  207. void NotifyXRun(jack_time_t cur_cycle_begin, float delayed_usecs)
  208. {
  209. // RT : no lock
  210. fEngine.NotifyXRun(cur_cycle_begin, delayed_usecs);
  211. }
  212. void NotifyXRun(int refnum)
  213. {
  214. TRY_CALL
  215. JackLock lock(this);
  216. fEngine.NotifyXRun(refnum);
  217. CATCH_EXCEPTION
  218. }
  219. void NotifyGraphReorder()
  220. {
  221. TRY_CALL
  222. JackLock lock(this);
  223. fEngine.NotifyGraphReorder();
  224. CATCH_EXCEPTION
  225. }
  226. void NotifyBufferSize(jack_nframes_t buffer_size)
  227. {
  228. TRY_CALL
  229. JackLock lock(this);
  230. fEngine.NotifyBufferSize(buffer_size);
  231. CATCH_EXCEPTION
  232. }
  233. void NotifySampleRate(jack_nframes_t sample_rate)
  234. {
  235. TRY_CALL
  236. JackLock lock(this);
  237. fEngine.NotifySampleRate(sample_rate);
  238. CATCH_EXCEPTION
  239. }
  240. void NotifyFreewheel(bool onoff)
  241. {
  242. TRY_CALL
  243. JackLock lock(this);
  244. fEngine.NotifyFreewheel(onoff);
  245. CATCH_EXCEPTION
  246. }
  247. void NotifyFailure(int code, const char* reason)
  248. {
  249. TRY_CALL
  250. JackLock lock(this);
  251. fEngine.NotifyFailure(code, reason);
  252. CATCH_EXCEPTION
  253. }
  254. int GetClientPID(const char* name)
  255. {
  256. TRY_CALL
  257. JackLock lock(this);
  258. return fEngine.GetClientPID(name);
  259. CATCH_EXCEPTION_RETURN
  260. }
  261. int GetClientRefNum(const char* name)
  262. {
  263. TRY_CALL
  264. JackLock lock(this);
  265. return fEngine.GetClientRefNum(name);
  266. CATCH_EXCEPTION_RETURN
  267. }
  268. void NotifyQuit()
  269. {
  270. TRY_CALL
  271. JackLock lock(this);
  272. return fEngine.NotifyQuit();
  273. CATCH_EXCEPTION
  274. }
  275. };
  276. } // end of namespace
  277. #endif