EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / lib / eina_sched.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2010 ProFUSION embedded systems
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 #ifdef EFL_HAVE_POSIX_THREADS
24 # include <pthread.h>
25 # ifdef __linux__
26 #  include <sched.h>
27 #  include <sys/time.h>
28 #  include <sys/resource.h>
29 #  include <errno.h>
30 # endif
31 #endif
32
33 #ifdef EFL_HAVE_WIN32_THREADS
34 # ifndef WIN32_LEAN_AND_MEAN
35 #  define WIN32_LEAN_AND_MEAN
36 # endif
37 # include <windows.h>
38 # undef WIN32_LEAN_AND_MEAN
39 #endif
40
41 #include "eina_sched.h"
42 #include "eina_log.h"
43
44 #define RTNICENESS 1
45 #define NICENESS 5
46
47 EAPI void
48 eina_sched_prio_drop(void)
49 {
50 #ifdef EFL_HAVE_POSIX_THREADS
51    struct sched_param param;
52    int pol, prio, ret;
53    pthread_t pthread_id;
54
55    pthread_id = pthread_self();
56    ret = pthread_getschedparam(pthread_id, &pol, &param);
57    if (ret)
58      {
59         EINA_LOG_ERR("Unable to query sched parameters");
60         return;
61      }
62
63    if (EINA_UNLIKELY(pol == SCHED_RR || pol == SCHED_FIFO))
64      {
65         param.sched_priority -= RTNICENESS;
66
67         /* We don't change the policy */
68         if (param.sched_priority < 1)
69           {
70              EINA_LOG_INFO("RT prio < 1, setting to 1 instead");
71              param.sched_priority = 1;
72           }
73
74         pthread_setschedparam(pthread_id, pol, &param);
75      }
76 # ifdef __linux__
77    else
78      {
79         errno = 0;
80         prio = getpriority(PRIO_PROCESS, 0);
81         if (errno == 0)
82           {
83              prio += NICENESS;
84              if (prio > 19)
85                {
86                   EINA_LOG_INFO("Max niceness reached; keeping max (19)");
87                   prio = 19;
88                }
89
90              setpriority(PRIO_PROCESS, 0, prio);
91           }
92      }
93 # endif
94 #elif defined EFL_HAVE_WIN32_THREADS
95    if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL))
96      EINA_LOG_ERR("Can not set thread priority");
97 #else
98    EINA_LOG_ERR("Eina does not have support for threads enabled"
99                 "or it doesn't support setting scheduler priorities");
100 #endif
101 }