sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob 522ac0a7cd444ef8e5dd8206a8747d6dc9563e4e 2592 bytes (raw)
$ git show v3.4.1:ext/sleepy_penguin/sleepy_penguin.h	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
#ifndef SLEEPY_PENGUIN_H
#define SLEEPY_PENGUIN_H

#include <ruby.h>
#ifdef HAVE_RUBY_IO_H
#  include <ruby/io.h>
#else
#  include <rubyio.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>

extern size_t rb_sp_l1_cache_line_size;
unsigned rb_sp_get_uflags(VALUE klass, VALUE flags);
int rb_sp_get_flags(VALUE klass, VALUE flags, int default_flags);
int rb_sp_io_closed(VALUE io);
int rb_sp_fileno(VALUE io);
void rb_sp_set_nonblock(int fd);

#if defined(HAVE_RB_THREAD_BLOCKING_REGION) || \
    defined(HAVE_RB_THREAD_IO_BLOCKING_REGION) || \
    defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
#  define RB_SP_GREEN_THREAD 0
#  define blocking_io_prepare(fd) ((void)(fd))
#else
#  define RB_SP_GREEN_THREAD 1
#  define blocking_io_prepare(fd) rb_sp_set_nonblock((fd))
#endif

#ifdef HAVE_RB_THREAD_IO_BLOCKING_REGION
/* Ruby 1.9.3 and 2.0.0 */
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *, void *, int);
#  define rb_sp_fd_region(fn,data,fd) \
	rb_thread_io_blocking_region((fn),(data),(fd))
#elif defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && \
	defined(HAVE_RUBY_THREAD_H) && HAVE_RUBY_THREAD_H
/* in case Ruby 2.0+ ever drops rb_thread_io_blocking_region: */
#  include <ruby/thread.h>
#  define COMPAT_FN (void *(*)(void *))
#  define rb_sp_fd_region(fn,data,fd) \
	rb_thread_call_without_gvl(COMPAT_FN(fn),(data),RUBY_UBF_IO,NULL)
#elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
/* Ruby 1.9.2 */
#  define rb_sp_fd_region(fn,data,fd) \
	rb_thread_blocking_region((fn),(data),RUBY_UBF_IO,NULL)
#else
/*
 * Ruby 1.8 does not have a GVL, we'll just enable signal interrupts
 * here in case we make interruptible syscalls.
 *
 * Note: epoll_wait with timeout=0 was interruptible until Linux 2.6.39
 */
#  include <rubysig.h>
static inline VALUE fake_blocking_region(VALUE (*fn)(void *), void *data)
{
	VALUE rv;

	TRAP_BEG;
	rv = fn(data);
	TRAP_END;

	return rv;
}
#  define rb_sp_fd_region(fn,data,fd) fake_blocking_region((fn),(data))
#endif

#define NODOC_CONST(klass,name,value) \
  rb_define_const((klass),(name),(value))

#ifdef HAVE_RB_FD_FIX_CLOEXEC
#  define RB_SP_CLOEXEC(flag) (flag)
#else
#  define RB_SP_CLOEXEC(flag) (0)
#endif

typedef int rb_sp_waitfn(int fd);
int rb_sp_wait(rb_sp_waitfn waiter, VALUE obj, int *fd);
void *rb_sp_gettlsbuf(size_t *size);

/* Flexible array elements are standard in C99 */
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
# define FLEX_ARRAY
#elif defined(__GNUC__)
# if (__GNUC__ >= 3)
#  define FLEX_ARRAY
# else
#  define FLEX_ARRAY 0
# endif
#endif

#endif /* SLEEPY_PENGUIN_H */

git clone https://yhbt.net/sleepy_penguin.git