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
|
/*
Copyright (C) 2004-2008 Grame
Copyright (C) 2016 Filipe Coelho
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 "JackLinuxFutex.h"
#include "JackTools.h"
#include "JackConstants.h"
#include "JackError.h"
#include "promiscuous.h"
#include <climits>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <syscall.h>
#include <linux/futex.h>
#if !defined(SYS_futex) && defined(SYS_futex_time64)
#define SYS_futex SYS_futex_time64
#endif
namespace Jack
{
JackLinuxFutex::JackLinuxFutex() : JackSynchro(), fSharedMem(-1), fFutex(NULL), fPrivate(false)
{
const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
fPromiscuous = (promiscuous != NULL);
fPromiscuousGid = jack_group2gid(promiscuous);
}
void JackLinuxFutex::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);
if (fPromiscuous) {
snprintf(res, size, "jack_sem.%s_%s", server_name, ext_client_name);
} else {
snprintf(res, size, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
}
}
bool JackLinuxFutex::Signal()
{
if (!fFutex) {
jack_error("JackLinuxFutex::Signal name = %s already deallocated!!", fName);
return false;
}
if (fFlush) {
return true;
}
if (! __sync_bool_compare_and_swap(&fFutex->futex, 0, 1))
{
// already unlocked, do not wake futex
if (! fFutex->internal) return true;
}
::syscall(SYS_futex, fFutex, fFutex->internal ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1, NULL, NULL, 0);
return true;
}
bool JackLinuxFutex::SignalAll()
{
return Signal();
}
bool JackLinuxFutex::Wait()
{
if (!fFutex) {
jack_error("JackLinuxFutex::Wait name = %s already deallocated!!", fName);
return false;
}
if (fFutex->needsChange)
{
fFutex->needsChange = false;
fFutex->internal = !fFutex->internal;
}
const int wait_mode = fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT;
for (;;)
{
if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
return true;
if (::syscall(SYS_futex, fFutex, wait_mode, 0, NULL, NULL, 0) != 0)
if (errno != EAGAIN && errno != EINTR)
return false;
}
}
bool JackLinuxFutex::TimedWait(long usec)
{
if (usec == LONG_MAX)
return Wait();
if (!fFutex) {
jack_error("JackLinuxFutex::TimedWait name = %s already deallocated!!", fName);
return false;
}
if (fFutex->needsChange)
{
fFutex->needsChange = false;
fFutex->internal = !fFutex->internal;
}
const uint secs = usec / 1000000;
const int nsecs = (usec % 1000000) * 1000;
const timespec timeout = { static_cast<time_t>(secs), nsecs };
const int wait_mode = fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT;
for (;;)
{
if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
return true;
if (::syscall(SYS_futex, fFutex, wait_mode, 0, &timeout, NULL, 0) != 0)
if (errno != EAGAIN && errno != EINTR)
return false;
}
}
// Server side : publish the futex in the global namespace
bool JackLinuxFutex::Allocate(const char* name, const char* server_name, int value, bool internal)
{
BuildName(name, server_name, fName, sizeof(fName));
jack_log("JackLinuxFutex::Allocate name = %s val = %ld", fName, value);
if ((fSharedMem = shm_open(fName, O_CREAT | O_RDWR, 0777)) < 0) {
jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
return false;
}
if (ftruncate(fSharedMem, sizeof(FutexData)) != 0) {
jack_error("Allocate: can't set shared memory size in named futex name = %s err = %s", fName, strerror(errno));
return false;
}
if (fPromiscuous && (jack_promiscuous_perms(fSharedMem, fName, fPromiscuousGid) < 0)) {
close(fSharedMem);
fSharedMem = -1;
shm_unlink(fName);
return false;
}
FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
if (futex == NULL || futex == MAP_FAILED) {
jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
close(fSharedMem);
fSharedMem = -1;
shm_unlink(fName);
return false;
}
fPrivate = internal;
futex->futex = value;
futex->internal = internal;
futex->wasInternal = internal;
futex->needsChange = false;
futex->externalCount = 0;
fFutex = futex;
return true;
}
// Client side : get the published futex from server
bool JackLinuxFutex::Connect(const char* name, const char* server_name)
{
BuildName(name, server_name, fName, sizeof(fName));
jack_log("JackLinuxFutex::Connect name = %s", fName);
// Temporary...
if (fFutex) {
jack_log("Already connected name = %s", name);
return true;
}
if ((fSharedMem = shm_open(fName, O_RDWR, 0)) < 0) {
jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
return false;
}
FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
if (futex == NULL || futex == MAP_FAILED) {
jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
close(fSharedMem);
fSharedMem = -1;
return false;
}
if (! fPrivate && futex->wasInternal)
{
const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++futex->externalCount == 1)
{
jack_error("Note: client %s running as external client temporarily", fName);
futex->needsChange = true;
}
}
fFutex = futex;
return true;
}
bool JackLinuxFutex::ConnectInput(const char* name, const char* server_name)
{
return Connect(name, server_name);
}
bool JackLinuxFutex::ConnectOutput(const char* name, const char* server_name)
{
return Connect(name, server_name);
}
bool JackLinuxFutex::Disconnect()
{
if (!fFutex) {
return true;
}
if (! fPrivate && fFutex->wasInternal)
{
const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
if (externalSync != NULL && strstr(fName, externalSync) != NULL && --fFutex->externalCount == 0)
{
jack_error("Note: client %s now running as internal client again", fName);
fFutex->needsChange = true;
}
}
munmap(fFutex, sizeof(FutexData));
fFutex = NULL;
close(fSharedMem);
fSharedMem = -1;
return true;
}
// Server side : destroy the futex
void JackLinuxFutex::Destroy()
{
if (!fFutex) {
return;
}
munmap(fFutex, sizeof(FutexData));
fFutex = NULL;
close(fSharedMem);
fSharedMem = -1;
shm_unlink(fName);
}
} // end of namespace
|