[c79800f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup generic
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief CPU mask manipulation functions.
|
---|
| 36 | */
|
---|
| 37 | #include <cpu/cpu_mask.h>
|
---|
| 38 | #include <cpu.h>
|
---|
| 39 | #include <config.h>
|
---|
| 40 |
|
---|
| 41 | static const size_t word_size = sizeof(unsigned int);
|
---|
| 42 | static const size_t word_bit_cnt = 8 * sizeof(unsigned int);
|
---|
| 43 |
|
---|
| 44 | /** Returns the size of cpu_mask_t for the detected number of cpus in bytes. */
|
---|
| 45 | size_t cpu_mask_size(void)
|
---|
| 46 | {
|
---|
| 47 | size_t word_cnt = (config.cpu_count + word_bit_cnt - 1) / word_bit_cnt;
|
---|
| 48 | return word_cnt * word_size;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /** Add first cpu_cnt cpus to the mask, ie sets the first cpu_cnt bits. */
|
---|
| 52 | static void cpu_mask_count(cpu_mask_t *cpus, size_t cpu_cnt)
|
---|
| 53 | {
|
---|
| 54 | ASSERT(NULL != cpus);
|
---|
| 55 | ASSERT(cpu_cnt <= config.cpu_count);
|
---|
| 56 |
|
---|
| 57 | for (size_t active_word = 0;
|
---|
| 58 | (active_word + 1) * word_bit_cnt <= cpu_cnt;
|
---|
| 59 | ++active_word) {
|
---|
| 60 | /* Set all bits in the cell/word. */
|
---|
| 61 | cpus->mask[active_word] = -1;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | size_t remaining_bits = (cpu_cnt % word_bit_cnt);
|
---|
| 65 | if (0 < remaining_bits) {
|
---|
| 66 | /* Set lower remaining_bits of the last word. */
|
---|
| 67 | cpus->mask[cpu_cnt / word_bit_cnt] = (1 << remaining_bits) - 1;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /** Sets bits corresponding to the active cpus, ie the first
|
---|
| 72 | * config.cpu_active cpus.
|
---|
| 73 | */
|
---|
| 74 | void cpu_mask_active(cpu_mask_t *cpus)
|
---|
| 75 | {
|
---|
| 76 | cpu_mask_none(cpus);
|
---|
| 77 | cpu_mask_count(cpus, config.cpu_active);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** Sets bits for all cpus of the mask. */
|
---|
| 81 | void cpu_mask_all(cpu_mask_t *cpus)
|
---|
| 82 | {
|
---|
| 83 | cpu_mask_count(cpus, config.cpu_count);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /** Resets/removes all bits. */
|
---|
| 87 | void cpu_mask_none(cpu_mask_t *cpus)
|
---|
| 88 | {
|
---|
| 89 | ASSERT(cpus);
|
---|
| 90 |
|
---|
| 91 | size_t word_cnt = cpu_mask_size() / word_size;
|
---|
| 92 |
|
---|
| 93 | for (size_t word = 0; word < word_cnt; ++word) {
|
---|
| 94 | cpus->mask[word] = 0;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Sets the bit corresponding to cpu_id to true. */
|
---|
| 99 | void cpu_mask_set(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
| 100 | {
|
---|
| 101 | size_t word = cpu_id / word_bit_cnt;
|
---|
| 102 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
| 103 |
|
---|
| 104 | cpus->mask[word] |= (1U << word_pos);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** Resets the bit corresponding to cpu_id to false. */
|
---|
| 108 | void cpu_mask_reset(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
| 109 | {
|
---|
| 110 | size_t word = cpu_id / word_bit_cnt;
|
---|
| 111 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
| 112 |
|
---|
| 113 | cpus->mask[word] &= ~(1U << word_pos);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Returns true if the bit corresponding to cpu_id is set. */
|
---|
| 117 | bool cpu_mask_is_set(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
| 118 | {
|
---|
| 119 | size_t word = cpu_id / word_bit_cnt;
|
---|
| 120 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
| 121 |
|
---|
| 122 | return 0 != (cpus->mask[word] & (1U << word_pos));
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Returns true if no bits are set. */
|
---|
| 126 | bool cpu_mask_is_none(cpu_mask_t *cpus)
|
---|
| 127 | {
|
---|
| 128 | size_t word_cnt = cpu_mask_size() / word_size;
|
---|
| 129 |
|
---|
| 130 | for (size_t word = 0; word < word_cnt; ++word) {
|
---|
| 131 | if (cpus->mask[word])
|
---|
| 132 | return false;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** @}
|
---|
| 139 | */ |
---|