summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcarlos <caiosignor@gmail.com>2020-04-09 17:34:42 -0300
committerJoão Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>2020-04-29 12:13:38 -0300
commit2ed5fe3ccc04505597ea2e70bbf262678f7ec7fd (patch)
tree93dceb401991cfe2ba7d1d7aff7b71fe705b0aa6
parentb8525700138e3e812847e723c53aab107ba1d9e4 (diff)
Renamed eina_thread_{win32,posix}.c to eina_inline_thread_{win32,posix}.x
All includes and types from these files were moved to eina_thread_{win32,posix}.h No more private functions of eina_sched_prio_drop and this functions are defined acording to SO, in eina_sched_{win32,posix}.c
-rw-r--r--src/lib/eina/eina_debug_timer.c8
-rw-r--r--src/lib/eina/eina_inline_thread_posix.x (renamed from src/lib/eina/eina_thread_posix.c)31
-rw-r--r--src/lib/eina/eina_inline_thread_win32.x170
-rw-r--r--src/lib/eina/eina_main.c2
-rw-r--r--src/lib/eina/eina_sched.h16
-rw-r--r--src/lib/eina/eina_sched_posix.c46
-rw-r--r--src/lib/eina/eina_sched_win32.c4
-rw-r--r--src/lib/eina/eina_thread.c13
-rw-r--r--src/lib/eina/eina_thread.h44
-rw-r--r--src/lib/eina/eina_thread_posix.h55
-rw-r--r--src/lib/eina/eina_thread_win32.c188
-rw-r--r--src/lib/eina/eina_thread_win32.h (renamed from src/lib/eina/eina_sched.c)36
-rw-r--r--src/lib/eina/meson.build13
13 files changed, 313 insertions, 313 deletions
diff --git a/src/lib/eina/eina_debug_timer.c b/src/lib/eina/eina_debug_timer.c
index b55cffb3a6..e498251ca2 100644
--- a/src/lib/eina/eina_debug_timer.c
+++ b/src/lib/eina/eina_debug_timer.c
@@ -68,10 +68,10 @@ _timer_append(Eina_Debug_Timer *t)
68 unsigned int prev_time = 0; 68 unsigned int prev_time = 0;
69 char c = '\0'; 69 char c = '\0';
70 EINA_LIST_FOREACH(_timers, itr, t2) 70 EINA_LIST_FOREACH(_timers, itr, t2)
71 { 71 {
72 if (t2->timeout > t->timeout) goto end; 72 if (t2->timeout > t->timeout) goto end;
73 prev_time = t2->timeout; 73 prev_time = t2->timeout;
74 } 74 }
75 t2 = NULL; 75 t2 = NULL;
76end: 76end:
77 t->rel_time = t->timeout - prev_time; 77 t->rel_time = t->timeout - prev_time;
diff --git a/src/lib/eina/eina_thread_posix.c b/src/lib/eina/eina_inline_thread_posix.x
index bea3f83cfd..1eb38e9af3 100644
--- a/src/lib/eina/eina_thread_posix.c
+++ b/src/lib/eina/eina_inline_thread_posix.x
@@ -16,34 +16,7 @@
16 * if not, see <http://www.gnu.org/licenses/>. 16 * if not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19#ifdef HAVE_CONFIG_H 19#include "eina_thread_posix.h"
20#include "config.h"
21#endif
22
23#include <stdlib.h>
24
25#include "eina_config.h"
26#include "eina_lock.h"
27#include "eina_thread.h"
28#include "eina_sched.h"
29#include "eina_cpu.h"
30
31/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
32#include "eina_safety_checks.h"
33
34#include "eina_debug_private.h"
35
36#include <pthread.h>
37#include <errno.h>
38
39#include <signal.h>
40
41#if defined(EINA_HAVE_PTHREAD_AFFINITY) || defined(EINA_HAVE_PTHREAD_SETNAME)
42#ifndef __linux__
43#include <pthread_np.h>
44#define cpu_set_t cpuset_t
45#endif
46#endif
47 20
48inline void * 21inline void *
49_eina_thread_join(Eina_Thread t) 22_eina_thread_join(Eina_Thread t)
@@ -116,7 +89,7 @@ _eina_thread_create(Eina_Thread *t, int affinity, void *(*func)(void *data), voi
116inline Eina_Bool 89inline Eina_Bool
117_eina_thread_equal(Eina_Thread t1, Eina_Thread t2) 90_eina_thread_equal(Eina_Thread t1, Eina_Thread t2)
118{ 91{
119 return pthread_equal((pthread_t)t1, (pthread_t)t2); 92 return !!pthread_equal((pthread_t)t1, (pthread_t)t2);
120} 93}
121 94
122Eina_Bool _eina_thread_cancel(Eina_Thread thread) 95Eina_Bool _eina_thread_cancel(Eina_Thread thread)
diff --git a/src/lib/eina/eina_inline_thread_win32.x b/src/lib/eina/eina_inline_thread_win32.x
new file mode 100644
index 0000000000..de3e399b44
--- /dev/null
+++ b/src/lib/eina/eina_inline_thread_win32.x
@@ -0,0 +1,170 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Carlos Ré Signor
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef EINA_INLINE_THREAD_WIN32_H_
19#define EINA_INLINE_THREAD_WIN32_H_
20
21
22
23typedef struct _Eina_Win32_Thread_Func
24{
25 void *data;
26 void *(*func)(void *data);
27
28} Eina_Win32_Thread_Func;
29
30typedef struct _Eina_Win32_Thread_Attr
31{
32 LPSECURITY_ATTRIBUTES lpThreadAttributes;
33 SIZE_T dwStackSize;
34 DWORD dwCreationFlags;
35} Eina_Win32_Thread_Attr;
36
37EAPI const void *EINA_THREAD_JOIN_CANCELED = EINA_THREAD_CANCELED;
38
39inline void *
40_eina_thread_join(Eina_Thread t)
41{
42
43 int ret = (int)WaitForSingleObject(t, INFINITE);
44
45 if (ret != 0) return ret;
46 return NULL;
47}
48
49DWORD WINAPI _eina_thread_func(void *params)
50{
51 return (DWORD)((Eina_Win32_Thread_Func *)params)->func((void *)((Eina_Win32_Thread_Func *)params)->data);
52}
53
54inline void _eina_thread_set_priority(Eina_Thread_Priority prio, Eina_Thread *t)
55{
56
57 int nPriority = THREAD_PRIORITY_NORMAL;
58
59 switch (prio)
60 {
61 case EINA_THREAD_URGENT:
62 nPriority = THREAD_PRIORITY_HIGHEST;
63 break;
64 case EINA_THREAD_NORMAL:
65 nPriority = THREAD_PRIORITY_NORMAL;
66 break;
67 case EINA_THREAD_BACKGROUND:
68 nPriority = THREAD_PRIORITY_BELOW_NORMAL;
69 break;
70 case EINA_THREAD_IDLE:
71 nPriority = THREAD_PRIORITY_IDLE;
72 break;
73 }
74
75 SetThreadPriority((HANDLE)*t, nPriority);
76}
77
78inline Eina_Bool
79_eina_thread_create(Eina_Thread *t, int affinity, void *(*func)(void *data), void *data)
80{
81 Eina_Bool ret;
82 Eina_Win32_Thread_Attr thread_attr;
83
84 SECURITY_ATTRIBUTES sec_attributes;
85
86 sec_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
87 sec_attributes.lpSecurityDescriptor = NULL;
88 sec_attributes.bInheritHandle = EINA_TRUE;
89
90 thread_attr.lpThreadAttributes = &sec_attributes;
91
92 thread_attr.dwStackSize = 4000;
93 thread_attr.dwCreationFlags = 0;
94
95 LPDWORD threadID;
96
97 Eina_Win32_Thread_Func thread_func;
98 Eina_Thread_Call *c = (Eina_Thread_Call *)(data);
99
100 thread_func.func = func;
101 thread_func.data = data;
102
103 *t = CreateThread(thread_attr.lpThreadAttributes, thread_attr.dwStackSize, &_eina_thread_func, &thread_func, thread_attr.dwCreationFlags, threadID);
104
105 _eina_thread_set_priority(c->prio, t);
106
107 ret = (*t != NULL) ? EINA_TRUE : EINA_FALSE;
108
109 if (affinity >= 0 && ret) SetThreadAffinityMask(*t, (DWORD_PTR *)&affinity);
110
111 return ret;
112}
113
114inline Eina_Bool
115_eina_thread_equal(Eina_Thread t1, Eina_Thread t2)
116{
117 DWORD t1_thread_id = GetThreadId(t1);
118 DWORD t2_thread_id = GetThreadId(t2);
119
120 return (t1_thread_id == t2_thread_id) ? EINA_TRUE : EINA_FALSE;
121}
122
123inline Eina_Thread
124_eina_thread_self(void)
125{
126 return (Eina_Thread)GetCurrentThread();
127}
128
129inline Eina_Bool _eina_thread_name_set(Eina_Thread thread, char *buf)
130{
131 HRESULT res = SetThreadDescription((HANDLE)thread, (PCWSTR)buf);
132 return HRESULT_CODE(res);
133}
134
135inline Eina_Bool _eina_thread_cancel(Eina_Thread thread)
136{
137 LPDWORD lpExitCode;
138 Eina_Bool success = GetExitCodeThread((HANDLE)thread, lpExitCode);
139 ExitThread(*lpExitCode);
140 return !success;
141}
142inline UNIMPLEMENTED void _eina_thread_setcanceltype(int type, int *oldtype)
143{
144 #warning _eina_thread_setcanceltype is not implemented.
145}
146inline UNIMPLEMENTED int _eina_thread_setcancelstate(int type, int *oldtype)
147{
148 #warning _eina_thread_setcancelstate is not implemented.
149 return 0;
150}
151inline UNIMPLEMENTED void _eina_thread_cancel_checkpoint(void)
152{
153 #warning _eina_thread_cancel_checkpoint is not implemented.
154}
155inline EAPI Eina_Bool
156_eina_thread_cancellable_set(Eina_Bool cancellable, Eina_Bool *was_cancellable)
157{
158 int state = cancellable ? EINA_THREAD_CANCEL_ENABLE : EINA_THREAD_CANCEL_DISABLE;
159 if (!state)
160 {
161 *was_cancellable = EINA_TRUE;
162 return EINA_FALSE;
163 }
164 else
165 {
166 *was_cancellable = EINA_TRUE;
167 return EINA_TRUE;
168 }
169}
170#endif \ No newline at end of file
diff --git a/src/lib/eina/eina_main.c b/src/lib/eina/eina_main.c
index 9ba8c2220a..61c21b01c3 100644
--- a/src/lib/eina/eina_main.c
+++ b/src/lib/eina/eina_main.c
@@ -452,7 +452,7 @@ eina_main_loop_is(void)
452EAPI void 452EAPI void
453eina_main_loop_define(void) 453eina_main_loop_define(void)
454{ 454{
455#ifdef EFL_HAVE_THREADS 455#ifdef EFL_HAVE_THREADS
456 _eina_main_loop = eina_thread_self(); 456 _eina_main_loop = eina_thread_self();
457#endif 457#endif
458} 458}
diff --git a/src/lib/eina/eina_sched.h b/src/lib/eina/eina_sched.h
index 5c066a1d28..f3567cdb42 100644
--- a/src/lib/eina/eina_sched.h
+++ b/src/lib/eina/eina_sched.h
@@ -27,12 +27,15 @@
27 */ 27 */
28 28
29#ifndef EINA_SCHED_H_ 29#ifndef EINA_SCHED_H_
30#define EINA_SCHED_H_ 30# define EINA_SCHED_H_
31 31# ifdef HAVE_CONFIG_H
32#include "eina_types.h" 32# include "config.h"
33#include "eina_thread.h" 33# endif
34#define RTNICENESS 1 34# include "eina_types.h"
35#define NICENESS 5 35# include "eina_thread.h"
36# include "eina_log.h"
37# define RTNICENESS 1
38# define NICENESS 5
36 39
37/** 40/**
38 * @brief Lowers the priority of the current thread. 41 * @brief Lowers the priority of the current thread.
@@ -46,7 +49,6 @@
46 * only one that is implemented as of now. In this case, the nice level is 49 * only one that is implemented as of now. In this case, the nice level is
47 * incremented on this thread by @c NICENESS. 50 * incremented on this thread by @c NICENESS.
48 */ 51 */
49inline void _eina_sched_prio_drop(void);
50EAPI void eina_sched_prio_drop(void); 52EAPI void eina_sched_prio_drop(void);
51 53
52/** 54/**
diff --git a/src/lib/eina/eina_sched_posix.c b/src/lib/eina/eina_sched_posix.c
index 0060471e32..89c8428e7d 100644
--- a/src/lib/eina/eina_sched_posix.c
+++ b/src/lib/eina/eina_sched_posix.c
@@ -23,48 +23,48 @@
23#include <errno.h> 23#include <errno.h>
24#include <pthread.h> 24#include <pthread.h>
25 25
26inline void _eina_sched_prio_drop(void) 26EAPI void eina_sched_prio_drop(void)
27{ 27{
28 struct sched_param param; 28 struct sched_param param;
29 int pol, ret; 29 int pol, ret;
30 Eina_Thread pthread_id; 30 Eina_Thread pthread_id;
31 31
32 pthread_id = eina_thread_self(); 32 pthread_id = eina_thread_self();
33 ret = pthread_getschedparam(pthread_id, &pol, &param); 33 ret = pthread_getschedparam(pthread_id, &pol, &param);
34 if (ret) 34 if (ret)
35 { 35 {
36 EINA_LOG_ERR("Unable to query sched parameters"); 36 EINA_LOG_ERR("Unable to query sched parameters");
37 return; 37 return;
38 } 38 }
39 39
40 if (EINA_UNLIKELY(pol == SCHED_RR || pol == SCHED_FIFO)) 40 if (EINA_UNLIKELY(pol == SCHED_RR || pol == SCHED_FIFO))
41 { 41 {
42 param.sched_priority -= RTNICENESS; 42 param.sched_priority -= RTNICENESS;
43 43
44 /* We don't change the policy */ 44 /* We don't change the policy */
45 if (param.sched_priority < 1) 45 if (param.sched_priority < 1)
46 { 46 {
47 EINA_LOG_INFO("RT prio < 1, setting to 1 instead"); 47 EINA_LOG_INFO("RT prio < 1, setting to 1 instead");
48 param.sched_priority = 1; 48 param.sched_priority = 1;
49 } 49 }
50 50
51 pthread_setschedparam(pthread_id, pol, &param); 51 pthread_setschedparam(pthread_id, pol, &param);
52 } 52 }
53 else 53 else
54 { 54 {
55 int prio; 55 int prio;
56 errno = 0; 56 errno = 0;
57 prio = getpriority(PRIO_PROCESS, 0); 57 prio = getpriority(PRIO_PROCESS, 0);
58 if (errno == 0) 58 if (errno == 0)
59 { 59 {
60 prio += NICENESS; 60 prio += NICENESS;
61 if (prio > 19) 61 if (prio > 19)
62 { 62 {
63 EINA_LOG_INFO("Max niceness reached; keeping max (19)"); 63 EINA_LOG_INFO("Max niceness reached; keeping max (19)");
64 prio = 19; 64 prio = 19;
65 } 65 }
66 66
67 setpriority(PRIO_PROCESS, 0, prio); 67 setpriority(PRIO_PROCESS, 0, prio);
68 } 68 }
69 } 69 }
70} \ No newline at end of file 70} \ No newline at end of file
diff --git a/src/lib/eina/eina_sched_win32.c b/src/lib/eina/eina_sched_win32.c
index 9f29d857b2..eaacdda0fd 100644
--- a/src/lib/eina/eina_sched_win32.c
+++ b/src/lib/eina/eina_sched_win32.c
@@ -18,11 +18,11 @@
18 18
19#include "eina_sched.h" 19#include "eina_sched.h"
20#ifndef WIN32_LEAN_AND_MEAN 20#ifndef WIN32_LEAN_AND_MEAN
21 #define WIN32_LEAN_AND_MEAN 21# define WIN32_LEAN_AND_MEAN
22#endif 22#endif
23#include <Windows.h> 23#include <Windows.h>
24 24
25inline void _eina_sched_prio_drop(void) 25EAPI void eina_sched_prio_drop(void)
26{ 26{
27 Eina_Thread pthread_id; 27 Eina_Thread pthread_id;
28 int sched_priority; 28 int sched_priority;
diff --git a/src/lib/eina/eina_thread.c b/src/lib/eina/eina_thread.c
index 0a33c12a8d..7300c5524b 100644
--- a/src/lib/eina/eina_thread.c
+++ b/src/lib/eina/eina_thread.c
@@ -30,12 +30,14 @@
30 30
31/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */ 31/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
32#include "eina_safety_checks.h" 32#include "eina_safety_checks.h"
33
34#include "eina_debug_private.h" 33#include "eina_debug_private.h"
35 34
36#include <errno.h> 35#include <errno.h>
37#ifndef _WIN32 36#ifndef _WIN32
38# include <signal.h> 37# include <signal.h>
38# include "eina_inline_thread_posix.x"
39#else
40# include "eina_inline_thread_win32.x"
39#endif 41#endif
40# include <string.h> 42# include <string.h>
41 43
@@ -43,8 +45,6 @@
43# define cpu_set_t cpuset_t 45# define cpu_set_t cpuset_t
44#endif 46#endif
45 47
46
47
48EAPI Eina_Thread 48EAPI Eina_Thread
49eina_thread_self(void) 49eina_thread_self(void)
50{ 50{
@@ -54,14 +54,13 @@ eina_thread_self(void)
54EAPI Eina_Bool 54EAPI Eina_Bool
55eina_thread_equal(Eina_Thread t1, Eina_Thread t2) 55eina_thread_equal(Eina_Thread t1, Eina_Thread t2)
56{ 56{
57 return !!_eina_thread_equal(t1, t2); 57 return _eina_thread_equal(t1, t2);
58} 58}
59static void *_eina_internal_call(void *context) 59static void *_eina_internal_call(void *context)
60{ 60{
61 Eina_Thread_Call *c = context; 61 Eina_Thread_Call *c = context;
62 void *r; 62 void *r;
63 Eina_Thread self; 63 Eina_Thread self;
64
65 64
66 // Default this thread to not cancellable as per Eina documentation 65 // Default this thread to not cancellable as per Eina documentation
67 eina_thread_cancellable_set(EINA_FALSE, NULL); 66 eina_thread_cancellable_set(EINA_FALSE, NULL);
@@ -71,9 +70,7 @@ static void *_eina_internal_call(void *context)
71 if (c->prio == EINA_THREAD_BACKGROUND || c->prio == EINA_THREAD_IDLE) 70 if (c->prio == EINA_THREAD_BACKGROUND || c->prio == EINA_THREAD_IDLE)
72 eina_sched_prio_drop(); 71 eina_sched_prio_drop();
73 72
74
75 self = eina_thread_self(); 73 self = eina_thread_self();
76
77 _eina_debug_thread_add(&self); 74 _eina_debug_thread_add(&self);
78 EINA_THREAD_CLEANUP_PUSH(_eina_debug_thread_del, &self); 75 EINA_THREAD_CLEANUP_PUSH(_eina_debug_thread_del, &self);
79 r = c->func((void*) c->data, eina_thread_self()); 76 r = c->func((void*) c->data, eina_thread_self());
@@ -167,7 +164,7 @@ eina_thread_cancellable_run(Eina_Thread_Cancellable_Run_Cb cb, Eina_Free_Cb clea
167 return ret; 164 return ret;
168} 165}
169 166
170EAPI const void *EINA_THREAD_JOIN_CANCELED = EINA_THREAD_CANCELED; 167
171 168
172Eina_Bool 169Eina_Bool
173eina_thread_init(void) 170eina_thread_init(void)
diff --git a/src/lib/eina/eina_thread.h b/src/lib/eina/eina_thread.h
index 0ee17e9cd7..dda08c86a5 100644
--- a/src/lib/eina/eina_thread.h
+++ b/src/lib/eina/eina_thread.h
@@ -25,6 +25,12 @@
25 25
26#include <stdint.h> 26#include <stdint.h>
27 27
28#ifdef _WIN32
29# include "eina_thread_win32.h"
30#else
31# include "eina_thread_posix.h"
32#endif
33
28/** 34/**
29 * @addtogroup Eina_Tools_Group Tools 35 * @addtogroup Eina_Tools_Group Tools
30 * 36 *
@@ -45,12 +51,6 @@
45 */ 51 */
46 52
47/** 53/**
48 * @typedef Eina_Thread
49 * Type for a generic thread.
50 */
51typedef uintptr_t Eina_Thread;
52
53/**
54 * @typedef Eina_Thread_Cb 54 * @typedef Eina_Thread_Cb
55 * Type for the definition of a thread callback function 55 * Type for the definition of a thread callback function
56 */ 56 */
@@ -347,38 +347,6 @@ struct _Eina_Thread_Call
347 Eina_Thread_Priority prio; 347 Eina_Thread_Priority prio;
348 int affinity; 348 int affinity;
349}; 349};
350/*
351 * Those are the functions definitions of wrappers that will implements the functions
352 * above with win32 and posix api
353 */
354inline Eina_Thread _eina_thread_self(void);
355inline Eina_Bool _eina_thread_equal(Eina_Thread t1, Eina_Thread t2);
356inline Eina_Bool _eina_thread_create(Eina_Thread *t,
357 int affinity,
358 void *(*func)(void *data),
359 void *data);
360inline void *_eina_thread_join(Eina_Thread t);
361Eina_Bool _eina_thread_name_set(Eina_Thread thread, char *buf);
362Eina_Bool _eina_thread_cancel(Eina_Thread thread);
363void _eina_thread_setcanceltype(int type, int *oldtype);
364void _eina_thread_cancel_checkpoint();
365EAPI Eina_Bool _eina_thread_cancellable_set(Eina_Bool cancellable, Eina_Bool *was_cancellable);
366int _eina_thread_setcancelstate(int type, int *oldtype);
367void _eina_thread_set_priority(Eina_Thread_Priority prio, Eina_Thread *t);
368
369#ifdef _WIN32
370# define EINA_THREAD_CANCEL_ENABLE 0
371# define EINA_THREAD_CANCEL_DISABLE 1
372# define EINA_THREAD_CANCEL_DEFERRED 0
373# define EINA_THREAD_CANCEL_ASYNCHRONOUS 1
374# define EINA_THREAD_CANCELED ((void *) -1)
375#else
376# define EINA_THREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
377# define EINA_THREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
378# define EINA_THREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
379# define EINA_THREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
380# define EINA_THREAD_CANCELED PTHREAD_CANCELED
381#endif
382 350
383/** 351/**
384 * @} 352 * @}
diff --git a/src/lib/eina/eina_thread_posix.h b/src/lib/eina/eina_thread_posix.h
new file mode 100644
index 0000000000..ecd00fe865
--- /dev/null
+++ b/src/lib/eina/eina_thread_posix.h
@@ -0,0 +1,55 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Carlos Signor
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef EINA_THREAD_POSIX_H
20#define EINA_THREAD_POSIX_H
21
22#ifdef HAVE_CONFIG_H
23# include "config.h"
24#endif
25
26#include <stdlib.h>
27
28#include "eina_config.h"
29#include "eina_thread.h"
30#include "eina_cpu.h"
31#include <pthread.h>
32#include "eina_safety_checks.h"
33#include <errno.h>
34
35#include <signal.h>
36#define EINA_THREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE
37#define EINA_THREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE
38#define EINA_THREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED
39#define EINA_THREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS
40#define EINA_THREAD_CANCELED PTHREAD_CANCELED
41
42#if defined(EINA_HAVE_PTHREAD_AFFINITY) || defined(EINA_HAVE_PTHREAD_SETNAME)
43# ifndef __linux__
44# include <pthread_np.h>
45# define cpu_set_t cpuset_t
46# endif
47#endif
48typedef uintptr_t Eina_Thread;
49/**
50 * @typedef Eina_Thread
51 * Type for a generic thread.
52 */
53EAPI const void *EINA_THREAD_JOIN_CANCELED = EINA_THREAD_CANCELED;
54
55#endif \ No newline at end of file
diff --git a/src/lib/eina/eina_thread_win32.c b/src/lib/eina/eina_thread_win32.c
deleted file mode 100644
index 40b019ee23..0000000000
--- a/src/lib/eina/eina_thread_win32.c
+++ /dev/null
@@ -1,188 +0,0 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Carlos Ré Signor
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <stdlib.h>
24#include "eina_config.h"
25#include "eina_lock.h"
26#include "eina_sched.h"
27#include "eina_cpu.h"
28#include "eina_thread.h"
29/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
30#include "eina_safety_checks.h"
31
32#include "eina_debug_private.h"
33
34#ifndef WIN32_LEAN_AND_MEAN
35#define WIN32_LEAN_AND_MEAN
36#endif
37#include <Windows.h>
38#include <errno.h>
39#include <string.h>
40
41typedef struct _Eina_win32_thread_func
42{
43 void *data;
44 void *(*func)(void *data);
45
46} Eina_win32_thread_func;
47
48typedef struct _Eina_win32_thread_attr
49{
50 LPSECURITY_ATTRIBUTES lpThreadAttributes;
51 SIZE_T dwStackSize;
52 DWORD dwCreationFlags;
53} Eina_win32_thread_attr;
54
55inline void *
56_eina_thread_join(Eina_Thread t)
57{
58
59 int ret = (int)WaitForSingleObject(t, INFINITE);
60
61 if (ret != 0)
62 return ret;
63 return NULL;
64}
65
66DWORD WINAPI _eina_thread_func(void *params)
67{
68 return (DWORD)((Eina_win32_thread_func *)params)->func((void *)((Eina_win32_thread_func *)params)->data);
69}
70
71void _eina_thread_set_priority(Eina_Thread_Priority prio, Eina_Thread *t)
72{
73
74 int nPriority;
75
76 switch (prio)
77 {
78 case EINA_THREAD_URGENT:
79 nPriority = THREAD_PRIORITY_HIGHEST;
80 break;
81 case EINA_THREAD_NORMAL:
82 nPriority = THREAD_PRIORITY_NORMAL;
83 break;
84 case EINA_THREAD_BACKGROUND:
85 nPriority = THREAD_PRIORITY_BELOW_NORMAL;
86 break;
87 case EINA_THREAD_IDLE:
88 nPriority = THREAD_PRIORITY_IDLE;
89 break;
90 }
91
92 SetThreadPriority((HANDLE)*t, nPriority);
93}
94
95inline Eina_Bool
96_eina_thread_create(Eina_Thread *t, int affinity, void *(*func)(void *data), void *data)
97{
98 Eina_Bool ret;
99 Eina_win32_thread_attr thread_attr;
100
101 SECURITY_ATTRIBUTES sec_attributes;
102
103 sec_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
104 sec_attributes.lpSecurityDescriptor = NULL;
105 sec_attributes.bInheritHandle = EINA_TRUE;
106
107 thread_attr.lpThreadAttributes = &sec_attributes;
108
109 thread_attr.dwStackSize = 4000;
110 thread_attr.dwCreationFlags = 0;
111
112 LPDWORD threadID;
113
114 Eina_win32_thread_func thread_func;
115 Eina_Thread_Call *c = (Eina_Thread_Call *)(data);
116
117 thread_func.func = func;
118 thread_func.data = data;
119
120 *t = (HANDLE)CreateThread(thread_attr.lpThreadAttributes, thread_attr.dwStackSize, &_eina_thread_func, &thread_func, thread_attr.dwCreationFlags, threadID);
121
122 _eina_thread_set_priority(c->prio, t);
123
124 ret = (*t != NULL) ? EINA_TRUE : EINA_FALSE;
125
126 if (affinity >= 0 && ret)
127 {
128 SetThreadAffinityMask(*t, (DWORD_PTR *)&affinity);
129 }
130
131 return ret;
132}
133
134inline Eina_Bool
135_eina_thread_equal(Eina_Thread t1, Eina_Thread t2)
136{
137 DWORD t1_thread_id = GetThreadId((HANDLE)t1);
138 DWORD t2_thread_id = GetThreadId((HANDLE)t2);
139
140 return (t1_thread_id == t2_thread_id) ? EINA_TRUE : EINA_FALSE;
141}
142
143inline Eina_Thread
144_eina_thread_self(void)
145{
146 return (Eina_Thread)GetCurrentThread();
147}
148
149Eina_Bool _eina_thread_name_set(Eina_Thread thread, char *buf)
150{
151 HRESULT res = SetThreadDescription((HANDLE)thread, (PCWSTR)buf);
152 return HRESULT_CODE(res);
153}
154
155Eina_Bool _eina_thread_cancel(Eina_Thread thread)
156{
157 LPDWORD lpExitCode;
158 Eina_Bool success = GetExitCodeThread((HANDLE)thread, lpExitCode);
159 ExitThread(*lpExitCode);
160 return !success;
161}
162void _eina_thread_setcanceltype(int type, int *oldtype)
163{
164 pthread_setcanceltype(EINA_THREAD_CANCEL_DEFERRED, &oldtype);
165}
166int _eina_thread_setcancelstate(int type, int *oldtype)
167{
168 return pthread_setcancelstate(type, &oldtype);
169}
170EAPI Eina_Bool
171_eina_thread_cancellable_set(Eina_Bool cancellable, Eina_Bool *was_cancellable)
172{
173 int state = cancellable ? EINA_THREAD_CANCEL_ENABLE : EINA_THREAD_CANCEL_DISABLE;
174 if (!state)
175 {
176 *was_cancellable = EINA_TRUE;
177 return EINA_FALSE;
178 }
179 else
180 {
181 *was_cancellable = EINA_TRUE;
182 return EINA_TRUE;
183 }
184}
185void _eina_thread_cancel_checkpoint(void)
186{
187 pthread_testcancel();
188} \ No newline at end of file
diff --git a/src/lib/eina/eina_sched.c b/src/lib/eina/eina_thread_win32.h
index 5bef0e6291..d0590668c8 100644
--- a/src/lib/eina/eina_sched.c
+++ b/src/lib/eina/eina_thread_win32.h
@@ -1,5 +1,5 @@
1/* EINA - EFL data type library 1/* EINA - EFL data type library
2 * Copyright (C) 2010 ProFUSION embedded systems 2 * Copyright (C) 2020 Carlos Signor
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public 5 * modify it under the terms of the GNU Lesser General Public
@@ -16,16 +16,34 @@
16 * if not, see <http://www.gnu.org/licenses/>. 16 * if not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19#ifndef EINA_THREAD_WIN32_H
20#define EINA_THREAD_WIN32_H
21
19#ifdef HAVE_CONFIG_H 22#ifdef HAVE_CONFIG_H
20#include "config.h" 23# include "config.h"
21#endif 24#endif
25
26#include <stdlib.h>
27#include "eina_config.h"
28#include "eina_cpu.h"
22#include "eina_thread.h" 29#include "eina_thread.h"
30#include "unimplemented.h"
31#include <errno.h>
32#include <string.h>
33#ifndef WIN32_LEAN_AND_MEAN
34# define WIN32_LEAN_AND_MEAN
35#endif
36#include <Windows.h>
23 37
24#include "eina_sched.h" 38#define EINA_THREAD_CANCEL_ENABLE 0
25#include "eina_log.h" 39#define EINA_THREAD_CANCEL_DISABLE 1
40#define EINA_THREAD_CANCEL_DEFERRED 0
41#define EINA_THREAD_CANCEL_ASYNCHRONOUS 1
42#define EINA_THREAD_CANCELED ((void *) -1)
43/**
44 * @typedef Eina_Thread
45 * Type for a generic thread.
46 */
47typedef HANDLE Eina_Thread;
26 48
27EAPI void 49#endif \ No newline at end of file
28eina_sched_prio_drop(void)
29{
30 _eina_sched_prio_drop();
31}
diff --git a/src/lib/eina/meson.build b/src/lib/eina/meson.build
index 3abcf48b27..5dbbe3052f 100644
--- a/src/lib/eina/meson.build
+++ b/src/lib/eina/meson.build
@@ -114,6 +114,14 @@ public_sub_headers = [
114'eina_abstract_content.h' 114'eina_abstract_content.h'
115] 115]
116 116
117if sys_windows == true
118 public_sub_headers += 'eina_thread_win32.h'
119 public_sub_headers += 'eina_inline_thread_win32.x'
120else
121 public_sub_headers += 'eina_thread_posix.h'
122 public_sub_headers += 'eina_inline_thread_posix.x'
123endif
124
117public_headers = [ 125public_headers = [
118 'Eina.h' 126 'Eina.h'
119] 127]
@@ -166,7 +174,6 @@ sources = [
166'eina_rbtree.c', 174'eina_rbtree.c',
167'eina_rectangle.c', 175'eina_rectangle.c',
168'eina_safety_checks.c', 176'eina_safety_checks.c',
169'eina_sched.c',
170'eina_share_common.c', 177'eina_share_common.c',
171'eina_simple_xml_parser.c', 178'eina_simple_xml_parser.c',
172'eina_str.c', 179'eina_str.c',
@@ -199,11 +206,9 @@ sources = [
199 206
200if sys_windows == true 207if sys_windows == true
201 sources += 'eina_file_win32.c' 208 sources += 'eina_file_win32.c'
202 sources += 'eina_thread_win32.c' 209 sources += 'eina_sched_win32.c'
203 sources += 'eina_sched_win32.c'
204else 210else
205 sources += 'eina_file.c' 211 sources += 'eina_file.c'
206 sources += 'eina_thread_posix.c'
207 sources += 'eina_sched_posix.c' 212 sources += 'eina_sched_posix.c'
208endif 213endif
209 214