prepare for rb_thread_blocking_region removal
[kgio.git] / ext / kgio / kgio.h
blob66a87050faa6ecc564a65a619199c141682f9646
1 #ifndef KGIO_H
2 #define KGIO_H
4 #include <ruby.h>
5 #ifdef HAVE_RUBY_IO_H
6 # include <ruby/io.h>
7 #else
8 # include <rubyio.h>
9 #endif
10 #ifdef HAVE_RUBY_THREAD_H
11 # include <ruby/thread.h>
12 #endif
13 #include <errno.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <netinet/in.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <arpa/inet.h>
21 #include <assert.h>
22 #include <netdb.h>
24 #include "ancient_ruby.h"
26 void init_kgio_wait(void);
27 void init_kgio_read(void);
28 void init_kgio_write(void);
29 void init_kgio_writev(void);
30 void init_kgio_accept(void);
31 void init_kgio_connect(void);
32 void init_kgio_autopush(void);
33 void init_kgio_poll(void);
34 void init_kgio_tryopen(void);
36 void kgio_autopush_accept(VALUE, VALUE);
37 void kgio_autopush_recv(VALUE);
38 void kgio_autopush_send(VALUE);
40 VALUE kgio_call_wait_writable(VALUE io);
41 VALUE kgio_call_wait_readable(VALUE io);
42 #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && \
43 !defined(HAVE_RB_THREAD_BLOCKING_REGION)
44 # define KGIO_WITHOUT_GVL(fn,data1,ubf,data2) \
45 rb_thread_call_without_gvl((fn),(data1),(ubf),(data2))
46 #elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
47 typedef VALUE(*kgio_blocking_fn_t)(void*);
48 # define KGIO_WITHOUT_GVL(fn,data1,ubf,data2) \
49 rb_thread_blocking_region((kgio_blocking_fn_t)(fn),(data1),(ubf),(data2))
50 #endif /* HAVE_RB_THREAD_CALL_WITHOUT_GVL || HAVE_RB_THREAD_BLOCKING_REGION */
52 #if defined(KGIO_WITHOUT_GVL) && defined(HAVE_POLL)
53 # define USE_KGIO_POLL
54 #endif /* USE_KGIO_POLL */
56 #ifndef HAVE_RB_UPDATE_MAX_FD
57 # define rb_update_max_fd(fd) for (;0;)
58 #endif
61 * 2012/12/13 - Linux 3.7 was released on 2012/12/10 with TFO.
62 * Headers distributed with glibc will take some time to catch up and
63 * be officially released. Most GNU/Linux distros will take a few months
64 * to a year longer. "Enterprise" distros will probably take 5-7 years.
65 * So keep these until 2017 at least...
67 #ifdef __linux__
68 # ifndef MSG_FASTOPEN
69 # define MSG_FASTOPEN 0x20000000 /* for clients */
70 # endif
71 # ifndef TCP_FASTOPEN
72 # define TCP_FASTOPEN 23 /* for listeners */
73 # endif
74 /* we _may_ have TFO support */
75 # define KGIO_TFO_MAYBE (1)
76 #else /* rely entirely on standard system headers */
77 # define KGIO_TFO_MAYBE (0)
78 #endif
80 extern unsigned kgio_tfo;
81 NORETURN(void kgio_raise_empty_bt(VALUE, const char *));
82 NORETURN(void kgio_wr_sys_fail(const char *));
83 NORETURN(void kgio_rd_sys_fail(const char *));
86 * we know MSG_DONTWAIT works properly on all stream sockets under Linux
87 * we can define this macro for other platforms as people care and
88 * notice.
90 # if defined(__linux__)
91 # define USE_MSG_DONTWAIT
92 # endif
94 #ifdef USE_MSG_DONTWAIT
95 /* we don't need these variants, we call kgio_autopush_send/recv directly */
96 static inline void kgio_autopush_write(VALUE io) { }
97 #else
98 static inline void kgio_autopush_write(VALUE io) { kgio_autopush_send(io); }
99 #endif
101 /* prefer rb_str_subseq because we don't use negative offsets */
102 #ifndef HAVE_RB_STR_SUBSEQ
103 #define rb_str_subseq rb_str_substr
104 #endif
106 #endif /* KGIO_H */