Skip to content

Commit

Permalink
Merge branch 'old-linux-timer-fix' of https://github.com/tatsuhiko-in…
Browse files Browse the repository at this point in the history
…oue/jubatus-mpio into tatsuhiko-inoue-old-linux-timer-fix
  • Loading branch information
y_oda committed Mar 31, 2013
2 parents 940d948 + 66c7bda commit 2d3bd2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mpsrc/wavy_kernel_epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,22 @@ class kernel {
#else // DISABLE_TIMERFD
class timer {
public:
timer() : fd(-1) { }
timer() : fd(-1), fd_writer(-1) { }
~timer() {
if(fd >= 0) {
timer_delete(timer_id);
::close(fd);
}
if(fd_writer >= 0) {
::close(fd_writer);
}
}

int ident() const { return fd; }

private:
int fd;
int fd_writer;
timer_t timer_id;
friend class kernel;
timer(const timer&);
Expand Down Expand Up @@ -259,6 +263,7 @@ class kernel {
}

tm->fd = pipefd[0];
tm->fd_writer = pipefd[1];
tm->timer_id = timer_id;

return pipefd[0];
Expand Down
3 changes: 3 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ check_PROGRAMS = \
handler \
signal \
timer \
many_timer \
sync

TESTS = $(check_PROGRAMS)
Expand All @@ -21,5 +22,7 @@ signal_SOURCES = signal.cc

timer_SOURCES = timer.cc

many_timer_SOURCES = many_timer.cc

sync_SOURCES = sync.cc

38 changes: 38 additions & 0 deletions test/many_timer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.

#include <jubatus/mp/wavy.h>
#include <jubatus/mp/functional.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>

using namespace mp::placeholders;

bool timer_handler(int* count, mp::wavy::loop* lo)
{
try {
std::cout << "*" << std::flush;
if(--(*count) < 0) {
lo->end();
}
else {
lo->add_timer(0.001, 0, mp::bind(&timer_handler, count, lo));
}
return false;
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
std::exit(1);
}
}

int main(void)
{
mp::wavy::loop lo;
int count = 2000;
lo.add_timer(0.01, 0, mp::bind(
&timer_handler, &count, &lo));
lo.run(4);
std::cout << std::endl;
}

0 comments on commit 2d3bd2e

Please sign in to comment.