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 
36 /**
37  * Describes the affinity of an Action or Task to a worker thread of a Node.
38  *
39  * \ingroup CPP_MTAPI
40  */
41 class Affinity {
42  public:
43   /**
44    * Constructs an Affinity object.
45    * \notthreadsafe
46    */
Affinity()47   Affinity() {
48     Init(true);
49   }
50 
51   /**
52    * Copies an Affinity object.
53    * \waitfree
54    */
Affinity(Affinity const & other)55   Affinity(
56     Affinity const & other             /**< The Affinity to copy from */
57     )
58     : affinity_(other.affinity_) {
59     // empty
60   }
61 
62   /**
63    * Copies an Affinity object.
64    * \waitfree
65    */
66   void operator=(
67     Affinity const & other             /**< The Affinity to copy from */
68     ) {
69     affinity_ = other.affinity_;
70   }
71 
72   /**
73    * Constructs an Affinity object with the given initial affinity.
74    * If \c initial_affinity is \c true the Affinity will map to all worker
75    * threads, otherwise it will map to no worker threads.
76    * \notthreadsafe
77    */
Affinity(bool initial_affinity)78   Affinity(
79     bool initial_affinity              /**< The initial affinity to set. */
80     ) {
81     Init(initial_affinity);
82   }
83 
84   /**
85    * Initializes an Affinity object with the given initial affinity.
86    * If \c initial_affinity is \c true the Affinity will map to all worker
87    * threads, otherwise it will map to no worker threads.
88    * \notthreadsafe
89    */
Init(bool initial_affinity)90   void Init(
91     bool initial_affinity              /**< The initial affinity to set. */
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 
99   /**
100    * Sets affinity to the given worker.
101    * \notthreadsafe
102    */
Set(mtapi_uint_t worker,bool state)103   void Set(
104     mtapi_uint_t worker,               /**< The worker to set affinity to. */
105     bool state                         /**< The state of the affinity. */
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 
113   /**
114    * Gets affinity to the given worker.
115    *
116    * \returns \c true, if the Affinity maps to the worker, \c false otherwise.
117    * \waitfree
118    */
Get(mtapi_uint_t worker)119   bool Get(
120     mtapi_uint_t worker                /**< The worker to get affinity of. */
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 
129   /**
130    * Returns the internal representation of this object.
131    * Allows for interoperability with the C interface.
132    *
133    * \returns The internal mtapi_affinity_t.
134    * \waitfree
135    */
GetInternal()136   mtapi_affinity_t GetInternal() const {
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_
148