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.

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