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.

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