dd46cea102f2a7016b786cb83eeb9bdab2e2b639
[clogger.git] / ext / clogger_ext / blocking_helpers.h
blobdd46cea102f2a7016b786cb83eeb9bdab2e2b639
1 #ifdef HAVE_RB_THREAD_BLOCKING_REGION
2 struct stat_args { const char *path; struct stat *buf; };
3 static VALUE ng_stat(void *ptr)
5 struct stat_args *a = ptr;
6 return (VALUE)stat(a->path, a->buf);
8 static int my_stat(const char *path, struct stat *buf)
10 struct stat_args a;
12 a.path = path;
13 a.buf = buf;
14 return (int)rb_thread_blocking_region(ng_stat, &a, RUBY_UBF_IO, 0);
16 #ifndef HAVE_RB_THREAD_IO_BLOCKING_REGION
17 # define rb_thread_io_blocking_region(fn,data,fd) \
18 rb_thread_blocking_region((fn),(data), RUBY_UBF_IO, 0)
19 #endif
21 struct fstat_args { int fd; struct stat *buf; };
22 static VALUE ng_fstat(void *ptr)
24 struct fstat_args *a = ptr;
25 return (VALUE)fstat(a->fd, a->buf);
28 static int my_fstat(int fd, struct stat *buf)
30 struct fstat_args a;
32 a.fd = fd;
33 a.buf = buf;
34 return (int)rb_thread_io_blocking_region(ng_fstat, &a, fd);
37 struct write_args { int fd; const void *buf; size_t count; };
38 static VALUE ng_write(void *ptr)
40 struct write_args *a = ptr;
41 return (VALUE)write(a->fd, a->buf, a->count);
43 static ssize_t my_write(int fd, const void *buf, size_t count)
45 struct write_args a;
46 ssize_t r;
48 a.fd = fd;
49 a.buf = buf;
50 a.count = count;
51 r = (ssize_t)rb_thread_io_blocking_region(ng_write, &a, fd);
53 return r;
55 # define stat(fd,buf) my_stat((fd),(buf))
56 # define fstat(fd,buf) my_fstat((fd),(buf))
57 # define write(fd,buf,count) my_write((fd),(buf),(count))
58 #endif