12 #include "ecore_private.h"
14 #ifdef EFL_HAVE_THREADS
16 # ifdef EFL_HAVE_POSIX_THREADS
20 # include <sys/resource.h>
22 # include <sys/syscall.h>
26 # define PH(x) pthread_t x
27 # define PHE(x, y) pthread_equal(x, y)
28 # define PHS() pthread_self()
29 # define PHC(x, f, d) pthread_create(&(x), NULL, (void*) f, d)
30 # define PHJ(x, p) pthread_join(x, (void**)(&(p)))
31 # define PHA(x) pthread_cancel(x)
33 # define CD(x) pthread_cond_t x
34 # define CDI(x) pthread_cond_init(&(x), NULL);
35 # define CDD(x) pthread_cond_destroy(&(x));
36 # define CDB(x) pthread_cond_broadcast(&(x));
37 # define CDW(x, y, t) pthread_cond_timedwait(&(x), &(y), t);
39 # define LK(x) pthread_mutex_t x
40 # define LKI(x) pthread_mutex_init(&(x), NULL);
41 # define LKD(x) pthread_mutex_destroy(&(x));
42 # define LKL(x) pthread_mutex_lock(&(x));
43 # define LKU(x) pthread_mutex_unlock(&(x));
45 # define LRWK(x) pthread_rwlock_t x
46 # define LRWKI(x) pthread_rwlock_init(&(x), NULL);
47 # define LRWKD(x) pthread_rwlock_destroy(&(x));
48 # define LRWKWL(x) pthread_rwlock_wrlock(&(x));
49 # define LRWKRL(x) pthread_rwlock_rdlock(&(x));
50 # define LRWKU(x) pthread_rwlock_unlock(&(x));
52 # else /* EFL_HAVE_WIN32_THREADS */
54 # define WIN32_LEAN_AND_MEAN
56 # undef WIN32_LEAN_AND_MEAN
64 # define PH(x) win32_thread *x
65 # define PHE(x, y) ((x) == (y))
66 # define PHS() (HANDLE)GetCurrentThreadId()
68 int _ecore_thread_win32_create(win32_thread **x, LPTHREAD_START_ROUTINE f, void *d)
71 t = (win32_thread *)calloc(1, sizeof(win32_thread));
75 (t)->thread = CreateThread(NULL, 0, f, d, 0, NULL);
86 # define PHC(x, f, d) _ecore_thread_win32_create(&(x), (LPTHREAD_START_ROUTINE)f, d)
88 int _ecore_thread_win32_join(win32_thread *x, void **res)
92 WaitForSingleObject(x->thread, INFINITE);
93 CloseHandle(x->thread);
95 if (res) *res = x->val;
100 # define PHJ(x, p) _ecore_thread_win32_join(x, (void**)(&(p)))
101 # define PHA(x) TerminateThread(x->thread, 0)
103 # define LK(x) HANDLE x
104 # define LKI(x) x = CreateMutex(NULL, FALSE, NULL)
105 # define LKD(x) CloseHandle(x)
106 # define LKL(x) WaitForSingleObject(x, INFINITE)
107 # define LKU(x) ReleaseMutex(x)
113 CRITICAL_SECTION threads_count_lock;
116 # define CD(x) win32_cond *x
120 x = (win32_cond *)calloc(1, sizeof(win32_cond)); \
123 x->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL); \
125 InitializeCriticalSection(&x->threads_count_lock); \
136 CloseHandle(x->semaphore); \
143 EnterCriticalSection(&x->threads_count_lock); \
144 if (x->threads_count > 0) \
145 ReleaseSemaphore(x->semaphore, x->threads_count, NULL); \
146 LeaveCriticalSection (&x->threads_count_lock); \
149 int _ecore_thread_win32_cond_timedwait(win32_cond *c, HANDLE *external_mutex, struct timeval *t)
152 DWORD val = t->tv_sec * 1000 + (t->tv_usec / 1000);
154 EnterCriticalSection (&c->threads_count_lock);
156 LeaveCriticalSection (&c->threads_count_lock);
158 res = WaitForSingleObject(c->semaphore, val);
159 if (res == WAIT_OBJECT_0)
164 # define CDW(x, y, t) _ecore_thread_win32_cond_timedwait(x, y, t)
177 # define LRWK(x) win32_rwl *x
180 x = (win32_rwl *)calloc(1, sizeof(win32_rwl)); \
189 CDI(x->cond_write); \
190 if (!x->cond_write) \
217 CDD(x->cond_write); \
225 if (x->writers || x->readers > 0) \
227 x->writers_count++; \
228 while (x->writers || x->readers > 0) \
230 EnterCriticalSection(&x->cond_write->threads_count_lock); \
231 x->cond_read->threads_count++; \
232 LeaveCriticalSection(&x->cond_write->threads_count_lock); \
233 res = WaitForSingleObject(x->cond_write->semaphore, INFINITE); \
234 if (res != WAIT_OBJECT_0) break; \
236 x->writers_count--; \
238 if (res == 0) x->writers_count = 1; \
247 x->readers_count++; \
250 EnterCriticalSection(&x->cond_write->threads_count_lock); \
251 x->cond_read->threads_count++; \
252 LeaveCriticalSection(&x->cond_write->threads_count_lock); \
253 res = WaitForSingleObject(x->cond_write->semaphore, INFINITE); \
254 if (res != WAIT_OBJECT_0) break; \
256 x->readers_count--; \
268 if (x->readers_count == 1) \
270 EnterCriticalSection(&x->cond_read->threads_count_lock); \
271 if (x->cond_read->threads_count > 0) \
272 ReleaseSemaphore(x->cond_read->semaphore, 1, 0); \
273 LeaveCriticalSection(&x->cond_read->threads_count_lock); \
275 else if (x->readers_count > 0) \
277 else if (x->writers_count > 0) \
279 EnterCriticalSection (&x->cond_write->threads_count_lock); \
280 if (x->cond_write->threads_count > 0) \
281 ReleaseSemaphore(x->cond_write->semaphore, 1, 0); \
282 LeaveCriticalSection (&x->cond_write->threads_count_lock); \
285 else if (x->readers > 0) \
288 if (x->readers == 0 && x->writers_count > 0) \
290 EnterCriticalSection (&x->cond_write->threads_count_lock); \
291 if (x->cond_write->threads_count > 0) \
292 ReleaseSemaphore(x->cond_write->semaphore, 1, 0); \
293 LeaveCriticalSection (&x->cond_write->threads_count_lock); \
303 typedef struct _Ecore_Pthread_Worker Ecore_Pthread_Worker;
304 typedef struct _Ecore_Pthread Ecore_Pthread;
305 typedef struct _Ecore_Thread_Data Ecore_Thread_Data;
307 struct _Ecore_Thread_Data
313 struct _Ecore_Pthread_Worker
317 Ecore_Thread_Cb func_blocking;
320 Ecore_Thread_Cb func_heavy;
321 Ecore_Thread_Notify_Cb func_notify;
324 Ecore_Pipe *direct_pipe;
325 Ecore_Pthread_Worker *direct_worker;
332 Ecore_Thread_Cb func_cancel;
333 Ecore_Thread_Cb func_end;
334 #ifdef EFL_HAVE_THREADS
343 Eina_Bool cancel : 1;
344 Eina_Bool feedback_run : 1;
346 Eina_Bool reschedule : 1;
349 #ifdef EFL_HAVE_THREADS
350 typedef struct _Ecore_Pthread_Data Ecore_Pthread_Data;
352 struct _Ecore_Pthread_Data
354 Ecore_Pthread_Worker *death_job;
361 static void _ecore_thread_handler(void *data __UNUSED__, void *buffer, unsigned int nbyte);
363 static int _ecore_thread_count_max = 0;
364 static int ECORE_THREAD_PIPE_DEL = 0;
365 static Eina_Array *_ecore_thread_pipe = NULL;
368 _ecore_thread_pipe_get(void)
370 if (eina_array_count_get(_ecore_thread_pipe) > 0)
371 return eina_array_pop(_ecore_thread_pipe);
373 return ecore_pipe_add(_ecore_thread_handler, NULL);
376 #ifdef EFL_HAVE_THREADS
377 static int _ecore_thread_count = 0;
379 static Ecore_Event_Handler *del_handler = NULL;
380 static Eina_List *_ecore_active_job_threads = NULL;
381 static Eina_List *_ecore_pending_job_threads = NULL;
382 static Eina_List *_ecore_pending_job_threads_feedback = NULL;
383 static LK(_ecore_pending_job_threads_mutex);
385 static Eina_Hash *_ecore_thread_global_hash = NULL;
386 static LRWK(_ecore_thread_global_hash_lock);
387 static LK(_ecore_thread_global_hash_mutex);
388 static CD(_ecore_thread_global_hash_cond);
390 static PH(main_loop_thread);
391 static Eina_Bool have_main_loop_thread = 0;
393 static Eina_Trash *_ecore_thread_worker_trash = NULL;
394 static int _ecore_thread_worker_count = 0;
397 _ecore_thread_worker_free(Ecore_Pthread_Worker *worker)
399 if (_ecore_thread_worker_count > (_ecore_thread_count_max + 1) * 16)
405 eina_trash_push(&_ecore_thread_worker_trash, worker);
409 _ecore_thread_data_free(void *data)
411 Ecore_Thread_Data *d = data;
413 if (d->cb) d->cb(d->data);
418 _ecore_thread_pipe_free(void *data __UNUSED__, void *event)
420 Ecore_Pipe *p = event;
422 eina_array_push(_ecore_thread_pipe, p);
423 eina_threads_shutdown();
427 _ecore_thread_pipe_del(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
429 /* This is a hack to delay pipe destruction until we are out of its internal loop. */
430 return ECORE_CALLBACK_CANCEL;
434 _ecore_thread_end(Ecore_Pthread_Data *pth, __UNUSED__ Ecore_Thread *work)
438 if (PHJ(pth->thread, p) != 0)
441 _ecore_active_job_threads = eina_list_remove(_ecore_active_job_threads, pth);
443 ecore_event_add(ECORE_THREAD_PIPE_DEL, pth->p, _ecore_thread_pipe_free, NULL);
448 _ecore_thread_kill(Ecore_Pthread_Worker *work)
452 if (work->func_cancel)
453 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
458 work->func_end((void *) work->data, (Ecore_Thread *) work);
461 if (work->feedback_run)
463 ecore_pipe_del(work->u.feedback_run.notify);
465 if (work->u.feedback_run.direct_pipe)
466 eina_array_push(_ecore_thread_pipe, work->u.feedback_run.direct_pipe);
467 if (work->u.feedback_run.direct_worker)
468 _ecore_thread_worker_free(work->u.feedback_run.direct_worker);
473 eina_hash_free(work->hash);
478 _ecore_thread_handler(void *data __UNUSED__, void *buffer, unsigned int nbyte)
480 Ecore_Pthread_Worker *work;
482 if (nbyte != sizeof (Ecore_Pthread_Worker *)) return ;
484 work = *(Ecore_Pthread_Worker **)buffer;
486 if (work->feedback_run)
488 if (work->u.feedback_run.send != work->u.feedback_run.received)
490 work->kill = EINA_TRUE;
495 _ecore_thread_kill(work);
499 _ecore_notify_handler(void *data, void *buffer, unsigned int nbyte)
501 Ecore_Pthread_Worker *work = data;
504 if (nbyte != sizeof (Ecore_Pthread_Worker *)) return ;
506 user_data = *(void **)buffer;
507 work->u.feedback_run.received++;
509 if (work->u.feedback_run.func_notify)
510 work->u.feedback_run.func_notify((void *) work->data, (Ecore_Thread *) work, user_data);
512 /* Force reading all notify event before killing the thread */
513 if (work->kill && work->u.feedback_run.send == work->u.feedback_run.received)
515 _ecore_thread_kill(work);
520 _ecore_short_job(Ecore_Pipe *end_pipe)
522 Ecore_Pthread_Worker *work;
524 while (_ecore_pending_job_threads)
526 LKL(_ecore_pending_job_threads_mutex);
528 if (!_ecore_pending_job_threads)
530 LKU(_ecore_pending_job_threads_mutex);
534 work = eina_list_data_get(_ecore_pending_job_threads);
535 _ecore_pending_job_threads = eina_list_remove_list(_ecore_pending_job_threads,
536 _ecore_pending_job_threads);
538 LKU(_ecore_pending_job_threads_mutex);
541 work->u.short_run.func_blocking((void *) work->data, (Ecore_Thread*) work);
543 if (work->reschedule)
545 work->reschedule = EINA_FALSE;
547 LKL(_ecore_pending_job_threads_mutex);
548 _ecore_pending_job_threads = eina_list_append(_ecore_pending_job_threads, work);
549 LKU(_ecore_pending_job_threads_mutex);
553 ecore_pipe_write(end_pipe, &work, sizeof (Ecore_Pthread_Worker *));
559 _ecore_feedback_job(Ecore_Pipe *end_pipe, PH(thread))
561 Ecore_Pthread_Worker *work;
563 while (_ecore_pending_job_threads_feedback)
565 LKL(_ecore_pending_job_threads_mutex);
567 if (!_ecore_pending_job_threads_feedback)
569 LKU(_ecore_pending_job_threads_mutex);
573 work = eina_list_data_get(_ecore_pending_job_threads_feedback);
574 _ecore_pending_job_threads_feedback = eina_list_remove_list(_ecore_pending_job_threads_feedback,
575 _ecore_pending_job_threads_feedback);
577 LKU(_ecore_pending_job_threads_mutex);
581 work->u.feedback_run.func_heavy((void *) work->data, (Ecore_Thread *) work);
583 if (work->reschedule)
585 work->reschedule = EINA_FALSE;
587 LKL(_ecore_pending_job_threads_mutex);
588 _ecore_pending_job_threads_feedback = eina_list_append(_ecore_pending_job_threads_feedback, work);
589 LKU(_ecore_pending_job_threads_mutex);
593 ecore_pipe_write(end_pipe, &work, sizeof (Ecore_Pthread_Worker *));
599 _ecore_direct_worker(Ecore_Pthread_Worker *work)
601 Ecore_Pthread_Data *pth;
603 #ifdef EFL_POSIX_THREADS
604 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
605 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
608 eina_sched_prio_drop();
610 pth = malloc(sizeof (Ecore_Pthread_Data));
611 if (!pth) return NULL;
613 pth->p = work->u.feedback_run.direct_pipe;
621 work->self = pth->thread;
622 work->u.feedback_run.func_heavy((void *) work->data, (Ecore_Thread *) work);
624 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
626 work = work->u.feedback_run.direct_worker;
634 work->u.short_run.func_blocking = NULL;
635 work->func_end = (void *) _ecore_thread_end;
636 work->func_cancel = NULL;
637 work->cancel = EINA_FALSE;
638 work->feedback_run = EINA_FALSE;
639 work->kill = EINA_FALSE;
644 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
650 _ecore_thread_worker(Ecore_Pthread_Data *pth)
652 Ecore_Pthread_Worker *work;
654 #ifdef EFL_POSIX_THREADS
655 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
656 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
659 eina_sched_prio_drop();
661 LKL(_ecore_pending_job_threads_mutex);
662 _ecore_thread_count++;
663 LKU(_ecore_pending_job_threads_mutex);
666 if (_ecore_pending_job_threads) _ecore_short_job(pth->p);
667 if (_ecore_pending_job_threads_feedback) _ecore_feedback_job(pth->p, pth->thread);
669 /* FIXME: Check if there is feedback running task todo, and switch to feedback run handler. */
671 LKL(_ecore_pending_job_threads_mutex);
672 if (_ecore_pending_job_threads || _ecore_pending_job_threads_feedback)
674 LKU(_ecore_pending_job_threads_mutex);
677 LKU(_ecore_pending_job_threads_mutex);
679 /* Sleep a little to prevent premature death */
682 LKL(_ecore_pending_job_threads_mutex);
683 if (_ecore_pending_job_threads || _ecore_pending_job_threads_feedback)
685 LKU(_ecore_pending_job_threads_mutex);
688 _ecore_thread_count--;
689 LKU(_ecore_pending_job_threads_mutex);
691 work = pth->death_job;
692 if (!work) return NULL;
695 work->u.short_run.func_blocking = NULL;
696 work->func_end = (void *) _ecore_thread_end;
697 work->func_cancel = NULL;
698 work->cancel = EINA_FALSE;
699 work->feedback_run = EINA_FALSE;
700 work->kill = EINA_FALSE;
705 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
712 static Ecore_Pthread_Worker *
713 _ecore_thread_worker_new(void)
715 Ecore_Pthread_Worker *result;
717 #ifdef EFL_HAVE_THREADS
718 result = eina_trash_pop(&_ecore_thread_worker_trash);
720 if (!result) result = malloc(sizeof (Ecore_Pthread_Worker));
721 else _ecore_thread_worker_count--;
725 return malloc(sizeof (Ecore_Pthread_Worker));
730 _ecore_thread_init(void)
732 _ecore_thread_count_max = eina_cpu_count();
733 if (_ecore_thread_count_max <= 0)
734 _ecore_thread_count_max = 1;
736 ECORE_THREAD_PIPE_DEL = ecore_event_type_new();
737 _ecore_thread_pipe = eina_array_new(8);
739 #ifdef EFL_HAVE_THREADS
740 del_handler = ecore_event_handler_add(ECORE_THREAD_PIPE_DEL, _ecore_thread_pipe_del, NULL);
741 main_loop_thread = PHS();
742 have_main_loop_thread = 1;
744 LKI(_ecore_pending_job_threads_mutex);
745 LRWKI(_ecore_thread_global_hash_lock);
746 LKI(_ecore_thread_global_hash_mutex);
747 CDI(_ecore_thread_global_hash_cond);
752 _ecore_thread_shutdown(void)
754 /* FIXME: If function are still running in the background, should we kill them ? */
756 Eina_Array_Iterator it;
759 #ifdef EFL_HAVE_THREADS
760 Ecore_Pthread_Worker *work;
761 Ecore_Pthread_Data *pth;
763 LKL(_ecore_pending_job_threads_mutex);
765 EINA_LIST_FREE(_ecore_pending_job_threads, work)
767 if (work->func_cancel)
768 work->func_cancel((void *)work->data, (Ecore_Thread *) work);
772 EINA_LIST_FREE(_ecore_pending_job_threads_feedback, work)
774 if (work->func_cancel)
775 work->func_cancel((void *)work->data, (Ecore_Thread *) work);
779 LKU(_ecore_pending_job_threads_mutex);
781 /* Improve emergency shutdown */
782 EINA_LIST_FREE(_ecore_active_job_threads, pth)
789 ecore_pipe_del(pth->p);
791 if (_ecore_thread_global_hash)
792 eina_hash_free(_ecore_thread_global_hash);
793 ecore_event_handler_del(del_handler);
794 have_main_loop_thread = 0;
797 LKD(_ecore_pending_job_threads_mutex);
798 LRWKD(_ecore_thread_global_hash_lock);
799 LKD(_ecore_thread_global_hash_mutex);
800 CDD(_ecore_thread_global_hash_cond);
803 EINA_ARRAY_ITER_NEXT(_ecore_thread_pipe, i, p, it)
806 eina_array_free(_ecore_thread_pipe);
807 _ecore_thread_pipe = NULL;
811 * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
817 * @addtogroup Ecore_Thread_Group Ecore Thread functions
819 * These functions allow for ecore-managed threads which integrate with ecore's main loop.
825 * @brief Run some blocking code in a parallel thread to avoid locking the main loop.
826 * @param func_blocking The function that should run in another thread.
827 * @param func_end The function that will be called in the main loop if the thread terminate correctly.
828 * @param func_cancel The function that will be called in the main loop if the thread is cancelled.
829 * @param data User context data to pass to all callback.
830 * @return A reference to the newly created thread instance, or NULL if it failed.
832 * ecore_thread_run provide a facility for easily managing blocking task in a
833 * parallel thread. You should provide three function. The first one, func_blocking,
834 * that will do the blocking work in another thread (so you should not use the
835 * EFL in it except Eina if you are careful). The second one, func_end,
836 * that will be called in Ecore main loop when func_blocking is done. So you
837 * can use all the EFL inside this function. The last one, func_cancel, will
838 * be called in the main loop if the thread is cancelled or could not run at all.
840 * Be aware, that you can't make assumption on the result order of func_end
841 * after many call to ecore_thread_run, as we start as much thread as the
842 * host CPU can handle.
845 ecore_thread_run(Ecore_Thread_Cb func_blocking,
846 Ecore_Thread_Cb func_end,
847 Ecore_Thread_Cb func_cancel,
850 Ecore_Pthread_Worker *work;
851 #ifdef EFL_HAVE_THREADS
852 Ecore_Pthread_Data *pth = NULL;
855 if (!func_blocking) return NULL;
857 work = _ecore_thread_worker_new();
861 func_cancel((void *) data, NULL);
865 work->u.short_run.func_blocking = func_blocking;
866 work->func_end = func_end;
867 work->func_cancel = func_cancel;
868 work->cancel = EINA_FALSE;
869 work->feedback_run = EINA_FALSE;
870 work->kill = EINA_FALSE;
871 work->reschedule = EINA_FALSE;
874 #ifdef EFL_HAVE_THREADS
879 LKL(_ecore_pending_job_threads_mutex);
880 _ecore_pending_job_threads = eina_list_append(_ecore_pending_job_threads, work);
882 if (_ecore_thread_count == _ecore_thread_count_max)
884 LKU(_ecore_pending_job_threads_mutex);
885 return (Ecore_Thread *) work;
888 LKU(_ecore_pending_job_threads_mutex);
890 /* One more thread could be created. */
891 pth = malloc(sizeof (Ecore_Pthread_Data));
892 if (!pth) goto on_error;
894 pth->p = _ecore_thread_pipe_get();
895 pth->death_job = _ecore_thread_worker_new();
896 if (!pth->p || !pth->death_job) goto on_error;
900 if (PHC(pth->thread, _ecore_thread_worker, pth) == 0)
901 return (Ecore_Thread *) work;
903 eina_threads_shutdown();
908 if (pth->p) eina_array_push(_ecore_thread_pipe, pth->p);
909 if (pth->death_job) _ecore_thread_worker_free(pth->death_job);
913 if (_ecore_thread_count == 0)
915 LKL(_ecore_pending_job_threads_mutex);
916 _ecore_pending_job_threads = eina_list_remove(_ecore_pending_job_threads, work);
917 LKU(_ecore_pending_job_threads_mutex);
919 if (work->func_cancel)
920 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
924 return (Ecore_Thread *) work;
927 If no thread and as we don't want to break app that rely on this
928 facility, we will lock the interface until we are done.
931 /* Handle reschedule by forcing it here. That would mean locking the app,
932 * would be better with an idler, but really to complex for a case where
933 * thread should really exist.
935 work->reschedule = EINA_FALSE;
937 func_blocking((void *)data, (Ecore_Thread *) work);
938 if (work->cancel == EINA_FALSE) func_end((void *)data, (Ecore_Thread *) work);
939 else func_end((void *)data, (Ecore_Thread *) work);
941 } while (work->reschedule == EINA_TRUE);
950 * @brief Cancel a running thread.
951 * @param thread The thread to cancel.
952 * @return Will return EINA_TRUE if the thread has been cancelled,
953 * EINA_FALSE if it is pending.
955 * ecore_thread_cancel give the possibility to cancel a task still running. It
956 * will return EINA_FALSE, if the destruction is delayed or EINA_TRUE if it is
957 * cancelled after this call.
959 * This function work in the main loop and in the thread, but you should not pass
960 * the Ecore_Thread variable from main loop to the worker thread in any structure.
961 * You should always use the one passed to the Ecore_Thread_Heavy_Cb.
963 * func_end, func_cancel will destroy the handler, so don't use it after.
964 * And if ecore_thread_cancel return EINA_TRUE, you should not use Ecore_Thread also.
967 ecore_thread_cancel(Ecore_Thread *thread)
969 #ifdef EFL_HAVE_THREADS
970 Ecore_Pthread_Worker *work = (Ecore_Pthread_Worker *)thread;
978 if (work->feedback_run)
982 if (work->u.feedback_run.send != work->u.feedback_run.received)
986 LKL(_ecore_pending_job_threads_mutex);
988 if ((have_main_loop_thread) &&
989 (PHE(main_loop_thread, PHS())))
991 if (!work->feedback_run)
992 EINA_LIST_FOREACH(_ecore_pending_job_threads, l, work)
994 if ((void *) work == (void *) thread)
996 _ecore_pending_job_threads = eina_list_remove_list(_ecore_pending_job_threads, l);
998 LKU(_ecore_pending_job_threads_mutex);
1000 if (work->func_cancel)
1001 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
1008 EINA_LIST_FOREACH(_ecore_pending_job_threads_feedback, l, work)
1010 if ((void *) work == (void *) thread)
1012 _ecore_pending_job_threads_feedback = eina_list_remove_list(_ecore_pending_job_threads_feedback, l);
1014 LKU(_ecore_pending_job_threads_mutex);
1016 if (work->func_cancel)
1017 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
1025 LKU(_ecore_pending_job_threads_mutex);
1027 /* Delay the destruction */
1029 ((Ecore_Pthread_Worker *)thread)->cancel = EINA_TRUE;
1037 * @brief Tell if a thread was canceled or not.
1038 * @param thread The thread to test.
1039 * @return EINA_TRUE if the thread is cancelled,
1040 * EINA_FALSE if it is not.
1042 * You can use this function in main loop and in the thread.
1045 ecore_thread_check(Ecore_Thread *thread)
1047 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1049 if (!worker) return EINA_TRUE;
1050 return worker->cancel;
1054 * @brief Run some heavy code in a parallel thread to avoid locking the main loop.
1055 * @param func_heavy The function that should run in another thread.
1056 * @param func_notify The function that will receive the data send by func_heavy in the main loop.
1057 * @param func_end The function that will be called in the main loop if the thread terminate correctly.
1058 * @param func_cancel The function that will be called in the main loop if the thread is cancelled.
1059 * @param data User context data to pass to all callback.
1060 * @param try_no_queue If you want to run outside of the thread pool.
1061 * @return A reference to the newly created thread instance, or NULL if it failed.
1063 * ecore_thread_feedback_run provide a facility for easily managing heavy task in a
1064 * parallel thread. You should provide four functions. The first one, func_heavy,
1065 * that will do the heavy work in another thread (so you should not use the
1066 * EFL in it except Eina and Eet if you are careful). The second one, func_notify,
1067 * will receive the data send from the thread function (func_heavy) by ecore_thread_feedback
1068 * in the main loop (and so, can use all the EFL). The third, func_end,
1069 * that will be called in Ecore main loop when func_heavy is done. So you
1070 * can use all the EFL inside this function. The last one, func_cancel, will
1071 * be called in the main loop also, if the thread is cancelled or could not run at all.
1073 * Be aware, that you can't make assumption on the result order of func_end
1074 * after many call to ecore_feedback_run, as we start as much thread as the
1075 * host CPU can handle.
1077 * If you set try_no_queue, it will try to run outside of the thread pool, this can bring
1078 * the CPU down, so be careful with that. Of course if it can't start a new thread, it will
1079 * try to use one from the pool.
1081 EAPI Ecore_Thread *ecore_thread_feedback_run(Ecore_Thread_Cb func_heavy,
1082 Ecore_Thread_Notify_Cb func_notify,
1083 Ecore_Thread_Cb func_end,
1084 Ecore_Thread_Cb func_cancel,
1086 Eina_Bool try_no_queue)
1089 #ifdef EFL_HAVE_THREADS
1090 Ecore_Pthread_Worker *worker;
1091 Ecore_Pthread_Data *pth = NULL;
1093 if (!func_heavy) return NULL;
1095 worker = _ecore_thread_worker_new();
1096 if (!worker) goto on_error;
1098 worker->u.feedback_run.func_heavy = func_heavy;
1099 worker->u.feedback_run.func_notify = func_notify;
1100 worker->hash = NULL;
1103 worker->func_cancel = func_cancel;
1104 worker->func_end = func_end;
1105 worker->data = data;
1106 worker->cancel = EINA_FALSE;
1107 worker->feedback_run = EINA_TRUE;
1108 worker->kill = EINA_FALSE;
1109 worker->reschedule = EINA_FALSE;
1111 worker->u.feedback_run.send = 0;
1112 worker->u.feedback_run.received = 0;
1114 worker->u.feedback_run.notify = ecore_pipe_add(_ecore_notify_handler, worker);
1115 worker->u.feedback_run.direct_pipe = NULL;
1116 worker->u.feedback_run.direct_worker = NULL;
1122 worker->u.feedback_run.direct_pipe = _ecore_thread_pipe_get();
1123 worker->u.feedback_run.direct_worker = _ecore_thread_worker_new();
1125 if (PHC(t, _ecore_direct_worker, worker) == 0)
1126 return (Ecore_Thread *) worker;
1129 LKL(_ecore_pending_job_threads_mutex);
1130 _ecore_pending_job_threads_feedback = eina_list_append(_ecore_pending_job_threads_feedback, worker);
1132 if (_ecore_thread_count == _ecore_thread_count_max)
1134 LKU(_ecore_pending_job_threads_mutex);
1135 return (Ecore_Thread *) worker;
1138 LKU(_ecore_pending_job_threads_mutex);
1140 /* One more thread could be created. */
1141 pth = malloc(sizeof (Ecore_Pthread_Data));
1142 if (!pth) goto on_error;
1144 pth->p = _ecore_thread_pipe_get();
1145 pth->death_job = _ecore_thread_worker_new();
1146 if (!pth->p || !pth->death_job) goto on_error;
1148 eina_threads_init();
1150 if (PHC(pth->thread, _ecore_thread_worker, pth) == 0)
1151 return (Ecore_Thread *) worker;
1153 eina_threads_shutdown();
1158 if (pth->p) eina_array_push(_ecore_thread_pipe, pth->p);
1159 if (pth->death_job) _ecore_thread_worker_free(pth->death_job);
1163 if (_ecore_thread_count == 0)
1165 LKL(_ecore_pending_job_threads_mutex);
1166 _ecore_pending_job_threads_feedback = eina_list_remove(_ecore_pending_job_threads_feedback,
1168 LKU(_ecore_pending_job_threads_mutex);
1170 if (func_cancel) func_cancel((void *) data, NULL);
1174 ecore_pipe_del(worker->u.feedback_run.notify);
1180 return (Ecore_Thread *) worker;
1182 Ecore_Pthread_Worker worker;
1184 (void) try_no_queue;
1187 If no thread and as we don't want to break app that rely on this
1188 facility, we will lock the interface until we are done.
1190 worker.u.feedback_run.func_heavy = func_heavy;
1191 worker.u.feedback_run.func_notify = func_notify;
1192 worker.u.feedback_run.notify = NULL;
1193 worker.u.feedback_run.send = 0;
1194 worker.u.feedback_run.received = 0;
1195 worker.func_cancel = func_cancel;
1196 worker.func_end = func_end;
1198 worker.cancel = EINA_FALSE;
1199 worker.feedback_run = EINA_TRUE;
1200 worker.kill = EINA_FALSE;
1203 worker.reschedule = EINA_FALSE;
1205 func_heavy((void *)data, (Ecore_Thread *) &worker);
1207 if (worker.cancel) func_cancel((void *)data, (Ecore_Thread *) &worker);
1208 else func_end((void *)data, (Ecore_Thread *) &worker);
1209 } while (worker.reschedule == EINA_FALSE);
1216 * @brief Send data to main loop from worker thread.
1217 * @param thread The current Ecore_Thread context to send data from
1218 * @param data Data to be transmitted to the main loop
1219 * @return EINA_TRUE if data was successfully send to main loop,
1220 * EINA_FALSE if anything goes wrong.
1222 * After a succesfull call, the data should be considered owned
1225 * You should use this function only in the func_heavy call.
1228 ecore_thread_feedback(Ecore_Thread *thread, const void *data)
1230 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1232 if (!worker) return EINA_FALSE;
1233 if (!worker->feedback_run) return EINA_FALSE;
1235 #ifdef EFL_HAVE_THREADS
1236 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1238 worker->u.feedback_run.send++;
1239 ecore_pipe_write(worker->u.feedback_run.notify, &data, sizeof (void *));
1243 worker->u.feedback_run.func_notify((void*) worker->data, thread, (void*) data);
1250 * @brief Plan to recall the heavy function once it exist it.
1251 * @param thread The current Ecore_Thread context to reschedule
1252 * @return EINA_TRUE if data was successfully send to main loop,
1253 * EINA_FALSE if anything goes wrong.
1255 * After a succesfull call, you can still do what you want in your thread, it
1256 * will only reschedule it once you exit the heavy loop.
1258 * You should use this function only in the func_heavy call.
1261 ecore_thread_reschedule(Ecore_Thread *thread)
1263 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1265 if (!worker) return EINA_FALSE;
1267 #ifdef EFL_HAVE_THREADS
1268 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1271 worker->reschedule = EINA_TRUE;
1276 * @brief Get number of active thread jobs
1277 * @return Number of active threads running jobs
1278 * This returns the number of threads currently running jobs through the
1282 ecore_thread_active_get(void)
1284 #ifdef EFL_HAVE_THREADS
1285 return _ecore_thread_count;
1292 * @brief Get number of pending (short) thread jobs
1293 * @return Number of pending threads running "short" jobs
1294 * This returns the number of threads currently running jobs through the
1295 * ecore_thread_run api call.
1298 ecore_thread_pending_get(void)
1301 #ifdef EFL_HAVE_THREADS
1302 LKL(_ecore_pending_job_threads_mutex);
1303 ret = eina_list_count(_ecore_pending_job_threads);
1304 LKU(_ecore_pending_job_threads_mutex);
1312 * @brief Get number of pending feedback thread jobs
1313 * @return Number of pending threads running "feedback" jobs
1314 * This returns the number of threads currently running jobs through the
1315 * ecore_thread_feedback_run api call.
1318 ecore_thread_pending_feedback_get(void)
1321 #ifdef EFL_HAVE_THREADS
1322 LKL(_ecore_pending_job_threads_mutex);
1323 ret = eina_list_count(_ecore_pending_job_threads_feedback);
1324 LKU(_ecore_pending_job_threads_mutex);
1332 * @brief Get number of pending thread jobs
1333 * @return Number of pending threads running jobs
1334 * This returns the number of threads currently running jobs through the
1335 * ecore_thread_run and ecore_thread_feedback_run api calls combined.
1338 ecore_thread_pending_total_get(void)
1341 #ifdef EFL_HAVE_THREADS
1342 LKL(_ecore_pending_job_threads_mutex);
1343 ret = eina_list_count(_ecore_pending_job_threads) + eina_list_count(_ecore_pending_job_threads_feedback);
1344 LKU(_ecore_pending_job_threads_mutex);
1352 * @brief Get the max number of threads that can run simultaneously
1353 * @return Max number of threads ecore will run
1354 * This returns the total number of threads that ecore will attempt to run
1358 ecore_thread_max_get(void)
1360 return _ecore_thread_count_max;
1364 * @brief Set the max number of threads that can run simultaneously
1365 * @param num The new maximum
1366 * This sets the maximum number of threads that ecore will try to run
1367 * simultaneously. This number cannot be < 1 or >= 2x the number of active cpus.
1370 ecore_thread_max_set(int num)
1372 if (num < 1) return;
1373 /* avoid doing something hilarious by blocking dumb users */
1374 if (num >= (2 * eina_cpu_count())) return;
1376 _ecore_thread_count_max = num;
1380 * @brief Reset the max number of threads that can run simultaneously
1381 * This resets the maximum number of threads that ecore will try to run
1382 * simultaneously to the number of active cpus.
1385 ecore_thread_max_reset(void)
1387 _ecore_thread_count_max = eina_cpu_count();
1391 * @brief Get the number of threads which are available to be used
1392 * @return The number of available threads
1393 * This returns the number of threads slots that ecore has currently available.
1394 * Assuming that you haven't changed the max number of threads with @ref ecore_thread_max_set
1395 * this should be equal to (num_cpus - (active_running + active_feedback_running))
1398 ecore_thread_available_get(void)
1401 #ifdef EFL_HAVE_THREADS
1402 LKL(_ecore_pending_job_threads_mutex);
1403 ret = _ecore_thread_count_max - _ecore_thread_count;
1404 LKU(_ecore_pending_job_threads_mutex);
1412 * @brief Add data to the thread for subsequent use
1413 * @param thread The thread context to add to
1414 * @param key The name string to add the data with
1415 * @param value The data to add
1416 * @param cb The callback to free the data with
1417 * @param direct If true, this will not copy the key string (like eina_hash_direct_add)
1418 * @return EINA_TRUE on success, EINA_FALSE on failure
1419 * This adds data to the thread context, allowing the thread
1420 * to retrieve and use it without complicated mutexing. This function can only be called by a
1421 * *_run thread INSIDE the thread and will return EINA_FALSE in any case but success.
1422 * All data added to the thread will be freed with its associated callback (if present)
1423 * upon thread termination. If no callback is specified, it is expected that the user will free the
1424 * data, but this is most likely not what you want.
1427 ecore_thread_local_data_add(Ecore_Thread *thread, const char *key, void *value, Eina_Free_Cb cb, Eina_Bool direct)
1429 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1430 Ecore_Thread_Data *d;
1433 if ((!thread) || (!key) || (!value))
1435 #ifdef EFL_HAVE_THREADS
1436 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1439 worker->hash = eina_hash_string_small_new(_ecore_thread_data_free);
1444 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1451 ret = eina_hash_direct_add(worker->hash, key, d);
1453 ret = eina_hash_add(worker->hash, key, d);
1462 * @brief Modify data in the thread, or add if not found
1463 * @param thread The thread context
1464 * @param key The name string to add the data with
1465 * @param value The data to add
1466 * @param cb The callback to free the data with
1467 * @return The old data associated with @p key on success if modified, NULL if added
1468 * This adds/modifies data in the thread context, adding only if modify fails.
1469 * This function can only be called by a *_run thread INSIDE the thread.
1470 * All data added to the thread pool will be freed with its associated callback (if present)
1471 * upon thread termination. If no callback is specified, it is expected that the user will free the
1472 * data, but this is most likely not what you want.
1475 ecore_thread_local_data_set(Ecore_Thread *thread, const char *key, void *value, Eina_Free_Cb cb)
1477 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1478 Ecore_Thread_Data *d, *r;
1480 if ((!thread) || (!key) || (!value))
1482 #ifdef EFL_HAVE_THREADS
1483 if (!PHE(worker->self, PHS())) return NULL;
1486 worker->hash = eina_hash_string_small_new(_ecore_thread_data_free);
1491 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1497 r = eina_hash_set(worker->hash, key, d);
1508 * @brief Find data in the thread's data
1509 * @param thread The thread context
1510 * @param key The name string the data is associated with
1511 * @return The value, or NULL on error
1512 * This finds data in the thread context that has been previously added with @ref ecore_thread_local_data_add
1513 * This function can only be called by a *_run thread INSIDE the thread, and will return NULL
1514 * in any case but success.
1518 ecore_thread_local_data_find(Ecore_Thread *thread, const char *key)
1520 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1521 Ecore_Thread_Data *d;
1523 if ((!thread) || (!key))
1525 #ifdef EFL_HAVE_THREADS
1526 if (!PHE(worker->self, PHS())) return NULL;
1531 d = eina_hash_find(worker->hash, key);
1539 * @brief Delete data from the thread's data
1540 * @param thread The thread context
1541 * @param key The name string the data is associated with
1542 * @return EINA_TRUE on success, EINA_FALSE on failure
1543 * This deletes the data pointer from the thread context which was previously added with @ref ecore_thread_local_data_add
1544 * This function can only be called by a *_run thread INSIDE the thread, and will return EINA_FALSE
1545 * in any case but success. Note that this WILL free the data if a callback was specified.
1548 ecore_thread_local_data_del(Ecore_Thread *thread, const char *key)
1550 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1551 Ecore_Thread_Data *d;
1552 if ((!thread) || (!key))
1554 #ifdef EFL_HAVE_THREADS
1555 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1559 if ((d = eina_hash_find(worker->hash, key)))
1560 _ecore_thread_data_free(d);
1561 return eina_hash_del_by_key(worker->hash, key);
1568 * @brief Add data to the global data
1569 * @param key The name string to add the data with
1570 * @param value The data to add
1571 * @param cb The optional callback to free the data with once ecore is shut down
1572 * @param direct If true, this will not copy the key string (like eina_hash_direct_add)
1573 * @return EINA_TRUE on success, EINA_FALSE on failure
1574 * This adds data to the global thread data, and will return EINA_FALSE in any case but success.
1575 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1576 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1577 * was specified for, you will most likely encounter a segv later on.
1580 ecore_thread_global_data_add(const char *key, void *value, Eina_Free_Cb cb, Eina_Bool direct)
1583 Ecore_Thread_Data *d;
1585 if ((!key) || (!value))
1587 #ifdef EFL_HAVE_THREADS
1588 LRWKWL(_ecore_thread_global_hash_lock);
1589 if (!_ecore_thread_global_hash)
1590 _ecore_thread_global_hash = eina_hash_string_small_new(_ecore_thread_data_free);
1591 LRWKU(_ecore_thread_global_hash_lock);
1593 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1599 if (!_ecore_thread_global_hash)
1601 LRWKWL(_ecore_thread_global_hash_lock);
1603 ret = eina_hash_direct_add(_ecore_thread_global_hash, key, d);
1605 ret = eina_hash_add(_ecore_thread_global_hash, key, d);
1606 LRWKU(_ecore_thread_global_hash_lock);
1607 CDB(_ecore_thread_global_hash_cond);
1615 * @brief Add data to the global data
1616 * @param key The name string to add the data with
1617 * @param value The data to add
1618 * @param cb The optional callback to free the data with once ecore is shut down
1619 * @return An Ecore_Thread_Data on success, NULL on failure
1620 * This adds data to the global thread data and returns NULL, or replaces the previous data
1621 * associated with @p key and returning the previous data if it existed. To see if an error occurred,
1622 * one must use eina_error_get.
1623 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1624 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1625 * was specified for, you will most likely encounter a segv later on.
1628 ecore_thread_global_data_set(const char *key, void *value, Eina_Free_Cb cb)
1630 Ecore_Thread_Data *d, *r;
1633 if ((!key) || (!value))
1635 #ifdef EFL_HAVE_THREADS
1636 LRWKWL(_ecore_thread_global_hash_lock);
1637 if (!_ecore_thread_global_hash)
1638 _ecore_thread_global_hash = eina_hash_string_small_new(_ecore_thread_data_free);
1639 LRWKU(_ecore_thread_global_hash_lock);
1641 if (!_ecore_thread_global_hash)
1644 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1650 LRWKWL(_ecore_thread_global_hash_lock);
1651 r = eina_hash_set(_ecore_thread_global_hash, key, d);
1652 LRWKU(_ecore_thread_global_hash_lock);
1653 CDB(_ecore_thread_global_hash_cond);
1664 * @brief Find data in the global data
1665 * @param key The name string the data is associated with
1666 * @return The value, or NULL on error
1667 * This finds data in the global data that has been previously added with @ref ecore_thread_global_data_add
1668 * This function will return NULL in any case but success.
1669 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1670 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1671 * was specified for, you will most likely encounter a segv later on.
1672 * @note Keep in mind that the data returned can be used by multiple threads at a time, so you will most likely want to mutex
1673 * if you will be doing anything with it.
1677 ecore_thread_global_data_find(const char *key)
1679 Ecore_Thread_Data *ret;
1682 #ifdef EFL_HAVE_THREADS
1683 if (!_ecore_thread_global_hash) return NULL;
1685 LRWKRL(_ecore_thread_global_hash_lock);
1686 ret = eina_hash_find(_ecore_thread_global_hash, key);
1687 LRWKU(_ecore_thread_global_hash_lock);
1695 * @brief Delete data from the global data
1696 * @param key The name string the data is associated with
1697 * @return EINA_TRUE on success, EINA_FALSE on failure
1698 * This deletes the data pointer from the global data which was previously added with @ref ecore_thread_global_data_add
1699 * This function will return EINA_FALSE in any case but success.
1700 * Note that this WILL free the data if an @c Eina_Free_Cb was specified when the data was added.
1703 ecore_thread_global_data_del(const char *key)
1706 Ecore_Thread_Data *d;
1710 #ifdef EFL_HAVE_THREADS
1711 if (!_ecore_thread_global_hash)
1714 LRWKWL(_ecore_thread_global_hash_lock);
1715 if ((d = eina_hash_find(_ecore_thread_global_hash, key)))
1716 _ecore_thread_data_free(d);
1717 ret = eina_hash_del_by_key(_ecore_thread_global_hash, key);
1718 LRWKU(_ecore_thread_global_hash_lock);
1726 * @brief Find data in the global data and optionally wait for the data if not found
1727 * @param key The name string the data is associated with
1728 * @param seconds The amount of time in seconds to wait for the data. If 0, the call will be async and not wait for data.
1729 * If < 0 the call will wait indefinitely for the data.
1730 * @return The value, or NULL on failure
1731 * This finds data in the global data that has been previously added with @ref ecore_thread_global_data_add
1732 * This function will return NULL in any case but success.
1733 * Use @p seconds to specify the amount of time to wait. Use > 0 for an actual wait time, 0 to not wait, and < 0 to wait indefinitely.
1734 * @note Keep in mind that the data returned can be used by multiple threads at a time, so you will most likely want to mutex
1735 * if you will be doing anything with it.
1738 ecore_thread_global_data_wait(const char *key, double seconds)
1741 Ecore_Thread_Data *ret = NULL;
1744 #ifdef EFL_HAVE_THREADS
1745 if (!_ecore_thread_global_hash)
1748 time = ecore_time_get() + seconds;
1753 struct timespec t = { 0, 0 };
1755 t.tv_sec = (long int)time;
1756 t.tv_nsec = (long int)((time - (double)t.tv_sec) * 1000000000);
1758 struct timeval t = { 0, 0 };
1760 t.tv_sec = (long int)time;
1761 t.tv_usec = (long int)((time - (double)t.tv_sec) * 1000000);
1763 LRWKRL(_ecore_thread_global_hash_lock);
1764 ret = eina_hash_find(_ecore_thread_global_hash, key);
1765 LRWKU(_ecore_thread_global_hash_lock);
1766 if ((ret) || (!seconds) || ((seconds > 0) && (time <= ecore_time_get())))
1768 LKL(_ecore_thread_global_hash_mutex);
1769 CDW(_ecore_thread_global_hash_cond, _ecore_thread_global_hash_mutex, &t);
1770 LKU(_ecore_thread_global_hash_mutex);
1772 if (ret) return ret->data;