|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 #include <sys/select.h>
|
|
|
4 #include <sys/time.h>
|
|
|
5 #include <set>
|
|
|
6
|
|
|
7 namespace pfc {
|
|
|
8
|
|
|
9 void nixFormatError( string_base & str, int code );
|
|
|
10
|
|
|
11 class exception_nix : public std::exception {
|
|
|
12 public:
|
|
|
13 exception_nix();
|
|
|
14 exception_nix(int code);
|
|
|
15
|
|
|
16 ~exception_nix() throw() { }
|
|
|
17
|
|
|
18 int code() const throw() {return m_code;}
|
|
|
19 const char * what() const throw() {return m_msg.get_ptr();}
|
|
|
20 private:
|
|
|
21 void _init(int code);
|
|
|
22 int m_code;
|
|
|
23 string8 m_msg;
|
|
|
24 };
|
|
|
25
|
|
|
26 // Toggles non-blocking mode on a file descriptor.
|
|
|
27 void setNonBlocking( int fd, bool bNonBlocking = true );
|
|
|
28
|
|
|
29 // Toggles close-on-exec mode on a file descriptor.
|
|
|
30 void setCloseOnExec( int fd, bool bCloseOnExec = true );
|
|
|
31
|
|
|
32 // Toggles inheritable mode on a file descriptor. Reverse of close-on-exec.
|
|
|
33 void setInheritable( int fd, bool bInheritable = true );
|
|
|
34
|
|
|
35 // Creates a pipe. The pipe is NOT inheritable by default (close-on-exec set).
|
|
|
36 void createPipe( int fd[2], bool bInheritable = false );
|
|
|
37
|
|
|
38 timeval makeTimeVal( double seconds );
|
|
|
39 double importTimeval(const timeval & tv);
|
|
|
40
|
|
|
41 class fdSet {
|
|
|
42 public:
|
|
|
43
|
|
|
44 void operator+=( int fd );
|
|
|
45 void operator-=( int fd );
|
|
|
46 bool operator[] (int fd );
|
|
|
47 void clear();
|
|
|
48
|
|
|
49 void operator+=( fdSet const & other );
|
|
|
50
|
|
|
51 std::set<int> m_fds;
|
|
|
52 };
|
|
|
53
|
|
|
54
|
|
|
55 bool fdCanRead( int fd );
|
|
|
56 bool fdCanWrite( int fd );
|
|
|
57
|
|
|
58 bool fdWaitRead( int fd, double timeOutSeconds );
|
|
|
59 bool fdWaitWrite( int fd, double timeOutSeconds );
|
|
|
60
|
|
|
61 class fdSelect {
|
|
|
62 public:
|
|
|
63
|
|
|
64 int Select();
|
|
|
65 int Select( double timeOutSeconds );
|
|
|
66 int Select_( int timeOutMS );
|
|
|
67
|
|
|
68 fdSet Reads, Writes, Errors;
|
|
|
69 };
|
|
|
70
|
|
|
71 void nixSleep(double seconds);
|
|
|
72 void sleepSeconds(double seconds);
|
|
|
73 void yield();
|
|
|
74
|
|
|
75 typedef int eventHandle_t;
|
|
|
76 static constexpr eventHandle_t eventInvalid = -1;
|
|
|
77
|
|
|
78 class nix_event {
|
|
|
79 public:
|
|
|
80 nix_event(bool state = false);
|
|
|
81 ~nix_event();
|
|
|
82
|
|
|
83 void set_state( bool state );
|
|
|
84
|
|
|
85 bool is_set( );
|
|
|
86
|
|
|
87 void wait_and_clear() { wait(); set_state(false); }
|
|
|
88 void wait() { wait_for(-1); }
|
|
|
89 bool wait_for( double p_timeout_seconds );
|
|
|
90
|
|
|
91 static bool g_wait_for( int p_event, double p_timeout_seconds );
|
|
|
92
|
|
|
93 eventHandle_t get_handle() const {return m_fd[0]; }
|
|
|
94
|
|
|
95 // Two-wait event functions, return 0 on timeout, 1 on evt1 set, 2 on evt2 set
|
|
|
96 static int g_twoEventWait( nix_event & ev1, nix_event & ev2, double timeout );
|
|
|
97 static int g_twoEventWait( int h1, int h2, double timeOut );
|
|
|
98
|
|
|
99 // Multi-wait. Returns SIZE_MAX on timeout, 0 based event index if either event becomes set.
|
|
|
100 static size_t g_multiWait( const eventHandle_t * events, size_t count, double timeout );
|
|
|
101 static size_t g_multiWait(std::initializer_list<eventHandle_t> const & arg, double timeout);
|
|
|
102
|
|
|
103 private:
|
|
|
104 nix_event(nix_event const&) = delete;
|
|
|
105 void operator=(nix_event const&) = delete;
|
|
|
106 int m_fd[2];
|
|
|
107 };
|
|
|
108
|
|
|
109 typedef nix_event event;
|
|
|
110
|
|
|
111 double nixGetTime();
|
|
|
112 typedef uint64_t tickcount_t;
|
|
|
113 tickcount_t getTickCount();
|
|
|
114
|
|
|
115 bool nixReadSymLink( string_base & strOut, const char * path );
|
|
|
116 bool nixSelfProcessPath( string_base & strOut );
|
|
|
117
|
|
|
118 void nixGetRandomData( void * outPtr, size_t outBytes );
|
|
|
119
|
|
|
120 bool isShiftKeyPressed();
|
|
|
121 bool isCtrlKeyPressed();
|
|
|
122 bool isAltKeyPressed();
|
|
|
123
|
|
|
124 #ifdef __APPLE__
|
|
|
125 bool appleRecycleFile( const char * path );
|
|
|
126 void appleSetThreadDescription( const char * str );
|
|
|
127 #endif
|
|
|
128 }
|
|
|
129
|
|
|
130 void uSleepSeconds( double seconds, bool );
|