On 02/11/2015 07:04 AM, Michal Privoznik wrote:
On 10.02.2015 16:35, Martin Kletzander wrote:
> This function uses sched_setscheduler() function so it works with
> processes and threads as well (even threads not created by us, which is
> what we'll need in the future).
>
>
> +VIR_ENUM_IMPL(virProcessSchedPolicy, VIR_PROC_POLICY_LAST,
> + "none",
> + "batch",
> + "idle",
> + "fifo",
> + "rr");
> +
> /**
> +int
> +virProcessSetScheduler(pid_t pid, virProcessSchedPolicy policy, int priority)
> +{
> + struct sched_param param = {0};
> + int pol = virProcessSchedTranslatePolicy(policy);
> +
> + VIR_DEBUG("pid=%d, policy=%d, priority=%u", pid, policy, priority);
> +
> + if (!policy)
> + return 0;
In order to do this, you have to make sure enum has a zero value
element. I think compilers don't guarantee enum values <-> integer
mapping unless told so.
> +
> +typedef enum {
> + VIR_PROC_POLICY_NONE,
> + VIR_PROC_POLICY_BATCH,
> + VIR_PROC_POLICY_IDLE,
> + VIR_PROC_POLICY_FIFO,
> + VIR_PROC_POLICY_RR,
> +
> + VIR_PROC_POLICY_LAST
> +} virProcessSchedPolicy;
The C language guarantees that VIR_PROC_POLICY_NONE == 0, and that
VIR_PROC_POLICY_BATCH == (VIR_PROC_POLICY_NONE + 1). That is, C
guarantees that an initial enum not otherwise initialized is 0, and that
all subsequent enums not otherwise initialized are one more than the
previous value (whether or not the previous value was explicitly
initialized). So the code you questioned is safe as-is.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org