// File: cybozu[P]yrmcds/src/server.cpp
// Function: yrmcds::server::serve_master()
void server::serve_master() {
    cybozu::logger::info() << "Entering master mode";

    for( auto& handler: m_handlers )
        handler->on_master_start();

    auto callback = [this](cybozu::reactor&) {
        std::time_t now = std::time(nullptr);
        g_current_time.store(now, std::memory_order_relaxed);

        if( ! m_syncer.empty() )
            m_syncer.check();
        for( auto& handler: m_handlers )
            handler->on_master_interval();

        if( reactor_gc_ready() ) {
            m_reactor.fix_garbage();
            m_syncer.add_request(
                std::unique_ptr<sync_request>(
                    new sync_request([this]{ m_reactor.gc(); })
                    ));
        }
    };

    for( unsigned int i = 0; i < g_config.workers(); ++i )
        m_workers.emplace_back(new cybozu::worker(WORKER_BUFSIZE));
    for( auto& w: m_workers )
        w->start();

    auto stop = [this] {
        m_reactor.invalidate();
        for( auto& w: m_workers )
            w->stop();
        for( auto& handler: m_handlers )
            handler->on_master_end();
    };

    try {
        cybozu::logger::info() << "Reactor thread id="
                               << std::this_thread::get_id();
        m_reactor.run(callback);
        cybozu::logger::info() << "Exiting";
    } catch( ... ) {
        stop();
        throw;
    }

    stop();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x18(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x22390
movq -0xd8(%rbp), %rdi
leaq 0x5a88e(%rip), %rsi     # 0x7aad6
callq 0x22640
jmp 0x2024f
leaq -0x18(%rbp), %rdi
callq 0xa770
movq -0xe0(%rbp), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x22270
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x222a0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x222d0
testb $0x1, %al
jne 0x20296
jmp 0x202da
leaq -0x38(%rbp), %rdi
callq 0x22310
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x22330
movq %rax, %rdi
movq (%rdi), %rax
callq *0x20(%rax)
leaq -0x38(%rbp), %rdi
callq 0x22350
jmp 0x20283
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x18(%rbp), %rdi
callq 0xa770
jmp 0x20589
movq -0xe0(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xe4(%rbp)
leaq 0x8fa04(%rip), %rdi     # 0xafd00
callq 0x22680
movl %eax, %ecx
movl -0xe4(%rbp), %eax
cmpl %ecx, %eax
jae 0x20398
movq -0xe0(%rbp), %rax
addq $0xe0, %rax
movq %rax, -0xf8(%rbp)
movl $0xc0, %edi
movl $0x40, %esi
callq 0x86c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf0(%rbp)
movl $0x500000, %esi         # imm = 0x500000
callq 0x22720
jmp 0x2034d
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rsi
callq 0x226a0
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x202ec
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x40, %esi
callq 0x8870
jmp 0x20589
movq -0xe0(%rbp), %rax
addq $0xe0, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x22800
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x22830
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x22860
testb $0x1, %al
jne 0x203d6
jmp 0x203ff
leaq -0x70(%rbp), %rdi
callq 0x228a0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x228c0
movq %rax, %rdi
callq 0x228e0
leaq -0x70(%rbp), %rdi
callq 0x229a0
jmp 0x203c3
movq -0xe0(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x22390
jmp 0x2041b
leaq 0x5a6c9(%rip), %rsi     # 0x7aaeb
leaq -0x98(%rbp), %rdi
callq 0xdf20
movq %rax, -0x100(%rbp)
jmp 0x20437
callq 0x22a10
movq -0x100(%rbp), %rdi
movq %rax, -0xa0(%rbp)
leaq -0xa0(%rbp), %rsi
callq 0x229c0
jmp 0x20458
leaq -0x98(%rbp), %rdi
callq 0xa770
movq -0xe0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x110(%rbp)
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x108(%rbp)
leaq -0x50(%rbp), %rsi
callq 0x20630
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
movl $0x1, %edx
callq 0xeb90
jmp 0x204a7
leaq -0xc0(%rbp), %rdi
callq 0x225e0
leaq -0xd0(%rbp), %rdi
callq 0x22390
jmp 0x204c1
leaq 0x5a636(%rip), %rsi     # 0x7aafe
leaq -0xd0(%rbp), %rdi
callq 0x22a40
jmp 0x204d6
leaq -0xd0(%rbp), %rdi
callq 0xa770
jmp 0x20574
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x20541
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xa770
jmp 0x20541
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x225e0
jmp 0x20541
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xa770
movq -0x20(%rbp), %rdi
callq 0x8230
leaq -0x88(%rbp), %rdi
callq 0x206c0
jmp 0x20558
callq 0x89c0
jmp 0x2059a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x8ae0
jmp 0x20572
jmp 0x20589
leaq -0x88(%rbp), %rdi
callq 0x206c0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x8bc0
movq %rax, %rdi
callq 0xa7e0
nopw (%rax,%rax)