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;
101 # define PHJ(x, p) _ecore_thread_win32_join(x, (void**)(&(p)))
102 # define PHA(x) TerminateThread(x->thread, 0)
104 # define LK(x) HANDLE x
105 # define LKI(x) x = CreateMutex(NULL, FALSE, NULL)
106 # define LKD(x) CloseHandle(x)
107 # define LKL(x) WaitForSingleObject(x, INFINITE)
108 # define LKU(x) ReleaseMutex(x)
114 CRITICAL_SECTION threads_count_lock;
117 # define CD(x) win32_cond *x
121 x = (win32_cond *)calloc(1, sizeof(win32_cond)); \
124 x->semaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL); \
126 InitializeCriticalSection(&x->threads_count_lock); \
137 CloseHandle(x->semaphore); \
144 EnterCriticalSection(&x->threads_count_lock); \
145 if (x->threads_count > 0) \
146 ReleaseSemaphore(x->semaphore, x->threads_count, NULL); \
147 LeaveCriticalSection (&x->threads_count_lock); \
150 int _ecore_thread_win32_cond_timedwait(win32_cond *c, HANDLE *external_mutex, struct timeval *t)
153 DWORD val = t->tv_sec * 1000 + (t->tv_usec / 1000);
155 EnterCriticalSection (&c->threads_count_lock);
157 LeaveCriticalSection (&c->threads_count_lock);
159 res = WaitForSingleObject(c->semaphore, val);
160 if (res == WAIT_OBJECT_0)
165 # define CDW(x, y, t) _ecore_thread_win32_cond_timedwait(x, y, t)
178 # define LRWK(x) win32_rwl *x
181 x = (win32_rwl *)calloc(1, sizeof(win32_rwl)); \
190 CDI(x->cond_write); \
191 if (!x->cond_write) \
218 CDD(x->cond_write); \
226 if (x->writers || x->readers > 0) \
228 x->writers_count++; \
229 while (x->writers || x->readers > 0) \
231 EnterCriticalSection(&x->cond_write->threads_count_lock); \
232 x->cond_read->threads_count++; \
233 LeaveCriticalSection(&x->cond_write->threads_count_lock); \
234 res = WaitForSingleObject(x->cond_write->semaphore, INFINITE); \
235 if (res != WAIT_OBJECT_0) break; \
237 x->writers_count--; \
239 if (res == 0) x->writers_count = 1; \
248 x->readers_count++; \
251 EnterCriticalSection(&x->cond_write->threads_count_lock); \
252 x->cond_read->threads_count++; \
253 LeaveCriticalSection(&x->cond_write->threads_count_lock); \
254 res = WaitForSingleObject(x->cond_write->semaphore, INFINITE); \
255 if (res != WAIT_OBJECT_0) break; \
257 x->readers_count--; \
269 if (x->readers_count == 1) \
271 EnterCriticalSection(&x->cond_read->threads_count_lock); \
272 if (x->cond_read->threads_count > 0) \
273 ReleaseSemaphore(x->cond_read->semaphore, 1, 0); \
274 LeaveCriticalSection(&x->cond_read->threads_count_lock); \
276 else if (x->readers_count > 0) \
278 else if (x->writers_count > 0) \
280 EnterCriticalSection (&x->cond_write->threads_count_lock); \
281 if (x->cond_write->threads_count > 0) \
282 ReleaseSemaphore(x->cond_write->semaphore, 1, 0); \
283 LeaveCriticalSection (&x->cond_write->threads_count_lock); \
286 else if (x->readers > 0) \
289 if (x->readers == 0 && x->writers_count > 0) \
291 EnterCriticalSection (&x->cond_write->threads_count_lock); \
292 if (x->cond_write->threads_count > 0) \
293 ReleaseSemaphore(x->cond_write->semaphore, 1, 0); \
294 LeaveCriticalSection (&x->cond_write->threads_count_lock); \
304 typedef struct _Ecore_Pthread_Worker Ecore_Pthread_Worker;
305 typedef struct _Ecore_Pthread Ecore_Pthread;
306 typedef struct _Ecore_Thread_Data Ecore_Thread_Data;
308 struct _Ecore_Thread_Data
314 struct _Ecore_Pthread_Worker
318 Ecore_Thread_Cb func_blocking;
321 Ecore_Thread_Cb func_heavy;
322 Ecore_Thread_Notify_Cb func_notify;
325 Ecore_Pipe *direct_pipe;
326 Ecore_Pthread_Worker *direct_worker;
333 Ecore_Thread_Cb func_cancel;
334 Ecore_Thread_Cb func_end;
335 #ifdef EFL_HAVE_THREADS
344 Eina_Bool cancel : 1;
345 Eina_Bool feedback_run : 1;
347 Eina_Bool reschedule : 1;
350 #ifdef EFL_HAVE_THREADS
351 typedef struct _Ecore_Pthread_Data Ecore_Pthread_Data;
353 struct _Ecore_Pthread_Data
355 Ecore_Pthread_Worker *death_job;
362 static void _ecore_thread_handler(void *data __UNUSED__, void *buffer, unsigned int nbyte);
364 static int _ecore_thread_count_max = 0;
365 static int ECORE_THREAD_PIPE_DEL = 0;
366 static Eina_Array *_ecore_thread_pipe = NULL;
369 _ecore_thread_pipe_get(void)
371 if (eina_array_count_get(_ecore_thread_pipe) > 0)
372 return eina_array_pop(_ecore_thread_pipe);
374 return ecore_pipe_add(_ecore_thread_handler, NULL);
377 #ifdef EFL_HAVE_THREADS
378 static int _ecore_thread_count = 0;
380 static Ecore_Event_Handler *del_handler = NULL;
381 static Eina_List *_ecore_active_job_threads = NULL;
382 static Eina_List *_ecore_pending_job_threads = NULL;
383 static Eina_List *_ecore_pending_job_threads_feedback = NULL;
384 static LK(_ecore_pending_job_threads_mutex);
386 static Eina_Hash *_ecore_thread_global_hash = NULL;
387 static LRWK(_ecore_thread_global_hash_lock);
388 static LK(_ecore_thread_global_hash_mutex);
389 static CD(_ecore_thread_global_hash_cond);
391 static PH(main_loop_thread);
392 static Eina_Bool have_main_loop_thread = 0;
394 static Eina_Trash *_ecore_thread_worker_trash = NULL;
395 static int _ecore_thread_worker_count = 0;
398 _ecore_thread_worker_free(Ecore_Pthread_Worker *worker)
400 if (_ecore_thread_worker_count > (_ecore_thread_count_max + 1) * 16)
406 eina_trash_push(&_ecore_thread_worker_trash, worker);
410 _ecore_thread_data_free(void *data)
412 Ecore_Thread_Data *d = data;
414 if (d->cb) d->cb(d->data);
419 _ecore_thread_pipe_free(void *data __UNUSED__, void *event)
421 Ecore_Pipe *p = event;
423 eina_array_push(_ecore_thread_pipe, p);
424 eina_threads_shutdown();
428 _ecore_thread_pipe_del(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
430 /* This is a hack to delay pipe destruction until we are out of its internal loop. */
431 return ECORE_CALLBACK_CANCEL;
435 _ecore_thread_end(Ecore_Pthread_Data *pth, __UNUSED__ Ecore_Thread *work)
439 if (PHJ(pth->thread, p) != 0)
442 _ecore_active_job_threads = eina_list_remove(_ecore_active_job_threads, pth);
444 ecore_event_add(ECORE_THREAD_PIPE_DEL, pth->p, _ecore_thread_pipe_free, NULL);
449 _ecore_thread_kill(Ecore_Pthread_Worker *work)
453 if (work->func_cancel)
454 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
459 work->func_end((void *) work->data, (Ecore_Thread *) work);
462 if (work->feedback_run)
464 ecore_pipe_del(work->u.feedback_run.notify);
466 if (work->u.feedback_run.direct_pipe)
467 eina_array_push(_ecore_thread_pipe, work->u.feedback_run.direct_pipe);
468 if (work->u.feedback_run.direct_worker)
469 _ecore_thread_worker_free(work->u.feedback_run.direct_worker);
474 eina_hash_free(work->hash);
479 _ecore_thread_handler(void *data __UNUSED__, void *buffer, unsigned int nbyte)
481 Ecore_Pthread_Worker *work;
483 if (nbyte != sizeof (Ecore_Pthread_Worker *)) return ;
485 work = *(Ecore_Pthread_Worker **)buffer;
487 if (work->feedback_run)
489 if (work->u.feedback_run.send != work->u.feedback_run.received)
491 work->kill = EINA_TRUE;
496 _ecore_thread_kill(work);
500 _ecore_notify_handler(void *data, void *buffer, unsigned int nbyte)
502 Ecore_Pthread_Worker *work = data;
505 if (nbyte != sizeof (Ecore_Pthread_Worker *)) return ;
507 user_data = *(void **)buffer;
508 work->u.feedback_run.received++;
510 if (work->u.feedback_run.func_notify)
511 work->u.feedback_run.func_notify((void *) work->data, (Ecore_Thread *) work, user_data);
513 /* Force reading all notify event before killing the thread */
514 if (work->kill && work->u.feedback_run.send == work->u.feedback_run.received)
516 _ecore_thread_kill(work);
521 _ecore_short_job(Ecore_Pipe *end_pipe)
523 Ecore_Pthread_Worker *work;
525 while (_ecore_pending_job_threads)
527 LKL(_ecore_pending_job_threads_mutex);
529 if (!_ecore_pending_job_threads)
531 LKU(_ecore_pending_job_threads_mutex);
535 work = eina_list_data_get(_ecore_pending_job_threads);
536 _ecore_pending_job_threads = eina_list_remove_list(_ecore_pending_job_threads,
537 _ecore_pending_job_threads);
539 LKU(_ecore_pending_job_threads_mutex);
542 work->u.short_run.func_blocking((void *) work->data, (Ecore_Thread*) work);
544 if (work->reschedule)
546 work->reschedule = EINA_FALSE;
548 LKL(_ecore_pending_job_threads_mutex);
549 _ecore_pending_job_threads = eina_list_append(_ecore_pending_job_threads, work);
550 LKU(_ecore_pending_job_threads_mutex);
554 ecore_pipe_write(end_pipe, &work, sizeof (Ecore_Pthread_Worker *));
560 _ecore_feedback_job(Ecore_Pipe *end_pipe, PH(thread))
562 Ecore_Pthread_Worker *work;
564 while (_ecore_pending_job_threads_feedback)
566 LKL(_ecore_pending_job_threads_mutex);
568 if (!_ecore_pending_job_threads_feedback)
570 LKU(_ecore_pending_job_threads_mutex);
574 work = eina_list_data_get(_ecore_pending_job_threads_feedback);
575 _ecore_pending_job_threads_feedback = eina_list_remove_list(_ecore_pending_job_threads_feedback,
576 _ecore_pending_job_threads_feedback);
578 LKU(_ecore_pending_job_threads_mutex);
582 work->u.feedback_run.func_heavy((void *) work->data, (Ecore_Thread *) work);
584 if (work->reschedule)
586 work->reschedule = EINA_FALSE;
588 LKL(_ecore_pending_job_threads_mutex);
589 _ecore_pending_job_threads_feedback = eina_list_append(_ecore_pending_job_threads_feedback, work);
590 LKU(_ecore_pending_job_threads_mutex);
594 ecore_pipe_write(end_pipe, &work, sizeof (Ecore_Pthread_Worker *));
600 _ecore_direct_worker(Ecore_Pthread_Worker *work)
602 Ecore_Pthread_Data *pth;
604 #ifdef EFL_POSIX_THREADS
605 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
606 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
609 eina_sched_prio_drop();
611 pth = malloc(sizeof (Ecore_Pthread_Data));
612 if (!pth) return NULL;
614 pth->p = work->u.feedback_run.direct_pipe;
622 work->self = pth->thread;
623 work->u.feedback_run.func_heavy((void *) work->data, (Ecore_Thread *) work);
625 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
627 work = work->u.feedback_run.direct_worker;
635 work->u.short_run.func_blocking = NULL;
636 work->func_end = (void *) _ecore_thread_end;
637 work->func_cancel = NULL;
638 work->cancel = EINA_FALSE;
639 work->feedback_run = EINA_FALSE;
640 work->kill = EINA_FALSE;
645 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
651 _ecore_thread_worker(Ecore_Pthread_Data *pth)
653 Ecore_Pthread_Worker *work;
655 #ifdef EFL_POSIX_THREADS
656 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
657 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
660 eina_sched_prio_drop();
662 LKL(_ecore_pending_job_threads_mutex);
663 _ecore_thread_count++;
664 LKU(_ecore_pending_job_threads_mutex);
667 if (_ecore_pending_job_threads) _ecore_short_job(pth->p);
668 if (_ecore_pending_job_threads_feedback) _ecore_feedback_job(pth->p, pth->thread);
670 /* FIXME: Check if there is feedback running task todo, and switch to feedback run handler. */
672 LKL(_ecore_pending_job_threads_mutex);
673 if (_ecore_pending_job_threads || _ecore_pending_job_threads_feedback)
675 LKU(_ecore_pending_job_threads_mutex);
678 LKU(_ecore_pending_job_threads_mutex);
680 /* Sleep a little to prevent premature death */
682 Sleep(1); /* around 50ms */
687 LKL(_ecore_pending_job_threads_mutex);
688 if (_ecore_pending_job_threads || _ecore_pending_job_threads_feedback)
690 LKU(_ecore_pending_job_threads_mutex);
693 _ecore_thread_count--;
694 LKU(_ecore_pending_job_threads_mutex);
696 work = pth->death_job;
697 if (!work) return NULL;
700 work->u.short_run.func_blocking = NULL;
701 work->func_end = (void *) _ecore_thread_end;
702 work->func_cancel = NULL;
703 work->cancel = EINA_FALSE;
704 work->feedback_run = EINA_FALSE;
705 work->kill = EINA_FALSE;
710 ecore_pipe_write(pth->p, &work, sizeof (Ecore_Pthread_Worker *));
717 static Ecore_Pthread_Worker *
718 _ecore_thread_worker_new(void)
720 Ecore_Pthread_Worker *result;
722 #ifdef EFL_HAVE_THREADS
723 result = eina_trash_pop(&_ecore_thread_worker_trash);
725 if (!result) result = malloc(sizeof (Ecore_Pthread_Worker));
726 else _ecore_thread_worker_count--;
730 return malloc(sizeof (Ecore_Pthread_Worker));
735 _ecore_thread_init(void)
737 _ecore_thread_count_max = eina_cpu_count();
738 if (_ecore_thread_count_max <= 0)
739 _ecore_thread_count_max = 1;
741 ECORE_THREAD_PIPE_DEL = ecore_event_type_new();
742 _ecore_thread_pipe = eina_array_new(8);
744 #ifdef EFL_HAVE_THREADS
745 del_handler = ecore_event_handler_add(ECORE_THREAD_PIPE_DEL, _ecore_thread_pipe_del, NULL);
746 main_loop_thread = PHS();
747 have_main_loop_thread = 1;
749 LKI(_ecore_pending_job_threads_mutex);
750 LRWKI(_ecore_thread_global_hash_lock);
751 LKI(_ecore_thread_global_hash_mutex);
752 CDI(_ecore_thread_global_hash_cond);
757 _ecore_thread_shutdown(void)
759 /* FIXME: If function are still running in the background, should we kill them ? */
761 Eina_Array_Iterator it;
764 #ifdef EFL_HAVE_THREADS
765 Ecore_Pthread_Worker *work;
766 Ecore_Pthread_Data *pth;
768 LKL(_ecore_pending_job_threads_mutex);
770 EINA_LIST_FREE(_ecore_pending_job_threads, work)
772 if (work->func_cancel)
773 work->func_cancel((void *)work->data, (Ecore_Thread *) work);
777 EINA_LIST_FREE(_ecore_pending_job_threads_feedback, work)
779 if (work->func_cancel)
780 work->func_cancel((void *)work->data, (Ecore_Thread *) work);
784 LKU(_ecore_pending_job_threads_mutex);
786 /* Improve emergency shutdown */
787 EINA_LIST_FREE(_ecore_active_job_threads, pth)
792 PHJ(pth->thread, ep);
794 ecore_pipe_del(pth->p);
796 if (_ecore_thread_global_hash)
797 eina_hash_free(_ecore_thread_global_hash);
798 ecore_event_handler_del(del_handler);
799 have_main_loop_thread = 0;
802 LKD(_ecore_pending_job_threads_mutex);
803 LRWKD(_ecore_thread_global_hash_lock);
804 LKD(_ecore_thread_global_hash_mutex);
805 CDD(_ecore_thread_global_hash_cond);
808 EINA_ARRAY_ITER_NEXT(_ecore_thread_pipe, i, p, it)
811 eina_array_free(_ecore_thread_pipe);
812 _ecore_thread_pipe = NULL;
816 * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
822 * @addtogroup Ecore_Thread_Group Ecore Thread functions
824 * These functions allow for ecore-managed threads which integrate with ecore's main loop.
830 * @brief Run some blocking code in a parallel thread to avoid locking the main loop.
831 * @param func_blocking The function that should run in another thread.
832 * @param func_end The function that will be called in the main loop if the thread terminate correctly.
833 * @param func_cancel The function that will be called in the main loop if the thread is cancelled.
834 * @param data User context data to pass to all callback.
835 * @return A reference to the newly created thread instance, or NULL if it failed.
837 * ecore_thread_run provide a facility for easily managing blocking task in a
838 * parallel thread. You should provide three function. The first one, func_blocking,
839 * that will do the blocking work in another thread (so you should not use the
840 * EFL in it except Eina if you are careful). The second one, func_end,
841 * that will be called in Ecore main loop when func_blocking is done. So you
842 * can use all the EFL inside this function. The last one, func_cancel, will
843 * be called in the main loop if the thread is cancelled or could not run at all.
845 * Be aware, that you can't make assumption on the result order of func_end
846 * after many call to ecore_thread_run, as we start as much thread as the
847 * host CPU can handle.
850 ecore_thread_run(Ecore_Thread_Cb func_blocking,
851 Ecore_Thread_Cb func_end,
852 Ecore_Thread_Cb func_cancel,
855 Ecore_Pthread_Worker *work;
856 #ifdef EFL_HAVE_THREADS
857 Ecore_Pthread_Data *pth = NULL;
860 if (!func_blocking) return NULL;
862 work = _ecore_thread_worker_new();
866 func_cancel((void *) data, NULL);
870 work->u.short_run.func_blocking = func_blocking;
871 work->func_end = func_end;
872 work->func_cancel = func_cancel;
873 work->cancel = EINA_FALSE;
874 work->feedback_run = EINA_FALSE;
875 work->kill = EINA_FALSE;
876 work->reschedule = EINA_FALSE;
879 #ifdef EFL_HAVE_THREADS
884 LKL(_ecore_pending_job_threads_mutex);
885 _ecore_pending_job_threads = eina_list_append(_ecore_pending_job_threads, work);
887 if (_ecore_thread_count == _ecore_thread_count_max)
889 LKU(_ecore_pending_job_threads_mutex);
890 return (Ecore_Thread *) work;
893 LKU(_ecore_pending_job_threads_mutex);
895 /* One more thread could be created. */
896 pth = malloc(sizeof (Ecore_Pthread_Data));
897 if (!pth) goto on_error;
899 pth->p = _ecore_thread_pipe_get();
900 pth->death_job = _ecore_thread_worker_new();
901 if (!pth->p || !pth->death_job) goto on_error;
905 if (PHC(pth->thread, _ecore_thread_worker, pth) == 0)
906 return (Ecore_Thread *) work;
908 eina_threads_shutdown();
913 if (pth->p) eina_array_push(_ecore_thread_pipe, pth->p);
914 if (pth->death_job) _ecore_thread_worker_free(pth->death_job);
918 if (_ecore_thread_count == 0)
920 LKL(_ecore_pending_job_threads_mutex);
921 _ecore_pending_job_threads = eina_list_remove(_ecore_pending_job_threads, work);
922 LKU(_ecore_pending_job_threads_mutex);
924 if (work->func_cancel)
925 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
929 return (Ecore_Thread *) work;
932 If no thread and as we don't want to break app that rely on this
933 facility, we will lock the interface until we are done.
936 /* Handle reschedule by forcing it here. That would mean locking the app,
937 * would be better with an idler, but really to complex for a case where
938 * thread should really exist.
940 work->reschedule = EINA_FALSE;
942 func_blocking((void *)data, (Ecore_Thread *) work);
943 if (work->cancel == EINA_FALSE) func_end((void *)data, (Ecore_Thread *) work);
944 else func_end((void *)data, (Ecore_Thread *) work);
946 } while (work->reschedule == EINA_TRUE);
955 * @brief Cancel a running thread.
956 * @param thread The thread to cancel.
957 * @return Will return EINA_TRUE if the thread has been cancelled,
958 * EINA_FALSE if it is pending.
960 * ecore_thread_cancel give the possibility to cancel a task still running. It
961 * will return EINA_FALSE, if the destruction is delayed or EINA_TRUE if it is
962 * cancelled after this call.
964 * This function work in the main loop and in the thread, but you should not pass
965 * the Ecore_Thread variable from main loop to the worker thread in any structure.
966 * You should always use the one passed to the Ecore_Thread_Heavy_Cb.
968 * func_end, func_cancel will destroy the handler, so don't use it after.
969 * And if ecore_thread_cancel return EINA_TRUE, you should not use Ecore_Thread also.
972 ecore_thread_cancel(Ecore_Thread *thread)
974 #ifdef EFL_HAVE_THREADS
975 Ecore_Pthread_Worker *work = (Ecore_Pthread_Worker *)thread;
983 if (work->feedback_run)
987 if (work->u.feedback_run.send != work->u.feedback_run.received)
991 LKL(_ecore_pending_job_threads_mutex);
993 if ((have_main_loop_thread) &&
994 (PHE(main_loop_thread, PHS())))
996 if (!work->feedback_run)
997 EINA_LIST_FOREACH(_ecore_pending_job_threads, l, work)
999 if ((void *) work == (void *) thread)
1001 _ecore_pending_job_threads = eina_list_remove_list(_ecore_pending_job_threads, l);
1003 LKU(_ecore_pending_job_threads_mutex);
1005 if (work->func_cancel)
1006 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
1013 EINA_LIST_FOREACH(_ecore_pending_job_threads_feedback, l, work)
1015 if ((void *) work == (void *) thread)
1017 _ecore_pending_job_threads_feedback = eina_list_remove_list(_ecore_pending_job_threads_feedback, l);
1019 LKU(_ecore_pending_job_threads_mutex);
1021 if (work->func_cancel)
1022 work->func_cancel((void *) work->data, (Ecore_Thread *) work);
1030 LKU(_ecore_pending_job_threads_mutex);
1032 /* Delay the destruction */
1034 ((Ecore_Pthread_Worker *)thread)->cancel = EINA_TRUE;
1042 * @brief Tell if a thread was canceled or not.
1043 * @param thread The thread to test.
1044 * @return EINA_TRUE if the thread is cancelled,
1045 * EINA_FALSE if it is not.
1047 * You can use this function in main loop and in the thread.
1050 ecore_thread_check(Ecore_Thread *thread)
1052 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1054 if (!worker) return EINA_TRUE;
1055 return worker->cancel;
1059 * @brief Run some heavy code in a parallel thread to avoid locking the main loop.
1060 * @param func_heavy The function that should run in another thread.
1061 * @param func_notify The function that will receive the data send by func_heavy in the main loop.
1062 * @param func_end The function that will be called in the main loop if the thread terminate correctly.
1063 * @param func_cancel The function that will be called in the main loop if the thread is cancelled.
1064 * @param data User context data to pass to all callback.
1065 * @param try_no_queue If you want to run outside of the thread pool.
1066 * @return A reference to the newly created thread instance, or NULL if it failed.
1068 * ecore_thread_feedback_run provide a facility for easily managing heavy task in a
1069 * parallel thread. You should provide four functions. The first one, func_heavy,
1070 * that will do the heavy work in another thread (so you should not use the
1071 * EFL in it except Eina and Eet if you are careful). The second one, func_notify,
1072 * will receive the data send from the thread function (func_heavy) by ecore_thread_feedback
1073 * in the main loop (and so, can use all the EFL). The third, func_end,
1074 * that will be called in Ecore main loop when func_heavy is done. So you
1075 * can use all the EFL inside this function. The last one, func_cancel, will
1076 * be called in the main loop also, if the thread is cancelled or could not run at all.
1078 * Be aware, that you can't make assumption on the result order of func_end
1079 * after many call to ecore_feedback_run, as we start as much thread as the
1080 * host CPU can handle.
1082 * If you set try_no_queue, it will try to run outside of the thread pool, this can bring
1083 * the CPU down, so be careful with that. Of course if it can't start a new thread, it will
1084 * try to use one from the pool.
1086 EAPI Ecore_Thread *ecore_thread_feedback_run(Ecore_Thread_Cb func_heavy,
1087 Ecore_Thread_Notify_Cb func_notify,
1088 Ecore_Thread_Cb func_end,
1089 Ecore_Thread_Cb func_cancel,
1091 Eina_Bool try_no_queue)
1094 #ifdef EFL_HAVE_THREADS
1095 Ecore_Pthread_Worker *worker;
1096 Ecore_Pthread_Data *pth = NULL;
1098 if (!func_heavy) return NULL;
1100 worker = _ecore_thread_worker_new();
1101 if (!worker) goto on_error;
1103 worker->u.feedback_run.func_heavy = func_heavy;
1104 worker->u.feedback_run.func_notify = func_notify;
1105 worker->hash = NULL;
1108 worker->func_cancel = func_cancel;
1109 worker->func_end = func_end;
1110 worker->data = data;
1111 worker->cancel = EINA_FALSE;
1112 worker->feedback_run = EINA_TRUE;
1113 worker->kill = EINA_FALSE;
1114 worker->reschedule = EINA_FALSE;
1116 worker->u.feedback_run.send = 0;
1117 worker->u.feedback_run.received = 0;
1119 worker->u.feedback_run.notify = ecore_pipe_add(_ecore_notify_handler, worker);
1120 worker->u.feedback_run.direct_pipe = NULL;
1121 worker->u.feedback_run.direct_worker = NULL;
1127 worker->u.feedback_run.direct_pipe = _ecore_thread_pipe_get();
1128 worker->u.feedback_run.direct_worker = _ecore_thread_worker_new();
1130 if (PHC(t, _ecore_direct_worker, worker) == 0)
1131 return (Ecore_Thread *) worker;
1134 LKL(_ecore_pending_job_threads_mutex);
1135 _ecore_pending_job_threads_feedback = eina_list_append(_ecore_pending_job_threads_feedback, worker);
1137 if (_ecore_thread_count == _ecore_thread_count_max)
1139 LKU(_ecore_pending_job_threads_mutex);
1140 return (Ecore_Thread *) worker;
1143 LKU(_ecore_pending_job_threads_mutex);
1145 /* One more thread could be created. */
1146 pth = malloc(sizeof (Ecore_Pthread_Data));
1147 if (!pth) goto on_error;
1149 pth->p = _ecore_thread_pipe_get();
1150 pth->death_job = _ecore_thread_worker_new();
1151 if (!pth->p || !pth->death_job) goto on_error;
1153 eina_threads_init();
1155 if (PHC(pth->thread, _ecore_thread_worker, pth) == 0)
1156 return (Ecore_Thread *) worker;
1158 eina_threads_shutdown();
1163 if (pth->p) eina_array_push(_ecore_thread_pipe, pth->p);
1164 if (pth->death_job) _ecore_thread_worker_free(pth->death_job);
1168 if (_ecore_thread_count == 0)
1170 LKL(_ecore_pending_job_threads_mutex);
1171 _ecore_pending_job_threads_feedback = eina_list_remove(_ecore_pending_job_threads_feedback,
1173 LKU(_ecore_pending_job_threads_mutex);
1175 if (func_cancel) func_cancel((void *) data, NULL);
1179 ecore_pipe_del(worker->u.feedback_run.notify);
1185 return (Ecore_Thread *) worker;
1187 Ecore_Pthread_Worker worker;
1189 (void) try_no_queue;
1192 If no thread and as we don't want to break app that rely on this
1193 facility, we will lock the interface until we are done.
1195 worker.u.feedback_run.func_heavy = func_heavy;
1196 worker.u.feedback_run.func_notify = func_notify;
1197 worker.u.feedback_run.notify = NULL;
1198 worker.u.feedback_run.send = 0;
1199 worker.u.feedback_run.received = 0;
1200 worker.func_cancel = func_cancel;
1201 worker.func_end = func_end;
1203 worker.cancel = EINA_FALSE;
1204 worker.feedback_run = EINA_TRUE;
1205 worker.kill = EINA_FALSE;
1208 worker.reschedule = EINA_FALSE;
1210 func_heavy((void *)data, (Ecore_Thread *) &worker);
1212 if (worker.cancel) func_cancel((void *)data, (Ecore_Thread *) &worker);
1213 else func_end((void *)data, (Ecore_Thread *) &worker);
1214 } while (worker.reschedule == EINA_FALSE);
1221 * @brief Send data to main loop from worker thread.
1222 * @param thread The current Ecore_Thread context to send data from
1223 * @param data Data to be transmitted to the main loop
1224 * @return EINA_TRUE if data was successfully send to main loop,
1225 * EINA_FALSE if anything goes wrong.
1227 * After a succesfull call, the data should be considered owned
1230 * You should use this function only in the func_heavy call.
1233 ecore_thread_feedback(Ecore_Thread *thread, const void *data)
1235 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1237 if (!worker) return EINA_FALSE;
1238 if (!worker->feedback_run) return EINA_FALSE;
1240 #ifdef EFL_HAVE_THREADS
1241 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1243 worker->u.feedback_run.send++;
1244 ecore_pipe_write(worker->u.feedback_run.notify, &data, sizeof (void *));
1248 worker->u.feedback_run.func_notify((void*) worker->data, thread, (void*) data);
1255 * @brief Plan to recall the heavy function once it exist it.
1256 * @param thread The current Ecore_Thread context to reschedule
1257 * @return EINA_TRUE if data was successfully send to main loop,
1258 * EINA_FALSE if anything goes wrong.
1260 * After a succesfull call, you can still do what you want in your thread, it
1261 * will only reschedule it once you exit the heavy loop.
1263 * You should use this function only in the func_heavy call.
1266 ecore_thread_reschedule(Ecore_Thread *thread)
1268 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1270 if (!worker) return EINA_FALSE;
1272 #ifdef EFL_HAVE_THREADS
1273 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1276 worker->reschedule = EINA_TRUE;
1281 * @brief Get number of active thread jobs
1282 * @return Number of active threads running jobs
1283 * This returns the number of threads currently running jobs through the
1287 ecore_thread_active_get(void)
1289 #ifdef EFL_HAVE_THREADS
1290 return _ecore_thread_count;
1297 * @brief Get number of pending (short) thread jobs
1298 * @return Number of pending threads running "short" jobs
1299 * This returns the number of threads currently running jobs through the
1300 * ecore_thread_run api call.
1303 ecore_thread_pending_get(void)
1306 #ifdef EFL_HAVE_THREADS
1307 LKL(_ecore_pending_job_threads_mutex);
1308 ret = eina_list_count(_ecore_pending_job_threads);
1309 LKU(_ecore_pending_job_threads_mutex);
1317 * @brief Get number of pending feedback thread jobs
1318 * @return Number of pending threads running "feedback" jobs
1319 * This returns the number of threads currently running jobs through the
1320 * ecore_thread_feedback_run api call.
1323 ecore_thread_pending_feedback_get(void)
1326 #ifdef EFL_HAVE_THREADS
1327 LKL(_ecore_pending_job_threads_mutex);
1328 ret = eina_list_count(_ecore_pending_job_threads_feedback);
1329 LKU(_ecore_pending_job_threads_mutex);
1337 * @brief Get number of pending thread jobs
1338 * @return Number of pending threads running jobs
1339 * This returns the number of threads currently running jobs through the
1340 * ecore_thread_run and ecore_thread_feedback_run api calls combined.
1343 ecore_thread_pending_total_get(void)
1346 #ifdef EFL_HAVE_THREADS
1347 LKL(_ecore_pending_job_threads_mutex);
1348 ret = eina_list_count(_ecore_pending_job_threads) + eina_list_count(_ecore_pending_job_threads_feedback);
1349 LKU(_ecore_pending_job_threads_mutex);
1357 * @brief Get the max number of threads that can run simultaneously
1358 * @return Max number of threads ecore will run
1359 * This returns the total number of threads that ecore will attempt to run
1363 ecore_thread_max_get(void)
1365 return _ecore_thread_count_max;
1369 * @brief Set the max number of threads that can run simultaneously
1370 * @param num The new maximum
1371 * This sets the maximum number of threads that ecore will try to run
1372 * simultaneously. This number cannot be < 1 or >= 2x the number of active cpus.
1375 ecore_thread_max_set(int num)
1377 if (num < 1) return;
1378 /* avoid doing something hilarious by blocking dumb users */
1379 if (num >= (2 * eina_cpu_count())) return;
1381 _ecore_thread_count_max = num;
1385 * @brief Reset the max number of threads that can run simultaneously
1386 * This resets the maximum number of threads that ecore will try to run
1387 * simultaneously to the number of active cpus.
1390 ecore_thread_max_reset(void)
1392 _ecore_thread_count_max = eina_cpu_count();
1396 * @brief Get the number of threads which are available to be used
1397 * @return The number of available threads
1398 * This returns the number of threads slots that ecore has currently available.
1399 * Assuming that you haven't changed the max number of threads with @ref ecore_thread_max_set
1400 * this should be equal to (num_cpus - (active_running + active_feedback_running))
1403 ecore_thread_available_get(void)
1406 #ifdef EFL_HAVE_THREADS
1407 LKL(_ecore_pending_job_threads_mutex);
1408 ret = _ecore_thread_count_max - _ecore_thread_count;
1409 LKU(_ecore_pending_job_threads_mutex);
1417 * @brief Add data to the thread for subsequent use
1418 * @param thread The thread context to add to
1419 * @param key The name string to add the data with
1420 * @param value The data to add
1421 * @param cb The callback to free the data with
1422 * @param direct If true, this will not copy the key string (like eina_hash_direct_add)
1423 * @return EINA_TRUE on success, EINA_FALSE on failure
1424 * This adds data to the thread context, allowing the thread
1425 * to retrieve and use it without complicated mutexing. This function can only be called by a
1426 * *_run thread INSIDE the thread and will return EINA_FALSE in any case but success.
1427 * All data added to the thread will be freed with its associated callback (if present)
1428 * upon thread termination. If no callback is specified, it is expected that the user will free the
1429 * data, but this is most likely not what you want.
1432 ecore_thread_local_data_add(Ecore_Thread *thread, const char *key, void *value, Eina_Free_Cb cb, Eina_Bool direct)
1434 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1435 Ecore_Thread_Data *d;
1438 if ((!thread) || (!key) || (!value))
1440 #ifdef EFL_HAVE_THREADS
1441 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1444 worker->hash = eina_hash_string_small_new(_ecore_thread_data_free);
1449 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1456 ret = eina_hash_direct_add(worker->hash, key, d);
1458 ret = eina_hash_add(worker->hash, key, d);
1467 * @brief Modify data in the thread, or add if not found
1468 * @param thread The thread context
1469 * @param key The name string to add the data with
1470 * @param value The data to add
1471 * @param cb The callback to free the data with
1472 * @return The old data associated with @p key on success if modified, NULL if added
1473 * This adds/modifies data in the thread context, adding only if modify fails.
1474 * This function can only be called by a *_run thread INSIDE the thread.
1475 * All data added to the thread pool will be freed with its associated callback (if present)
1476 * upon thread termination. If no callback is specified, it is expected that the user will free the
1477 * data, but this is most likely not what you want.
1480 ecore_thread_local_data_set(Ecore_Thread *thread, const char *key, void *value, Eina_Free_Cb cb)
1482 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1483 Ecore_Thread_Data *d, *r;
1485 if ((!thread) || (!key) || (!value))
1487 #ifdef EFL_HAVE_THREADS
1488 if (!PHE(worker->self, PHS())) return NULL;
1491 worker->hash = eina_hash_string_small_new(_ecore_thread_data_free);
1496 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1502 r = eina_hash_set(worker->hash, key, d);
1513 * @brief Find data in the thread's data
1514 * @param thread The thread context
1515 * @param key The name string the data is associated with
1516 * @return The value, or NULL on error
1517 * This finds data in the thread context that has been previously added with @ref ecore_thread_local_data_add
1518 * This function can only be called by a *_run thread INSIDE the thread, and will return NULL
1519 * in any case but success.
1523 ecore_thread_local_data_find(Ecore_Thread *thread, const char *key)
1525 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1526 Ecore_Thread_Data *d;
1528 if ((!thread) || (!key))
1530 #ifdef EFL_HAVE_THREADS
1531 if (!PHE(worker->self, PHS())) return NULL;
1536 d = eina_hash_find(worker->hash, key);
1544 * @brief Delete data from the thread's data
1545 * @param thread The thread context
1546 * @param key The name string the data is associated with
1547 * @return EINA_TRUE on success, EINA_FALSE on failure
1548 * This deletes the data pointer from the thread context which was previously added with @ref ecore_thread_local_data_add
1549 * This function can only be called by a *_run thread INSIDE the thread, and will return EINA_FALSE
1550 * in any case but success. Note that this WILL free the data if a callback was specified.
1553 ecore_thread_local_data_del(Ecore_Thread *thread, const char *key)
1555 Ecore_Pthread_Worker *worker = (Ecore_Pthread_Worker *) thread;
1556 Ecore_Thread_Data *d;
1557 if ((!thread) || (!key))
1559 #ifdef EFL_HAVE_THREADS
1560 if (!PHE(worker->self, PHS())) return EINA_FALSE;
1564 if ((d = eina_hash_find(worker->hash, key)))
1565 _ecore_thread_data_free(d);
1566 return eina_hash_del_by_key(worker->hash, key);
1573 * @brief Add data to the global data
1574 * @param key The name string to add the data with
1575 * @param value The data to add
1576 * @param cb The optional callback to free the data with once ecore is shut down
1577 * @param direct If true, this will not copy the key string (like eina_hash_direct_add)
1578 * @return EINA_TRUE on success, EINA_FALSE on failure
1579 * This adds data to the global thread data, and will return EINA_FALSE in any case but success.
1580 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1581 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1582 * was specified for, you will most likely encounter a segv later on.
1585 ecore_thread_global_data_add(const char *key, void *value, Eina_Free_Cb cb, Eina_Bool direct)
1588 Ecore_Thread_Data *d;
1590 if ((!key) || (!value))
1592 #ifdef EFL_HAVE_THREADS
1593 LRWKWL(_ecore_thread_global_hash_lock);
1594 if (!_ecore_thread_global_hash)
1595 _ecore_thread_global_hash = eina_hash_string_small_new(_ecore_thread_data_free);
1596 LRWKU(_ecore_thread_global_hash_lock);
1598 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1604 if (!_ecore_thread_global_hash)
1606 LRWKWL(_ecore_thread_global_hash_lock);
1608 ret = eina_hash_direct_add(_ecore_thread_global_hash, key, d);
1610 ret = eina_hash_add(_ecore_thread_global_hash, key, d);
1611 LRWKU(_ecore_thread_global_hash_lock);
1612 CDB(_ecore_thread_global_hash_cond);
1620 * @brief Add data to the global data
1621 * @param key The name string to add the data with
1622 * @param value The data to add
1623 * @param cb The optional callback to free the data with once ecore is shut down
1624 * @return An Ecore_Thread_Data on success, NULL on failure
1625 * This adds data to the global thread data and returns NULL, or replaces the previous data
1626 * associated with @p key and returning the previous data if it existed. To see if an error occurred,
1627 * one must use eina_error_get.
1628 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1629 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1630 * was specified for, you will most likely encounter a segv later on.
1633 ecore_thread_global_data_set(const char *key, void *value, Eina_Free_Cb cb)
1635 Ecore_Thread_Data *d, *r;
1638 if ((!key) || (!value))
1640 #ifdef EFL_HAVE_THREADS
1641 LRWKWL(_ecore_thread_global_hash_lock);
1642 if (!_ecore_thread_global_hash)
1643 _ecore_thread_global_hash = eina_hash_string_small_new(_ecore_thread_data_free);
1644 LRWKU(_ecore_thread_global_hash_lock);
1646 if (!_ecore_thread_global_hash)
1649 if (!(d = malloc(sizeof(Ecore_Thread_Data))))
1655 LRWKWL(_ecore_thread_global_hash_lock);
1656 r = eina_hash_set(_ecore_thread_global_hash, key, d);
1657 LRWKU(_ecore_thread_global_hash_lock);
1658 CDB(_ecore_thread_global_hash_cond);
1669 * @brief Find data in the global data
1670 * @param key The name string the data is associated with
1671 * @return The value, or NULL on error
1672 * This finds data in the global data that has been previously added with @ref ecore_thread_global_data_add
1673 * This function will return NULL in any case but success.
1674 * All data added to global can be manually freed, or a callback can be provided with @p cb which will
1675 * be called upon ecore_thread shutting down. Note that if you have manually freed data that a callback
1676 * was specified for, you will most likely encounter a segv later on.
1677 * @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
1678 * if you will be doing anything with it.
1682 ecore_thread_global_data_find(const char *key)
1684 Ecore_Thread_Data *ret;
1687 #ifdef EFL_HAVE_THREADS
1688 if (!_ecore_thread_global_hash) return NULL;
1690 LRWKRL(_ecore_thread_global_hash_lock);
1691 ret = eina_hash_find(_ecore_thread_global_hash, key);
1692 LRWKU(_ecore_thread_global_hash_lock);
1700 * @brief Delete data from the global data
1701 * @param key The name string the data is associated with
1702 * @return EINA_TRUE on success, EINA_FALSE on failure
1703 * This deletes the data pointer from the global data which was previously added with @ref ecore_thread_global_data_add
1704 * This function will return EINA_FALSE in any case but success.
1705 * Note that this WILL free the data if an @c Eina_Free_Cb was specified when the data was added.
1708 ecore_thread_global_data_del(const char *key)
1711 Ecore_Thread_Data *d;
1715 #ifdef EFL_HAVE_THREADS
1716 if (!_ecore_thread_global_hash)
1719 LRWKWL(_ecore_thread_global_hash_lock);
1720 if ((d = eina_hash_find(_ecore_thread_global_hash, key)))
1721 _ecore_thread_data_free(d);
1722 ret = eina_hash_del_by_key(_ecore_thread_global_hash, key);
1723 LRWKU(_ecore_thread_global_hash_lock);
1731 * @brief Find data in the global data and optionally wait for the data if not found
1732 * @param key The name string the data is associated with
1733 * @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.
1734 * If < 0 the call will wait indefinitely for the data.
1735 * @return The value, or NULL on failure
1736 * This finds data in the global data that has been previously added with @ref ecore_thread_global_data_add
1737 * This function will return NULL in any case but success.
1738 * 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.
1739 * @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
1740 * if you will be doing anything with it.
1743 ecore_thread_global_data_wait(const char *key, double seconds)
1746 Ecore_Thread_Data *ret = NULL;
1750 #ifdef EFL_HAVE_THREADS
1751 if (!_ecore_thread_global_hash)
1754 tm = ecore_time_get() + seconds;
1759 struct timespec t = { 0, 0 };
1761 t.tv_sec = (long int)tm;
1762 t.tv_nsec = (long int)((tm - (double)t.tv_sec) * 1000000000);
1764 struct timeval t = { 0, 0 };
1766 t.tv_sec = (long int)tm;
1767 t.tv_usec = (long int)((tm - (double)t.tv_sec) * 1000000);
1769 LRWKRL(_ecore_thread_global_hash_lock);
1770 ret = eina_hash_find(_ecore_thread_global_hash, key);
1771 LRWKU(_ecore_thread_global_hash_lock);
1772 if ((ret) || (!seconds) || ((seconds > 0) && (tm <= ecore_time_get())))
1774 LKL(_ecore_thread_global_hash_mutex);
1775 CDW(_ecore_thread_global_hash_cond, _ecore_thread_global_hash_mutex, &t);
1776 LKU(_ecore_thread_global_hash_mutex);
1778 if (ret) return ret->data;