TreeFrogFramework  1.18
tfcore.h
Go to the documentation of this file.
1 #ifndef TFCORE_H
2 #define TFCORE_H
3 
4 #include <QtGlobal>
5 #include <cstdio>
6 #include <cerrno>
7 #ifdef Q_OS_WIN
8 # include <windows.h>
9 # include <io.h>
10 # include <winbase.h>
11 #else
12 # include <unistd.h>
13 # include <fcntl.h>
14 # include <sys/file.h>
15 # include <sys/socket.h>
16 #endif
17 
18 #define TF_EINTR_LOOP(func) \
19  int ret; \
20  do { \
21  errno = 0; \
22  ret = (func); \
23  } while (ret == -1 && errno == EINTR); \
24  return ret;
25 
26 
27 static inline int tf_close(int fd)
28 {
29 #ifdef Q_OS_WIN
30  return ::_close(fd);
31 #else
32  TF_EINTR_LOOP(::close(fd));
33 #endif
34 }
35 
36 
37 static inline int tf_read(int fd, void *buf, size_t count)
38 {
39 #ifdef Q_OS_WIN
40  return ::_read(fd, buf, (uint)count);
41 #else
42  TF_EINTR_LOOP(::read(fd, buf, count));
43 #endif
44 }
45 
46 
47 static inline int tf_write(int fd, const void *buf, size_t count)
48 {
49 #ifdef Q_OS_WIN
50  return ::_write(fd, buf, (uint)count);
51 #else
52  TF_EINTR_LOOP(::write(fd, buf, count));
53 #endif
54 }
55 
56 
57 static inline int tf_send(int sockfd, const void *buf, size_t len, int flags)
58 {
59 #ifdef Q_OS_WIN
60  Q_ASSERT(0);
61  Q_UNUSED(sockfd);
62  Q_UNUSED(buf);
63  Q_UNUSED(len);
64  Q_UNUSED(flags);
65  return 0;
66 #else
67  TF_EINTR_LOOP(::send(sockfd, buf, len, flags));
68 #endif
69 }
70 
71 
72 static inline int tf_recv(int sockfd, void *buf, size_t len, int flags)
73 {
74 #ifdef Q_OS_WIN
75  Q_ASSERT(0);
76  Q_UNUSED(sockfd);
77  Q_UNUSED(buf);
78  Q_UNUSED(len);
79  Q_UNUSED(flags);
80  return 0;
81 #else
82  TF_EINTR_LOOP(::recv(sockfd, buf, len, flags));
83 #endif
84 }
85 
86 
87 static inline int tf_dup(int fd)
88 {
89 #ifdef Q_OS_WIN
90  return ::_dup(fd);
91 #else
92  return ::fcntl(fd, F_DUPFD, 0);
93 #endif
94 }
95 
96 
97 static inline int tf_flock(int fd, int op)
98 {
99 #ifdef Q_OS_WIN
100  Q_UNUSED(fd);
101  Q_UNUSED(op);
102  return 0;
103 #else
104  TF_EINTR_LOOP(::flock(fd, op));
105 #endif
106 }
107 
108 // advisory lock. exclusive:true=exclusive lock, false=shared lock
109 static inline int tf_lockfile(int fd, bool exclusive, bool blocking)
110 {
111 #ifdef Q_OS_WIN
112  auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
113  DWORD dwFlags = (exclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
114  dwFlags |= (blocking) ? 0 : LOCKFILE_FAIL_IMMEDIATELY;
115  OVERLAPPED ov;
116  memset(&ov, 0, sizeof(OVERLAPPED));
117  BOOL res = LockFileEx(handle, dwFlags, 0, 0, 0, &ov);
118  return (res) ? 0 : -1;
119 #else
120  struct flock lck;
121 
122  memset(&lck, 0, sizeof(struct flock));
123  lck.l_type = (exclusive) ? F_WRLCK : F_RDLCK;
124  lck.l_whence = SEEK_SET;
125  auto cmd = (blocking) ? F_SETLKW : F_SETLK;
126  TF_EINTR_LOOP(::fcntl(fd, cmd, &lck));
127 #endif
128 }
129 
130 
131 static inline int tf_unlink(const char *pathname)
132 {
133 #ifdef Q_OS_WIN
134  return ::_unlink(pathname);
135 #else
136  return ::unlink(pathname);
137 #endif
138 }
139 
140 
141 static inline int tf_fileno(FILE *stream)
142 {
143 #ifdef Q_OS_WIN
144  return ::_fileno(stream);
145 #else
146  return ::fileno(stream);
147 #endif
148 }
149 
150 
151 #ifdef Q_OS_UNIX
152 # include "tfcore_unix.h"
153 #endif
154 
155 #endif // TFCORE_H
#define TF_EINTR_LOOP(func)
Definition: tfcore.h:18