Library of Assembled Shared Sources
process.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "process.h"
45#include "dictionary.h"
46
47#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
48# define LASS_UTIL_PROCESS_HAVE_WIN32
49#elif LASS_HAVE_SYS_RESOURCE_H
50# define LASS_UTIL_PROCESS_HAVE_SYS_RESOURCE
51#else
52# error lass/util/process.h is not supported on this platform
53#endif
54
55#if defined(LASS_UTIL_PROCESS_HAVE_WIN32)
56# define NOMINMAX
57# define WIN32_LEAN_AND_MEAN
58# include <windows.h>
59#elif defined(LASS_UTIL_PROCESS_HAVE_SYS_RESOURCE)
60# include <sys/resource.h>
61# if LASS_HAVE_LIMITS_H
62# include <limits.h>
63# endif
64# ifndef NZERO
65# define NZERO 20
66# endif
67#endif
68
69namespace lass
70{
71namespace util
72{
73
74LASS_EXECUTE_BEFORE_MAIN_EX(lassImpl_processPriorityDictionary,
75 processPriorityDictionary().add("low", ppLow);
76 processPriorityDictionary().add("belownormal", ppBelowNormal);
77 processPriorityDictionary().add("normal", ppNormal);
78 processPriorityDictionary().add("abovenormal", ppAboveNormal);
79 processPriorityDictionary().add("high", ppHigh);
80 )
81
82
83
85{
86 LASS_ASSERT(iPriority >= 0 && iPriority < numberOfProcessPriorities);
87
88#if defined(LASS_UTIL_PROCESS_HAVE_WIN32)
89 static const int winPriorities[numberOfProcessPriorities] =
90 {
91 IDLE_PRIORITY_CLASS,
92 BELOW_NORMAL_PRIORITY_CLASS,
93 NORMAL_PRIORITY_CLASS,
94 ABOVE_NORMAL_PRIORITY_CLASS,
95 HIGH_PRIORITY_CLASS
96 };
97 const int priority = winPriorities[iPriority];
98 LASS_ENFORCE_WINAPI(SetPriorityClass(GetCurrentProcess(), priority))
99 ("Failed to set process priority");
100
101#elif defined(LASS_UTIL_PROCESS_HAVE_SYS_RESOURCE)
102 static const int niceValues[numberOfProcessPriorities] =
103 {
104 NZERO - 1,
105 NZERO / 2 - 1,
106 0,
107 -NZERO / 2,
108 -NZERO
109 };
110 const int niceValue = niceValues[iPriority];
111 LASS_ENFORCE_CLIB(setpriority(PRIO_PROCESS, 0, niceValue));
112
113#else
114# error setProcessPriority is not supported on this platform
115#endif
116}
117
118
119
120void setProcessPriority(const std::string& iPriority)
121{
122 ProcessPriority priority = ppNormal;
123 try
124 {
125 priority = processPriorityDictionary()[iPriority];
126 }
127 catch(const util::Exception&)
128 {
129 LASS_THROW("'" << iPriority << "' is an invalid priority setting. "
130 "It should be one of these: " << processPriorityDictionary().keys() << ".");
131 }
132
133 setProcessPriority(priority);
134}
135
136
137
138}
139
140}
141
142
143// EOF
void add(const TKey &iKey, const TValue &iValue)
add a key and value to dictionary as new entry.
type of all exceptions in lass
ProcessPriority
different process priority levels
Definition process.h:65
TProcessPriorityDictionary & processPriorityDictionary()
singleton with dictionary that can translate strings to ProcessPriority.
Definition process.h:87
void setProcessPriority(const std::string &iPriority)
set process priority by string
Definition process.cpp:120
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53