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
|
/*
* e-operation-pool.c
*
* Copyright (C) 2011 Novell, Inc. (www.novell.com)
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "e-operation-pool.h"
struct _EOperationPool {
GThreadPool *pool;
GMutex ops_lock;
GHashTable *ops;
guint32 last_opid;
};
/**
* e_operation_pool_new: (skip)
* @max_threads: Maximum number of threads for this pool
* @thread_func: Function to run for a given thread
* @user_data: The user data to pass to @thread_func
*
* Returns: (transfer full): a new #EOperationPool with the given settings.
* Free it with e_operation_pool_free(), when no longer needed.
*
* Since: 3.2
**/
EOperationPool *
e_operation_pool_new (guint max_threads,
GFunc thread_func,
gpointer user_data)
{
EOperationPool *pool;
GThreadPool *thread_pool;
GError *error = NULL;
g_return_val_if_fail (thread_func != NULL, NULL);
thread_pool = g_thread_pool_new (thread_func, user_data, max_threads, FALSE, &error);
if (error) {
g_warning ("%s: Failed to create thread pool: %s", G_STRFUNC, error->message);
g_error_free (error);
return NULL;
}
pool = g_slice_new0 (EOperationPool);
pool->pool = thread_pool;
g_mutex_init (&pool->ops_lock);
pool->ops = g_hash_table_new (g_direct_hash, g_direct_equal);
pool->last_opid = 0;
/* Kill threads which don't do anything for 10 seconds */
g_thread_pool_set_max_idle_time (10 * 1000);
return pool;
}
/**
* e_operation_pool_free:
* @pool: an #EOperationPool
*
* Frees previously created @pool.
*
* Since: 3.2
**/
void
e_operation_pool_free (EOperationPool *pool)
{
g_return_if_fail (pool != NULL);
g_thread_pool_free (pool->pool, FALSE, FALSE);
g_mutex_clear (&pool->ops_lock);
g_hash_table_destroy (pool->ops);
g_slice_free (EOperationPool, pool);
}
/**
* e_operation_pool_reserve_opid:
* @pool: an #EOperationPool
*
* Reserves new operation ID, which is returned. This operation ID may
* be released by e_operation_pool_release_opid() when the operation
* is finished.
*
* Returns: a new operation ID
*
* Since: 3.2
**/
guint32
e_operation_pool_reserve_opid (EOperationPool *pool)
{
guint32 opid;
g_return_val_if_fail (pool != NULL, 0);
g_return_val_if_fail (pool->ops != NULL, 0);
g_mutex_lock (&pool->ops_lock);
pool->last_opid++;
if (!pool->last_opid)
pool->last_opid = 1;
while (pool->last_opid && g_hash_table_lookup (pool->ops, GUINT_TO_POINTER (pool->last_opid)))
pool->last_opid++;
opid = pool->last_opid;
if (opid)
g_hash_table_insert (pool->ops, GUINT_TO_POINTER (opid), GUINT_TO_POINTER (1));
g_mutex_unlock (&pool->ops_lock);
g_return_val_if_fail (opid != 0, 0);
return opid;
}
/**
* e_operation_pool_release_opid:
* @pool: an #EOperationPool
* @opid: an operation ID
*
* Releases @opid previously reserved by e_operation_pool_reserve_opid().
*
* Since: 3.2
**/
void
e_operation_pool_release_opid (EOperationPool *pool,
guint32 opid)
{
g_return_if_fail (pool != NULL);
g_return_if_fail (pool->ops != NULL);
g_mutex_lock (&pool->ops_lock);
g_hash_table_remove (pool->ops, GUINT_TO_POINTER (opid));
g_mutex_unlock (&pool->ops_lock);
}
/**
* e_operation_pool_push:
* @pool: an #EOperationPool
* @opdata: user data for the operation
*
* Pushes an operation to be processed. @opdata is passed to the function
* provided in e_operation_pool_new().
*
* Since: 3.2
**/
void
e_operation_pool_push (EOperationPool *pool,
gpointer opdata)
{
GError *error = NULL;
g_return_if_fail (pool != NULL);
g_return_if_fail (pool->pool != NULL);
g_thread_pool_push (pool->pool, opdata, &error);
if (error) {
g_warning ("%s: Failed to push to thread pool: %s", G_STRFUNC, error->message);
g_error_free (error);
}
}
|