Skip to content

Commit

Permalink
Add request_timer API
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 1, 2016
1 parent b6a22e0 commit 4f4429f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/groonga.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "groonga/output.h"
#include "groonga/pat.h"
#include "groonga/request_canceler.h"
#include "groonga/request_timer.h"
#include "groonga/thread.h"
#include "groonga/type.h"
#include "groonga/util.h"
Expand Down
1 change: 1 addition & 0 deletions include/groonga/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ groonga_include_HEADERS = \
plugin.h \
portability.h \
request_canceler.h \
request_timer.h \
scorer.h \
thread.h \
token.h \
Expand Down
28 changes: 28 additions & 0 deletions lib/grn_request_timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2016 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#pragma once

#include "grn.h"

grn_bool grn_request_timer_init(void);
void grn_request_timer_fin(void);

#ifdef __cplusplus
}
#endif
95 changes: 95 additions & 0 deletions lib/request_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2016 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "grn_ctx.h"
#include "grn_request_timer.h"

static grn_ctx grn_request_timer_ctx;
static grn_mutex grn_request_timer_mutex;
static grn_request_timer grn_current_request_timer = { 0 };

grn_bool
grn_request_timer_init(void)
{
grn_ctx *ctx = &grn_request_timer_ctx;

grn_ctx_init(ctx, 0);

MUTEX_INIT(grn_request_timer_mutex);

return GRN_TRUE;
}

void *
grn_request_timer_register(grn_ctx *ctx,
const char *request_id,
unsigned int request_id_size,
double timeout)
{
void *timer_id = NULL;

MUTEX_LOCK(grn_request_timer_mutex);
if (grn_current_request_timer.register_func) {
void *user_data = grn_current_request_timer.user_data;
timer_id = grn_current_request_timer.register_func(ctx,
request_id,
request_id_size,
timeout,
user_data);
}
MUTEX_UNLOCK(grn_request_timer_mutex);

return timer_id;
}

void
grn_request_timer_unregister(grn_ctx *ctx, void *timer_id)
{
MUTEX_LOCK(grn_request_timer_mutex);
if (grn_current_request_timer.unregister_func) {
void *user_data = grn_current_request_timer.user_data;
grn_current_request_timer.unregister_func(ctx, timer_id, user_data);
}
MUTEX_UNLOCK(grn_request_timer_mutex);
}

void
grn_request_timer_set(grn_ctx *ctx, grn_request_timer *timer)
{
MUTEX_LOCK(grn_request_timer_mutex);
if (grn_current_request_timer.fin_func) {
void *user_data = grn_current_request_timer.user_data;
grn_current_request_timer.fin_func(ctx, user_data);
}
if (timer) {
grn_current_request_timer = *timer;
} else {
memset(&grn_current_request_timer, 0, sizeof(grn_request_timer));
}
MUTEX_UNLOCK(grn_request_timer_mutex);
}

void
grn_request_timer_fin(void)
{
grn_ctx *ctx = &grn_request_timer_ctx;

grn_request_timer_set(ctx, NULL);
MUTEX_FIN(grn_request_timer_mutex);
grn_ctx_fin(ctx);
}
2 changes: 2 additions & 0 deletions lib/sources.am
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ libgroonga_la_SOURCES = \
grn_report.h \
request_canceler.c \
grn_request_canceler.h \
request_timer.c \
grn_request_timer.h \
rset.c \
grn_rset.h \
scanner.c \
Expand Down

0 comments on commit 4f4429f

Please sign in to comment.