# Path: tornado/platform/interface.py
# def set_close_exec(fd):
#     def fileno(self):
#     def write_fileno(self):
#     def wake(self):
#     def consume(self):
#     def close(self):
# class Waker(object):
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


import fcntl
import os
from tornado.platform import interface
from tornado.util import b


def set_close_exec(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)


def _set_nonblocking(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)


class Waker(interface.Waker):
    def __init__(self):
        r, w = os.pipe()
        _set_nonblocking(r)
        _set_nonblocking(w)
        set_close_exec(r)
        set_close_exec(w)
        self.reader = os.fdopen(r, "rb", 0)
        self.writer = os.fdopen(w, "wb", 0)

    def fileno(self):
        return self.reader.fileno()

    def write_fileno(self):
        return self.writer.fileno()

    def wake(self):
        try:

# Path: tornado/platform/interface.py
# def set_close_exec(fd):
#     def fileno(self):
#     def write_fileno(self):
#     def wake(self):
#     def consume(self):
#     def close(self):
# class Waker(object):
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


# Path: tornado/platform/posix.py
import fcntl
import os
from tornado.platform import interface
from tornado.util import b



def set_close_exec(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)


def _set_nonblocking(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)


class Waker(interface.Waker):
    def __init__(self):
        r, w = os.pipe()
        _set_nonblocking(r)
        _set_nonblocking(w)
        set_close_exec(r)
        set_close_exec(w)
        self.reader = os.fdopen(r, "rb", 0)
        self.writer = os.fdopen(w, "wb", 0)

    def fileno(self):
        return self.reader.fileno()

    def write_fileno(self):
        return self.writer.fileno()

    def wake(self):
        try:

            self.writer.write(b("x"))