Embedded Multicore Building Blocks V1.0.0
task_context.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_TASK_CONTEXT_H_
28 #define EMBB_MTAPI_TASK_CONTEXT_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 TaskContext {
42  public:
47  explicit TaskContext(
48  mtapi_task_context_t * task_context
50  )
51  : context_(task_context) {
52  // empty
53  }
54 
60  bool ShouldCancel() {
61  return MTAPI_TASK_CANCELLED == GetTaskState();
62  }
63 
69  mtapi_task_state_t GetTaskState() {
70  mtapi_status_t status;
71  mtapi_task_state_t result =
72  mtapi_context_taskstate_get(context_, &status);
73  internal::CheckStatus(status);
74  return result;
75  }
76 
82  mtapi_uint_t GetCurrentWorkerNumber() {
83  mtapi_status_t status;
84  mtapi_uint_t result = mtapi_context_corenum_get(context_, &status);
85  internal::CheckStatus(status);
86  return result;
87  }
88 
94  mtapi_uint_t GetInstanceNumber() {
95  mtapi_status_t status;
96  mtapi_uint_t result = mtapi_context_instnum_get(context_, &status);
97  internal::CheckStatus(status);
98  return result;
99  }
100 
106  mtapi_uint_t GetNumberOfInstances() {
107  mtapi_status_t status;
108  mtapi_uint_t result = mtapi_context_numinst_get(context_, &status);
109  internal::CheckStatus(status);
110  return result;
111  }
112 
118  void SetStatus(
119  mtapi_status_t error_code
122  ) {
123  mtapi_status_t status;
124  mtapi_context_status_set(context_, error_code, &status);
125  internal::CheckStatus(status);
126  }
127 
135  mtapi_task_context_t * GetInternal() const {
136  return context_;
137  }
138 
139  private:
140  // no default constructor
141  TaskContext();
142 
143  mtapi_task_context_t * context_;
144 };
145 
146 } // namespace mtapi
147 } // namespace embb
148 
149 #endif // EMBB_MTAPI_TASK_CONTEXT_H_
Definition: lock_free_mpmc_queue.h:40
mtapi_uint_t mtapi_context_corenum_get(const mtapi_task_context_t *task_context, mtapi_status_t *status)
This function can be called from an action function to query the current core number for debugging pu...
mtapi_uint_t mtapi_context_numinst_get(const mtapi_task_context_t *task_context, mtapi_status_t *status)
This function can be called from an action function to query the total number of parallel task instan...
mtapi_uint_t GetInstanceNumber()
Queries the current instance of the currently executing Task.
Definition: task_context.h:94
mtapi_uint_t mtapi_context_instnum_get(const mtapi_task_context_t *task_context, mtapi_status_t *status)
This function can be called from an action function to query the instance number of the associated ta...
mtapi_task_context_t * GetInternal() const
Returns the internal representation of this object.
Definition: task_context.h:135
mtapi_task_state_t GetTaskState()
Queries the current Task state.
Definition: task_context.h:69
mtapi_task_state_t mtapi_context_taskstate_get(const mtapi_task_context_t *task_context, mtapi_status_t *status)
An action function may call this function to obtain the state of the task that is associated with the...
TaskContext(mtapi_task_context_t *task_context)
Constructs a TaskContext from the given C representation.
Definition: task_context.h:47
void SetStatus(mtapi_status_t error_code)
Sets the return status of the running Task.
Definition: task_context.h:118
mtapi_uint_t GetNumberOfInstances()
Queries the number of instances of the currently executing Task.
Definition: task_context.h:106
bool ShouldCancel()
Queries whether the Task running in the TaskContext should finish.
Definition: task_context.h:60
mtapi_uint_t GetCurrentWorkerNumber()
Queries the index of the worker thread the Task is running on.
Definition: task_context.h:82
void mtapi_context_status_set(mtapi_task_context_t *task_context, const mtapi_status_t error_code, mtapi_status_t *status)
This function can be called from an action function to set the status that can be obtained by a subse...
Provides information about the status of the currently running Task.
Definition: task_context.h:41