summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/event.c
blob: aeef67107f585c05c91b88e63884dd9983ac1562 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <sys/poll.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "logging.h"
#include "event.h"
#include "mem-pool.h"
#include "common-utils.h"
#include "libglusterfs-messages.h"



struct event_pool *
event_pool_new (int count, int eventthreadcount)
{
        struct event_pool *event_pool = NULL;
	extern struct event_ops event_ops_poll;

#ifdef HAVE_SYS_EPOLL_H
	extern struct event_ops event_ops_epoll;

        event_pool = event_ops_epoll.new (count, eventthreadcount);

        if (event_pool) {
                event_pool->ops = &event_ops_epoll;
        } else {
                gf_msg ("event", GF_LOG_WARNING, 0, LG_MSG_FALLBACK_TO_POLL,
                        "falling back to poll based event handling");
        }
#endif

        if (!event_pool) {
                event_pool = event_ops_poll.new (count, eventthreadcount);

                if (event_pool)
                        event_pool->ops = &event_ops_poll;
        }

        return event_pool;
}


int
event_register (struct event_pool *event_pool, int fd,
                event_handler_t handler,
                void *data, int poll_in, int poll_out)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = event_pool->ops->event_register (event_pool, fd, handler, data,
                                               poll_in, poll_out);
out:
        return ret;
}


int
event_unregister (struct event_pool *event_pool, int fd, int idx)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = event_pool->ops->event_unregister (event_pool, fd, idx);

out:
        return ret;
}


int
event_unregister_close (struct event_pool *event_pool, int fd, int idx)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = event_pool->ops->event_unregister_close (event_pool, fd, idx);

out:
        return ret;
}


int
event_select_on (struct event_pool *event_pool, int fd, int idx_hint,
                 int poll_in, int poll_out)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = event_pool->ops->event_select_on (event_pool, fd, idx_hint,
                                                poll_in, poll_out);
out:
        return ret;
}


int
event_dispatch (struct event_pool *event_pool)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = event_pool->ops->event_dispatch (event_pool);
        if (ret)
                goto out;

out:
        return ret;
}

int
event_reconfigure_threads (struct event_pool *event_pool, int value)
{
        int ret = -1;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        /* call event refresh function */
        ret = event_pool->ops->event_reconfigure_threads (event_pool,
                                                          value);

out:
        return ret;
}

int
event_pool_destroy (struct event_pool *event_pool)
{
        int ret = -1;
        int destroy = 0, activethreadcount = 0;

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        pthread_mutex_lock (&event_pool->mutex);
        {
                destroy = event_pool->destroy;
                activethreadcount = event_pool->activethreadcount;
        }
        pthread_mutex_unlock (&event_pool->mutex);

        if (!destroy || (activethreadcount > 0))
                goto out;

        ret = event_pool->ops->event_pool_destroy (event_pool);
out:
        return ret;
}

int
poller_destroy_handler (int fd, int idx, void *data,
                       int poll_out, int poll_in, int poll_err)
{
        int readfd = -1;
        char buf = '\0';

        readfd = *(int *)data;
        if (readfd < 0)
                return -1;

        while (read (readfd, &buf, 1) > 0) {
        }
        return 0;
}

/* This function destroys all the poller threads.
 * Note: to be called before event_pool_destroy is called.
 * The order in which cleaning is performed:
 * - Register a pipe fd(this is for waking threads in poll()/epoll_wait())
 * - Set the destroy mode, which this no new event registration will succede
 * - Reconfigure the thread count to 0(this will succede only in destroy mode)
 * - Wake up all the threads in poll() or epoll_wait(), so that they can
 *   destroy themselves.
 * - Wait for the thread to join(which will happen only after all the other
 *   threads are destroyed)
 */
int
event_dispatch_destroy (struct event_pool *event_pool)
{
        int  ret     = -1;
        int  fd[2]   = {-1};
        int  idx     = -1;
        int  flags   = 0;
        struct timespec   sleep_till = {0, };

        GF_VALIDATE_OR_GOTO ("event", event_pool, out);

        ret = pipe (fd);
        if (ret < 0)
                goto out;

        /* Make the read end of the pipe nonblocking */
        flags = fcntl(fd[0], F_GETFL);
        flags |= O_NONBLOCK;
        ret = fcntl(fd[0], F_SETFL, flags);
        if (ret < 0)
                goto out;

        /* Make the write end of the pipe nonblocking */
        flags = fcntl(fd[1], F_GETFL);
        flags |= O_NONBLOCK;
        fcntl(fd[1], F_SETFL, flags);
        if (ret < 0)
                goto out;

        /* From the main thread register an event on the pipe fd[0],
         */
        idx = event_register (event_pool, fd[0], poller_destroy_handler,
                              &fd[1], 1, 0);
        if (idx < 0)
                goto out;

        /* Enter the destroy mode first, set this before reconfiguring to 0
         * threads, to prevent further reconfigure to thread count > 0.
         */
        pthread_mutex_lock (&event_pool->mutex);
        {
                event_pool->destroy = 1;
        }
        pthread_mutex_unlock (&event_pool->mutex);

        ret = event_reconfigure_threads (event_pool, 0);
        if (ret < 0)
                goto out;

        /* Write something onto the write end of the pipe(fd[1]) so that
         * poll wakes up and calls the handler, poller_destroy_handler()
         */
        pthread_mutex_lock (&event_pool->mutex);
        {
                /* Write to pipe(fd[1]) and then wait for 1 second or until
                 * a poller thread that is dying, broadcasts. Make sure we
                 * do not loop forever by limiting to 10 retries
                 */
                int retry = 0;

                while (event_pool->activethreadcount > 0 && retry++ < 10) {
                        if (write (fd[1], "dummy", 6) == -1)
                                break;
                        sleep_till.tv_sec = time (NULL) + 1;
                        ret = pthread_cond_timedwait (&event_pool->cond,
                                                      &event_pool->mutex,
                                                      &sleep_till);
                }
        }
        pthread_mutex_unlock (&event_pool->mutex);

        ret = event_unregister (event_pool, fd[0], idx);

 out:
        if (fd[0] != -1)
                close (fd[0]);
        if (fd[1] != -1)
                close (fd[1]);

        return ret;
}