2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "etpan-thread-manager.h"
31 #include <libetpan/mailsem.h>
32 #include <semaphore.h>
36 #include "etpan-errors.h"
38 #define POOL_UNBOUND_MAX 4
40 #define POOL_INIT_SIZE 8
41 #define OP_INIT_SIZE 8
43 static int etpan_thread_start(struct etpan_thread * thread);
44 static void etpan_thread_free(struct etpan_thread * thread);
45 static unsigned int etpan_thread_get_load(struct etpan_thread * thread);
46 static int etpan_thread_is_bound(struct etpan_thread * thread);
47 static int etpan_thread_manager_is_stopped(struct etpan_thread_manager * manager);
48 static void etpan_thread_join(struct etpan_thread * thread);
49 static struct etpan_thread * etpan_thread_new(void);
50 static int etpan_thread_op_cancelled(struct etpan_thread_op * op);
51 static void etpan_thread_op_lock(struct etpan_thread_op * op);
52 static void etpan_thread_op_unlock(struct etpan_thread_op * op);
53 static void etpan_thread_stop(struct etpan_thread * thread);
56 static void etpan_thread_bind(struct etpan_thread * thread);
57 static int etpan_thread_manager_op_schedule(struct etpan_thread_manager * manager,
58 struct etpan_thread_op * op);
59 static void etpan_thread_manager_start(struct etpan_thread_manager * manager);
60 static void etpan_thread_op_cancel(struct etpan_thread_op * op);
65 TERMINATE_STATE_REQUESTED,
69 struct etpan_thread_manager * etpan_thread_manager_new(void)
71 struct etpan_thread_manager * manager;
74 manager = malloc(sizeof(* manager));
78 manager->thread_pool = carray_new(POOL_INIT_SIZE);
79 if (manager->thread_pool == NULL)
82 manager->thread_pending = carray_new(POOL_INIT_SIZE);
83 if (manager->thread_pending == NULL)
86 manager->can_create_thread = 1;
87 manager->unbound_count = 0;
89 r = pipe(manager->notify_fds);
96 carray_free(manager->thread_pending);
98 carray_free(manager->thread_pool);
105 void etpan_thread_manager_free(struct etpan_thread_manager * manager)
107 close(manager->notify_fds[1]);
108 close(manager->notify_fds[0]);
109 carray_free(manager->thread_pending);
110 carray_free(manager->thread_pool);
114 static struct etpan_thread * etpan_thread_new(void)
116 struct etpan_thread * thread;
119 thread = malloc(sizeof(* thread));
123 r = pthread_mutex_init(&thread->lock, NULL);
127 thread->op_list = carray_new(OP_INIT_SIZE);
128 if (thread->op_list == NULL)
131 thread->op_done_list = carray_new(OP_INIT_SIZE);
132 if (thread->op_done_list == NULL)
135 thread->start_sem = mailsem_new();
136 if (thread->start_sem == NULL)
137 goto free_op_done_list;
139 thread->stop_sem = mailsem_new();
140 if (thread->stop_sem == NULL)
143 thread->op_sem = mailsem_new();
144 if (thread->op_sem == NULL)
147 thread->manager = NULL;
148 thread->bound_count = 0;
149 thread->terminate_state = TERMINATE_STATE_NONE;
154 mailsem_free(thread->stop_sem);
156 mailsem_free(thread->start_sem);
158 carray_free(thread->op_done_list);
160 carray_free(thread->op_list);
162 pthread_mutex_destroy(&thread->lock);
169 static void etpan_thread_free(struct etpan_thread * thread)
171 mailsem_free(thread->op_sem);
172 mailsem_free(thread->stop_sem);
173 mailsem_free(thread->start_sem);
174 carray_free(thread->op_done_list);
175 carray_free(thread->op_list);
176 pthread_mutex_destroy(&thread->lock);
180 struct etpan_thread_op * etpan_thread_op_new(void)
182 struct etpan_thread_op * op;
185 op = malloc(sizeof(* op));
192 op->callback_data = NULL;
193 op->callback_called = 0;
202 r = pthread_mutex_init(&op->lock, NULL);
214 void etpan_thread_op_free(struct etpan_thread_op * op)
216 pthread_mutex_destroy(&op->lock);
220 static struct etpan_thread *
221 etpan_thread_manager_create_thread(struct etpan_thread_manager * manager)
223 struct etpan_thread * thread;
226 thread = etpan_thread_new();
230 thread->manager = manager;
232 r = etpan_thread_start(thread);
236 r = carray_add(manager->thread_pool, thread, NULL);
238 etpan_thread_stop(thread);
245 etpan_thread_free(thread);
251 etpan_thread_manager_terminate_thread(struct etpan_thread_manager * manager,
252 struct etpan_thread * thread)
257 for(i = 0 ; i < carray_count(manager->thread_pool) ; i ++) {
258 if (carray_get(manager->thread_pool, i) == thread) {
259 carray_delete(manager->thread_pool, i);
264 if (!etpan_thread_is_bound(thread))
265 manager->unbound_count --;
267 r = carray_add(manager->thread_pending, thread, NULL);
269 g_warning("complete failure of thread due to lack of memory (thread stop)");
272 etpan_thread_stop(thread);
275 static void manager_notify(struct etpan_thread_manager * manager)
281 r = write(manager->notify_fds[1], &ch, 1);
284 static void manager_ack(struct etpan_thread_manager * manager)
289 r = read(manager->notify_fds[0], &ch, 1);
292 static void thread_lock(struct etpan_thread * thread)
294 pthread_mutex_lock(&thread->lock);
297 static void thread_unlock(struct etpan_thread * thread)
299 pthread_mutex_unlock(&thread->lock);
302 static void thread_notify(struct etpan_thread * thread)
304 manager_notify(thread->manager);
307 static void * thread_run(void * data)
309 struct etpan_thread * thread;
314 mailsem_up(thread->start_sem);
318 struct etpan_thread_op * op;
320 mailsem_down(thread->op_sem);
325 if (carray_count(thread->op_list) > 0) {
326 op = carray_get(thread->op_list, 0);
327 carray_delete_slow(thread->op_list, 0);
332 thread_unlock(thread);
338 if (!etpan_thread_op_cancelled(op)) {
344 r = carray_add(thread->op_done_list, op, NULL);
346 g_warning("complete failure of thread due to lack of memory (op done)");
348 thread_unlock(thread);
350 thread_notify(thread);
354 thread->terminate_state = TERMINATE_STATE_DONE;
355 thread_unlock(thread);
357 thread_notify(thread);
359 mailsem_up(thread->stop_sem);
364 static int etpan_thread_start(struct etpan_thread * thread)
368 r = pthread_create(&thread->th_id, NULL, thread_run, thread);
372 mailsem_down(thread->start_sem);
377 static void etpan_thread_stop(struct etpan_thread * thread)
380 thread->terminate_state = TERMINATE_STATE_REQUESTED;
381 thread_unlock(thread);
383 mailsem_up(thread->op_sem);
385 /* this thread will be joined in the manager loop */
388 static int etpan_thread_is_stopped(struct etpan_thread * thread)
393 stopped = (thread->terminate_state == TERMINATE_STATE_DONE);
394 thread_unlock(thread);
399 static void etpan_thread_join(struct etpan_thread * thread)
401 mailsem_down(thread->stop_sem);
402 pthread_join(thread->th_id, NULL);
405 struct etpan_thread *
406 etpan_thread_manager_get_thread(struct etpan_thread_manager * manager)
408 struct etpan_thread * chosen_thread;
409 unsigned int chosen_thread_load;
411 struct etpan_thread * thread;
415 chosen_thread = NULL;
416 chosen_thread_load = 0;
418 for(i = 0 ; i < carray_count(manager->thread_pool) ; i ++) {
419 thread = carray_get(manager->thread_pool, i);
420 if (etpan_thread_is_bound(thread))
423 if (chosen_thread == NULL) {
424 chosen_thread = thread;
425 chosen_thread_load = etpan_thread_get_load(thread);
427 if (chosen_thread_load == 0)
433 load = etpan_thread_get_load(thread);
435 if (load < chosen_thread_load) {
436 chosen_thread = thread;
437 chosen_thread_load = load;
442 if (chosen_thread != NULL) {
443 if (manager->can_create_thread && (chosen_thread_load != 0)) {
444 chosen_thread = NULL;
450 if (chosen_thread != NULL)
451 return chosen_thread;
453 thread = etpan_thread_manager_create_thread(manager);
457 manager->unbound_count ++;
458 if (manager->unbound_count >= POOL_UNBOUND_MAX)
459 manager->can_create_thread = 0;
467 static unsigned int etpan_thread_get_load(struct etpan_thread * thread)
472 load = carray_count(thread->op_list);
473 thread_unlock(thread);
479 static void etpan_thread_bind(struct etpan_thread * thread)
481 thread->bound_count ++;
485 void etpan_thread_unbind(struct etpan_thread * thread)
487 thread->bound_count --;
490 static int etpan_thread_is_bound(struct etpan_thread * thread)
492 return (thread->bound_count != 0);
495 int etpan_thread_op_schedule(struct etpan_thread * thread,
496 struct etpan_thread_op * op)
500 if (thread->terminate_state != TERMINATE_STATE_NONE)
504 r = carray_add(thread->op_list, op, NULL);
505 thread_unlock(thread);
511 mailsem_up(thread->op_sem);
516 static void etpan_thread_op_lock(struct etpan_thread_op * op)
518 pthread_mutex_lock(&op->lock);
521 static void etpan_thread_op_unlock(struct etpan_thread_op * op)
523 pthread_mutex_unlock(&op->lock);
526 static int etpan_thread_op_cancelled(struct etpan_thread_op * op)
531 etpan_thread_op_lock(op);
533 cancelled = op->cancelled;
534 etpan_thread_op_unlock(op);
540 static void etpan_thread_op_cancel(struct etpan_thread_op * op)
542 etpan_thread_op_lock(op);
544 g_warning("cancelled twice");
547 if ((op->callback != NULL) && (!op->callback_called)) {
548 op->callback(op->cancelled, op->result, op->callback_data);
549 op->callback_called = 1;
551 etpan_thread_op_unlock(op);
556 static int etpan_thread_manager_op_schedule(struct etpan_thread_manager * manager,
557 struct etpan_thread_op * op)
559 struct etpan_thread * thread;
561 thread = etpan_thread_manager_get_thread(manager);
566 return etpan_thread_op_schedule(thread, op);
573 int etpan_thread_manager_get_fd(struct etpan_thread_manager * manager)
575 return manager->notify_fds[0];
578 static void loop_thread_list(carray * op_to_notify,
579 carray * thread_list)
584 for(i = 0 ; i < carray_count(thread_list) ; i ++) {
585 struct etpan_thread * thread;
588 thread = carray_get(thread_list, i);
592 for(j = 0 ; j < carray_count(thread->op_done_list) ; j ++) {
593 struct etpan_thread_op * op;
595 op = carray_get(thread->op_done_list, j);
596 r = carray_add(op_to_notify, op, NULL);
598 g_warning("complete failure of thread due to lack of memory (callback)");
602 carray_set_size(thread->op_done_list, 0);
604 thread_unlock(thread);
608 void etpan_thread_manager_loop(struct etpan_thread_manager * manager)
610 carray * op_to_notify;
613 manager_ack(manager);
615 op_to_notify = carray_new(OP_INIT_SIZE);
617 loop_thread_list(op_to_notify, manager->thread_pool);
618 loop_thread_list(op_to_notify, manager->thread_pending);
620 for(i = 0 ; i < carray_count(op_to_notify) ; i ++) {
621 struct etpan_thread_op * op;
624 op = carray_get(op_to_notify, i);
626 cancelled = etpan_thread_op_cancelled(op);
628 etpan_thread_op_lock(op);
630 if (!op->callback_called) {
631 if (op->callback != NULL)
632 op->callback(op->cancelled, op->result, op->callback_data);
635 etpan_thread_op_unlock(op);
637 if (op->cleanup != NULL)
641 carray_free(op_to_notify);
644 while (i < carray_count(manager->thread_pending)) {
645 struct etpan_thread * thread;
647 thread = carray_get(manager->thread_pending, i);
649 if (etpan_thread_is_stopped(thread)) {
650 etpan_thread_join(thread);
652 etpan_thread_free(thread);
654 carray_delete_slow(manager->thread_pending, i);
663 static void etpan_thread_manager_start(struct etpan_thread_manager * manager)
669 void etpan_thread_manager_stop(struct etpan_thread_manager * manager)
671 while (carray_count(manager->thread_pool) > 0) {
672 struct etpan_thread * thread;
674 thread = carray_get(manager->thread_pool, 0);
675 etpan_thread_manager_terminate_thread(manager, thread);
679 static int etpan_thread_manager_is_stopped(struct etpan_thread_manager * manager)
681 return ((carray_count(manager->thread_pending) == 0) &&
682 (carray_count(manager->thread_pool) == 0));
685 void etpan_thread_manager_join(struct etpan_thread_manager * manager)
687 while (!etpan_thread_manager_is_stopped(manager)) {
688 etpan_thread_manager_loop(manager);