~ubuntu-branches/ubuntu/intrepid/gnunet/intrepid

« back to all changes in this revision

Viewing changes to src/util/os/priority.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-07-03 14:17:00 UTC
  • mfrom: (1.2.11 upstream) (16 hardy)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20080703141700-355g0g164kaw2kwk
Tags: 0.8.0-2
* Creating /var/run/gnunetd in initscript in case /var/run is on a tmpfs.
* Adding versioned conflicts/replaces against gnunet in order to allow etch to
  lenny upgrades.
* Updating contact address in po files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
     This file is part of GNUnet
 
3
     (C) 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
 
4
 
 
5
     GNUnet is free software; you can redistribute it and/or modify
 
6
     it under the terms of the GNU General Public License as published
 
7
     by the Free Software Foundation; either version 2, or (at your
 
8
     option) any later version.
 
9
 
 
10
     GNUnet is distributed in the hope that it will be useful, but
 
11
     WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
     General Public License for more details.
 
14
 
 
15
     You should have received a copy of the GNU General Public License
 
16
     along with GNUnet; see the file COPYING.  If not, write to the
 
17
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
     Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
/**
 
22
 * @file util/os/priority.c
 
23
 * @brief Methods to set process priority
 
24
 * @author Nils Durner
 
25
 */
 
26
 
 
27
#include "platform.h"
 
28
#include "gnunet_util_error.h"
 
29
#include "gnunet_util_os.h"
 
30
 
 
31
/**
 
32
 * Set our process priority
 
33
 */
 
34
int
 
35
GNUNET_set_process_priority (struct GNUNET_GE_Context *ectx, const char *str)
 
36
{
 
37
  int prio = 0;
 
38
 
 
39
  GNUNET_GE_ASSERT (ectx, str != NULL);
 
40
  /* We support four levels (NORMAL, ABOVE NORMAL, BELOW NORMAL, HIGH and IDLE)
 
41
   * and the usual numeric nice() increments */
 
42
  if (strcmp (str, "NORMAL") == 0)
 
43
#ifdef MINGW
 
44
    prio = NORMAL_PRIORITY_CLASS;
 
45
#else
 
46
    prio = 0;
 
47
#endif
 
48
  else if (strcmp (str, "ABOVE NORMAL") == 0)
 
49
#ifdef MINGW
 
50
    prio = ABOVE_NORMAL_PRIORITY_CLASS;
 
51
#else
 
52
    prio = -5;
 
53
#endif
 
54
  else if (strcmp (str, "BELOW NORMAL") == 0)
 
55
#ifdef MINGW
 
56
    prio = BELOW_NORMAL_PRIORITY_CLASS;
 
57
#else
 
58
    prio = 10;
 
59
#endif
 
60
  else if (strcmp (str, "HIGH") == 0)
 
61
#ifdef MINGW
 
62
    prio = HIGH_PRIORITY_CLASS;
 
63
#else
 
64
    prio = -10;
 
65
#endif
 
66
  else if (strcmp (str, "IDLE") == 0)
 
67
#ifdef MINGW
 
68
    prio = IDLE_PRIORITY_CLASS;
 
69
#else
 
70
    prio = 19;
 
71
#endif
 
72
  else
 
73
    {
 
74
      if (1 != sscanf (str, "%d", &prio))
 
75
        {
 
76
          GNUNET_GE_LOG (ectx,
 
77
                         GNUNET_GE_USER | GNUNET_GE_BULK | GNUNET_GE_ERROR,
 
78
                         _("Invalid process priority `%s'\n"), str);
 
79
          return GNUNET_SYSERR;
 
80
        }
 
81
 
 
82
#ifdef MINGW
 
83
      /* Convert the nice increment to a priority class */
 
84
      if (prio == 0)
 
85
        prio = NORMAL_PRIORITY_CLASS;
 
86
      else if (prio > 0 && prio <= 10)
 
87
        prio = BELOW_NORMAL_PRIORITY_CLASS;
 
88
      else if (prio > 0)
 
89
        prio = IDLE_PRIORITY_CLASS;
 
90
      else if (prio < 0 && prio >= -10)
 
91
        prio = ABOVE_NORMAL_PRIORITY_CLASS;
 
92
      else if (prio < 0)
 
93
        prio = HIGH_PRIORITY_CLASS;
 
94
#endif
 
95
    }
 
96
 
 
97
  /* Set process priority */
 
98
#ifdef MINGW
 
99
  SetPriorityClass (GetCurrentProcess (), prio);
 
100
#else
 
101
  errno = 0;
 
102
  nice (prio);
 
103
  if (errno != 0)
 
104
    {
 
105
      GNUNET_GE_LOG_STRERROR (ectx,
 
106
                              GNUNET_GE_WARNING | GNUNET_GE_ADMIN |
 
107
                              GNUNET_GE_BULK, "nice");
 
108
      return GNUNET_SYSERR;
 
109
    }
 
110
#endif
 
111
  return GNUNET_OK;
 
112
}