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.

1658 lines
41KB

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