1 /* -*- mode: c; c-file-style: "bsd"; -*- */
3 Copyright (C) 2001-2003 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef __jack_engine_h__
22 #define __jack_engine_h__
24 #include <jack/jack.h>
25 #include <jack/internal.h>
26 #include <jack/driver_interface.h>
29 struct _jack_client_internal
;
30 struct _jack_port_internal
;
32 /* Structures is allocated by the engine in local memory to keep track
33 * of port buffers and connections.
36 jack_shm_info_t
* shm_info
;
37 jack_shmsize_t offset
;
38 } jack_port_buffer_info_t
;
40 /* The engine keeps an array of these in its local memory. */
41 typedef struct _jack_port_internal
{
42 struct _jack_port_shared
*shared
;
44 jack_port_buffer_info_t
*buffer_info
;
45 } jack_port_internal_t
;
47 /* The engine's internal port type structure. */
48 typedef struct _jack_port_buffer_list
{
49 pthread_mutex_t lock
; /* only lock within server */
50 JSList
*freelist
; /* list of free buffers */
51 jack_port_buffer_info_t
*info
; /* jack_buffer_info_t array */
52 } jack_port_buffer_list_t
;
54 #define JACKD_WATCHDOG_TIMEOUT 10000
55 #define JACKD_CLIENT_EVENT_TIMEOUT 2000
57 /* The main engine structure in local memory. */
59 jack_control_t
*control
;
62 struct _jack_driver
*driver
;
63 jack_driver_desc_t
*driver_desc
;
64 JSList
*driver_params
;
66 /* these are "callbacks" made by the driver backend */
67 int (*set_buffer_size
) (struct _jack_engine
*, jack_nframes_t frames
);
68 int (*set_sample_rate
) (struct _jack_engine
*, jack_nframes_t frames
);
69 int (*run_cycle
) (struct _jack_engine
*, jack_nframes_t nframes
,
71 void (*delay
) (struct _jack_engine
*, float delayed_usecs
);
72 void (*transport_cycle_start
) (struct _jack_engine
*, jack_time_t time
);
73 void (*driver_exit
) (struct _jack_engine
*);
75 /* "private" sections starts here */
77 /* engine serialization -- use precedence for deadlock avoidance */
78 pthread_mutex_t request_lock
; /* precedes client_lock */
79 pthread_rwlock_t client_lock
;
80 pthread_mutex_t port_lock
;
81 pthread_mutex_t problem_lock
; /* must hold write lock on client_lock */
85 /* Time to wait for clients in msecs. Used when jackd is run
86 * without realtime priority enabled. */
87 int client_timeout_msecs
;
89 /* info on the shm segment containing this->control */
91 jack_shm_info_t control_shm
;
93 /* address-space local port buffer and segment info,
94 indexed by the port type_id
96 jack_port_buffer_list_t port_buffers
[JACK_MAX_PORT_TYPES
];
97 jack_shm_info_t port_segment
[JACK_MAX_PORT_TYPES
];
99 unsigned int port_max
;
100 pthread_t server_thread
;
101 pthread_t watchdog_thread
;
105 jack_client_id_t next_client_id
;
109 char fifo_prefix
[PATH_MAX
+1];
111 unsigned long fifo_size
;
112 unsigned long external_client_cnt
;
117 const char *server_name
;
122 int removing_clients
;
124 pthread_t freewheel_thread
;
126 volatile int problems
;
128 /* these lists are protected by `client_lock' */
130 JSList
*clients_waiting
;
132 jack_port_internal_t
*internal_ports
;
133 jack_client_internal_t
*timebase_client
;
134 jack_port_buffer_info_t
*silent_buffer
;
135 jack_client_internal_t
*current_client
;
137 #define JACK_ENGINE_ROLLING_COUNT 32
138 #define JACK_ENGINE_ROLLING_INTERVAL 1024
140 jack_time_t rolling_client_usecs
[JACK_ENGINE_ROLLING_COUNT
];
141 int rolling_client_usecs_cnt
;
142 int rolling_client_usecs_index
;
143 int rolling_interval
;
149 #ifdef JACK_USE_MACH_THREADS
150 /* specific resources for server/client real-time thread communication */
151 mach_port_t servertask
, bp
;
155 /* used for port names munging */
162 /* public functions */
164 jack_engine_t
*jack_engine_new (int real_time
, int real_time_priority
,
165 int do_mlock
, int do_unlock
,
166 const char *server_name
, int temporary
,
167 int verbose
, int client_timeout
,
168 unsigned int port_max
,
169 pid_t waitpid
, jack_nframes_t frame_time_offset
, int nozombies
,
171 void jack_engine_delete (jack_engine_t
*);
172 int jack_run (jack_engine_t
*engine
);
173 int jack_wait (jack_engine_t
*engine
);
174 int jack_engine_load_driver (jack_engine_t
*engine
,
175 jack_driver_desc_t
* driver_desc
,
176 JSList
* driver_params
);
177 void jack_dump_configuration(jack_engine_t
*engine
, int take_lock
);
179 /* private engine functions */
180 void jack_engine_reset_rolling_usecs (jack_engine_t
*engine
);
181 int internal_client_request (void* ptr
, jack_request_t
*request
);
182 int jack_get_fifo_fd (jack_engine_t
*engine
,
183 unsigned int which_fifo
);
185 extern jack_timer_type_t clock_source
;
187 extern jack_client_internal_t
*
188 jack_client_internal_by_id (jack_engine_t
*engine
, jack_client_id_t id
);
190 #define jack_rdlock_graph(e) { DEBUG ("acquiring graph read lock"); if (pthread_rwlock_rdlock (&e->client_lock)) abort(); }
191 #define jack_lock_graph(e) { DEBUG ("acquiring graph write lock"); if (pthread_rwlock_wrlock (&e->client_lock)) abort(); }
192 #define jack_try_rdlock_graph(e) pthread_rwlock_tryrdlock (&e->client_lock)
193 #define jack_unlock_graph(e) { DEBUG ("release graph lock"); if (pthread_rwlock_unlock (&e->client_lock)) abort(); }
195 #define jack_trylock_problems(e) pthread_mutex_trylock (&e->problem_lock)
196 #define jack_lock_problems(e) { DEBUG ("acquiring problem lock"); if (pthread_mutex_lock (&e->problem_lock)) abort(); }
197 #define jack_unlock_problems(e) { DEBUG ("release problem lock"); if (pthread_mutex_unlock (&e->problem_lock)) abort(); }
200 static inline void jack_rdlock_graph (jack_engine_t
* engine
) {
201 DEBUG ("acquiring graph read lock");
202 pthread_rwlock_rdlock (&engine
->client_lock
);
205 static inline void jack_lock_graph (jack_engine_t
* engine
) {
206 DEBUG ("acquiring graph lock");
207 pthread_rwlock_wrlock (&engine
->client_lock
);
210 static inline int jack_try_rdlock_graph (jack_engine_t
*engine
)
212 DEBUG ("TRYING to acquiring graph read lock");
213 return pthread_rwlock_tryrdlock (&engine
->client_lock
);
216 static inline void jack_unlock_graph (jack_engine_t
* engine
)
218 DEBUG ("releasing graph lock");
219 pthread_rwlock_unlock (&engine
->client_lock
);
223 static inline unsigned int jack_power_of_two (unsigned int n
)
225 return !(n
& (n
- 1));
228 /* Internal port handling interfaces for JACK engine. */
229 void jack_port_clear_connections (jack_engine_t
*engine
,
230 jack_port_internal_t
*port
);
231 void jack_port_registration_notify (jack_engine_t
*, jack_port_id_t
, int);
232 void jack_port_release (jack_engine_t
*engine
, jack_port_internal_t
*);
233 void jack_sort_graph (jack_engine_t
*engine
);
235 #endif /* __jack_engine_h__ */