stop & detach driver when exiting due to -T/--temporary flag
[jack.git] / jack / internal.h
blobeb0e5469a589cb63ca2539af9c947eea342bda4f
1 /* -*- mode: c; c-file-style: "bsd"; -*- */
2 /*
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__
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <pthread.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
37 #ifndef POST_PACKED_STRUCTURE
38 #ifdef __GNUC__
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
44 compiler.
46 #define POST_PACKED_STRUCTURE __attribute__((__packed__))
47 #else
48 /* Add other things here for non-gcc platforms */
49 #endif
50 #endif
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>
62 typedef enum {
63 JACK_TIMER_SYSTEM_CLOCK,
64 JACK_TIMER_CYCLE_COUNTER,
65 JACK_TIMER_HPET,
66 } jack_timer_type_t;
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>
77 #endif
79 #include <jack/messagebuffer.h>
81 #ifdef DEBUG_ENABLED
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)
90 #endif
91 #else
92 #define GETTID() getpid()
93 #endif
95 #define DEBUG(format,args...) \
96 MESSAGE("jack:%5d:%" PRIu64 " %s:%s:%d: " format "", GETTID(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
98 #else
99 #if JACK_CPP_VARARGS_BROKEN
100 #define DEBUG(format...)
101 #else
102 #define DEBUG(format,args...)
103 #endif
104 #endif
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))
116 #else
117 #define CHECK_PREEMPTION(engine, onoff)
118 #endif
120 #ifndef FALSE
121 #define FALSE (0)
122 #endif
124 #ifndef TRUE
125 #define TRUE (1)
126 #endif
128 typedef struct _jack_engine jack_engine_t;
129 typedef struct _jack_request jack_request_t;
131 typedef void * dlhandle;
133 typedef enum {
134 TransportCommandNone = 0,
135 TransportCommandStart = 1,
136 TransportCommandStop = 2,
137 } transport_command_t;
139 typedef struct {
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. */
157 typedef struct {
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;
175 int32_t internal;
176 jack_timer_type_t clock_source;
177 pid_t engine_pid;
178 jack_nframes_t buffer_size;
179 int8_t real_time;
180 int8_t do_mlock;
181 int8_t do_munlock;
182 int32_t client_priority;
183 int32_t max_client_priority;
184 int32_t has_capabilities;
185 float cpu_load;
186 float xrun_delayed_usecs;
187 float max_delayed_usecs;
188 uint32_t port_max;
189 int32_t engine_ok;
190 jack_port_type_id_t n_port_types;
191 jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
192 jack_port_shared_t ports[0];
194 } POST_PACKED_STRUCTURE jack_control_t;
196 typedef enum {
197 BufferSizeChange,
198 SampleRateChange,
199 AttachPortSegment,
200 PortConnected,
201 PortDisconnected,
202 GraphReordered,
203 PortRegistered,
204 PortUnregistered,
205 XRun,
206 StartFreewheel,
207 StopFreewheel,
208 ClientRegistered,
209 ClientUnregistered
210 } JackEventType;
212 typedef struct {
213 JackEventType type;
214 union {
215 uint32_t n;
216 char name[JACK_CLIENT_NAME_SIZE];
217 jack_port_id_t port_id;
218 jack_port_id_t self_id;
219 } x;
220 union {
221 uint32_t n;
222 jack_port_type_id_t ptid;
223 jack_port_id_t other_id;
224 } y;
225 } POST_PACKED_STRUCTURE jack_event_t;
227 typedef enum {
228 ClientInternal, /* connect request just names .so */
229 ClientDriver, /* code is loaded along with driver */
230 ClientExternal /* client is in another process */
231 } ClientType;
233 typedef enum {
234 NotTriggered,
235 Triggered,
236 Running,
237 Finished
238 } jack_client_state_t;
240 /* JACK client shared memory data structure. */
241 typedef volatile struct {
243 volatile jack_client_id_t id; /* w: engine r: engine and client */
244 volatile jack_nframes_t nframes; /* w: engine r: client */
245 volatile jack_client_state_t state; /* w: engine and client r: engine */
246 volatile char name[JACK_CLIENT_NAME_SIZE];
247 volatile ClientType type; /* w: engine r: engine and client */
248 volatile int8_t active; /* w: engine r: engine and client */
249 volatile int8_t dead; /* r/w: engine */
250 volatile int8_t timed_out; /* r/w: engine */
251 volatile int8_t is_timebase; /* w: engine, r: engine and client */
252 volatile int8_t timebase_new; /* w: engine and client, r: engine */
253 volatile int8_t is_slowsync; /* w: engine, r: engine and client */
254 volatile int8_t active_slowsync; /* w: engine, r: engine and client */
255 volatile int8_t sync_poll; /* w: engine and client, r: engine */
256 volatile int8_t sync_new; /* w: engine and client, r: engine */
257 volatile pid_t pid; /* w: client r: engine; client pid */
258 volatile pid_t pgrp; /* w: client r: engine; client pgrp */
259 volatile uint64_t signalled_at;
260 volatile uint64_t awake_at;
261 volatile uint64_t finished_at;
262 volatile int32_t last_status; /* w: client, r: engine and client */
264 /* indicators for whether callbacks have been set for this client.
265 We do not include ptrs to the callbacks here (or their arguments)
266 so that we can avoid 32/64 bit pointer size mismatches between
267 the jack server and a client. The pointers are in the client-
268 local structure which is part of the libjack compiled for
269 either 32 bit or 64 bit clients.
271 volatile uint8_t process_cbset;
272 volatile uint8_t thread_init_cbset;
273 volatile uint8_t bufsize_cbset;
274 volatile uint8_t srate_cbset;
275 volatile uint8_t port_register_cbset;
276 volatile uint8_t port_connect_cbset;
277 volatile uint8_t graph_order_cbset;
278 volatile uint8_t xrun_cbset;
279 volatile uint8_t sync_cb_cbset;
280 volatile uint8_t timebase_cb_cbset;
281 volatile uint8_t freewheel_cb_cbset;
282 volatile uint8_t client_register_cbset;
283 volatile uint8_t thread_cb_cbset;
285 } POST_PACKED_STRUCTURE jack_client_control_t;
287 typedef struct {
289 uint32_t protocol_v; /* protocol version, must go first */
290 int32_t load;
291 ClientType type;
292 jack_options_t options;
294 char name[JACK_CLIENT_NAME_SIZE];
295 char object_path[PATH_MAX+1];
296 char object_data[1024];
298 } POST_PACKED_STRUCTURE jack_client_connect_request_t;
300 typedef struct {
302 jack_status_t status;
304 jack_shm_registry_index_t client_shm_index;
305 jack_shm_registry_index_t engine_shm_index;
307 char fifo_prefix[PATH_MAX+1];
309 int32_t realtime;
310 int32_t realtime_priority;
312 char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
314 /* these are actually pointers, but they must
315 be the same size regardless of whether the
316 server and/or client are 64 bit or 32 bit.
317 force them to be 64 bit.
320 uint64_t client_control;
321 uint64_t engine_control;
323 #ifdef JACK_USE_MACH_THREADS
324 /* specific resources for server/client real-time thread communication */
325 int32_t portnum;
326 #endif
328 } POST_PACKED_STRUCTURE jack_client_connect_result_t;
330 typedef struct {
331 jack_client_id_t client_id;
332 } POST_PACKED_STRUCTURE jack_client_connect_ack_request_t;
334 typedef struct {
335 int8_t status;
336 } POST_PACKED_STRUCTURE jack_client_connect_ack_result_t;
338 typedef enum {
339 RegisterPort = 1,
340 UnRegisterPort = 2,
341 ConnectPorts = 3,
342 DisconnectPorts = 4,
343 SetTimeBaseClient = 5,
344 ActivateClient = 6,
345 DeactivateClient = 7,
346 DisconnectPort = 8,
347 SetClientCapabilities = 9,
348 GetPortConnections = 10,
349 GetPortNConnections = 11,
350 ResetTimeBaseClient = 12,
351 SetSyncClient = 13,
352 ResetSyncClient = 14,
353 SetSyncTimeout = 15,
354 SetBufferSize = 16,
355 FreeWheel = 17,
356 StopFreeWheel = 18,
357 IntClientHandle = 19,
358 IntClientLoad = 20,
359 IntClientName = 21,
360 IntClientUnload = 22,
361 RecomputeTotalLatencies = 23,
362 RecomputeTotalLatency = 24
363 } RequestType;
365 struct _jack_request {
367 //RequestType type;
368 uint32_t type;
369 union {
370 struct {
371 char name[JACK_PORT_NAME_SIZE];
372 char type[JACK_PORT_TYPE_SIZE];
373 uint32_t flags;
374 jack_shmsize_t buffer_size;
375 jack_port_id_t port_id;
376 jack_client_id_t client_id;
377 } POST_PACKED_STRUCTURE port_info;
378 struct {
379 char source_port[JACK_PORT_NAME_SIZE];
380 char destination_port[JACK_PORT_NAME_SIZE];
381 } POST_PACKED_STRUCTURE connect;
382 struct {
383 int32_t nports;
384 const char **ports; /* this is only exposed to internal clients, so there
385 is no 64/32 issue. external clients read the ports
386 one by one from the server, and allocate their
387 own "ports" array in their own address space.
389 we are lucky, because this is part of a union
390 whose other components are bigger than this one.
391 otherwise it would change structure size when
392 comparing the 64 and 32 bit versions.
394 } POST_PACKED_STRUCTURE port_connections;
395 struct {
396 jack_client_id_t client_id;
397 int32_t conditional;
398 } POST_PACKED_STRUCTURE timebase;
399 struct {
400 //jack_options_t options;
401 uint32_t options;
402 jack_client_id_t id;
403 char name[JACK_CLIENT_NAME_SIZE];
404 char path[PATH_MAX+1];
405 char init[JACK_LOAD_INIT_LIMIT];
406 } POST_PACKED_STRUCTURE intclient;
407 jack_client_id_t client_id;
408 jack_nframes_t nframes;
409 jack_time_t timeout;
410 pid_t cap_pid;
411 } POST_PACKED_STRUCTURE x;
412 int32_t status;
413 } POST_PACKED_STRUCTURE;
415 /* Per-client structure allocated in the server's address space.
416 * It's here because its not part of the engine structure.
419 typedef struct _jack_client_internal {
421 jack_client_control_t *control;
423 int request_fd;
424 int event_fd;
425 int subgraph_start_fd;
426 int subgraph_wait_fd;
427 JSList *ports; /* protected by engine->client_lock */
428 JSList *truefeeds; /* protected by engine->client_lock */
429 JSList *sortfeeds; /* protected by engine->client_lock */
430 int fedcount;
431 int tfedcount;
432 jack_shm_info_t control_shm;
433 unsigned long execution_order;
434 struct _jack_client_internal *next_client; /* not a linked list! */
435 dlhandle handle;
436 int (*initialize)(jack_client_t*, const char*); /* int. clients only */
437 void (*finish)(void *); /* internal clients only */
438 int error;
440 #ifdef JACK_USE_MACH_THREADS
441 /* specific resources for server/client real-time thread communication */
442 mach_port_t serverport;
443 trivial_message message;
444 int running;
445 int portnum;
446 #endif /* JACK_USE_MACH_THREADS */
448 /* external clients: set by libjack
449 * internal clients: set by engine */
450 //int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
451 //void *deliver_arg;
452 jack_client_t *private_client;
453 } jack_client_internal_t;
455 typedef struct _jack_thread_arg {
456 jack_client_t* client;
457 void* (*work_function)(void*);
458 int priority;
459 int realtime;
460 void* arg;
461 pid_t cap_pid;
462 } jack_thread_arg_t;
464 extern int jack_client_handle_port_connection (jack_client_t *client,
465 jack_event_t *event);
466 extern jack_client_t *jack_driver_client_new (jack_engine_t *,
467 const char *client_name);
468 extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
469 jack_engine_t*);
471 /* internal clients call this. it's defined in jack/engine.c */
472 void handle_internal_client_request (jack_control_t*, jack_request_t*);
474 extern char *jack_tmpdir;
476 extern char *jack_user_dir (void);
478 extern char *jack_server_dir (const char *server_name, char *server_dir);
480 extern void *jack_zero_filled_buffer;
482 extern jack_port_functions_t jack_builtin_audio_functions;
484 extern jack_port_type_info_t jack_builtin_port_types[];
486 extern void jack_client_invalidate_port_buffers (jack_client_t *client);
488 extern void jack_transport_copy_position (jack_position_t *from,
489 jack_position_t *to);
490 extern void jack_call_sync_client (jack_client_t *client);
492 extern void jack_call_timebase_master (jack_client_t *client);
494 extern char *jack_default_server_name (void);
496 void silent_jack_error_callback (const char *desc);
498 /* needed for port management */
499 jack_port_t *jack_port_by_id_int (const jack_client_t *client,
500 jack_port_id_t id, int* free);
502 jack_port_t *jack_port_by_name_int (jack_client_t *client,
503 const char *port_name);
504 int jack_port_name_equals (jack_port_shared_t* port, const char* target);
506 #ifdef __GNUC__
507 # define likely(x) __builtin_expect((x),1)
508 # define unlikely(x) __builtin_expect((x),0)
509 #else
510 # define likely(x) (x)
511 # define unlikely(x) (x)
512 #endif
514 #endif /* __jack_internal_h__ */