Skip to content

Instantly share code, notes, and snippets.

@chergert
Last active December 19, 2015 10:49
Show Gist options
  • Select an option

  • Save chergert/5942959 to your computer and use it in GitHub Desktop.

Select an option

Save chergert/5942959 to your computer and use it in GitHub Desktop.
A GSource backed by timerfd. I'm not sure this is even useful, just a prototype.
/* g-interval-source.c
*
* Copyright (C) 2013 Christian Hergert <christian@hergert.me>
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/timerfd.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "g-interval-source.h"
struct _GIntervalSource
{
GSource source;
GPollFD pollfd;
struct itimerspec spec;
};
static gboolean
g_interval_source_prepare (GSource *source,
gint *timeout_)
{
GIntervalSource *isource = (GIntervalSource *)source;
*timeout_ = -1;
return !!isource->pollfd.revents;
}
static gboolean
g_interval_source_check (GSource *source)
{
GIntervalSource *isource = (GIntervalSource *)source;
return !!isource->pollfd.revents;
}
static gboolean
g_interval_source_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
{
GIntervalSource *isource = (GIntervalSource *)source;
guint64 buf;
if (isource->pollfd.fd != -1) {
while (read(isource->pollfd.fd, &buf, sizeof buf) >= 8) {
/* Do nothing */
}
isource->pollfd.revents = 0;
}
return callback ? callback(user_data) : TRUE;
}
static void
g_interval_source_finalize (GSource *source)
{
GIntervalSource *isource = (GIntervalSource *)source;
if (source) {
g_source_remove_poll(source, &isource->pollfd);
if (isource->pollfd.fd != -1) {
close(isource->pollfd.fd);
isource->pollfd.fd = -1;
}
}
}
static GSourceFuncs gSourceFuncs = {
g_interval_source_prepare,
g_interval_source_check,
g_interval_source_dispatch,
g_interval_source_finalize,
};
GSource *
g_interval_source_new (guint64 interval_msec)
{
GIntervalSource *isource;
GSource *source;
int flags;
int fd;
source = g_source_new(&gSourceFuncs, sizeof(GIntervalSource));
isource = (GIntervalSource *)source;
memset(&isource->spec, 0, sizeof isource->spec);
isource->spec.it_interval.tv_sec = interval_msec / 1000UL;
isource->spec.it_interval.tv_nsec = (interval_msec % 1000UL) * 1000000UL;
isource->spec.it_value.tv_nsec = 1;
flags = TFD_NONBLOCK | TFD_CLOEXEC;
errno = 0;
if (-1 == (fd = timerfd_create(CLOCK_MONOTONIC, flags))) {
g_warning("timerfd_create() faliure: %s", strerror(errno));
g_source_destroy(source);
return NULL;
}
errno = 0;
if (-1 == timerfd_settime(fd, 0, &isource->spec, NULL)) {
g_warning("timerfd_settime() failure: %s", strerror(errno));
g_source_destroy(source);
close(fd);
return NULL;
}
isource->pollfd.fd = fd;
isource->pollfd.events = G_IO_IN | G_IO_ERR;
isource->pollfd.revents = 0;
g_source_add_poll(source, &isource->pollfd);
return source;
}
void
g_interval_source_pause (GIntervalSource *source)
{
g_return_if_fail(source);
source->spec.it_value.tv_nsec = 0;
}
void
g_interval_source_unpause (GIntervalSource *source)
{
g_return_if_fail(source);
source->spec.it_value.tv_nsec = 1;
}
/* g-interval-source.h
*
* Copyright (C) 2013 Christian Hergert <christian@hergert.me>
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef G_INTERVAL_SOURCE_H
#define G_INTERVAL_SOURCE_H
#include <glib.h>
G_BEGIN_DECLS
typedef struct _GIntervalSource GIntervalSource;
GSource *g_interval_source_new (guint64 interval_msec);
void g_interval_source_pause (GIntervalSource *source);
void g_interval_source_unpause (GIntervalSource *source);
G_END_DECLS
#endif /* G_INTERVAL_SOURCE_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment