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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "etpan-thread-manager.h"
30 #include <libetpan/mailsem.h>
31 #include <semaphore.h>
34 #include "etpan-log.h"
35 #include "etpan-errors.h"
37 #define POOL_UNBOUND_MAX 4
39 #define POOL_INIT_SIZE 8
40 #define OP_INIT_SIZE 8
44 TERMINATE_STATE_REQUESTED,
48 struct etpan_thread_manager * etpan_thread_manager_new(void)
50 struct etpan_thread_manager * manager;
53 manager = malloc(sizeof(* manager));
57 manager->thread_pool = carray_new(POOL_INIT_SIZE);
58 if (manager->thread_pool == NULL)
61 manager->thread_pending = carray_new(POOL_INIT_SIZE);
62 if (manager->thread_pending == NULL)
65 manager->can_create_thread = 1;
66 manager->unbound_count = 0;
68 r = pipe(manager->notify_fds);
75 carray_free(manager->thread_pending);
77 carray_free(manager->thread_pool);
84 void etpan_thread_manager_free(struct etpan_thread_manager * manager)
86 close(manager->notify_fds[1]);
87 close(manager->notify_fds[0]);
88 carray_free(manager->thread_pending);
89 carray_free(manager->thread_pool);
93 struct etpan_thread * etpan_thread_new(void)
95 struct etpan_thread * thread;
98 thread = malloc(sizeof(* thread));
102 r = pthread_mutex_init(&thread->lock, NULL);
106 thread->op_list = carray_new(OP_INIT_SIZE);
107 if (thread->op_list == NULL)
110 thread->op_done_list = carray_new(OP_INIT_SIZE);
111 if (thread->op_done_list == NULL)
114 thread->start_sem = mailsem_new();
115 if (thread->start_sem == NULL)
116 goto free_op_done_list;
118 thread->stop_sem = mailsem_new();
119 if (thread->stop_sem == NULL)
122 thread->op_sem = mailsem_new();
123 if (thread->op_sem == NULL)
126 thread->manager = NULL;
127 thread->bound_count = 0;
128 thread->terminate_state = TERMINATE_STATE_NONE;
133 mailsem_free(thread->stop_sem);
135 mailsem_free(thread->start_sem);
137 carray_free(thread->op_done_list);
139 carray_free(thread->op_list);
141 pthread_mutex_destroy(&thread->lock);
148 void etpan_thread_free(struct etpan_thread * thread)
150 mailsem_free(thread->op_sem);
151 mailsem_free(thread->stop_sem);
152 mailsem_free(thread->start_sem);
153 carray_free(thread->op_done_list);
154 carray_free(thread->op_list);
155 pthread_mutex_destroy(&thread->lock);
159 struct etpan_thread_op * etpan_thread_op_new(void)
161 struct etpan_thread_op * op;
164 op = malloc(sizeof(* op));
171 op->callback_data = NULL;
172 op->callback_called = 0;
178 r = pthread_mutex_init(&op->lock, NULL);
190 void etpan_thread_op_free(struct etpan_thread_op * op)
192 pthread_mutex_destroy(&op->lock);
196 static struct etpan_thread *
197 etpan_thread_manager_create_thread(struct etpan_thread_manager * manager)
199 struct etpan_thread * thread;
202 thread = etpan_thread_new();
206 thread->manager = manager;
208 r = etpan_thread_start(thread);
212 r = carray_add(manager->thread_pool, thread, NULL);
214 etpan_thread_stop(thread);
221 etpan_thread_free(thread);
227 etpan_thread_manager_terminate_thread(struct etpan_thread_manager * manager,
228 struct etpan_thread * thread)
233 for(i = 0 ; i < carray_count(manager->thread_pool) ; i ++) {
234 if (carray_get(manager->thread_pool, i) == thread) {
235 carray_delete(manager->thread_pool, i);
240 if (!etpan_thread_is_bound(thread))
241 manager->unbound_count --;
243 r = carray_add(manager->thread_pending, thread, NULL);
245 ETPAN_LOG("complete failure of thread due to lack of memory (thread stop)");
248 etpan_thread_stop(thread);
251 static void manager_notify(struct etpan_thread_manager * manager)
257 r = write(manager->notify_fds[1], &ch, 1);
260 static void manager_ack(struct etpan_thread_manager * manager)
265 r = read(manager->notify_fds[0], &ch, 1);
268 static void thread_lock(struct etpan_thread * thread)
270 pthread_mutex_lock(&thread->lock);
273 static void thread_unlock(struct etpan_thread * thread)
275 pthread_mutex_unlock(&thread->lock);
278 static void thread_notify(struct etpan_thread * thread)
280 manager_notify(thread->manager);
283 static void * thread_run(void * data)
285 struct etpan_thread * thread;
290 mailsem_up(thread->start_sem);
294 struct etpan_thread_op * op;
296 mailsem_down(thread->op_sem);
301 if (carray_count(thread->op_list) > 0) {
302 op = carray_get(thread->op_list, 0);
303 carray_delete_slow(thread->op_list, 0);
308 thread_unlock(thread);
314 if (!etpan_thread_op_cancelled(op)) {
320 r = carray_add(thread->op_done_list, op, NULL);
322 ETPAN_LOG("complete failure of thread due to lack of memory (op done)");
324 thread_unlock(thread);
326 thread_notify(thread);
330 thread->terminate_state = TERMINATE_STATE_DONE;
331 thread_unlock(thread);
333 thread_notify(thread);
335 mailsem_up(thread->stop_sem);
340 int etpan_thread_start(struct etpan_thread * thread)
344 r = pthread_create(&thread->th_id, NULL, thread_run, thread);
348 mailsem_down(thread->start_sem);
353 void etpan_thread_stop(struct etpan_thread * thread)
356 thread->terminate_state = TERMINATE_STATE_REQUESTED;
357 thread_unlock(thread);
359 mailsem_up(thread->op_sem);
361 /* this thread will be joined in the manager loop */
364 int etpan_thread_is_stopped(struct etpan_thread * thread)
369 stopped = (thread->terminate_state == TERMINATE_STATE_DONE);
370 thread_unlock(thread);
375 void etpan_thread_join(struct etpan_thread * thread)
377 mailsem_down(thread->stop_sem);
378 pthread_join(thread->th_id, NULL);
381 struct etpan_thread *
382 etpan_thread_manager_get_thread(struct etpan_thread_manager * manager)
384 struct etpan_thread * chosen_thread;
385 unsigned int chosen_thread_load;
387 struct etpan_thread * thread;
391 chosen_thread = NULL;
392 chosen_thread_load = 0;
394 for(i = 0 ; i < carray_count(manager->thread_pool) ; i ++) {
395 thread = carray_get(manager->thread_pool, i);
396 if (etpan_thread_is_bound(thread))
399 if (chosen_thread == NULL) {
400 chosen_thread = thread;
401 chosen_thread_load = etpan_thread_get_load(thread);
403 if (chosen_thread_load == 0)
409 load = etpan_thread_get_load(thread);
411 if (load < chosen_thread_load) {
412 chosen_thread = thread;
413 chosen_thread_load = load;
418 if (chosen_thread != NULL) {
419 if (manager->can_create_thread && (chosen_thread_load != 0)) {
420 chosen_thread = NULL;
426 if (chosen_thread != NULL)
427 return chosen_thread;
429 thread = etpan_thread_manager_create_thread(manager);
433 manager->unbound_count ++;
434 if (manager->unbound_count >= POOL_UNBOUND_MAX)
435 manager->can_create_thread = 0;
443 unsigned int etpan_thread_get_load(struct etpan_thread * thread)
448 load = carray_count(thread->op_list);
449 thread_unlock(thread);
454 void etpan_thread_bind(struct etpan_thread * thread)
456 thread->bound_count ++;
459 void etpan_thread_unbind(struct etpan_thread * thread)
461 thread->bound_count --;
464 int etpan_thread_is_bound(struct etpan_thread * thread)
466 return (thread->bound_count != 0);
469 int etpan_thread_op_schedule(struct etpan_thread * thread,
470 struct etpan_thread_op * op)
474 if (thread->terminate_state != TERMINATE_STATE_NONE)
478 r = carray_add(thread->op_list, op, NULL);
479 thread_unlock(thread);
485 mailsem_up(thread->op_sem);
490 void etpan_thread_op_lock(struct etpan_thread_op * op)
492 pthread_mutex_lock(&op->lock);
495 void etpan_thread_op_unlock(struct etpan_thread_op * op)
497 pthread_mutex_unlock(&op->lock);
500 int etpan_thread_op_cancelled(struct etpan_thread_op * op)
505 etpan_thread_op_lock(op);
507 cancelled = op->cancelled;
508 etpan_thread_op_unlock(op);
513 void etpan_thread_op_cancel(struct etpan_thread_op * op)
515 etpan_thread_op_lock(op);
517 ETPAN_LOG("cancelled twice");
520 if ((op->callback != NULL) && (!op->callback_called)) {
521 op->callback(op->cancelled, op->result, op->callback_data);
522 op->callback_called = 1;
524 etpan_thread_op_unlock(op);
527 int etpan_thread_manager_op_schedule(struct etpan_thread_manager * manager,
528 struct etpan_thread_op * op)
530 struct etpan_thread * thread;
532 thread = etpan_thread_manager_get_thread(manager);
537 return etpan_thread_op_schedule(thread, op);
543 int etpan_thread_manager_get_fd(struct etpan_thread_manager * manager)
545 return manager->notify_fds[0];
548 static void loop_thread_list(carray * op_to_notify,
549 carray * thread_list)
554 for(i = 0 ; i < carray_count(thread_list) ; i ++) {
555 struct etpan_thread * thread;
558 thread = carray_get(thread_list, i);
562 for(j = 0 ; j < carray_count(thread->op_done_list) ; j ++) {
563 struct etpan_thread_op * op;
565 op = carray_get(thread->op_done_list, j);
566 r = carray_add(op_to_notify, op, NULL);
568 ETPAN_LOG("complete failure of thread due to lack of memory (callback)");
572 carray_set_size(thread->op_done_list, 0);
574 thread_unlock(thread);
578 void etpan_thread_manager_loop(struct etpan_thread_manager * manager)
580 carray * op_to_notify;
583 manager_ack(manager);
585 op_to_notify = carray_new(OP_INIT_SIZE);
587 loop_thread_list(op_to_notify, manager->thread_pool);
588 loop_thread_list(op_to_notify, manager->thread_pending);
590 for(i = 0 ; i < carray_count(op_to_notify) ; i ++) {
591 struct etpan_thread_op * op;
594 op = carray_get(op_to_notify, i);
596 cancelled = etpan_thread_op_cancelled(op);
598 etpan_thread_op_lock(op);
600 if (!op->callback_called) {
601 if (op->callback != NULL)
602 op->callback(op->cancelled, op->result, op->callback_data);
605 etpan_thread_op_unlock(op);
607 if (op->cleanup != NULL)
611 carray_free(op_to_notify);
614 while (i < carray_count(manager->thread_pending)) {
615 struct etpan_thread * thread;
617 thread = carray_get(manager->thread_pending, i);
619 if (etpan_thread_is_stopped(thread)) {
620 etpan_thread_join(thread);
622 etpan_thread_free(thread);
624 carray_delete_slow(manager->thread_pending, i);
632 void etpan_thread_manager_start(struct etpan_thread_manager * manager)
637 void etpan_thread_manager_stop(struct etpan_thread_manager * manager)
639 while (carray_count(manager->thread_pool) > 0) {
640 struct etpan_thread * thread;
642 thread = carray_get(manager->thread_pool, 0);
643 etpan_thread_manager_terminate_thread(manager, thread);
647 int etpan_thread_manager_is_stopped(struct etpan_thread_manager * manager)
649 return ((carray_count(manager->thread_pending) == 0) &&
650 (carray_count(manager->thread_pool) == 0));
653 void etpan_thread_manager_join(struct etpan_thread_manager * manager)
655 while (!etpan_thread_manager_is_stopped(manager)) {
656 etpan_thread_manager_loop(manager);