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.

236 lines
7.9KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __jack_engine_h__
  17. #define __jack_engine_h__
  18. #include <jack/jack.h>
  19. #include <jack/internal.h>
  20. #include <jack/driver_interface.h>
  21. struct _jack_driver;
  22. struct _jack_client_internal;
  23. struct _jack_port_internal;
  24. /* Structures is allocated by the engine in local memory to keep track
  25. * of port buffers and connections.
  26. */
  27. typedef struct {
  28. jack_shm_info_t* shm_info;
  29. jack_shmsize_t offset;
  30. } jack_port_buffer_info_t;
  31. /* The engine keeps an array of these in its local memory. */
  32. typedef struct _jack_port_internal {
  33. struct _jack_port_shared *shared;
  34. JSList *connections;
  35. jack_port_buffer_info_t *buffer_info;
  36. } jack_port_internal_t;
  37. /* The engine's internal port type structure. */
  38. typedef struct _jack_port_buffer_list {
  39. pthread_mutex_t lock; /* only lock within server */
  40. JSList *freelist; /* list of free buffers */
  41. jack_port_buffer_info_t *info; /* jack_buffer_info_t array */
  42. } jack_port_buffer_list_t;
  43. #define JACKD_WATCHDOG_TIMEOUT 10000
  44. #define JACKD_CLIENT_EVENT_TIMEOUT 2000
  45. /* The main engine structure in local memory. */
  46. struct _jack_engine {
  47. jack_control_t *control;
  48. JSList *drivers;
  49. struct _jack_driver *driver;
  50. jack_driver_desc_t *driver_desc;
  51. JSList *driver_params;
  52. /* these are "callbacks" made by the driver backend */
  53. int (*set_buffer_size) (struct _jack_engine *, jack_nframes_t frames);
  54. int (*set_sample_rate) (struct _jack_engine *, jack_nframes_t frames);
  55. int (*run_cycle) (struct _jack_engine *, jack_nframes_t nframes,
  56. float delayed_usecs);
  57. void (*delay) (struct _jack_engine *, float delayed_usecs);
  58. void (*transport_cycle_start) (struct _jack_engine *, jack_time_t time);
  59. void (*driver_exit) (struct _jack_engine *);
  60. /* "private" sections starts here */
  61. /* engine serialization -- use precedence for deadlock avoidance */
  62. pthread_mutex_t request_lock; /* precedes client_lock */
  63. pthread_rwlock_t client_lock;
  64. pthread_mutex_t port_lock;
  65. pthread_mutex_t problem_lock; /* must hold write lock on client_lock */
  66. int process_errors;
  67. int period_msecs;
  68. /* Time to wait for clients in msecs. Used when jackd is run
  69. * without realtime priority enabled. */
  70. int client_timeout_msecs;
  71. /* info on the shm segment containing this->control */
  72. jack_shm_info_t control_shm;
  73. /* address-space local port buffer and segment info,
  74. indexed by the port type_id
  75. */
  76. jack_port_buffer_list_t port_buffers[JACK_MAX_PORT_TYPES];
  77. jack_shm_info_t port_segment[JACK_MAX_PORT_TYPES];
  78. unsigned int port_max;
  79. pthread_t server_thread;
  80. pthread_t watchdog_thread;
  81. int fds[2];
  82. int cleanup_fifo[2];
  83. jack_client_id_t next_client_id;
  84. size_t pfd_size;
  85. size_t pfd_max;
  86. struct pollfd *pfd;
  87. char fifo_prefix[PATH_MAX+1];
  88. int *fifo;
  89. unsigned long fifo_size;
  90. unsigned long external_client_cnt;
  91. int rtpriority;
  92. char freewheeling;
  93. char verbose;
  94. char do_munlock;
  95. const char *server_name;
  96. char temporary;
  97. int reordered;
  98. int watchdog_check;
  99. int feedbackcount;
  100. int removing_clients;
  101. pid_t wait_pid;
  102. pthread_t freewheel_thread;
  103. int nozombies;
  104. volatile int problems;
  105. /* these lists are protected by `client_lock' */
  106. JSList *clients;
  107. JSList *clients_waiting;
  108. jack_port_internal_t *internal_ports;
  109. jack_client_internal_t *timebase_client;
  110. jack_port_buffer_info_t *silent_buffer;
  111. jack_client_internal_t *current_client;
  112. #define JACK_ENGINE_ROLLING_COUNT 32
  113. #define JACK_ENGINE_ROLLING_INTERVAL 1024
  114. jack_time_t rolling_client_usecs[JACK_ENGINE_ROLLING_COUNT];
  115. int rolling_client_usecs_cnt;
  116. int rolling_client_usecs_index;
  117. int rolling_interval;
  118. float max_usecs;
  119. float spare_usecs;
  120. int first_wakeup;
  121. #ifdef JACK_USE_MACH_THREADS
  122. /* specific resources for server/client real-time thread communication */
  123. mach_port_t servertask, bp;
  124. int portnum;
  125. #endif
  126. /* used for port names munging */
  127. int audio_out_cnt;
  128. int audio_in_cnt;
  129. int midi_out_cnt;
  130. int midi_in_cnt;
  131. };
  132. /* public functions */
  133. jack_engine_t *jack_engine_new (int real_time, int real_time_priority,
  134. int do_mlock, int do_unlock,
  135. const char *server_name, int temporary,
  136. int verbose, int client_timeout,
  137. unsigned int port_max,
  138. pid_t waitpid, jack_nframes_t frame_time_offset, int nozombies,
  139. JSList *drivers);
  140. void jack_engine_delete (jack_engine_t *);
  141. int jack_run (jack_engine_t *engine);
  142. int jack_wait (jack_engine_t *engine);
  143. int jack_engine_load_driver (jack_engine_t *engine,
  144. jack_driver_desc_t * driver_desc,
  145. JSList * driver_params);
  146. void jack_dump_configuration(jack_engine_t *engine, int take_lock);
  147. /* private engine functions */
  148. void jack_engine_reset_rolling_usecs (jack_engine_t *engine);
  149. int internal_client_request (void* ptr, jack_request_t *request);
  150. int jack_get_fifo_fd (jack_engine_t *engine,
  151. unsigned int which_fifo);
  152. extern jack_timer_type_t clock_source;
  153. extern jack_client_internal_t *
  154. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  155. #define jack_rdlock_graph(e) { DEBUG ("acquiring graph read lock"); if (pthread_rwlock_rdlock (&e->client_lock)) abort(); }
  156. #define jack_lock_graph(e) { DEBUG ("acquiring graph write lock"); if (pthread_rwlock_wrlock (&e->client_lock)) abort(); }
  157. #define jack_try_rdlock_graph(e) pthread_rwlock_tryrdlock (&e->client_lock)
  158. #define jack_unlock_graph(e) { DEBUG ("release graph lock"); if (pthread_rwlock_unlock (&e->client_lock)) abort(); }
  159. #define jack_trylock_problems(e) pthread_mutex_trylock (&e->problem_lock)
  160. #define jack_lock_problems(e) { DEBUG ("acquiring problem lock"); if (pthread_mutex_lock (&e->problem_lock)) abort(); }
  161. #define jack_unlock_problems(e) { DEBUG ("release problem lock"); if (pthread_mutex_unlock (&e->problem_lock)) abort(); }
  162. #if 0
  163. static inline void jack_rdlock_graph (jack_engine_t* engine) {
  164. DEBUG ("acquiring graph read lock");
  165. pthread_rwlock_rdlock (&engine->client_lock);
  166. }
  167. static inline void jack_lock_graph (jack_engine_t* engine) {
  168. DEBUG ("acquiring graph lock");
  169. pthread_rwlock_wrlock (&engine->client_lock);
  170. }
  171. static inline int jack_try_rdlock_graph (jack_engine_t *engine)
  172. {
  173. DEBUG ("TRYING to acquiring graph read lock");
  174. return pthread_rwlock_tryrdlock (&engine->client_lock);
  175. }
  176. static inline void jack_unlock_graph (jack_engine_t* engine)
  177. {
  178. DEBUG ("releasing graph lock");
  179. pthread_rwlock_unlock (&engine->client_lock);
  180. }
  181. #endif
  182. static inline unsigned int jack_power_of_two (unsigned int n)
  183. {
  184. return !(n & (n - 1));
  185. }
  186. /* Internal port handling interfaces for JACK engine. */
  187. void jack_port_clear_connections (jack_engine_t *engine,
  188. jack_port_internal_t *port);
  189. void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  190. void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  191. void jack_sort_graph (jack_engine_t *engine);
  192. #endif /* __jack_engine_h__ */