Jack2  1.9.11-RC1
JackLinuxFutex.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3 Copyright (C) 2016 Filipe Coelho
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 */
20 
21 #include "JackLinuxFutex.h"
22 #include "JackTools.h"
23 #include "JackConstants.h"
24 #include "JackError.h"
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <sys/mman.h>
28 #include <syscall.h>
29 #include <linux/futex.h>
30 
31 namespace Jack
32 {
33 
34 void JackLinuxFutex::BuildName(const char* client_name, const char* server_name, char* res, int size)
35 {
36  char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
37  JackTools::RewriteName(client_name, ext_client_name);
38  if (getenv("JACK_PROMISCUOUS_SERVER")) {
39  snprintf(res, size, "jack_sem.%s_%s", server_name, ext_client_name);
40  } else {
41  snprintf(res, size, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
42  }
43 }
44 
45 bool JackLinuxFutex::Signal()
46 {
47  if (!fFutex) {
48  jack_error("JackLinuxFutex::Signal name = %s already deallocated!!", fName);
49  return false;
50  }
51 
52  if (fFlush) {
53  return true;
54  }
55 
56  if (! __sync_bool_compare_and_swap(&fFutex->futex, 0, 1))
57  {
58  // already unlocked, do not wake futex
59  if (! fFutex->internal) return true;
60  }
61 
62  ::syscall(__NR_futex, fFutex, fFutex->internal ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1, NULL, NULL, 0);
63  return true;
64 }
65 
66 bool JackLinuxFutex::SignalAll()
67 {
68  return Signal();
69 }
70 
71 bool JackLinuxFutex::Wait()
72 {
73  if (!fFutex) {
74  jack_error("JackLinuxFutex::Wait name = %s already deallocated!!", fName);
75  return false;
76  }
77 
78  if (fFutex->needsChange)
79  {
80  fFutex->needsChange = false;
81  fFutex->internal = !fFutex->internal;
82  }
83 
84  for (;;)
85  {
86  if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
87  return true;
88 
89  if (::syscall(__NR_futex, fFutex, fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, 0, NULL, NULL, 0) != 0 && errno != EWOULDBLOCK)
90  return false;
91  }
92 }
93 
94 bool JackLinuxFutex::TimedWait(long usec)
95 {
96  if (!fFutex) {
97  jack_error("JackLinuxFutex::TimedWait name = %s already deallocated!!", fName);
98  return false;
99  }
100 
101  if (fFutex->needsChange)
102  {
103  fFutex->needsChange = false;
104  fFutex->internal = !fFutex->internal;
105  }
106 
107  const uint secs = usec / 1000000;
108  const int nsecs = (usec % 1000000) * 1000;
109 
110  const timespec timeout = { static_cast<time_t>(secs), nsecs };
111 
112  for (;;)
113  {
114  if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
115  return true;
116 
117  if (::syscall(__NR_futex, fFutex, fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, 0, &timeout, NULL, 0) != 0 && errno != EWOULDBLOCK)
118  return false;
119  }
120 }
121 
122 // Server side : publish the futex in the global namespace
123 bool JackLinuxFutex::Allocate(const char* name, const char* server_name, int value, bool internal)
124 {
125  BuildName(name, server_name, fName, sizeof(fName));
126  jack_log("JackLinuxFutex::Allocate name = %s val = %ld", fName, value);
127 
128  if ((fSharedMem = shm_open(fName, O_CREAT | O_RDWR, 0777)) < 0) {
129  jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
130  return false;
131  }
132 
133  ftruncate(fSharedMem, sizeof(FutexData));
134 
135  if ((fFutex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0)) == NULL) {
136  jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
137  close(fSharedMem);
138  fSharedMem = -1;
139  shm_unlink(fName);
140  return false;
141  }
142 
143  fPrivate = internal;
144 
145  fFutex->futex = value;
146  fFutex->internal = internal;
147  fFutex->wasInternal = internal;
148  fFutex->needsChange = false;
149  fFutex->externalCount = 0;
150  return true;
151 }
152 
153 // Client side : get the published futex from server
154 bool JackLinuxFutex::Connect(const char* name, const char* server_name)
155 {
156  BuildName(name, server_name, fName, sizeof(fName));
157  jack_log("JackLinuxFutex::Connect name = %s", fName);
158 
159  // Temporary...
160  if (fFutex) {
161  jack_log("Already connected name = %s", name);
162  return true;
163  }
164 
165  if ((fSharedMem = shm_open(fName, O_RDWR, 0)) < 0) {
166  jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
167  return false;
168  }
169 
170  if ((fFutex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0)) == NULL) {
171  jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
172  close(fSharedMem);
173  fSharedMem = -1;
174  return false;
175  }
176 
177  if (! fPrivate && fFutex->wasInternal)
178  {
179  const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
180 
181  if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++fFutex->externalCount == 1)
182  {
183  jack_error("Note: client %s running as external client temporarily", fName);
184  fFutex->needsChange = true;
185  }
186  }
187 
188  return true;
189 }
190 
191 bool JackLinuxFutex::ConnectInput(const char* name, const char* server_name)
192 {
193  return Connect(name, server_name);
194 }
195 
196 bool JackLinuxFutex::ConnectOutput(const char* name, const char* server_name)
197 {
198  return Connect(name, server_name);
199 }
200 
201 bool JackLinuxFutex::Disconnect()
202 {
203  if (!fFutex) {
204  return true;
205  }
206 
207  if (! fPrivate && fFutex->wasInternal)
208  {
209  const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
210 
211  if (externalSync != NULL && strstr(fName, externalSync) != NULL && --fFutex->externalCount == 0)
212  {
213  jack_error("Note: client %s now running as internal client again", fName);
214  fFutex->needsChange = true;
215  }
216  }
217 
218  munmap(fFutex, sizeof(FutexData));
219  fFutex = NULL;
220 
221  close(fSharedMem);
222  fSharedMem = -1;
223  return true;
224 }
225 
226 // Server side : destroy the futex
227 void JackLinuxFutex::Destroy()
228 {
229  if (!fFutex) {
230  return;
231  }
232 
233  munmap(fFutex, sizeof(FutexData));
234  fFutex = NULL;
235 
236  close(fSharedMem);
237  fSharedMem = -1;
238 
239  shm_unlink(fName);
240 }
241 
242 } // end of namespace
243 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108