4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) version 3.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with the program; if not, see <http://www.gnu.org/licenses/>
18
* Copyright (C) 2011 Novell, Inc. (www.novell.com)
22
#include "e-operation-pool.h"
24
struct _EOperationPool {
33
e_operation_pool_new (guint max_threads, GFunc thread_func, gpointer user_data)
36
GThreadPool *thread_pool;
39
g_return_val_if_fail (thread_func != NULL, NULL);
41
thread_pool = g_thread_pool_new (thread_func, user_data, max_threads, FALSE, &error);
43
g_warning ("%s: Failed to create thread pool: %s", G_STRFUNC, error->message);
48
pool = g_new0 (EOperationPool, 1);
49
pool->pool = thread_pool;
50
pool->ops_lock = g_mutex_new ();
51
pool->ops = g_hash_table_new (g_direct_hash, g_direct_equal);
54
/* Kill threads which don't do anything for 10 seconds */
55
g_thread_pool_set_max_idle_time (10 * 1000);
61
e_operation_pool_free (EOperationPool *pool)
63
g_return_if_fail (pool != NULL);
65
g_thread_pool_free (pool->pool, FALSE, FALSE);
66
g_mutex_free (pool->ops_lock);
67
g_hash_table_destroy (pool->ops);
71
/* Reserves new operation ID, which is returned. This operation ID may
72
be released by e_operation_pool_release_opid () when the operation
76
e_operation_pool_reserve_opid (EOperationPool *pool)
80
g_return_val_if_fail (pool != NULL, 0);
81
g_return_val_if_fail (pool->ops != NULL, 0);
82
g_return_val_if_fail (pool->ops_lock != NULL, 0);
84
g_mutex_lock (pool->ops_lock);
90
while (pool->last_opid && g_hash_table_lookup (pool->ops, GUINT_TO_POINTER (pool->last_opid)))
93
opid = pool->last_opid;
95
g_hash_table_insert (pool->ops, GUINT_TO_POINTER (opid), GUINT_TO_POINTER (1));
97
g_mutex_unlock (pool->ops_lock);
99
g_return_val_if_fail (opid != 0, 0);
104
/* Releases operation ID previously reserved by e_operation_pool_reserve_opid(). */
106
e_operation_pool_release_opid (EOperationPool *pool, guint32 opid)
108
g_return_if_fail (pool != NULL);
109
g_return_if_fail (pool->ops != NULL);
110
g_return_if_fail (pool->ops_lock != NULL);
112
g_mutex_lock (pool->ops_lock);
113
g_hash_table_remove (pool->ops, GUINT_TO_POINTER (opid));
114
g_mutex_unlock (pool->ops_lock);
117
/* Pushes operation to be processed. 'opdata' is passed to the function
118
provided in e_operation_pool_new ().
121
e_operation_pool_push (EOperationPool *pool, gpointer opdata)
123
GError *error = NULL;
125
g_return_if_fail (pool != NULL);
126
g_return_if_fail (pool->pool != NULL);
128
g_thread_pool_push (pool->pool, opdata, &error);
131
g_warning ("%s: Failed to push to thread pool: %s", G_STRFUNC, error->message);
132
g_error_free (error);