summaryrefslogtreecommitdiff
path: root/macosx/JackMachSemaphore.mm
blob: f2ba3dd7e93e0a32f2fe9b9fd0bd0c6dbb1a5a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
Copyright (C) 2004-2008 Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "JackMachSemaphore.h"
#include "JackMachUtils.h"
#include "JackConstants.h"
#include "JackTools.h"
#include "JackError.h"

#include <mach/message.h>

#define jack_mach_error(kern_result, message) \
        jack_mach_error_uncurried("JackMachSemaphore", kern_result, message)

#define jack_mach_bootstrap_err(kern_result, message, name) \
        jack_mach_bootstrap_err_uncurried("JackMachSemaphore", kern_result, message, name)

namespace Jack
{

void JackMachSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
{
    char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
    JackTools::RewriteName(client_name, ext_client_name);

    // make the name as small as possible, as macos has issues with long semaphore names
    if (strcmp(server_name, "default") == 0)
        server_name = "";

    snprintf(res, std::min(size, 32), "js%d.%s%s", JackTools::GetUID(), server_name, ext_client_name);
}

bool JackMachSemaphore::Signal()
{
    if (fSemaphore == MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::Signal name = %s already deallocated!!", fName);
        return false;
    }

    if (fFlush) {
        return true;
    }

    kern_return_t res;
    if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
        jack_error("JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
    }
    return (res == KERN_SUCCESS);
}

bool JackMachSemaphore::SignalAll()
{
    if (fSemaphore == MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::SignalAll name = %s already deallocated!!", fName);
        return false;
    }

    if (fFlush) {
        return true;
    }

    kern_return_t res;
    // When signaled several times, do not accumulate signals...
    if ((res = semaphore_signal_all(fSemaphore)) != KERN_SUCCESS) {
        jack_error("JackMachSemaphore::SignalAll name = %s err = %s", fName, mach_error_string(res));
    }
    return (res == KERN_SUCCESS);
}

bool JackMachSemaphore::Wait()
{
    if (fSemaphore == MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::Wait name = %s already deallocated!!", fName);
        return false;
    }

    kern_return_t res = semaphore_wait(fSemaphore);

    // killing a thread will abort the semaphore wait
    if (res == KERN_SUCCESS || res == KERN_ABORTED) {
        return true;
    }

    jack_error("JackMachSemaphore::Wait name = %s err = %s", fName, mach_error_string(res));
    return false;
}

bool JackMachSemaphore::TimedWait(long usec)
{
    if (fSemaphore == MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::TimedWait name = %s already deallocated!!", fName);
        return false;
    }

    mach_timespec time;
    time.tv_sec = usec / 1000000;
    time.tv_nsec = (usec % 1000000) * 1000;

    kern_return_t res = semaphore_timedwait(fSemaphore, time);

    // killing a thread will abort the semaphore wait
    if (res == KERN_SUCCESS || res == KERN_ABORTED) {
        return true;
    }

    jack_error("JackMachSemaphore::TimedWait name = %s usec = %ld err = %s", fName, usec, mach_error_string(res));
    return false;
}

/*! \brief Server side: create semaphore and publish IPC primitives to make it accessible.
 *
 * This method;
 * - Allocates a mach semaphore
 * - Allocates a new mach IPC port and obtains a send right for it
 * - Publishes IPC port send right to the bootstrap server
 * - Starts a new JackMachSemaphoreServer thread, which listens for messages on the IPC port and
 * replies with a send right to the mach semaphore.
 *
 * \returns false if any of the above steps fails, or true otherwise.
 */
bool JackMachSemaphore::Allocate(const char* client_name, const char* server_name, int value)
{
    if (fSemaphore != MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::Allocate: Semaphore already allocated; called twice? [%s]", fName);
        return false;
    }

    BuildName(client_name, server_name, fName, sizeof(fName));

    mach_port_t task = mach_task_self();
    kern_return_t res;

    if (fBootPort == MACH_PORT_NULL) {
        if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
            jack_mach_error(res, "can't find bootstrap mach port");
            return false;
        }
    }

    if ((res = semaphore_create(task, &fSemaphore, SYNC_POLICY_FIFO, value)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to create semaphore");
        return false;
    }

    if ((res = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &fServicePort)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to allocate IPC port");

        // Cleanup created semaphore
        this->Destroy();

        return false;
    }

    if ((res = mach_port_insert_right(mach_task_self(), fServicePort, fServicePort, MACH_MSG_TYPE_MAKE_SEND)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to obtain send right for IPC port");

        // Cleanup created semaphore & mach port
        this->Destroy();

        return false;
    }

    if ((res = bootstrap_register(fBootPort, fName, fServicePort)) != KERN_SUCCESS) {
        jack_mach_bootstrap_err(res, "can't register IPC port with bootstrap server", fName);

        // Cleanup created semaphore & mach port
        this->Destroy();

        return false;
    }

    fSemServer = new JackMachSemaphoreServer(fSemaphore, fServicePort, fName);
    fThreadSemServer = new JackMachThread(fSemServer);

    if (fThreadSemServer->Start() < 0) {
        jack_error("JackMachSemaphore::Allocate: failed to start semaphore IPC server thread [%s]", fName);

        // Cleanup created semaphore, mach port (incl. service registration), and server
        this->Destroy();

        return false;
    }

    jack_log("JackMachSemaphore::Allocate: OK, name = %s", fName);
    return true;
}

/*! \brief Client side: Obtain semaphore from server via published IPC port.
 *
 * This method;
 * - Looks up the service port for the jackd semaphore server for this client by name
 * - Sends a message to that server asking for a semaphore port send right
 * - Receives a semaphore send right in return and stores it locally
 *
 * \returns False if any of the above steps fails, or true otherwise.
 */
bool JackMachSemaphore::ConnectInput(const char* client_name, const char* server_name)
{
    BuildName(client_name, server_name, fName, sizeof(fName));

    mach_port_t task = mach_task_self();
    kern_return_t res;

    if (fSemaphore != MACH_PORT_NULL) {
        jack_log("JackMachSemaphore::Connect: Already connected name = %s", fName);
        return true;
    }

    if (fBootPort == MACH_PORT_NULL) {
        if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
            jack_mach_error(res, "can't find bootstrap port");
            return false;
        }
    }

    if ((res = bootstrap_look_up(fBootPort, fName, &fServicePort)) != KERN_SUCCESS) {
        jack_mach_bootstrap_err(res, "can't find IPC service port to request semaphore", fName);
        return false;
    }

    mach_port_t semaphore_req_port;

    if ((res = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &semaphore_req_port)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to allocate request port");

        if ((res = mach_port_deallocate(task, fServicePort)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to deallocate IPC service port during cleanup");
        } else {
            fServicePort = MACH_PORT_NULL;
        }

        return false;
    }

    // Prepare a message buffer on the stack. We'll use it for both sending and receiving a message.
    struct {
        mach_msg_header_t hdr;
        mach_msg_trailer_t trailer;
    } msg;

    /*
     * Configure the message to consume the destination port we give it (_MOVE_SEND), and to
     * transmute the local port receive right we give it into a send_once right at the destination.
     * The server will use that send_once right to reply to us.
     */
    msg.hdr.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
    msg.hdr.msgh_local_port = semaphore_req_port;
    msg.hdr.msgh_remote_port = fServicePort;

    mach_msg_return_t send_err = mach_msg(
        &msg.hdr,
        MACH_SEND_MSG,
        sizeof(msg.hdr), // no trailer on send
        0,
        MACH_PORT_NULL,
        MACH_MSG_TIMEOUT_NONE,
        MACH_PORT_NULL);

    if (send_err != MACH_MSG_SUCCESS) {
        jack_mach_error(send_err, "failed to send semaphore port request IPC");

        if ((res = mach_port_deallocate(task, fServicePort)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to deallocate IPC service port during cleanup");
        } else {
            fServicePort = MACH_PORT_NULL;
        }

        if ((res = mach_port_destroy(task, semaphore_req_port)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to destroy IPC request port during cleanup");
        }

        return false;
    } else {
        fServicePort = MACH_PORT_NULL; // We moved it into the message and away to the destination
    }

    mach_msg_return_t recv_err = mach_msg(
        &msg.hdr,
        MACH_RCV_MSG,
        0,
        sizeof(msg),
        semaphore_req_port,
        MACH_MSG_TIMEOUT_NONE,
        MACH_PORT_NULL
    );

    /* Don't leak ports: irrespective of if we succeeded to read or not, destroy the port we created
     * to send/receive the request as we have no further use for it either way. */
    if ((res = mach_port_destroy(task, semaphore_req_port)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to destroy semaphore_req_port");
        // This isn't good, but doesn't actually stop the semaphore from working... don't bail
    }

    if (recv_err != MACH_MSG_SUCCESS) {
        jack_mach_error(recv_err, "failed to receive semaphore port");
        return false;
    } else {
        fSemaphore = msg.hdr.msgh_remote_port;

        jack_log("JackMachSemaphore::Connect: OK, name = %s", fName);
        return true;
    }
}

bool JackMachSemaphore::Connect(const char* name, const char* server_name)
{
    return ConnectInput(name, server_name);
}

bool JackMachSemaphore::ConnectOutput(const char* name, const char* server_name)
{
    return ConnectInput(name, server_name);
}

bool JackMachSemaphore::Disconnect()
{
    if (fSemaphore == MACH_PORT_NULL) {
        return true;
    }

    mach_port_t task = mach_task_self();
    kern_return_t res;

    jack_log("JackMachSemaphore::Disconnect name = %s", fName);

    if (fServicePort != MACH_PORT_NULL) {
        // If we're still holding onto a service port send right for some reason, deallocate it
        if ((res = mach_port_deallocate(task, fServicePort)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to deallocate stray service port");
            // Continue cleanup even if this fails; don't bail
        } else {
            fServicePort = MACH_PORT_NULL;
        }
    }

    if ((res = mach_port_deallocate(task, fSemaphore)) != KERN_SUCCESS) {
        jack_mach_error(res, "failed to deallocate semaphore port");
        return false;
    } else {
        fSemaphore = MACH_PORT_NULL;
        return true;
    }
}

// Server side : destroy the JackGlobals
void JackMachSemaphore::Destroy()
{
    const mach_port_t task = mach_task_self();
    kern_return_t res;

    if (fSemaphore == MACH_PORT_NULL) {
        jack_error("JackMachSemaphore::Destroy semaphore is MACH_PORT_NULL; already destroyed?");
        return;
    }

    if (fSemServer && fSemServer->Invalidate()) {
        fServicePort = MACH_PORT_NULL;
        fSemaphore = MACH_PORT_NULL;
    }

    if (fThreadSemServer) {
        if (fThreadSemServer->Stop() < 0) {
            jack_error("JackMachSemaphore::Destroy failed to stop semaphore server thread...");
            // Oh dear. How sad. Never mind.
        }

        JackMachThread* thread = fThreadSemServer;
        fThreadSemServer = NULL;
        delete thread;
    }

    if (fSemServer) {
        JackMachSemaphoreServer* server = fSemServer;
        fSemServer = NULL;
        delete server;
    }

    if (fServicePort != MACH_PORT_NULL) {
        if ((res = mach_port_destroy(task, fServicePort)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to destroy IPC port");
        } else {
            fServicePort = MACH_PORT_NULL;
        }
    }

    if (fSemaphore != MACH_PORT_NULL) {
        if ((res = semaphore_destroy(mach_task_self(), fSemaphore)) != KERN_SUCCESS) {
            jack_mach_error(res, "failed to destroy semaphore");
        } else {
            fSemaphore = MACH_PORT_NULL;
        }
    }

    jack_log("JackMachSemaphore::Destroy: OK, name = %s", fName);
}

} // end of namespace