1 /* -*- mode: c; c-file-style: "bsd"; -*- */
3 Internal shared data and functions.
5 If you edit this file, you should carefully consider changing the
6 JACK_PROTOCOL_VERSION in configure.in.
8 Copyright (C) 2001-2003 Paul Davis
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #ifndef __jack_internal_h__
27 #define __jack_internal_h__
34 #include <sys/types.h>
37 #ifndef POST_PACKED_STRUCTURE
39 /* POST_PACKED_STRUCTURE needs to be a macro which
40 expands into a compiler directive. The directive must
41 tell the compiler to arrange the preceding structure
42 declaration so that it is packed on byte-boundaries rather
43 than use the natural alignment of the processor and/or
46 #define POST_PACKED_STRUCTURE __attribute__((__packed__))
48 /* Add other things here for non-gcc platforms */
52 /* Needed by <sysdeps/time.h> */
53 extern void jack_error (const char *fmt
, ...);
55 extern void jack_info (const char *fmt
, ...);
57 #include <jack/jack.h>
58 #include <jack/types.h>
59 #include <jack/port.h>
60 #include <jack/transport.h>
63 JACK_TIMER_SYSTEM_CLOCK
,
64 JACK_TIMER_CYCLE_COUNTER
,
68 void jack_init_time ();
69 void jack_set_clock_source (jack_timer_type_t
);
70 const char* jack_clock_source_name (jack_timer_type_t
);
72 #include <sysdeps/time.h>
73 #include <sysdeps/atomicity.h>
75 #ifdef JACK_USE_MACH_THREADS
76 #include <sysdeps/mach_port.h>
79 #include <jack/messagebuffer.h>
83 /* grab thread id instead of PID on linux */
84 #if defined(__gnu_linux__)
85 #ifdef gettid /* glibc has a version */
86 #define GETTID() gettid()
87 #else /* use our own version */
88 #include <sys/syscall.h>
89 #define GETTID() syscall(__NR_gettid)
92 #define GETTID() getpid()
95 #define DEBUG(format,args...) \
96 MESSAGE("jack:%5d:%" PRIu64 " %s:%s:%d: " format "", GETTID(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
99 #if JACK_CPP_VARARGS_BROKEN
100 #define DEBUG(format...)
102 #define DEBUG(format,args...)
106 /* Enable preemption checking for Linux Realtime Preemption kernels.
108 * This checks if any RT-safe code section does anything to cause CPU
109 * preemption. Examples are sleep() or other system calls that block.
110 * If a problem is detected, the kernel writes a syslog entry, and
111 * sends SIGUSR2 to the client.
113 #ifdef DO_PREEMPTION_CHECKING
114 #define CHECK_PREEMPTION(engine, onoff) \
115 if ((engine)->real_time) gettimeofday (1, (onoff))
117 #define CHECK_PREEMPTION(engine, onoff)
128 typedef struct _jack_engine jack_engine_t
;
129 typedef struct _jack_request jack_request_t
;
131 typedef void * dlhandle
;
134 TransportCommandNone
= 0,
135 TransportCommandStart
= 1,
136 TransportCommandStop
= 2,
137 } transport_command_t
;
141 volatile uint32_t guard1
;
142 volatile jack_nframes_t frames
;
143 volatile jack_time_t current_wakeup
;
144 volatile jack_time_t next_wakeup
;
145 volatile float second_order_integrator
;
146 volatile int32_t initialized
;
147 volatile uint32_t guard2
;
149 /* not accessed by clients */
151 int32_t reset_pending
; /* xrun happened, deal with it */
152 float filter_coefficient
; /* set once, never altered */
154 } POST_PACKED_STRUCTURE jack_frame_timer_t
;
156 /* JACK engine shared memory data structure. */
159 jack_transport_state_t transport_state
;
160 volatile transport_command_t transport_cmd
;
161 transport_command_t previous_cmd
; /* previous transport_cmd */
162 jack_position_t current_time
; /* position for current cycle */
163 jack_position_t pending_time
; /* position for next cycle */
164 jack_position_t request_time
; /* latest requested position */
165 jack_unique_t prev_request
; /* previous request unique ID */
166 volatile _Atomic_word seq_number
; /* unique ID sequence number */
167 int8_t new_pos
; /* new position this cycle */
168 int8_t pending_pos
; /* new position request pending */
169 jack_nframes_t pending_frame
; /* pending frame number */
170 int32_t sync_clients
; /* number of active_slowsync clients */
171 int32_t sync_remain
; /* number of them with sync_poll */
172 jack_time_t sync_timeout
;
173 jack_time_t sync_time_left
;
174 jack_frame_timer_t frame_timer
;
176 jack_timer_type_t clock_source
;
178 jack_nframes_t buffer_size
;
182 int32_t client_priority
;
183 int32_t has_capabilities
;
185 float xrun_delayed_usecs
;
186 float max_delayed_usecs
;
189 jack_port_type_id_t n_port_types
;
190 jack_port_type_info_t port_types
[JACK_MAX_PORT_TYPES
];
191 jack_port_shared_t ports
[0];
193 } POST_PACKED_STRUCTURE jack_control_t
;
215 char name
[JACK_CLIENT_NAME_SIZE
];
216 jack_port_id_t port_id
;
217 jack_port_id_t self_id
;
221 jack_port_type_id_t ptid
;
222 jack_port_id_t other_id
;
224 } POST_PACKED_STRUCTURE jack_event_t
;
227 ClientInternal
, /* connect request just names .so */
228 ClientDriver
, /* code is loaded along with driver */
229 ClientExternal
/* client is in another process */
237 } jack_client_state_t
;
239 /* JACK client shared memory data structure. */
240 typedef volatile struct {
242 volatile jack_client_id_t id
; /* w: engine r: engine and client */
243 volatile jack_nframes_t nframes
; /* w: engine r: client */
244 volatile jack_client_state_t state
; /* w: engine and client r: engine */
245 volatile char name
[JACK_CLIENT_NAME_SIZE
];
246 volatile ClientType type
; /* w: engine r: engine and client */
247 volatile int8_t active
; /* w: engine r: engine and client */
248 volatile int8_t dead
; /* r/w: engine */
249 volatile int8_t timed_out
; /* r/w: engine */
250 volatile int8_t is_timebase
; /* w: engine, r: engine and client */
251 volatile int8_t timebase_new
; /* w: engine and client, r: engine */
252 volatile int8_t is_slowsync
; /* w: engine, r: engine and client */
253 volatile int8_t active_slowsync
; /* w: engine, r: engine and client */
254 volatile int8_t sync_poll
; /* w: engine and client, r: engine */
255 volatile int8_t sync_new
; /* w: engine and client, r: engine */
256 volatile pid_t pid
; /* w: client r: engine; client pid */
257 volatile pid_t pgrp
; /* w: client r: engine; client pgrp */
258 volatile uint64_t signalled_at
;
259 volatile uint64_t awake_at
;
260 volatile uint64_t finished_at
;
261 volatile int32_t last_status
; /* w: client, r: engine and client */
263 /* indicators for whether callbacks have been set for this client.
264 We do not include ptrs to the callbacks here (or their arguments)
265 so that we can avoid 32/64 bit pointer size mismatches between
266 the jack server and a client. The pointers are in the client-
267 local structure which is part of the libjack compiled for
268 either 32 bit or 64 bit clients.
270 volatile uint8_t process_cbset
;
271 volatile uint8_t thread_init_cbset
;
272 volatile uint8_t bufsize_cbset
;
273 volatile uint8_t srate_cbset
;
274 volatile uint8_t port_register_cbset
;
275 volatile uint8_t port_connect_cbset
;
276 volatile uint8_t graph_order_cbset
;
277 volatile uint8_t xrun_cbset
;
278 volatile uint8_t sync_cb_cbset
;
279 volatile uint8_t timebase_cb_cbset
;
280 volatile uint8_t freewheel_cb_cbset
;
281 volatile uint8_t client_register_cbset
;
282 volatile uint8_t thread_cb_cbset
;
284 } POST_PACKED_STRUCTURE jack_client_control_t
;
288 uint32_t protocol_v
; /* protocol version, must go first */
291 jack_options_t options
;
293 char name
[JACK_CLIENT_NAME_SIZE
];
294 char object_path
[PATH_MAX
+1];
295 char object_data
[1024];
297 } POST_PACKED_STRUCTURE jack_client_connect_request_t
;
301 jack_status_t status
;
303 jack_shm_registry_index_t client_shm_index
;
304 jack_shm_registry_index_t engine_shm_index
;
306 char fifo_prefix
[PATH_MAX
+1];
309 int32_t realtime_priority
;
311 char name
[JACK_CLIENT_NAME_SIZE
]; /* unique name, if assigned */
313 /* these two are valid only for internal clients, and thus
314 are exempt from the requirement that we not export
315 pointers back to clients. an internal client must
316 necessarily match the host, so 32/64 bit issues
317 do not apply to these pointers.
319 jack_client_control_t
* client_control
;
320 jack_control_t
* engine_control
;
322 #ifdef JACK_USE_MACH_THREADS
323 /* specific resources for server/client real-time thread communication */
327 } POST_PACKED_STRUCTURE jack_client_connect_result_t
;
330 jack_client_id_t client_id
;
331 } POST_PACKED_STRUCTURE jack_client_connect_ack_request_t
;
335 } POST_PACKED_STRUCTURE jack_client_connect_ack_result_t
;
342 SetTimeBaseClient
= 5,
344 DeactivateClient
= 7,
346 SetClientCapabilities
= 9,
347 GetPortConnections
= 10,
348 GetPortNConnections
= 11,
349 ResetTimeBaseClient
= 12,
351 ResetSyncClient
= 14,
356 IntClientHandle
= 19,
359 IntClientUnload
= 22,
360 RecomputeTotalLatencies
= 23,
361 RecomputeTotalLatency
= 24
364 struct _jack_request
{
370 char name
[JACK_PORT_NAME_SIZE
];
371 char type
[JACK_PORT_TYPE_SIZE
];
373 jack_shmsize_t buffer_size
;
374 jack_port_id_t port_id
;
375 jack_client_id_t client_id
;
376 } POST_PACKED_STRUCTURE port_info
;
378 char source_port
[JACK_PORT_NAME_SIZE
];
379 char destination_port
[JACK_PORT_NAME_SIZE
];
380 } POST_PACKED_STRUCTURE connect
;
383 const char **ports
; /* this is only exposed to internal clients, so there
384 is no 64/32 issue. external clients read the ports
385 one by one from the server, and allocate their
386 own "ports" array in their own address space.
388 } POST_PACKED_STRUCTURE port_connections
;
390 jack_client_id_t client_id
;
392 } POST_PACKED_STRUCTURE timebase
;
394 //jack_options_t options;
397 char name
[JACK_CLIENT_NAME_SIZE
];
398 char path
[PATH_MAX
+1];
399 char init
[JACK_LOAD_INIT_LIMIT
];
400 } POST_PACKED_STRUCTURE intclient
;
401 jack_client_id_t client_id
;
402 jack_nframes_t nframes
;
405 } POST_PACKED_STRUCTURE x
;
407 } POST_PACKED_STRUCTURE
;
409 /* Per-client structure allocated in the server's address space.
410 * It's here because its not part of the engine structure.
413 typedef struct _jack_client_internal
{
415 jack_client_control_t
*control
;
419 int subgraph_start_fd
;
420 int subgraph_wait_fd
;
421 JSList
*ports
; /* protected by engine->client_lock */
422 JSList
*truefeeds
; /* protected by engine->client_lock */
423 JSList
*sortfeeds
; /* protected by engine->client_lock */
426 jack_shm_info_t control_shm
;
427 unsigned long execution_order
;
428 struct _jack_client_internal
*next_client
; /* not a linked list! */
430 int (*initialize
)(jack_client_t
*, const char*); /* int. clients only */
431 void (*finish
)(void *); /* internal clients only */
434 #ifdef JACK_USE_MACH_THREADS
435 /* specific resources for server/client real-time thread communication */
436 mach_port_t serverport
;
437 trivial_message message
;
440 #endif /* JACK_USE_MACH_THREADS */
445 JackProcessCallback process
;
447 JackThreadInitCallback thread_init
;
448 void *thread_init_arg
;
449 JackBufferSizeCallback bufsize
;
451 JackSampleRateCallback srate
;
453 JackPortRegistrationCallback port_register
;
454 void *port_register_arg
;
455 JackPortConnectCallback port_connect
;
456 void *port_connect_arg
;
457 JackGraphOrderCallback graph_order
;
458 void *graph_order_arg
;
459 JackXRunCallback xrun
;
461 JackSyncCallback sync_cb
;
463 JackTimebaseCallback timebase_cb
;
465 JackFreewheelCallback freewheel_cb
;
467 JackClientRegistrationCallback client_register
;
468 void *client_register_arg
;
469 JackThreadCallback thread_cb
;
472 /* external clients: set by libjack
473 * internal clients: set by engine */
474 //int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
476 jack_client_t
*private_client
;
477 } jack_client_internal_t
;
479 typedef struct _jack_thread_arg
{
480 jack_client_t
* client
;
481 void* (*work_function
)(void*);
488 extern int jack_client_handle_port_connection (jack_client_t
*client
,
489 jack_event_t
*event
);
490 extern jack_client_t
*jack_driver_client_new (jack_engine_t
*,
491 const char *client_name
);
492 extern jack_client_t
*jack_client_alloc_internal (jack_client_control_t
*,
495 /* internal clients call this. it's defined in jack/engine.c */
496 void handle_internal_client_request (jack_control_t
*, jack_request_t
*);
498 extern char *jack_tmpdir
;
500 extern char *jack_user_dir (void);
502 extern char *jack_server_dir (const char *server_name
, char *server_dir
);
504 extern void *jack_zero_filled_buffer
;
506 extern jack_port_functions_t jack_builtin_audio_functions
;
508 extern jack_port_type_info_t jack_builtin_port_types
[];
510 extern void jack_client_invalidate_port_buffers (jack_client_t
*client
);
512 extern void jack_transport_copy_position (jack_position_t
*from
,
513 jack_position_t
*to
);
514 extern void jack_call_sync_client (jack_client_t
*client
);
516 extern void jack_call_timebase_master (jack_client_t
*client
);
518 extern char *jack_default_server_name (void);
520 void silent_jack_error_callback (const char *desc
);
522 /* needed for port management */
523 jack_port_t
*jack_port_by_id_int (const jack_client_t
*client
,
524 jack_port_id_t id
, int* free
);
526 jack_port_t
*jack_port_by_name_int (jack_client_t
*client
,
527 const char *port_name
);
528 int jack_port_name_equals (jack_port_shared_t
* port
, const char* target
);
531 # define likely(x) __builtin_expect((x),1)
532 # define unlikely(x) __builtin_expect((x),0)
534 # define likely(x) (x)
535 # define unlikely(x) (x)
538 #endif /* __jack_internal_h__ */