summaryrefslogtreecommitdiff
path: root/src-ng/nautilus-task-manager.c
blob: 637bed30b42c036320cee0338ed1d2010a8178f9 (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
/* 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-task-manager.h"

struct _NautilusTaskManager
{
    GObject parent_instance;

    GThreadPool *thread_pool;
};

G_DEFINE_TYPE (NautilusTaskManager, nautilus_task_manager, G_TYPE_OBJECT)

static NautilusTaskManager *instance = NULL;

static GObject *
constructor (GType                  type,
             guint                  n_construct_properties,
             GObjectConstructParam *construct_properties)
{
    static GMutex mutex;
    GObjectClass *parent_class;

    g_mutex_lock (&mutex);

    if (instance != NULL)
    {
        g_mutex_unlock (&mutex);
        return g_object_ref (instance);
    }

    parent_class = G_OBJECT_CLASS (nautilus_task_manager_parent_class);
    instance = NAUTILUS_TASK_MANAGER (parent_class->constructor (type,
                                                                 n_construct_properties,
                                                                 construct_properties));

    g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance);
    /* The task manager should be like a daemon, always present while
     * Nautilus is running, so we avoid the memory handling all around
     * and have a way to know statistics an status of the task management in
     * general during the whole Nautilus proccess duration.
     */
    g_object_ref (instance);

    g_mutex_unlock (&mutex);

    return G_OBJECT (instance);
}

static void
finalize (GObject *object)
{
    NautilusTaskManager *self;

    self = NAUTILUS_TASK_MANAGER (object);

    g_thread_pool_free (self->thread_pool, TRUE, TRUE);

    G_OBJECT_CLASS (nautilus_task_manager_parent_class)->finalize (object);
}

static void
nautilus_task_manager_class_init (NautilusTaskManagerClass *klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (klass);

    object_class->constructor = constructor;
    object_class->finalize = finalize;
}

static void
execute_task (gpointer data,
              gpointer user_data)
{
    g_autoptr (NautilusTask) task = NULL;

    task = NAUTILUS_TASK (data);

    nautilus_task_run_sync (task);
}

static void
nautilus_task_manager_init (NautilusTaskManager *self)
{
    self->thread_pool = g_thread_pool_new (execute_task, self,
                                           16, FALSE,
                                           NULL);
}

void
nautilus_task_manager_queue_task (NautilusTaskManager  *self,
                                  NautilusTask         *task)
{
    g_return_if_fail (NAUTILUS_IS_TASK_MANAGER (self));
    g_return_if_fail (NAUTILUS_IS_TASK (task));

    g_thread_pool_push (self->thread_pool, g_object_ref (task), NULL);
}

NautilusTaskManager *
nautilus_task_manager_dup_singleton (void)
{
    return g_object_new (NAUTILUS_TYPE_TASK_MANAGER, NULL);
}