summaryrefslogtreecommitdiff
path: root/glib/gpollcore.h
blob: cb86eb4d160480ff1764a877841a97285ebe60ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright © 2014 Canonical Limited
 *
 * This library 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 of the licence, or (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#ifndef __gpollcore_h__
#define __gpollcore_h__

#include <glib.h>

#if defined(POLLCORE_KQUEUE)

  #include <sys/types.h>
  #include <sys/event.h>
  #include <sys/time.h>

  typedef gint ghandle;

  typedef struct kevent GPollEvent;

  #define g_poll_event_get_user_data(gpe)       ((gpe).udata)
  #define g_poll_event_get_revents(gpe)         (((gpe).filter == EVFILT_WRITE) ? POLLOUT : POLLIN)

  typedef struct
  {
    gint kqueue_fd;
    gint kqueue_timer;
  } GPollCore;

#elif defined(POLLCORE_EPOLL)

  #include <sys/epoll.h>

  typedef gint ghandle;

  typedef struct epoll_event GPollEvent;

  #define g_poll_event_get_user_data(gpe)       ((gpe).data.ptr)
  #define g_poll_event_get_revents(gpe)         ((gpe).events)

  typedef struct
  {
    gint epollfd;
    gint timerfd;
  } GPollCore;

#elif defined(POLLCORE_WIN32)

  #include <windows.h>

  typedef HANDLE ghandle;

  typedef gpointer GPollEvent;

  #define g_poll_event_get_user_data(gpe)       (gpe)
  #define g_poll_event_get_revents(gpe)         (G_IO_IN)

  typedef struct
  {
    gboolean polling_msgs;
    gpointer msgs_user_data;
    HANDLE   handles[MAXIMUM_WAIT_OBJECTS];
    gpointer user_data[MAXIMUM_WAIT_OBJECTS];
    gint     n_handles;
    gint64   ready_time;
    HANDLE   waiting_thread;
    GMutex   mutex;
  } GPollCore;

#elif defined(POLLCORE_POLL)

  typedef gint ghandle;

  typedef struct
  {
    struct pollfd *pfds;
    gpointer      *user_data;
    gint           n_pfds;
    gint           n_allocated_pfds;
    gint64         ready_time;
    gboolean       waiting;
    gint           pipes[2];
    GMutex         mutex;
  } GPollCore;

  typedef struct
  {
    guint    revents;
    gpointer user_data;
  } GPollEvent;

  #define g_poll_event_get_user_data(gpe)       ((gpe).user_data)
  #define g_poll_event_get_revents(gpe)         ((gpe).revents)

#else
  #error This should not be possible.  Check your configuration...
#endif

typedef struct
{
  gpointer user_data;
  gushort old_events;
  gushort new_events;
} GPollUpdate;

GLIB_AVAILABLE_IN_ALL
void            g_poll_core_init                (GPollCore  *core);
GLIB_AVAILABLE_IN_ALL
void            g_poll_core_clear               (GPollCore  *core);

/* Called from owner thread with lock held */
GLIB_AVAILABLE_IN_ALL
gint            g_poll_core_update_and_collect  (GPollCore  *core,
                                                 GHashTable *updates,
                                                 gint64     *ready_time_update,
                                                 GPollEvent *events,
                                                 gint        max_events);

/* Called with lock held and must release it before sleeping */
GLIB_AVAILABLE_IN_ALL
void            g_poll_core_wait                (GPollCore  *core,
                                                 GMutex     *mutex);

/* Called from another thread with context lock held */
GLIB_AVAILABLE_IN_ALL
void            g_poll_core_update              (GPollCore  *core,
                                                 ghandle     handle,
                                                 guint       old_events,
                                                 guint       new_events,
                                                 gpointer    user_data);

GLIB_AVAILABLE_IN_ALL
void            g_poll_core_set_ready_time      (GPollCore  *core,
                                                 gint64      ready_time);

/* Only on UNIX */
GLIB_AVAILABLE_IN_ALL
gint            g_poll_core_get_unix_fd         (GPollCore  *core);

#endif /* __gpollcore_h__ */