summaryrefslogtreecommitdiff
path: root/src-ng/nautilus-context-scheduler.c
blob: 5f7723a9b7ddff6224e2e37eff360f2e39183490 (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
/* Copyright (C) 2017 Ernestas Kulik <ernestask@gnome.org>
 *
 * This file is part of Nautilus.
 *
 * Nautilus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Nautilus 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nautilus.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "nautilus-context-scheduler.h"

#include "nautilus-scheduler-private.h"

struct _NautilusContextScheduler
{
    NautilusScheduler parent_instance;

    GMainContext *context;
    guint source_id;

    GQueue *queue;
};

G_DEFINE_TYPE (NautilusContextScheduler, nautilus_context_scheduler,
               NAUTILUS_TYPE_SCHEDULER)

typedef struct
{
    GSource parent_instance;

    NautilusContextScheduler *scheduler;
} NautilusSource;

G_LOCK_DEFINE_STATIC (table_mutex);
static GHashTable *scheduler_table = NULL;

static void
queue (NautilusScheduler *scheduler,
       NautilusCallback   func,
       gpointer           func_data)
{
    NautilusContextScheduler *context_scheduler;
    NautilusThreadWork *work;

    context_scheduler = NAUTILUS_CONTEXT_SCHEDULER (scheduler);
    work = nautilus_thread_work_new (func, func_data);

    g_queue_push_tail (context_scheduler->queue, work);
}

static void
nautilus_context_scheduler_class_init (NautilusContextSchedulerClass *klass)
{
    NautilusSchedulerClass *scheduler_class;

    scheduler_class = NAUTILUS_SCHEDULER_CLASS (klass);

    scheduler_class->queue = queue;
}

static gboolean
source_prepare (GSource *source,
                gint    *timeout)
{
    NautilusSource *nautilus_source;

    nautilus_source = (NautilusSource *) source;

    *timeout = -1;

    return !g_queue_is_empty (nautilus_source->scheduler->queue);
}

static gboolean
source_check (GSource *source)
{
    NautilusSource *nautilus_source;

    nautilus_source = (NautilusSource *) source;

    return !g_queue_is_empty (nautilus_source->scheduler->queue);
}

static gboolean
source_dispatch (GSource     *source,
                 GSourceFunc  callback,
                 gpointer     user_data)
{
    return callback (user_data);
}

static GSourceFuncs source_funcs =
{
    source_prepare,
    source_check,
    source_dispatch,
    NULL
};

static gboolean
source_callback (gpointer data)
{
    NautilusContextScheduler *scheduler;
    NautilusThreadWork *work;

    scheduler = data;

    while ((work = g_queue_pop_head (scheduler->queue)) != NULL)
    {
        nautilus_thread_work_run (work);
    }

    return G_SOURCE_CONTINUE;
}

static void
nautilus_context_scheduler_init (NautilusContextScheduler *self)
{
    g_autoptr (GSource) source = NULL;

    source = g_source_new (&source_funcs, sizeof (NautilusSource));

    g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
    g_source_set_callback (source, source_callback, self, NULL);

    self->source_id = g_source_attach (source, self->context);
}

NautilusScheduler *
nautilus_context_scheduler_get_for_context (GMainContext *context)
{
    NautilusContextScheduler *scheduler;

    g_return_val_if_fail (context != NULL, NULL);

    G_LOCK (table_mutex);

    if (scheduler_table == NULL)
    {
        scheduler_table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                                                 NULL, g_object_unref);
    }

    scheduler = g_hash_table_lookup (scheduler_table, context);
    if (scheduler == NULL)
    {
        scheduler = g_object_new (NAUTILUS_TYPE_CONTEXT_SCHEDULER, NULL);

        scheduler->context = context;

        g_hash_table_insert (scheduler_table, context, scheduler);
    }

    G_UNLOCK (table_mutex);

    return NAUTILUS_SCHEDULER (scheduler);
}