gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Futex.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef FUTEX_H_
18 #define FUTEX_H_
19 
20 #include <linux/futex.h>
21 #include <cstddef>
22 #include <limits.h>
23 #include <unistd.h>
24 #include <sys/syscall.h>
25 #include "Debug.H"
26 
29 class Futex {
30  #define DEFAULT_START_VAL 0
31  int f;
32 public:
33  Futex(){
34  f=DEFAULT_START_VAL; // start with default wait value
35  }
36 
40  int wait(){
41  return waitVal(DEFAULT_START_VAL);
42  }
43 
48  int waitVal(int val){
49  // if (__sync_bool_compare_and_swap(&f, 1, 0))
50  // return 0;
51  int ret = syscall(SYS_futex, &f, FUTEX_WAIT, val, NULL, NULL, 0);
52  if (ret<0)
53  return Debug().evaluateError(ret);
54  return ret;
55  }
56 
61  int wake(int howMany){
62  // __sync_bool_compare_and_swap(&f, 0, 1);
63  howMany = syscall(SYS_futex, &f, FUTEX_WAKE, howMany, NULL, NULL, 0);
64  if (howMany<0) // do we have an error ? Force system strerror printing if so ,,,
65  return Debug().evaluateError(howMany);
66  return howMany; // howMany woken up, report back
67  }
68 
72  int wakeAll(){
73  return wake(INT_MAX);
74  }
75 };
76 
77 #endif //FUTEX_H_
int wakeAll()
Definition: Futex.H:72
int f
The futex variable.
Definition: Futex.H:31
Futex()
Definition: Futex.H:33
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
Definition: Futex.H:29
int waitVal(int val)
Definition: Futex.H:48
#define DEFAULT_START_VAL
Definition: Futex.H:30
Definition: Debug.H:112
int wake(int howMany)
Definition: Futex.H:61
int wait()
Definition: Futex.H:40
gtkIOStream: /tmp/gtkiostream/include/Futex.H Source File
GTK+ IOStream  Beta