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.

1734 lines
42KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #if defined(__APPLE__) && defined(__POWERPC__)
  18. #include "pThreadUtilities.h"
  19. #include "ipc.h"
  20. #include "fakepoll.h"
  21. #else
  22. #include <sys/poll.h>
  23. #endif
  24. #include <sys/socket.h>
  25. #include <sys/un.h>
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/ipc.h>
  31. #include <sys/mman.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <regex.h>
  36. #include <config.h>
  37. #include <jack/jack.h>
  38. #include <jack/internal.h>
  39. #include <jack/engine.h>
  40. #include <jack/pool.h>
  41. #include <jack/time.h>
  42. #include <jack/jslist.h>
  43. #include <jack/version.h>
  44. #include <jack/shm.h>
  45. #include "local.h"
  46. #ifdef WITH_TIMESTAMPS
  47. #include <jack/timestamps.h>
  48. #endif /* WITH_TIMESTAMPS */
  49. #ifdef DEFAULT_TMP_DIR
  50. char *jack_server_dir = DEFAULT_TMP_DIR;
  51. #else
  52. char *jack_server_dir = "/tmp";
  53. #endif
  54. static pthread_mutex_t client_lock;
  55. static pthread_cond_t client_ready;
  56. void *jack_zero_filled_buffer = NULL;
  57. #define event_fd pollfd[0].fd
  58. #define graph_wait_fd pollfd[1].fd
  59. typedef struct {
  60. int status;
  61. struct _jack_client *client;
  62. const char *client_name;
  63. } client_info;
  64. void
  65. jack_error (const char *fmt, ...)
  66. {
  67. va_list ap;
  68. char buffer[300];
  69. va_start (ap, fmt);
  70. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  71. jack_error_callback (buffer);
  72. va_end (ap);
  73. }
  74. void default_jack_error_callback (const char *desc)
  75. {
  76. fprintf(stderr, "%s\n", desc);
  77. }
  78. void silent_jack_error_callback (const char *desc)
  79. {
  80. }
  81. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  82. static int
  83. oop_client_deliver_request (void *ptr, jack_request_t *req)
  84. {
  85. jack_client_t *client = (jack_client_t*) ptr;
  86. if (write (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  87. jack_error ("cannot send request type %d to server", req->type);
  88. req->status = -1;
  89. }
  90. if (read (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  91. jack_error ("cannot read result for request type %d from server (%s)", req->type, strerror (errno));
  92. req->status = -1;
  93. }
  94. return req->status;
  95. }
  96. int
  97. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  98. {
  99. /* indirect through the function pointer that was set
  100. either by jack_client_new() (external) or handle_new_client()
  101. in the server.
  102. */
  103. return client->control->deliver_request (client->control->deliver_arg, req);
  104. }
  105. jack_client_t *
  106. jack_client_alloc ()
  107. {
  108. jack_client_t *client;
  109. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  110. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  111. client->pollmax = 2;
  112. client->request_fd = -1;
  113. client->event_fd = -1;
  114. client->graph_wait_fd = -1;
  115. client->graph_next_fd = -1;
  116. client->ports = NULL;
  117. client->engine = NULL;
  118. client->control = NULL;
  119. client->thread_ok = FALSE;
  120. client->first_active = TRUE;
  121. client->on_shutdown = NULL;
  122. client->n_port_types = 0;
  123. client->port_segment = NULL;
  124. return client;
  125. }
  126. jack_client_t *
  127. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  128. {
  129. jack_client_t* client;
  130. client = jack_client_alloc ();
  131. client->control = cc;
  132. client->engine = engine->control;
  133. client->n_port_types = client->engine->n_port_types;
  134. client->port_segment = &engine->port_segment[0];
  135. return client;
  136. }
  137. static void
  138. jack_client_free (jack_client_t *client)
  139. {
  140. if (client->pollfd) {
  141. free (client->pollfd);
  142. }
  143. free (client);
  144. }
  145. void
  146. jack_client_invalidate_port_buffers (jack_client_t *client)
  147. {
  148. JSList *node;
  149. jack_port_t *port;
  150. /* This releases all local memory owned by input ports
  151. and sets the buffer pointer to NULL. This will cause
  152. jack_port_get_buffer() to reallocate space for the
  153. buffer on the next call (if there is one).
  154. */
  155. for (node = client->ports; node; node = jack_slist_next (node)) {
  156. port = (jack_port_t *) node->data;
  157. if (port->shared->flags & JackPortIsInput) {
  158. if (port->mix_buffer) {
  159. jack_pool_release (port->mix_buffer);
  160. port->mix_buffer = NULL;
  161. }
  162. }
  163. }
  164. }
  165. int
  166. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  167. {
  168. jack_port_t *control_port;
  169. jack_port_t *other;
  170. JSList *node;
  171. switch (event->type) {
  172. case PortConnected:
  173. other = jack_port_new (client, event->y.other_id, client->engine);
  174. control_port = jack_port_by_id (client, event->x.self_id);
  175. pthread_mutex_lock (&control_port->connection_lock);
  176. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  177. pthread_mutex_unlock (&control_port->connection_lock);
  178. break;
  179. case PortDisconnected:
  180. control_port = jack_port_by_id (client, event->x.self_id);
  181. pthread_mutex_lock (&control_port->connection_lock);
  182. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  183. other = (jack_port_t *) node->data;
  184. if (other->shared->id == event->y.other_id) {
  185. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  186. jack_slist_free_1 (node);
  187. free (other);
  188. break;
  189. }
  190. }
  191. pthread_mutex_unlock (&control_port->connection_lock);
  192. break;
  193. default:
  194. /* impossible */
  195. break;
  196. }
  197. return 0;
  198. }
  199. static int
  200. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  201. {
  202. char path[PATH_MAX+1];
  203. if (client->graph_wait_fd >= 0) {
  204. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  205. close (client->graph_wait_fd);
  206. client->graph_wait_fd = -1;
  207. }
  208. if (client->graph_next_fd >= 0) {
  209. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  210. close (client->graph_next_fd);
  211. client->graph_next_fd = -1;
  212. }
  213. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  214. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  215. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  216. return -1;
  217. }
  218. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  219. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  220. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  221. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  222. return -1;
  223. }
  224. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  225. /* If the client registered its own callback for graph order events,
  226. execute it now.
  227. */
  228. if (client->control->graph_order) {
  229. client->control->graph_order (client->control->graph_order_arg);
  230. }
  231. return 0;
  232. }
  233. static int
  234. server_connect (int which)
  235. {
  236. int fd;
  237. struct sockaddr_un addr;
  238. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  239. jack_error ("cannot create client socket (%s)", strerror (errno));
  240. return -1;
  241. }
  242. addr.sun_family = AF_UNIX;
  243. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_server_dir, which);
  244. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  245. jack_error ("cannot connect to jack server", strerror (errno));
  246. close (fd);
  247. return -1;
  248. }
  249. return fd;
  250. }
  251. static int
  252. server_event_connect (jack_client_t *client)
  253. {
  254. int fd;
  255. struct sockaddr_un addr;
  256. jack_client_connect_ack_request_t req;
  257. jack_client_connect_ack_result_t res;
  258. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  259. jack_error ("cannot create client event socket (%s)", strerror (errno));
  260. return -1;
  261. }
  262. addr.sun_family = AF_UNIX;
  263. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_server_dir);
  264. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  265. jack_error ("cannot connect to jack server for events", strerror (errno));
  266. close (fd);
  267. return -1;
  268. }
  269. req.client_id = client->control->id;
  270. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  271. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  272. close (fd);
  273. return -1;
  274. }
  275. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  276. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  277. close (fd);
  278. return -1;
  279. }
  280. if (res.status != 0) {
  281. close (fd);
  282. return -1;
  283. }
  284. return fd;
  285. }
  286. static int
  287. jack_request_client (ClientType type, const char* client_name, const char* so_name,
  288. const char* so_data, jack_client_connect_result_t *res, int *req_fd)
  289. {
  290. jack_client_connect_request_t req;
  291. *req_fd = -1;
  292. memset (&req, 0, sizeof (req));
  293. if (strlen (client_name) > sizeof (req.name) - 1) {
  294. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  295. "Please use %lu characters or less.",
  296. client_name, sizeof (req.name) - 1);
  297. return -1;
  298. }
  299. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  300. jack_error ("\"%s\" is too long to be used as a JACK shared object name.\n"
  301. "Please use %lu characters or less.",
  302. so_name, sizeof (req.object_path) - 1);
  303. return -1;
  304. }
  305. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  306. jack_error ("\"%s\" is too long to be used as a JACK shared object data string.\n"
  307. "Please use %lu characters or less.",
  308. so_data, sizeof (req.object_data) - 1);
  309. return -1;
  310. }
  311. if ((*req_fd = server_connect (0)) < 0) {
  312. jack_error ("cannot connect to default JACK server");
  313. goto fail;
  314. }
  315. req.load = TRUE;
  316. req.type = type;
  317. snprintf (req.name, sizeof (req.name), "%s", client_name);
  318. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  319. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  320. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  321. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  322. goto fail;
  323. }
  324. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  325. if (errno == 0) {
  326. /* server shut the socket */
  327. jack_error ("could not attach as client (duplicate client name?)");
  328. goto fail;
  329. }
  330. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  331. goto fail;
  332. }
  333. if (res->status) {
  334. jack_error ("could not attach as client (duplicate client name?)");
  335. goto fail;
  336. }
  337. if (res->protocol_v != jack_protocol_version){
  338. jack_error ("application linked against too wrong of a version of libjack.");
  339. goto fail;
  340. }
  341. switch (type) {
  342. case ClientDriver:
  343. case ClientInternal:
  344. close (*req_fd);
  345. *req_fd = -1;
  346. break;
  347. default:
  348. break;
  349. }
  350. return 0;
  351. fail:
  352. if (*req_fd >= 0) {
  353. close (*req_fd);
  354. *req_fd = -1;
  355. }
  356. return -1;
  357. }
  358. int
  359. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  360. {
  361. /* Lookup, attach and register the port/buffer segments in use
  362. * right now.
  363. */
  364. if (client->control->type != ClientExternal) {
  365. jack_error("Only external clients need attach port segments");
  366. abort();
  367. }
  368. /* make sure we have space to store the port
  369. segment information.
  370. */
  371. if (ptid >= client->n_port_types) {
  372. client->port_segment = (jack_shm_info_t*)
  373. realloc (client->port_segment,
  374. sizeof (jack_shm_info_t) * (ptid+1));
  375. memset (&client->port_segment[client->n_port_types],
  376. 0,
  377. sizeof (jack_shm_info_t) *
  378. (ptid - client->n_port_types));
  379. client->n_port_types = ptid + 1;
  380. } else {
  381. /* release any previous segment */
  382. jack_release_shm (&client->port_segment[ptid]);
  383. }
  384. /* get the index into the shm registry */
  385. client->port_segment[ptid].index =
  386. client->engine->port_types[ptid].shm_registry_index;
  387. /* attach the relevant segment */
  388. if (jack_attach_shm (&client->port_segment[ptid])) {
  389. jack_error ("cannot attach port segment shared memory"
  390. " (%s)", strerror (errno));
  391. return -1;
  392. }
  393. /* The first chunk of the audio port segment will be set by
  394. * the engine to be a zero-filled buffer. This hasn't been
  395. * done yet, but it will happen before the process cycle
  396. * (re)starts.
  397. */
  398. if (ptid == JACK_AUDIO_PORT_TYPE) {
  399. jack_zero_filled_buffer =
  400. jack_shm_addr (&client->port_segment[ptid]);
  401. }
  402. return 0;
  403. }
  404. jack_client_t *
  405. jack_client_new (const char *client_name)
  406. {
  407. int req_fd = -1;
  408. int ev_fd = -1;
  409. jack_client_connect_result_t res;
  410. jack_client_t *client;
  411. jack_port_type_id_t ptid;
  412. /* external clients need this initialized; internal clients
  413. will use the setup in the server's address space.
  414. */
  415. jack_init_time ();
  416. jack_initialize_shm ();
  417. if (jack_request_client (ClientExternal, client_name, "", "",
  418. &res, &req_fd)) {
  419. return NULL;
  420. }
  421. client = jack_client_alloc ();
  422. strcpy (client->fifo_prefix, res.fifo_prefix);
  423. client->request_fd = req_fd;
  424. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  425. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  426. /* attach the engine control/info block */
  427. client->engine_shm = res.engine_shm;
  428. if (jack_attach_shm (&client->engine_shm)) {
  429. jack_error ("cannot attached engine control shared memory"
  430. " segment");
  431. goto fail;
  432. }
  433. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  434. /* now attach the client control block */
  435. client->control_shm = res.client_shm;
  436. if (jack_attach_shm (&client->control_shm)) {
  437. jack_error ("cannot attached client control shared memory"
  438. " segment");
  439. goto fail;
  440. }
  441. client->control = (jack_client_control_t *) jack_shm_addr (&client->control_shm);
  442. /* nobody else needs to access this shared memory any more, so
  443. destroy it. because we have our own attachment to it, it won't
  444. vanish till we exit (and release it).
  445. */
  446. jack_destroy_shm (&client->control_shm);
  447. client->n_port_types = client->engine->n_port_types;
  448. client->port_segment = (jack_shm_info_t *) malloc (sizeof (jack_shm_info_t) *
  449. client->n_port_types);
  450. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  451. client->port_segment[ptid].index =
  452. client->engine->port_types[ptid].shm_registry_index;
  453. jack_attach_port_segment (client, ptid);
  454. }
  455. /* set up the client so that it does the right thing for an
  456. * external client
  457. */
  458. client->control->deliver_request = oop_client_deliver_request;
  459. client->control->deliver_arg = client;
  460. if ((ev_fd = server_event_connect (client)) < 0) {
  461. jack_error ("cannot connect to server for event stream (%s)",
  462. strerror (errno));
  463. goto fail;
  464. }
  465. client->event_fd = ev_fd;
  466. #if defined(__APPLE__) && defined(__POWERPC__)
  467. /* specific resources for server/client real-time thread
  468. * communication */
  469. client->clienttask = mach_task_self();
  470. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  471. jack_error ("Can't find bootstrap port");
  472. goto fail;
  473. }
  474. if (allocate_mach_clientport(client, res.portnum) < 0) {
  475. jack_error("Can't allocate mach port");
  476. goto fail;
  477. };
  478. #endif
  479. return client;
  480. fail:
  481. if (client->engine) {
  482. jack_release_shm (&client->engine_shm);
  483. client->engine = 0;
  484. }
  485. if (client->control) {
  486. jack_release_shm (&client->control_shm);
  487. client->control = 0;
  488. }
  489. if (req_fd >= 0) {
  490. close (req_fd);
  491. }
  492. if (ev_fd >= 0) {
  493. close (ev_fd);
  494. }
  495. return 0;
  496. }
  497. int
  498. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  499. {
  500. jack_client_connect_result_t res;
  501. int req_fd;
  502. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  503. }
  504. void
  505. jack_internal_client_close (const char *client_name)
  506. {
  507. jack_client_connect_request_t req;
  508. int fd;
  509. req.load = FALSE;
  510. snprintf (req.name, sizeof (req.name), "%s", client_name);
  511. if ((fd = server_connect (0)) < 0) {
  512. jack_error ("cannot connect to default JACK server.");
  513. return;
  514. }
  515. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  516. jack_error ("cannot deliver ClientUnload request to JACK server.");
  517. }
  518. /* no response to this request */
  519. close (fd);
  520. return;
  521. }
  522. int
  523. jack_drop_real_time_scheduling (pthread_t thread)
  524. {
  525. struct sched_param rtparam;
  526. int x;
  527. memset (&rtparam, 0, sizeof (rtparam));
  528. rtparam.sched_priority = 0;
  529. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  530. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  531. return -1;
  532. }
  533. return 0;
  534. }
  535. int
  536. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  537. {
  538. struct sched_param rtparam;
  539. int x;
  540. memset (&rtparam, 0, sizeof (rtparam));
  541. rtparam.sched_priority = priority;
  542. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  543. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  544. "(%d: %s)", rtparam.sched_priority, x,
  545. strerror (x));
  546. return -1;
  547. }
  548. return 0;
  549. }
  550. int
  551. jack_set_freewheel (jack_client_t* client, int onoff)
  552. {
  553. jack_request_t request;
  554. request.type = onoff ? FreeWheel : StopFreeWheel;
  555. return jack_client_deliver_request (client, &request);
  556. }
  557. void
  558. jack_start_freewheel (jack_client_t* client)
  559. {
  560. jack_drop_real_time_scheduling (client->thread);
  561. }
  562. void
  563. jack_stop_freewheel (jack_client_t* client)
  564. {
  565. jack_acquire_real_time_scheduling (client->thread,
  566. client->engine->client_priority);
  567. }
  568. static void *
  569. jack_client_thread (void *arg)
  570. {
  571. jack_client_t *client = (jack_client_t *) arg;
  572. jack_client_control_t *control = client->control;
  573. jack_event_t event;
  574. char status = 0;
  575. char c;
  576. int err = 0;
  577. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  578. pthread_mutex_lock (&client_lock);
  579. client->thread_ok = TRUE;
  580. client->thread_id = pthread_self();
  581. pthread_cond_signal (&client_ready);
  582. pthread_mutex_unlock (&client_lock);
  583. client->control->pid = getpid();
  584. client->control->pgrp = getpgrp();
  585. DEBUG ("client thread is now running");
  586. while (err == 0) {
  587. if (client->engine->engine_ok == 0) {
  588. jack_error ("engine unexpectedly shutdown; "
  589. "thread exiting\n");
  590. if (client->on_shutdown) {
  591. client->on_shutdown (client->on_shutdown_arg);
  592. }
  593. pthread_exit (0);
  594. }
  595. DEBUG ("client polling on event_fd and graph_wait_fd...");
  596. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  597. if (errno == EINTR) {
  598. fprintf (stderr, "poll interrupted\n");
  599. continue;
  600. }
  601. jack_error ("poll failed in client (%s)",
  602. strerror (errno));
  603. status = -1;
  604. break;
  605. }
  606. /* get an accurate timestamp on waking from poll for a
  607. * process() cycle.
  608. */
  609. if (client->pollfd[1].revents & POLLIN) {
  610. control->awake_at = jack_get_microseconds();
  611. }
  612. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  613. client->pollfd[0].revents,
  614. client->pollfd[1].revents);
  615. pthread_testcancel();
  616. if ((client->pollfd[0].revents & ~POLLIN) ||
  617. client->control->dead) {
  618. goto zombie;
  619. }
  620. if (client->pollfd[0].revents & POLLIN) {
  621. DEBUG ("client receives an event, "
  622. "now reading on event fd");
  623. /* server has sent us an event. process the
  624. * event and reply */
  625. if (read (client->event_fd, &event, sizeof (event))
  626. != sizeof (event)) {
  627. jack_error ("cannot read server event (%s)",
  628. strerror (errno));
  629. err++;
  630. break;
  631. }
  632. status = 0;
  633. switch (event.type) {
  634. case PortRegistered:
  635. if (control->port_register) {
  636. control->port_register
  637. (event.x.port_id, TRUE,
  638. control->port_register_arg);
  639. }
  640. break;
  641. case PortUnregistered:
  642. if (control->port_register) {
  643. control->port_register
  644. (event.x.port_id, FALSE,
  645. control->port_register_arg);
  646. }
  647. break;
  648. case GraphReordered:
  649. status = jack_handle_reorder (client, &event);
  650. break;
  651. case PortConnected:
  652. case PortDisconnected:
  653. status = jack_client_handle_port_connection
  654. (client, &event);
  655. break;
  656. case BufferSizeChange:
  657. jack_client_invalidate_port_buffers (client);
  658. if (control->bufsize) {
  659. status = control->bufsize
  660. (control->nframes,
  661. control->bufsize_arg);
  662. }
  663. break;
  664. case SampleRateChange:
  665. if (control->srate) {
  666. status = control->srate
  667. (control->nframes,
  668. control->srate_arg);
  669. }
  670. break;
  671. case XRun:
  672. if (control->xrun) {
  673. status = control->xrun
  674. (control->xrun_arg);
  675. }
  676. break;
  677. case AttachPortSegment:
  678. jack_attach_port_segment (client, event.y.ptid);
  679. break;
  680. case StartFreewheel:
  681. jack_start_freewheel (client);
  682. break;
  683. case StopFreewheel:
  684. jack_stop_freewheel (client);
  685. break;
  686. }
  687. DEBUG ("client has dealt with the event, writing "
  688. "response on event fd");
  689. if (write (client->event_fd, &status, sizeof (status))
  690. != sizeof (status)) {
  691. jack_error ("cannot send event response to "
  692. "engine (%s)", strerror (errno));
  693. err++;
  694. break;
  695. }
  696. }
  697. if (client->pollfd[1].revents & ~POLLIN) {
  698. goto zombie;
  699. }
  700. if (client->pollfd[1].revents & POLLIN) {
  701. #ifdef WITH_TIMESTAMPS
  702. jack_reset_timestamps ();
  703. #endif
  704. DEBUG ("client %d signalled at %" PRIu64
  705. ", awake for process at %" PRIu64
  706. " (delay = %" PRIu64
  707. " usecs) (wakeup on graph_wait_fd==%d)",
  708. getpid(),
  709. control->signalled_at,
  710. control->awake_at,
  711. control->awake_at - control->signalled_at,
  712. client->pollfd[1].fd);
  713. control->state = Running;
  714. if (control->sync_cb)
  715. jack_call_sync_client (client);
  716. if (control->process) {
  717. if (control->process (control->nframes,
  718. control->process_arg)
  719. == 0) {
  720. control->state = Finished;
  721. }
  722. } else {
  723. control->state = Finished;
  724. }
  725. if (control->timebase_cb)
  726. jack_call_timebase_master (client);
  727. control->finished_at = jack_get_microseconds();
  728. #ifdef WITH_TIMESTAMPS
  729. jack_timestamp ("finished");
  730. #endif
  731. /* pass the execution token along */
  732. DEBUG ("client finished processing at %" PRIu64
  733. " (elapsed = %" PRIu64
  734. " usecs), writing on graph_next_fd==%d",
  735. control->finished_at,
  736. control->finished_at - control->awake_at,
  737. client->graph_next_fd);
  738. if (write (client->graph_next_fd, &c, sizeof (c))
  739. != sizeof (c)) {
  740. jack_error ("cannot continue execution of the "
  741. "processing graph (%s)",
  742. strerror(errno));
  743. err++;
  744. break;
  745. }
  746. DEBUG ("client sent message to next stage by %" PRIu64
  747. ", client reading on graph_wait_fd==%d",
  748. jack_get_microseconds(), client->graph_wait_fd);
  749. #ifdef WITH_TIMESTAMPS
  750. jack_timestamp ("read pending byte from wait");
  751. #endif
  752. DEBUG("reading cleanup byte from pipe\n");
  753. if ((read (client->graph_wait_fd, &c, sizeof (c))
  754. != sizeof (c))) {
  755. DEBUG ("WARNING: READ FAILED!");
  756. #if 0
  757. jack_error ("cannot complete execution of the "
  758. "processing graph (%s)",
  759. strerror(errno));
  760. err++;
  761. break;
  762. #endif
  763. }
  764. /* check if we were killed during the process
  765. * cycle (or whatever) */
  766. if (client->control->dead) {
  767. goto zombie;
  768. }
  769. DEBUG("process cycle fully complete\n");
  770. #ifdef WITH_TIMESTAMPS
  771. jack_timestamp ("read done");
  772. jack_dump_timestamps (stdout);
  773. #endif
  774. }
  775. }
  776. return (void *) ((intptr_t)err);
  777. zombie:
  778. if (client->on_shutdown) {
  779. jack_error ("zombified - calling shutdown handler");
  780. client->on_shutdown (client->on_shutdown_arg);
  781. } else {
  782. jack_error ("zombified - exiting from JACK");
  783. jack_client_close (client);
  784. /* Need a fix : possibly make client crash if
  785. * zombified without shutdown handler
  786. */
  787. }
  788. pthread_exit (0);
  789. /*NOTREACHED*/
  790. return 0;
  791. }
  792. #if defined(__APPLE__) && defined(__POWERPC__)
  793. /* real-time thread : separated from the normal client thread, it will communicate with the server using fast mach RPC mechanism */
  794. static void *
  795. jack_client_process_thread (void *arg)
  796. {
  797. jack_client_t *client = (jack_client_t *) arg;
  798. jack_client_control_t *control = client->control;
  799. int err = 0;
  800. client->control->pid = getpid();
  801. DEBUG ("client process thread is now running");
  802. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  803. while (err == 0) {
  804. if (jack_client_suspend(client) < 0) {
  805. pthread_exit (0);
  806. }
  807. control->awake_at = jack_get_microseconds();
  808. DEBUG ("client resumed");
  809. control->state = Running;
  810. if (control->process) {
  811. if (control->process (control->nframes, control->process_arg) == 0) {
  812. control->state = Finished;
  813. }
  814. } else {
  815. control->state = Finished;
  816. }
  817. control->finished_at = jack_get_microseconds();
  818. #ifdef WITH_TIMESTAMPS
  819. jack_timestamp ("finished");
  820. #endif
  821. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  822. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  823. /* check if we were killed during the process cycle (or whatever) */
  824. if (client->control->dead) {
  825. jack_error ("jack_client_process_thread : client->control->dead");
  826. goto zombie;
  827. }
  828. DEBUG("process cycle fully complete\n");
  829. }
  830. return (void *) ((intptr_t)err);
  831. zombie:
  832. jack_error ("jack_client_process_thread : zombified");
  833. if (client->on_shutdown) {
  834. jack_error ("zombified - calling shutdown handler");
  835. client->on_shutdown (client->on_shutdown_arg);
  836. } else {
  837. jack_error ("zombified - exiting from JACK");
  838. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  839. }
  840. pthread_exit (0);
  841. /*NOTREACHED*/
  842. return 0;
  843. }
  844. #endif
  845. static int
  846. jack_start_thread (jack_client_t *client)
  847. {
  848. pthread_attr_t *attributes = 0;
  849. #ifdef USE_CAPABILITIES
  850. int policy = SCHED_OTHER;
  851. struct sched_param client_param, temp_param;
  852. #endif
  853. if (client->engine->real_time) {
  854. /* Get the client thread to run as an RT-FIFO
  855. scheduled thread of appropriate priority.
  856. */
  857. struct sched_param rt_param;
  858. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  859. pthread_attr_init (attributes);
  860. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  861. jack_error ("cannot set FIFO scheduling class for RT thread");
  862. return -1;
  863. }
  864. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  865. jack_error ("Cannot set scheduling scope for RT thread");
  866. return -1;
  867. }
  868. memset (&rt_param, 0, sizeof (rt_param));
  869. rt_param.sched_priority = client->engine->client_priority;
  870. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  871. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  872. return -1;
  873. }
  874. #if defined(__APPLE__) && defined(__POWERPC__)
  875. // To be implemented
  876. #else
  877. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  878. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  879. return -1;
  880. }
  881. #endif
  882. }
  883. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  884. #ifdef USE_CAPABILITIES
  885. if (client->engine->real_time && client->engine->has_capabilities) {
  886. /* we are probably dealing with a broken glibc so try
  887. to work around the bug, see below for more details
  888. */
  889. goto capabilities_workaround;
  890. }
  891. #endif
  892. return -1;
  893. }
  894. #if defined(__APPLE__) && defined(__POWERPC__)
  895. /* a spcial real-time thread to call the "process" callback. It will communicate with the server using fast mach RPC mechanism */
  896. if (pthread_create (&client->process_thread, attributes, jack_client_process_thread, client)) {
  897. jack_error("pthread_create failed for process_thread \n");
  898. return -1;
  899. }
  900. if (client->engine->real_time){
  901. /* time constraint thread */
  902. setThreadToPriority(client->process_thread, 96, true, 10000000);
  903. }else{
  904. /* fixed priority thread */
  905. setThreadToPriority(client->process_thread, 63, true, 10000000);
  906. }
  907. #endif
  908. return 0;
  909. #ifdef USE_CAPABILITIES
  910. /* we get here only with engine running realtime and capabilities */
  911. capabilities_workaround:
  912. /* the version of glibc I've played with has a bug that makes
  913. that code fail when running under a non-root user but with the
  914. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  915. does not check for capabilities, only for the uid being
  916. zero). Newer versions apparently have this fixed. This
  917. workaround temporarily switches the client thread to the
  918. proper scheduler and priority, then starts the realtime
  919. thread so that it can inherit them and finally switches the
  920. client thread back to what it was before. Sigh. For ardour
  921. I have to check again and switch the thread explicitly to
  922. realtime, don't know why or how to debug - nando
  923. */
  924. /* get current scheduler and parameters of the client process */
  925. if ((policy = sched_getscheduler (0)) < 0) {
  926. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  927. return -1;
  928. }
  929. memset (&client_param, 0, sizeof (client_param));
  930. if (sched_getparam (0, &client_param)) {
  931. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  932. return -1;
  933. }
  934. /* temporarily change the client process to SCHED_FIFO so that
  935. the realtime thread can inherit the scheduler and priority
  936. */
  937. memset (&temp_param, 0, sizeof (temp_param));
  938. temp_param.sched_priority = client->engine->client_priority;
  939. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  940. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  941. return -1;
  942. }
  943. /* prepare the attributes for the realtime thread */
  944. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  945. pthread_attr_init (attributes);
  946. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  947. sched_setscheduler (0, policy, &client_param);
  948. jack_error ("Cannot set scheduling scope for RT thread");
  949. return -1;
  950. }
  951. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  952. sched_setscheduler (0, policy, &client_param);
  953. jack_error ("Cannot set scheduler inherit policy for RT thread");
  954. return -1;
  955. }
  956. /* create the RT thread */
  957. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  958. sched_setscheduler (0, policy, &client_param);
  959. return -1;
  960. }
  961. /* return the client process to the scheduler it was in before */
  962. if (sched_setscheduler (0, policy, &client_param)) {
  963. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  964. return -1;
  965. }
  966. /* check again... inheritance of policy and priority works in jack_simple_client
  967. but not in ardour! So I check again and force the policy if it is not set
  968. correctly. This does not really really work either, the manager thread
  969. of the linuxthreads implementation is left running with SCHED_OTHER,
  970. that is presumably very bad.
  971. */
  972. memset (&client_param, 0, sizeof (client_param));
  973. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  974. if (policy != SCHED_FIFO) {
  975. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  976. memset (&client_param, 0, sizeof (client_param));
  977. client_param.sched_priority = client->engine->client_priority;
  978. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  979. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  980. return -1;
  981. }
  982. }
  983. }
  984. return 0;
  985. #endif
  986. }
  987. int
  988. jack_activate (jack_client_t *client)
  989. {
  990. jack_request_t req;
  991. /* we need to scribble on our stack to ensure that its memory pages are
  992. * actually mapped (more important for mlockall(2) usage in
  993. * jack_start_thread())
  994. */
  995. #if defined(__APPLE__) && defined(__POWERPC__)
  996. #define BIG_ENOUGH_STACK 10000 // a bigger stack make the application crash...
  997. #else
  998. #define BIG_ENOUGH_STACK 1048576
  999. #endif
  1000. char buf[BIG_ENOUGH_STACK];
  1001. int i;
  1002. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  1003. buf[i] = (char) (i & 0xff);
  1004. }
  1005. #undef BIG_ENOUGH_STACK
  1006. if (client->control->type == ClientInternal || client->control->type == ClientDriver) {
  1007. goto startit;
  1008. }
  1009. /* get the pid of the client process to pass it to engine */
  1010. client->control->pid = getpid ();
  1011. #ifdef USE_CAPABILITIES
  1012. if (client->engine->has_capabilities != 0 &&
  1013. client->control->pid != 0 && client->engine->real_time != 0) {
  1014. /* we need to ask the engine for realtime capabilities
  1015. before trying to start the realtime thread
  1016. */
  1017. req.type = SetClientCapabilities;
  1018. req.x.client_id = client->control->id;
  1019. jack_client_deliver_request (client, &req);
  1020. if (req.status) {
  1021. /* what to do? engine is running realtime, it is using capabilities and has
  1022. them (otherwise we would not get an error return) but for some reason it
  1023. could not give the client the required capabilities, so for now downgrade
  1024. the client so that it still runs, albeit non-realtime - nando
  1025. */
  1026. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  1027. /* XXX wrong, this is a property of the engine
  1028. client->engine->real_time = 0;
  1029. */
  1030. }
  1031. }
  1032. #endif
  1033. if (client->first_active) {
  1034. pthread_mutex_init (&client_lock, NULL);
  1035. pthread_cond_init (&client_ready, NULL);
  1036. pthread_mutex_lock (&client_lock);
  1037. if (jack_start_thread (client)) {
  1038. pthread_mutex_unlock (&client_lock);
  1039. return -1;
  1040. }
  1041. pthread_cond_wait (&client_ready, &client_lock);
  1042. pthread_mutex_unlock (&client_lock);
  1043. if (!client->thread_ok) {
  1044. jack_error ("could not start client thread");
  1045. return -1;
  1046. }
  1047. client->first_active = FALSE;
  1048. }
  1049. startit:
  1050. req.type = ActivateClient;
  1051. req.x.client_id = client->control->id;
  1052. return jack_client_deliver_request (client, &req);
  1053. }
  1054. int
  1055. jack_deactivate (jack_client_t *client)
  1056. {
  1057. jack_request_t req;
  1058. req.type = DeactivateClient;
  1059. req.x.client_id = client->control->id;
  1060. return jack_client_deliver_request (client, &req);
  1061. }
  1062. int
  1063. jack_client_close (jack_client_t *client)
  1064. {
  1065. JSList *node;
  1066. void *status;
  1067. jack_port_type_id_t ptid;
  1068. if (client->control->active) {
  1069. jack_deactivate (client);
  1070. }
  1071. if (client->control->type == ClientExternal) {
  1072. /* stop the thread that communicates with the jack
  1073. * server, only if it was actually running
  1074. */
  1075. if (client->thread_ok){
  1076. pthread_cancel (client->thread);
  1077. pthread_join (client->thread, &status);
  1078. }
  1079. if (client->port_segment) {
  1080. free (client->port_segment);
  1081. }
  1082. if (client->control) {
  1083. jack_release_shm (&client->control_shm);
  1084. client->control = NULL;
  1085. }
  1086. if (client->engine) {
  1087. jack_release_shm (&client->engine_shm);
  1088. client->engine = NULL;
  1089. }
  1090. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1091. jack_release_shm (&client->port_segment[ptid]);
  1092. }
  1093. if (client->graph_wait_fd) {
  1094. close (client->graph_wait_fd);
  1095. }
  1096. if (client->graph_next_fd) {
  1097. close (client->graph_next_fd);
  1098. }
  1099. close (client->event_fd);
  1100. close (client->request_fd);
  1101. }
  1102. for (node = client->ports; node; node = jack_slist_next (node)) {
  1103. free (node->data);
  1104. }
  1105. jack_slist_free (client->ports);
  1106. jack_client_free (client);
  1107. return 0;
  1108. }
  1109. int
  1110. jack_is_realtime (jack_client_t *client)
  1111. {
  1112. return client->engine->real_time;
  1113. }
  1114. jack_nframes_t
  1115. jack_get_buffer_size (jack_client_t *client)
  1116. {
  1117. return client->engine->buffer_size;
  1118. }
  1119. int
  1120. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1121. {
  1122. jack_request_t req;
  1123. req.type = SetBufferSize;
  1124. req.x.nframes = nframes;
  1125. return jack_client_deliver_request (client, &req);
  1126. }
  1127. int
  1128. jack_connect (jack_client_t *client, const char *source_port,
  1129. const char *destination_port)
  1130. {
  1131. jack_request_t req;
  1132. req.type = ConnectPorts;
  1133. snprintf (req.x.connect.source_port,
  1134. sizeof (req.x.connect.source_port), "%s", source_port);
  1135. snprintf (req.x.connect.destination_port,
  1136. sizeof (req.x.connect.destination_port),
  1137. "%s", destination_port);
  1138. return jack_client_deliver_request (client, &req);
  1139. }
  1140. int
  1141. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1142. {
  1143. jack_request_t req;
  1144. pthread_mutex_lock (&port->connection_lock);
  1145. if (port->connections == NULL) {
  1146. pthread_mutex_unlock (&port->connection_lock);
  1147. return 0;
  1148. }
  1149. pthread_mutex_unlock (&port->connection_lock);
  1150. req.type = DisconnectPort;
  1151. req.x.port_info.port_id = port->shared->id;
  1152. return jack_client_deliver_request (client, &req);
  1153. }
  1154. int
  1155. jack_disconnect (jack_client_t *client, const char *source_port,
  1156. const char *destination_port)
  1157. {
  1158. jack_request_t req;
  1159. req.type = DisconnectPorts;
  1160. snprintf (req.x.connect.source_port,
  1161. sizeof (req.x.connect.source_port), "%s", source_port);
  1162. snprintf (req.x.connect.destination_port,
  1163. sizeof (req.x.connect.destination_port),
  1164. "%s", destination_port);
  1165. return jack_client_deliver_request (client, &req);
  1166. }
  1167. void
  1168. jack_set_error_function (void (*func) (const char *))
  1169. {
  1170. jack_error_callback = func;
  1171. }
  1172. int
  1173. jack_set_graph_order_callback (jack_client_t *client,
  1174. JackGraphOrderCallback callback, void *arg)
  1175. {
  1176. if (client->control->active) {
  1177. jack_error ("You cannot set callbacks on an active client.");
  1178. return -1;
  1179. }
  1180. client->control->graph_order = callback;
  1181. client->control->graph_order_arg = arg;
  1182. return 0;
  1183. }
  1184. int jack_set_xrun_callback (jack_client_t *client,
  1185. JackXRunCallback callback, void *arg)
  1186. {
  1187. if (client->control->active) {
  1188. jack_error ("You cannot set callbacks on an active client.");
  1189. return -1;
  1190. }
  1191. client->control->xrun = callback;
  1192. client->control->xrun_arg = arg;
  1193. return 0;
  1194. }
  1195. int
  1196. jack_set_process_callback (jack_client_t *client,
  1197. JackProcessCallback callback, void *arg)
  1198. {
  1199. if (client->control->active) {
  1200. jack_error ("You cannot set callbacks on an active client.");
  1201. return -1;
  1202. }
  1203. client->control->process_arg = arg;
  1204. client->control->process = callback;
  1205. return 0;
  1206. }
  1207. int
  1208. jack_set_buffer_size_callback (jack_client_t *client,
  1209. JackBufferSizeCallback callback, void *arg)
  1210. {
  1211. client->control->bufsize_arg = arg;
  1212. client->control->bufsize = callback;
  1213. return 0;
  1214. }
  1215. int
  1216. jack_set_port_registration_callback(jack_client_t *client,
  1217. JackPortRegistrationCallback callback,
  1218. void *arg)
  1219. {
  1220. if (client->control->active) {
  1221. jack_error ("You cannot set callbacks on an active client.");
  1222. return -1;
  1223. }
  1224. client->control->port_register_arg = arg;
  1225. client->control->port_register = callback;
  1226. return 0;
  1227. }
  1228. int
  1229. jack_get_process_start_fd (jack_client_t *client)
  1230. {
  1231. /* once this has been called, the client thread
  1232. does not sleep on the graph wait fd.
  1233. */
  1234. client->pollmax = 1;
  1235. return client->graph_wait_fd;
  1236. }
  1237. int
  1238. jack_get_process_done_fd (jack_client_t *client)
  1239. {
  1240. return client->graph_next_fd;
  1241. }
  1242. void
  1243. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1244. {
  1245. client->on_shutdown = function;
  1246. client->on_shutdown_arg = arg;
  1247. }
  1248. const char **
  1249. jack_get_ports (jack_client_t *client,
  1250. const char *port_name_pattern,
  1251. const char *type_name_pattern,
  1252. unsigned long flags)
  1253. {
  1254. jack_control_t *engine;
  1255. const char **matching_ports;
  1256. unsigned long match_cnt;
  1257. jack_port_shared_t *psp;
  1258. unsigned long i;
  1259. regex_t port_regex;
  1260. regex_t type_regex;
  1261. int matching;
  1262. engine = client->engine;
  1263. if (port_name_pattern && port_name_pattern[0]) {
  1264. regcomp (&port_regex, port_name_pattern,
  1265. REG_EXTENDED|REG_NOSUB);
  1266. }
  1267. if (type_name_pattern && type_name_pattern[0]) {
  1268. regcomp (&type_regex, type_name_pattern,
  1269. REG_EXTENDED|REG_NOSUB);
  1270. }
  1271. psp = engine->ports;
  1272. match_cnt = 0;
  1273. matching_ports = (const char **)
  1274. malloc (sizeof (char *) * engine->port_max);
  1275. for (i = 0; i < engine->port_max; i++) {
  1276. matching = 1;
  1277. if (!psp[i].in_use) {
  1278. continue;
  1279. }
  1280. if (flags) {
  1281. if ((psp[i].flags & flags) != flags) {
  1282. matching = 0;
  1283. }
  1284. }
  1285. if (matching && port_name_pattern && port_name_pattern[0]) {
  1286. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1287. matching = 0;
  1288. }
  1289. }
  1290. if (matching && type_name_pattern && type_name_pattern[0]) {
  1291. jack_port_type_id_t ptid = psp[i].ptype_id;
  1292. if (regexec (&type_regex,
  1293. engine->port_types[ptid].type_name,
  1294. 0, NULL, 0)) {
  1295. matching = 0;
  1296. }
  1297. }
  1298. if (matching) {
  1299. matching_ports[match_cnt++] = psp[i].name;
  1300. }
  1301. }
  1302. matching_ports[match_cnt] = 0;
  1303. if (match_cnt == 0) {
  1304. free (matching_ports);
  1305. matching_ports = 0;
  1306. }
  1307. return matching_ports;
  1308. }
  1309. float
  1310. jack_cpu_load (jack_client_t *client)
  1311. {
  1312. return client->engine->cpu_load;
  1313. }
  1314. pthread_t
  1315. jack_client_thread_id (jack_client_t *client)
  1316. {
  1317. return client->thread_id;
  1318. }
  1319. #if defined(__APPLE__) && defined(__POWERPC__)
  1320. double __jack_time_ratio;
  1321. void jack_init_time ()
  1322. {
  1323. mach_timebase_info_data_t info;
  1324. mach_timebase_info(&info);
  1325. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1326. }
  1327. #else
  1328. jack_time_t
  1329. jack_get_mhz (void)
  1330. {
  1331. FILE *f = fopen("/proc/cpuinfo", "r");
  1332. if (f == 0)
  1333. {
  1334. perror("can't open /proc/cpuinfo\n");
  1335. exit(1);
  1336. }
  1337. for ( ; ; )
  1338. {
  1339. jack_time_t mhz;
  1340. int ret;
  1341. char buf[1000];
  1342. if (fgets(buf, sizeof(buf), f) == NULL)
  1343. {
  1344. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1345. exit(1);
  1346. }
  1347. #if defined(__powerpc__)
  1348. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  1349. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  1350. defined(__x86_64__)
  1351. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  1352. #elif defined( __sparc__ )
  1353. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  1354. #elif defined( __mc68000__ )
  1355. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  1356. #elif defined( __s390__ )
  1357. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  1358. #else /* MIPS, ARM, alpha */
  1359. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  1360. #endif
  1361. if (ret == 1)
  1362. {
  1363. fclose(f);
  1364. return (jack_time_t)mhz;
  1365. }
  1366. }
  1367. }
  1368. jack_time_t __jack_cpu_mhz;
  1369. void jack_init_time ()
  1370. {
  1371. __jack_cpu_mhz = jack_get_mhz ();
  1372. }
  1373. #endif