Embedded Multicore Building Blocks V1.0.0
affinity.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_MTAPI_AFFINITY_H_
28 #define EMBB_MTAPI_AFFINITY_H_
29 
30 #include <embb/mtapi/c/mtapi.h>
31 #include <embb/mtapi/internal/check_status.h>
32 
33 namespace embb {
34 namespace mtapi {
35 
41 class Affinity {
42  public:
48  Init(true);
49  }
50 
56  Affinity const & other
57  )
58  : affinity_(other.affinity_) {
59  // empty
60  }
61 
66  void operator=(
67  Affinity const & other
68  ) {
69  affinity_ = other.affinity_;
70  }
71 
79  bool initial_affinity
80  ) {
81  Init(initial_affinity);
82  }
83 
90  void Init(
91  bool initial_affinity
92  ) {
93  mtapi_status_t status;
94  mtapi_boolean_t ia = initial_affinity ? MTAPI_TRUE : MTAPI_FALSE;
95  mtapi_affinity_init(&affinity_, ia, &status);
96  internal::CheckStatus(status);
97  }
98 
103  void Set(
104  mtapi_uint_t worker,
105  bool state
106  ) {
107  mtapi_status_t status;
108  mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE;
109  mtapi_affinity_set(&affinity_, worker, st, &status);
110  internal::CheckStatus(status);
111  }
112 
119  bool Get(
120  mtapi_uint_t worker
121  ) {
122  mtapi_status_t status;
123  mtapi_boolean_t state =
124  mtapi_affinity_get(&affinity_, worker, &status);
125  internal::CheckStatus(status);
126  return (state != MTAPI_FALSE) ? true : false;
127  }
128 
137  return affinity_;
138  }
139 
140  private:
141  mtapi_affinity_t affinity_;
142 };
143 
144 } // namespace mtapi
145 } // namespace embb
146 
147 #endif // EMBB_MTAPI_AFFINITY_H_
Definition: lock_free_mpmc_queue.h:40
bool Get(mtapi_uint_t worker)
Gets affinity to the given worker.
Definition: affinity.h:119
void Set(mtapi_uint_t worker, bool state)
Sets affinity to the given worker.
Definition: affinity.h:103
Describes the affinity of an Action or Task to a worker thread of a Node.
Definition: affinity.h:41
Affinity(bool initial_affinity)
Constructs an Affinity object with the given initial affinity.
Definition: affinity.h:78
mtapi_uint64_t mtapi_affinity_t
Core affinity type.
Definition: mtapi.h:350
mtapi_affinity_t GetInternal() const
Returns the internal representation of this object.
Definition: affinity.h:136
mtapi_boolean_t mtapi_affinity_get(mtapi_affinity_t *mask, const mtapi_uint_t core_num, mtapi_status_t *status)
Returns the affinity that corresponds to the given core_num for this affinity mask.
void operator=(Affinity const &other)
Copies an Affinity object.
Definition: affinity.h:66
void mtapi_affinity_set(mtapi_affinity_t *mask, const mtapi_uint_t core_num, const mtapi_boolean_t affinity, mtapi_status_t *status)
This function is used to change the default values of an affinity mask object.
Affinity()
Constructs an Affinity object.
Definition: affinity.h:47
void mtapi_affinity_init(mtapi_affinity_t *mask, const mtapi_boolean_t affinity, mtapi_status_t *status)
This function initializes an affinity mask object.
void Init(bool initial_affinity)
Initializes an Affinity object with the given initial affinity.
Definition: affinity.h:90
Affinity(Affinity const &other)
Copies an Affinity object.
Definition: affinity.h:55