Skip to content

Commit

Permalink
boardd: decrease ref at end of function and remove callbacks from buf…
Browse files Browse the repository at this point in the history
…ferevent.
  • Loading branch information
robertabcd committed Mar 24, 2015
1 parent 889f58d commit 2134819
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions daemon/boardd/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ class LineProcessJob

static ThreadPool<LineProcessJob> *g_threadpool;

static void read_cb(struct bufferevent *bev, void *ctx)
void read_cb(struct bufferevent *bev, void *ctx)
{
reinterpret_cast<Session *>(ctx)->on_read();
}

static void event_cb(struct bufferevent *bev, short events, void *ctx)
void event_cb(struct bufferevent *bev, short events, void *ctx)
{
reinterpret_cast<Session *>(ctx)->on_error(events);
}

void blackhole_read_cb(bufferevent *bev, void *ctx)
{
evbuffer *buf = bufferevent_get_input(bev);
evbuffer_drain(buf, evbuffer_get_length(buf));
}

} // namespace

void session_init()
Expand Down Expand Up @@ -82,7 +88,7 @@ Session::Session(LineFunc process_line,
bev_ = bufferevent_socket_new(
base, fd,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_THREADSAFE);
bufferevent_setcb(bev_, read_cb, NULL, event_cb, this);
bufferevent_setcb(bev_, read_cb, nullptr, event_cb, this);
bufferevent_set_timeouts(bev_, common_timeout, common_timeout);
bufferevent_enable(bev_, EV_READ|EV_WRITE);
add_ref();
Expand Down Expand Up @@ -117,7 +123,7 @@ void Session::on_read()
if (!line)
return;

job = new LineProcessJob(this, NULL, line);
job = new LineProcessJob(this, nullptr, line);
#ifdef BOARDD_MT
// One request at a time.
bufferevent_disable(bev_, EV_READ);
Expand Down Expand Up @@ -147,12 +153,18 @@ void Session::process_line(void *ctx, char *line)

void Session::shutdown()
{
std::lock_guard<std::mutex> _(mutex_);
if (bev_) {
bufferevent_free(bev_);
bev_ = nullptr;
dec_ref();
bool detached = false;
{
std::lock_guard<std::mutex> _(mutex_);
if (bev_) {
bufferevent_setcb(bev_, blackhole_read_cb, nullptr, nullptr, nullptr);
bufferevent_free(bev_);
bev_ = nullptr;
detached = true;
}
}
if (detached)
dec_ref();
}

void Session::send_and_resume(evbuffer *buf)
Expand Down

0 comments on commit 2134819

Please sign in to comment.