]> code.delx.au - pulseaudio/blob - src/pulsecore/core.c
rework memory block management to be thread-safe and mostly lock-free.
[pulseaudio] / src / pulsecore / core.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <signal.h>
30
31 #include <pulse/timeval.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/module.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/autoload.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/props.h>
43 #include <pulsecore/random.h>
44 #include <pulsecore/log.h>
45
46 #include "core.h"
47
48 pa_core* pa_core_new(pa_mainloop_api *m, int shared) {
49 pa_core* c;
50 pa_mempool *pool;
51
52 if (shared) {
53 if (!(pool = pa_mempool_new(shared))) {
54 pa_log_warn("failed to allocate shared memory pool. Falling back to a normal memory pool.");
55 shared = 0;
56 }
57 }
58
59 if (!shared) {
60 if (!(pool = pa_mempool_new(shared))) {
61 pa_log("pa_mempool_new() failed.");
62 return NULL;
63 }
64 }
65
66 c = pa_xnew(pa_core, 1);
67
68 c->mainloop = m;
69 c->clients = pa_idxset_new(NULL, NULL);
70 c->sinks = pa_idxset_new(NULL, NULL);
71 c->sources = pa_idxset_new(NULL, NULL);
72 c->source_outputs = pa_idxset_new(NULL, NULL);
73 c->sink_inputs = pa_idxset_new(NULL, NULL);
74
75 c->default_source_name = c->default_sink_name = NULL;
76
77 c->modules = NULL;
78 c->namereg = NULL;
79 c->scache = NULL;
80 c->autoload_idxset = NULL;
81 c->autoload_hashmap = NULL;
82 c->running_as_daemon = 0;
83
84 c->default_sample_spec.format = PA_SAMPLE_S16NE;
85 c->default_sample_spec.rate = 44100;
86 c->default_sample_spec.channels = 2;
87
88 c->module_auto_unload_event = NULL;
89 c->module_defer_unload_event = NULL;
90 c->scache_auto_unload_event = NULL;
91
92 c->subscription_defer_event = NULL;
93 PA_LLIST_HEAD_INIT(pa_subscription, c->subscriptions);
94 PA_LLIST_HEAD_INIT(pa_subscription_event, c->subscription_event_queue);
95 c->subscription_event_last = NULL;
96
97 c->mempool = pool;
98
99 c->disallow_module_loading = 0;
100
101 c->quit_event = NULL;
102
103 c->exit_idle_time = -1;
104 c->module_idle_time = 20;
105 c->scache_idle_time = 20;
106
107 c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
108
109 c->is_system_instance = 0;
110
111 pa_hook_init(&c->hook_sink_input_new, c);
112 pa_hook_init(&c->hook_sink_disconnect, c);
113 pa_hook_init(&c->hook_source_output_new, c);
114 pa_hook_init(&c->hook_source_disconnect, c);
115
116 pa_property_init(c);
117
118 pa_random(&c->cookie, sizeof(c->cookie));
119
120 #ifdef SIGPIPE
121 pa_check_signal_is_blocked(SIGPIPE);
122 #endif
123 return c;
124 }
125
126 void pa_core_free(pa_core *c) {
127 assert(c);
128
129 pa_module_unload_all(c);
130 assert(!c->modules);
131
132 assert(pa_idxset_isempty(c->clients));
133 pa_idxset_free(c->clients, NULL, NULL);
134
135 assert(pa_idxset_isempty(c->sinks));
136 pa_idxset_free(c->sinks, NULL, NULL);
137
138 assert(pa_idxset_isempty(c->sources));
139 pa_idxset_free(c->sources, NULL, NULL);
140
141 assert(pa_idxset_isempty(c->source_outputs));
142 pa_idxset_free(c->source_outputs, NULL, NULL);
143
144 assert(pa_idxset_isempty(c->sink_inputs));
145 pa_idxset_free(c->sink_inputs, NULL, NULL);
146
147 pa_scache_free(c);
148 pa_namereg_free(c);
149 pa_autoload_free(c);
150 pa_subscription_free_all(c);
151
152 if (c->quit_event)
153 c->mainloop->time_free(c->quit_event);
154
155 pa_xfree(c->default_source_name);
156 pa_xfree(c->default_sink_name);
157
158 pa_mempool_free(c->mempool);
159
160 pa_property_cleanup(c);
161
162 pa_hook_free(&c->hook_sink_input_new);
163 pa_hook_free(&c->hook_sink_disconnect);
164 pa_hook_free(&c->hook_source_output_new);
165 pa_hook_free(&c->hook_source_disconnect);
166
167 pa_xfree(c);
168 }
169
170 static void quit_callback(pa_mainloop_api*m, pa_time_event *e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
171 pa_core *c = userdata;
172 assert(c->quit_event = e);
173
174 m->quit(m, 0);
175 }
176
177 void pa_core_check_quit(pa_core *c) {
178 assert(c);
179
180 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_size(c->clients) == 0) {
181 struct timeval tv;
182 pa_gettimeofday(&tv);
183 tv.tv_sec+= c->exit_idle_time;
184 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
185 } else if (c->quit_event && pa_idxset_size(c->clients) > 0) {
186 c->mainloop->time_free(c->quit_event);
187 c->quit_event = NULL;
188 }
189 }
190