File size: 5,319 Bytes
8be366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Unix fd watcher thread.
 *
 *      By Peter Wang.
 *
 *      See readme.txt for copyright information.
 *
 *      This module implements a background thread that waits for data
 *      to arrive in file descriptors, at which point it dispatches to
 *      functions which will process that data.
 */


#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>

#include "allegro5/allegro.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_thread.h"
#include "allegro5/internal/aintern_vector.h"
#include "allegro5/platform/aintunix.h"



typedef struct WATCH_ITEM
{
   int fd;
   void (*callback)(void *);
   void *cb_data;
} WATCH_ITEM;


static _AL_THREAD fd_watch_thread;
static _AL_MUTEX fd_watch_mutex = _AL_MUTEX_UNINITED;
static _AL_VECTOR fd_watch_list = _AL_VECTOR_INITIALIZER(WATCH_ITEM);



/* fd_watch_thread_func: [fdwatch thread]
 *  The thread loop function.
 */
static void fd_watch_thread_func(_AL_THREAD *self, void *unused)
{
   (void)unused;

   while (!_al_get_thread_should_stop(self)) {

      fd_set rfds;
      int max_fd;

      /* set up max_fd and rfds */
      _al_mutex_lock(&fd_watch_mutex);
      {
         WATCH_ITEM *wi;
         unsigned int i;

         FD_ZERO(&rfds);
         max_fd = -1;

         for (i = 0; i < _al_vector_size(&fd_watch_list); i++) {
            wi = _al_vector_ref(&fd_watch_list, i);
            FD_SET(wi->fd, &rfds);
            if (wi->fd > max_fd)
               max_fd = wi->fd;
         }
      }
      _al_mutex_unlock(&fd_watch_mutex);

      /* wait for something to happen on one of the fds */
      {
         struct timeval tv;
         int retval;

         tv.tv_sec = 0;
         tv.tv_usec = 250000;

         retval = select(max_fd+1, &rfds, NULL, NULL, &tv);
         if (retval < 1)
            continue;
      }

      /* one or more of the fds has activity */
      _al_mutex_lock(&fd_watch_mutex);
      {
         WATCH_ITEM *wi;
         unsigned int i;

         for (i = 0; i < _al_vector_size(&fd_watch_list); i++) {
            wi = _al_vector_ref(&fd_watch_list, i);
            if (FD_ISSET(wi->fd, &rfds)) {
               /* The callback is allowed to modify the watch list so the mutex
                * must be recursive.
                */
               wi->callback(wi->cb_data);
            }
         }
      }
      _al_mutex_unlock(&fd_watch_mutex);
   }
}



/* _al_unix_start_watching_fd: [primary thread]
 * 
 *  Start watching for data on file descriptor `fd'.  This is done in
 *  a background thread, which is started if necessary.  When there is
 *  data waiting to be read on fd, `callback' is applied to `cb_data'.
 *  The callback function should read as much data off fd as possible.
 *
 *  Note: the callback is run from the background thread.  You can
 *  assume there is only one callback being called from the fdwatch
 *  module at a time.
 */
void _al_unix_start_watching_fd(int fd, void (*callback)(void *), void *cb_data)
{
   ASSERT(fd >= 0);
   ASSERT(callback);

   /* start the background thread if necessary */
   if (_al_vector_size(&fd_watch_list) == 0) {
      /* We need a recursive mutex to allow callbacks to modify the fd watch
       * list.
       */
      _al_mutex_init_recursive(&fd_watch_mutex);
      _al_thread_create(&fd_watch_thread, fd_watch_thread_func, NULL);
   }

   /* now add the watch item to the list */
   _al_mutex_lock(&fd_watch_mutex);
   {
      WATCH_ITEM *wi = _al_vector_alloc_back(&fd_watch_list);

      wi->fd = fd;
      wi->callback = callback;
      wi->cb_data = cb_data;
   }
   _al_mutex_unlock(&fd_watch_mutex);
}



/* _al_unix_stop_watching_fd: [primary thread]
 *
 *  Stop watching for data on `fd'.  Once there are no more file
 *  descriptors to watch, the background thread will be stopped.  This
 *  function is synchronised with the background thread, so you don't
 *  have to do any locking before calling it.
 */
void _al_unix_stop_watching_fd(int fd)
{
   bool list_empty = false;

   /* find the fd in the watch list and remove it */
   _al_mutex_lock(&fd_watch_mutex);
   {
      WATCH_ITEM *wi;
      unsigned int i;

      for (i = 0; i < _al_vector_size(&fd_watch_list); i++) {
         wi = _al_vector_ref(&fd_watch_list, i);
         if (wi->fd == fd) {
            _al_vector_delete_at(&fd_watch_list, i);
            list_empty = _al_vector_is_empty(&fd_watch_list);
            break;
         }
      }
   }
   _al_mutex_unlock(&fd_watch_mutex);

   /* if no more fd's are being watched, stop the background thread */
   if (list_empty) {
      _al_thread_join(&fd_watch_thread);
      _al_mutex_destroy(&fd_watch_mutex);
      _al_vector_free(&fd_watch_list);
   }
}



/*
 * Local Variables:
 * c-basic-offset: 3
 * indent-tabs-mode: nil
 * End:
 */