jack1 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.

168 lines
4.7KB

  1. /*
  2. Copyright © Grame 2003
  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. Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
  15. grame@rd.grame.fr
  16. */
  17. #ifndef __ipc__
  18. #define __ipc__
  19. #include <mach/mach_error.h>
  20. #include <servers/bootstrap.h>
  21. #include "internal.h"
  22. #include "engine.h"
  23. #include "libjack/local.h" /* JOQ: fix me */
  24. /*
  25. RPC without time out can put the jack server in a blocked state
  26. (waiting for the client answer) when a client is killed. The
  27. mach_msg function does not return any error in this case. Using
  28. time out solve the problem but does not seems really satisfactory.
  29. */
  30. #define WAIT 2500 /* in millisecond */
  31. static inline int
  32. jack_client_resume (jack_client_internal_t *client)
  33. {
  34. mach_msg_header_t *head = &client->message.header;
  35. int err;
  36. if (!client->running) {
  37. err = mach_msg (head, MACH_RCV_MSG, 0, sizeof(client->message),
  38. client->serverport, 0, MACH_PORT_NULL);
  39. if (err) {
  40. jack_error ("jack_client_resume: priming receive error: %s\n",
  41. mach_error_string (err));
  42. return -1;
  43. }
  44. client->running = TRUE;
  45. } else {
  46. /* remote port is already the send-once he sent us */
  47. head->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MOVE_SEND_ONCE, 0);
  48. head->msgh_local_port = MACH_PORT_NULL;
  49. head->msgh_size = sizeof(mach_msg_header_t);
  50. err = mach_msg (head, (MACH_SEND_MSG | MACH_RCV_MSG |
  51. MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT),
  52. sizeof(*head), sizeof(client->message),
  53. client->serverport, WAIT, MACH_PORT_NULL);
  54. if (err) {
  55. /*
  56. switch(err) {
  57. case MACH_SEND_TIMED_OUT:
  58. jack_error("MACH_SEND_TIMED_OUT %s\n",
  59. client->control->name);
  60. break;
  61. case MACH_RCV_TIMED_OUT:
  62. jack_error("MACH_RCV_TIMED_OUT %s\n",
  63. client->control->name);
  64. break;
  65. case MACH_SEND_INVALID_DEST:
  66. jack_error("MACH_SEND_INVALID_DEST %s\n",
  67. client->control->name);
  68. break;
  69. }
  70. */
  71. jack_error ("jack_client_resume: send error for %s\n",
  72. mach_error_string (err));
  73. return err;
  74. }
  75. }
  76. return 0;
  77. }
  78. static inline int
  79. jack_client_suspend (jack_client_t * client)
  80. {
  81. int err = 0;
  82. mach_msg_header_t * head = &client->message.header;
  83. head->msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
  84. MACH_MSG_TYPE_MAKE_SEND_ONCE);
  85. head->msgh_remote_port = client->serverport;
  86. head->msgh_local_port = client->replyport;
  87. head->msgh_size = sizeof(mach_msg_header_t);
  88. err = mach_msg (head, MACH_SEND_MSG | MACH_RCV_MSG | MACH_SEND_TIMEOUT,
  89. sizeof(mach_msg_header_t), sizeof(client->message),
  90. client->replyport, WAIT, MACH_PORT_NULL);
  91. if (err) {
  92. jack_error ("jack_client_suspend: RPC error: %s\n",
  93. mach_error_string (err));
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. static inline void
  99. allocate_mach_serverport (jack_engine_t * engine, jack_client_internal_t *client)
  100. {
  101. char buf[256];
  102. snprintf (buf, 256, "JackMachPort_%d", engine->portnum);
  103. if (mach_port_allocate (engine->servertask, MACH_PORT_RIGHT_RECEIVE,
  104. &client->serverport)) {
  105. jack_error ("allocate_mach_serverport: can't allocate mach port");
  106. }
  107. if (mach_port_insert_right (engine->servertask, client->serverport,
  108. client->serverport, MACH_MSG_TYPE_MAKE_SEND)) {
  109. jack_error ("allocate_mach_serverport: error inserting mach rights");
  110. }
  111. if (bootstrap_register (engine->bp, buf, client->serverport)) {
  112. jack_error ("allocate_mach_serverport: can't check in mach port");
  113. }
  114. client->portnum = engine->portnum;
  115. engine->portnum++;
  116. }
  117. static inline int
  118. allocate_mach_clientport (jack_client_t * client, int portnum)
  119. {
  120. char buf[256];
  121. snprintf (buf, 256, "JackMachPort_%d", portnum);
  122. if (bootstrap_look_up (client->bp, buf, &client->serverport)) {
  123. jack_error ("allocate_mach_clientport: can't find mach server port");
  124. return -1;
  125. }
  126. if (mach_port_allocate (client->clienttask, MACH_PORT_RIGHT_RECEIVE,
  127. &client->replyport)) {
  128. jack_error ("allocate_mach_clientport: can't allocate mach port");
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. #endif /* __ipc__ */