File: ap_signal.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (353 lines) | stat: -rw-r--r-- 9,071 bytes parent folder | download | duplicates (2)
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
/*******************************************************************************
*                         Goggles Audio Player Library                         *
********************************************************************************
*           Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved      *
*                               ---                                            *
* This program is free software: you can redistribute it and/or modify         *
* it under the terms of the GNU General Public License as published by         *
* the Free Software Foundation, either version 3 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 General Public License for more details.                                 *
*                                                                              *
* You should have received a copy of the GNU General Public License            *
* along with this program.  If not, see http://www.gnu.org/licenses.           *
********************************************************************************/
#include "ap_defs.h"
#include "ap_signal.h"
#include "ap_utils.h"

/// On Linux we want to use pipe2
#if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
  #define HAVE_PIPE2
  #ifndef _GNU_SOURCE
    #define _GNU_SOURCE
  #endif
  #include <fcntl.h>
#endif

#ifndef _WIN32
  #include <signal.h>
  #include <poll.h>
  #include <unistd.h>
  #include <errno.h>
#endif


#ifdef HAVE_EVENTFD
  #include <sys/eventfd.h>
#endif

#ifdef _WIN32
  #include <windows.h>
#endif


namespace ap {


#if !defined(_WIN32) && !defined(HAVE_EVENTFD)
static FXbool create_pipe(FXInputHandle & rh,FXInputHandle & wh) {
  FXInputHandle h[2];
#ifdef HAVE_PIPE2
  if (pipe2(h,O_CLOEXEC)==0) {

    // Set the read end non-blocking
    if (!ap_set_nonblocking(h[0])){
      ::close(h[0]);
      ::close(h[1]);
      return false;
      }

    rh=h[0];
    wh=h[1];
    return true;
    }

  // In case of EINVAL (invalid flags) try again using regular pipe api
  if (errno!=EINVAL)
    return false;
#endif

  /// Create Pipe
  if (pipe(h)==-1)
    return false;

  /// Set the close on exec. flag.
  if (!ap_set_closeonexec(h[0]) || !ap_set_closeonexec(h[1])) {
    ::close(h[0]);
    ::close(h[1]);
    return false;
    }

  /// Set the read end non-blocking
  if (!ap_set_nonblocking(h[0])){
    ::close(h[0]);
    ::close(h[1]);
    return false;
    }
  rh=h[0];
  wh=h[1];
  return true;
}

#endif



#if !defined(_WIN32) && !defined(HAVE_EVENTFD)
Signal::Signal() : wrptr(BadHandle), device(BadHandle) {}
#else
Signal::Signal() : device(BadHandle) {}
#endif

FXbool Signal::create() {
#if defined(_WIN32)
  device=CreateEvent(nullptr,true,false,nullptr);
  if (device==BadHandle) return false;
#elif defined(HAVE_EVENTFD)
  device=eventfd(0,EFD_CLOEXEC|EFD_NONBLOCK);
  if (device==BadHandle) return false;
#else
  if (!create_pipe(device,wrptr)) return false;
#endif
  return true;
  }

void Signal::set() {
#if defined(_WIN32)
  SetEvent(device);
#elif defined(HAVE_EVENTFD)
  const FXlong value=1;
  if (__unlikely(write(device,&value,sizeof(FXlong))!=sizeof(FXlong) && errno!=EAGAIN))
    fxerror("gap: failed to set signal, write to eventfd failed");
#else
  const FXuchar value=1;
  if (__unlikely(write(wrptr,&value,sizeof(FXuchar))!=sizeof(FXuchar) && errno!=EAGAIN))
    fxerror("gap: failed to set signal, write to pipe failed");
#endif
  }

void Signal::clear() {
#if defined(_WIN32)
  ResetEvent(device);
#elif defined(HAVE_EVENTFD)
  FXlong value;
  if (__unlikely(read(device,&value,sizeof(FXlong))!=sizeof(FXlong) && errno!=EAGAIN))
    fxerror("gap: failed to clear signal, read from eventfd failed");
#else
  FXuchar value[16];
  while(read(device,value,16)>0);
#endif
  }

void Signal::close() {
#if defined(_WIN32)
  if(device!=BadHandle) CloseHandle(device);
#elif defined(HAVE_EVENTFD)
  if(device!=BadHandle) ::close(device);
#else
  if(device!=BadHandle) ::close(device);
  if(wrptr!=BadHandle) ::close(wrptr);
#endif
  }

void Signal::wait() {
#if defined(_WIN32)
  WaitForSingleObject(device,INFINITE);
#else
  FXint n;
  struct pollfd fds;
  fds.fd     = device;
  fds.events = POLLIN;
  do {
    n = poll(&fds,1,-1);
    }
  while(n==-1 && (errno==EAGAIN || errno==EINTR));
#endif
  }


WaitEvent Signal::wait(FXInputHandle input,WaitMode mode,FXTime timeout/*=0*/) const{
#ifdef _WIN32
  HANDLE handles[2]={input,device}
  DWORD result=WaitForMultipleObjects(2,handles,false,(timeout>0) ? (timeout / NANOSECONDS_PER_MILLISECOND) : INFINITE);
  if (__likely(result>=WAIT_OBJECT_0)) {
    if (WaitForSingleObject(device,0)==WAIT_OBJECT_0)
      return WaitEvent::Signal;
    else
      return WaitEvent::Input;
    }
  else if (result==WAIT_TIMEOUT) {
    return WaitEvent::Timeout;
    }
  return WaitEvent::Error;
#else
  int n;
  struct pollfd handles[2];
  handles[0].fd=input;
  handles[0].events = (mode==WaitMode::Read) ? POLLIN : POLLOUT;
  handles[1].fd=device;
  handles[1].events = POLLIN;
#ifdef HAVE_PPOL
  struct timespec ts;
  if (timeout) {
    ts.tv_sec  = timeout / NANOSECONDS_PER_SECOND;
    ts.tv_nsec = timeout % NANOSECONDS_PER_SECOND;
    }
x:n=ppoll(handles,2,timeout ? &ts : nullptr,nullptr);
  if (__unlikely(n<0)) {
    if (errno==EAGAIN || errno==EINTR)
      goto x;
    return WaitEvent::Error;
    }
#else
x:n=poll(handles,2,timeout ? (timeout/NANOSECONDS_PER_MILLISECOND) : -1);
  if (__unlikely(n<0)) {
    if (errno==EAGAIN || errno==EINTR)
      goto x;
    return WaitEvent::Error;
    }
#endif
  if(__likely(n>0)) {
    if (handles[1].revents)
      return WaitEvent::Signal;
    else
      return WaitEvent::Input;
    }
  return WaitEvent::Timeout;
#endif
  }



//---------------------------------------------------



#if !defined(_WIN32) && !defined(HAVE_EVENTFD)
Semaphore::Semaphore() : wrptr(BadHandle), device(BadHandle) {}
#else
Semaphore::Semaphore() : device(BadHandle) {}
#endif


FXbool Semaphore::create(FXint count) {
#if defined(_WIN32)
  device=CreateSemaphore(nullptr,count,count,nullptr);
  if (device==BadHandle) return false;
#elif defined(HAVE_EVENTFD)
  device=eventfd(count,EFD_SEMAPHORE|EFD_CLOEXEC|EFD_NONBLOCK);
  if (device==BadHandle) return false;
#else
  if (!create_pipe(device,wrptr)) return false;
  while(count--) release();
#endif
  return true;
  }

void Semaphore::release() {
#if defined(_WIN32)
  ReleaseSemaphore(device,1,nullptr);
#elif defined(HAVE_EVENTFD)
  const FXlong value=1;
  if (__unlikely(write(device,&value,sizeof(FXlong))!=sizeof(FXlong) && errno!=EAGAIN))
    fxerror("gap: failed to release semaphore, write to eventfd failed");
#else
  const FXuchar value=1;
  if (__unlikely(write(wrptr,&value,sizeof(FXuchar))!=sizeof(FXuchar) && errno!=EAGAIN))
    fxerror("gap: failed to release semaphore, write to pipe failed");
#endif
  }


FXbool Semaphore::wait(const Signal & input) {
#if defined(_WIN32)
  HANDLE handles[2]={device,input.handle()}
  DWORD result=WaitForMultipleObjects(2,devices,false,INFINITE);
  if(result==WAIT_OBJECT_0) {
    if (WaitForSingleObject(device,0)==WAIT_OBJECT_0){
      release();
      return false;
      }
    return true;
    }
  return false;
#else

  /// Maybe read first before calling poll??

  struct pollfd fds[2];
  FXint n;
  fds[0].fd     = device;
  fds[0].events = POLLIN;
  fds[1].fd     = input.handle();
  fds[1].events = POLLIN;
  do {
    n = poll(fds,2,-1);
    }
  while(n==-1 && (errno==EAGAIN || errno==EINTR));
  if (n>0) {
    if (fds[1].revents)
      return false;

    if (fds[0].revents) {
#ifdef HAVE_EVENTFD
      FXlong value;
      if (read(device,&value,sizeof(FXlong))==sizeof(FXlong)){
        return true;
        }
#else
      FXuchar value;
      if (read(device,&value,sizeof(FXuchar))==sizeof(FXuchar)){
        return true;
        }
#endif
      // This shouldn't happen in a single consumer version
      FXASSERT(0);
      fxwarning("Fatal in Semaphore::wait");
      }
    }
  return false;
#endif
  }



void Semaphore::close() {
#if defined(_WIN32)
  if(device!=BadHandle) CloseHandle(device);
#elif defined(HAVE_EVENTFD)
  if(device!=BadHandle) ::close(device);
#else
  if(device!=BadHandle) ::close(device);
  if(wrptr!=BadHandle) ::close(wrptr);
#endif
  }

/*
FXbool Semaphore::trywait() {
#if defined(_WIN32)
  return WaitForSingleObject(device,0)==WAIT_OBJECT_0;
#elif defined(HAVE_EVENTFD)
  FXlong value;
  if (read(device,&value,sizeof(FXlong))==sizeof(FXlong)){
    return true;
    }
  else {
    return false;
    }
#else
  FXuchar value;
  if (read(device,&value,sizeof(FXuchar))==sizeof(FXuchar))
    return true;
  else
    return false;
#endif
  }
*/

}