summaryrefslogtreecommitdiff
path: root/gvfs/gioscheduler.c
blob: b938eaf73bef3ad3e2a6fe33b80bf2c9959efa28 (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
277
278
279
280
281
282
283
284
285
286
#include <config.h>

#include "gioscheduler.h"

struct _GIOJob {
  gint id;
  GIOJobFunc job_func;
  GIODataFunc cancel_func; /* Runs under job map lock */
  gpointer data;
  GDestroyNotify destroy_notify;
  
  GMainContext *callback_context;
  gint io_priority;
  gboolean cancelled;
};

static GHashTable *job_map = NULL;
static GThreadPool *job_thread_pool = NULL;
static gint next_job_id = 1;
/* Serializes access to the job_map hash, and handles
 * lifetime issues for the jobs. (i.e. other than the
 * io thread you can only access the job when the job_map
 * lock is held) */
G_LOCK_DEFINE_STATIC(job_map);

static void io_job_thread (gpointer       data,
			   gpointer       user_data);


static gint
g_io_job_compare (gconstpointer  a,
		  gconstpointer  b,
		  gpointer       user_data)
{
  const GIOJob *aa = a;
  const GIOJob *bb = b;

  /* Always run cancelled ops first, they are quick and should be gotten rid of */
  if (aa->cancelled && !bb->cancelled)
    return -1;
  if (!aa->cancelled && bb->cancelled)
    return 1;

  /* Lower value => higher priority */
  if (aa->io_priority < bb->io_priority)
    return -1;
  if (aa->io_priority == bb->io_priority)
    return 0;
  return 1;
}

static void
init_scheduler (void)
{
  if (job_map == NULL)
    {
      /* TODO: thread_pool_new can fail */
      job_thread_pool = g_thread_pool_new (io_job_thread,
					   NULL,
					   10,
					   FALSE,
					   NULL);
      g_thread_pool_set_sort_function (job_thread_pool,
				       g_io_job_compare,
				       NULL);
      job_map = g_hash_table_new (g_direct_hash, g_direct_equal);
    }
}

static void
io_job_thread (gpointer       data,
	       gpointer       user_data)
{
  GIOJob *job = data;

  job->job_func (job, job->data);

  /* Note: We can still get cancel calls here if the job didn't
   * mark itself done, which means we can't free the data until
   * after removal from the job_map.
   */
  
  g_io_job_mark_done (job);

  if (job->destroy_notify)
    job->destroy_notify (job->data);

  g_main_context_unref (job->callback_context);
  g_free (job);
}


gint
g_schedule_io_job (GIOJobFunc    job_func,
		   GIODataFunc   cancel_func,
		   gpointer      data,
		   GDestroyNotify notify,
		   gint          io_priority,
		   GMainContext *callback_context)
{
  GIOJob *job;
  gint id;

  if (callback_context == NULL)
    callback_context = g_main_context_default ();
  
  job = g_new0 (GIOJob, 1);
  job->id = g_atomic_int_exchange_and_add (&next_job_id, 1);
  job->job_func = job_func;
  job->cancel_func = cancel_func;
  job->data = data;
  job->destroy_notify = notify;
  job->io_priority = io_priority;
  job->callback_context = g_main_context_ref (callback_context);
  job->cancelled = FALSE;
  
  G_LOCK (job_map);
  init_scheduler ();

  g_hash_table_insert (job_map, GINT_TO_POINTER (job->id), job);
  
  /* TODO: We ignore errors */
  g_thread_pool_push (job_thread_pool, job, NULL);

  id = job->id;
  G_UNLOCK (job_map);

  return id;
}

  
void
g_cancel_io_job (gint id)
{
  GIOJob *job;
  
  G_LOCK (job_map);
  init_scheduler ();

  job = g_hash_table_lookup (job_map, GINT_TO_POINTER (id));

  if (job && !job->cancelled)
    {
      job->cancelled = TRUE;
      if (job->cancel_func)
	job->cancel_func (job->data);
    }
  
  G_UNLOCK (job_map);
}

/* Called with job_map lock held */
static void
foreach_job_cancel (gpointer       key,
		    gpointer       value,
		    gpointer       user_data)
{
  GIOJob *job = value;

  if (!job->cancelled)
    {
      job->cancelled = TRUE;
      if (job->cancel_func)
	job->cancel_func (job->data);
    }
}

void
g_cancel_all_io_jobs (void)
{
  G_LOCK (job_map);
  init_scheduler ();
  g_hash_table_foreach (job_map,
			foreach_job_cancel,
			NULL);
  
  
  G_UNLOCK (job_map);
}

typedef struct {
  GIODataFunc func;
  gpointer    data;
  GDestroyNotify notify;

  GMutex *ack_lock;
  GCond *ack_condition;
} MainLoopProxy;

static gboolean
mainloop_proxy_func (gpointer data)
{
  MainLoopProxy *proxy = data;

  proxy->func (proxy->data);

  if (proxy->ack_lock)
    {
      g_mutex_lock (proxy->ack_lock);
      g_cond_signal (proxy->ack_condition);
      g_mutex_unlock (proxy->ack_lock);
    }
  
  return FALSE;
}

static void
mainloop_proxy_free (MainLoopProxy *proxy)
{
  if (proxy->ack_lock)
    {
      g_mutex_free (proxy->ack_lock);
      g_cond_free (proxy->ack_condition);
    }
  
  g_free (proxy);
}

static void
mainloop_proxy_notify (gpointer data)
{
  MainLoopProxy *proxy = data;

  if (proxy->notify)
    proxy->notify (proxy->data);

  /* If nonblocking we free here, otherwise we free in io thread */
  if (proxy->ack_lock == NULL)
    mainloop_proxy_free (proxy);
}

void
g_io_job_send_to_mainloop (GIOJob        *job,
			   GIODataFunc    func,
			   gpointer       data,
			   GDestroyNotify notify,
			   gboolean       block)
{
  GSource *source;
  MainLoopProxy *proxy;
  guint id;

  proxy = g_new0 (MainLoopProxy, 1);
  proxy->func = func;
  proxy->data = data;
  proxy->notify = notify;
  if (block)
    {
      proxy->ack_lock = g_mutex_new ();
      proxy->ack_condition = g_cond_new ();
    }
  
  source = g_idle_source_new ();
  g_source_set_priority (source, G_PRIORITY_DEFAULT);

  g_source_set_callback (source, mainloop_proxy_func, proxy, mainloop_proxy_notify);

  if (block)
    g_mutex_lock (proxy->ack_lock);
		  
  id = g_source_attach (source, job->callback_context);
  g_source_unref (source);

  if (block) {
	g_cond_wait (proxy->ack_condition, proxy->ack_lock);
	g_mutex_unlock (proxy->ack_lock);

	/* destroy notify didn't free proxy */
	mainloop_proxy_free (proxy);
  }
}

gboolean
g_io_job_is_cancelled (GIOJob *job)
{
  return job->cancelled;
}

/* Means you can't cancel it */
void
g_io_job_mark_done (GIOJob *job)
{
  G_LOCK (job_map);
  g_hash_table_remove (job_map, GINT_TO_POINTER (job->id));
  G_UNLOCK (job_map);
}