3e17222f0a042d2dd2acbc7212b02011cdf8e797
[platform/upstream/evolution-data-server.git] / camel / camel-op-queue.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* 
4  * Author : 
5  *  Bertrand Guiheneuf <bertrand@helixcode.com>
6  *
7  * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
8  *
9  * This program is free software; you can redistribute it and/or 
10  * modify it under the terms of the GNU General Public License as 
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */
24
25
26 /* MT safe */
27
28  
29 #include <config.h>
30 #include "camel-op-queue.h"
31
32 static GStaticMutex op_queue_mutex = G_STATIC_MUTEX_INIT;
33
34
35
36 /**
37  * camel_op_queue_new: create a new operation queue
38  * 
39  * Create a new operation queue. 
40  *
41  * Return value: the newly allcated object
42  **/
43 CamelOpQueue *
44 camel_op_queue_new ()
45 {
46         CamelOpQueue *op_queue;
47
48         op_queue = g_new (CamelOpQueue, 1);
49         op_queue->ops_tail = NULL;
50         op_queue->ops_head = NULL;
51         op_queue->service_available = TRUE;
52
53         return op_queue;
54 }
55
56
57 void 
58 camel_op_queue_free (CamelOpQueue *op_queue)
59 {
60         g_list_free (op_queue->ops_head);       
61         g_free (op_queue);
62 }
63
64 /**
65  * camel_op_queue_push_op: Add an operation to the queue
66  * @queue: queue object
67  * @op: operation to add
68  * 
69  * Add an operation to an operation queue. 
70  * The queue is a FIFO queue. 
71  **/
72 void
73 camel_op_queue_push_op (CamelOpQueue *queue, CamelOp *op)
74 {
75         g_assert (queue);
76         g_static_mutex_lock (&op_queue_mutex);
77         if (!queue->ops_tail) {
78                 queue->ops_head = g_list_prepend (NULL, op);
79                 queue->ops_tail = queue->ops_head;
80         } else 
81                 queue->ops_head = g_list_prepend (queue->ops_head, op); 
82         g_static_mutex_unlock (&op_queue_mutex);
83 }
84
85
86 /**
87  * camel_op_queue_pop_op: Pop the next operation pending in the queue
88  * @queue: queue object
89  * 
90  * Pop the next operation pending in the queue.
91  * 
92  * Return value: 
93  **/
94 CamelOp *
95 camel_op_queue_pop_op (CamelOpQueue *queue)
96 {
97         GList *op_list;
98         CamelOp *op;
99
100         g_assert (queue);
101
102         g_static_mutex_lock (&op_queue_mutex);
103         op_list = queue->ops_tail;
104         if (!op_list) return NULL;
105
106         queue->ops_tail = queue->ops_tail->prev;
107         op = (CamelOp *)op_list->data;
108         g_static_mutex_unlock (&op_queue_mutex);
109
110         return op;
111 }
112
113
114 /**
115  * camel_op_queue_run_next_op: run the next pending operation
116  * @queue: queue object
117  * 
118  * Run the next pending operation in the queue.
119  * 
120  * Return value: TRUE if an operation was launched FALSE if there was no operation pending in the queue.
121  **/
122 gboolean
123 camel_op_queue_run_next_op (CamelOpQueue *queue)
124 {
125         CamelOp *op;
126
127         op = camel_op_queue_pop_op (queue);
128         if (!op) return FALSE;
129         
130         return FALSE;
131 }
132
133 /**
134  * camel_op_queue_set_service_availability: set the service availability for an operation queue
135  * @queue: queue object
136  * @available: availability flag
137  * 
138  * set the service availability
139  **/
140 void
141 camel_op_queue_set_service_availability (CamelOpQueue *queue, gboolean available)
142 {
143         g_static_mutex_lock (&op_queue_mutex);
144         queue->service_available = available;
145         g_static_mutex_unlock (&op_queue_mutex);
146 }
147
148 /**
149  * camel_op_queue_get_service_availability: determine if an operation queue service is available 
150  * @queue: queue object
151  * 
152  * Determine if the service associated to an operation queue is available.
153  * 
154  * Return value: service availability.
155  **/
156 gboolean
157 camel_op_queue_get_service_availability (CamelOpQueue *queue)
158 {
159         gboolean available;
160
161         g_static_mutex_lock (&op_queue_mutex);
162         available = queue->service_available;
163         g_static_mutex_unlock (&op_queue_mutex);
164         return available;
165 }
166