Debian Codegraph @

prov/psm3/inc/ofi_signal.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2011-2015 Intel Corporation. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34 #ifndef _OFI_SIGNAL_H_
35 #define _OFI_SIGNAL_H_
36
37 #include "config.h"
38
39 #include <unistd.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdbool.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45
46 #include <ofi_file.h>
47 #include <ofi_osd.h>
48 #include <ofi_atom.h>
49 #include <rdma/fi_errno.h>
50
51
52 enum {
53 FI_READ_FD,
54 FI_WRITE_FD
55 };
56
57 struct fd_signal {
58 ofi_mutex_t lock;
59 int fd[2];
60 int byte_avail;
61 };
62
63 static inline int fd_signal_init(struct fd_signal *signal)
64 {
65 int ret;
66
67 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, signal->fd);
68 if (ret < 0)
69 return -ofi_sockerr();
70
71 signal->byte_avail = 0;
72 ret = ofi_mutex_init(&signal->lock);
73 if (ret)
74 goto err1;
75
76 /* The read fd is accessed directly by fd_signal users to add
77 * it to epoll fd's and wait sets.
78 */
79 ret = fi_fd_nonblock(signal->fd[FI_READ_FD]);
80 if (ret)
81 goto err2;
82
83 return 0;
84
85 err2:
86 ofi_mutex_destroy(&signal->lock);
87 err1:
88 ofi_close_socket(signal->fd[0]);
89 ofi_close_socket(signal->fd[1]);
90 return ret;
91 }
92
93 static inline void fd_signal_free(struct fd_signal *signal)
94 {
95 ofi_close_socket(signal->fd[0]);
96 ofi_close_socket(signal->fd[1]);
97 ofi_mutex_destroy(&signal->lock);
98 }
99
100 static inline void fd_signal_set(struct fd_signal *signal)
101 {
102 char c = 0;
103 int ret;
104
105 ofi_mutex_lock(&signal->lock);
106 if (!signal->byte_avail) {
107 ret = ofi_write_socket(signal->fd[FI_WRITE_FD], &c, sizeof c);
108 assert(ret == sizeof c);
109 if (ret == sizeof c)
110 signal->byte_avail++;
111 }
112 ofi_mutex_unlock(&signal->lock);
113 }
114
115 static inline int fd_signal_poll(struct fd_signal *signal, int timeout)
116 {
117 int ret;
118
119 ret = fi_poll_fd(signal->fd[FI_READ_FD], timeout);
120 if (ret < 0)
121 return ret;
122
123 return (ret == 0) ? -FI_ETIMEDOUT : 0;
124 }
125
126 /* There's a race where we can write data to the fd and increment byte_avail,
127 * but the kernel won't have the data available for reading from the fd yet.
128 * If the data isn't ready for reading, but has already been written, we'll
129 * wait for it to show up. A timeout is given just so that we don't end up
130 * blocking forever in case something goes terribly astray.
131 */
132 static inline void fd_signal_reset(struct fd_signal *signal)
133 {
134 char c;
135 int ret;
136
137 ofi_mutex_lock(&signal->lock);
138 while (signal->byte_avail) {
139 ret = ofi_read_socket(signal->fd[FI_READ_FD], &c, sizeof c);
140 if (ret == sizeof c) {
141 signal->byte_avail--;
142 continue;
143 }
144 if (!OFI_SOCK_TRY_SND_RCV_AGAIN(ofi_sockerr())) {
145 assert(0);
146 break;
147 }
148
149 /* Give the kernel up to 10 seconds to get the data there. */
150 ret = fd_signal_poll(signal, 10000);
151 if (ret) {
152 assert(0);
153 break;
154 }
155 }
156 ofi_mutex_unlock(&signal->lock);
157 }
158
159 static inline int fd_signal_get(struct fd_signal *signal)
160 {
161 return signal->fd[FI_READ_FD];
162 }
163
164 #endif /* _OFI_SIGNAL_H_ */