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 #include <assert.h>
28 #include <string.h>
29 
30 #include <embb/mtapi/c/mtapi.h>
31 
32 #include <embb_mtapi_log.h>
33 #include <mtapi_status_t.h>
34 #include <embb_mtapi_attr.h>
35 
36 
37 /* ---- INTERFACE FUNCTIONS ------------------------------------------------ */
38 
mtapi_taskattr_init(MTAPI_OUT mtapi_task_attributes_t * attributes,MTAPI_OUT mtapi_status_t * status)39 void mtapi_taskattr_init(
40   MTAPI_OUT mtapi_task_attributes_t* attributes,
41   MTAPI_OUT mtapi_status_t* status) {
42   mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
43 
44   embb_mtapi_log_trace("mtapi_taskattr_init() called\n");
45 
46   if (MTAPI_NULL != attributes) {
47     attributes->num_instances = 1;
48     attributes->is_detached = MTAPI_FALSE;
49     attributes->priority = 0;
50     attributes->complete_func = MTAPI_NULL;
51     attributes->user_data = MTAPI_NULL;
52     attributes->problem_size = 1;
53     mtapi_affinity_init(&attributes->affinity, MTAPI_TRUE, &local_status);
54   } else {
55     local_status = MTAPI_ERR_PARAMETER;
56   }
57 
58   mtapi_status_set(status, local_status);
59 }
60 
mtapi_taskattr_set(MTAPI_INOUT mtapi_task_attributes_t * attributes,MTAPI_IN mtapi_uint_t attribute_num,MTAPI_IN void * attribute,MTAPI_IN mtapi_size_t attribute_size,MTAPI_OUT mtapi_status_t * status)61 void mtapi_taskattr_set(
62   MTAPI_INOUT mtapi_task_attributes_t* attributes,
63   MTAPI_IN mtapi_uint_t attribute_num,
64   MTAPI_IN void* attribute,
65   MTAPI_IN mtapi_size_t attribute_size,
66   MTAPI_OUT mtapi_status_t* status) {
67   mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
68 
69   embb_mtapi_log_trace("mtapi_taskattr_set() called\n");
70 
71   if (MTAPI_NULL != attributes) {
72     if (MTAPI_ATTRIBUTE_POINTER_AS_VALUE != attribute_size &&
73       MTAPI_NULL == attribute) {
74       local_status = MTAPI_ERR_PARAMETER;
75     } else {
76       switch (attribute_num) {
77       case MTAPI_TASK_DETACHED:
78         local_status = embb_mtapi_attr_set_mtapi_boolean_t(
79           &attributes->is_detached, attribute, attribute_size);
80         break;
81 
82       case MTAPI_TASK_INSTANCES:
83         local_status = embb_mtapi_attr_set_mtapi_uint_t(
84           &attributes->num_instances, attribute, attribute_size);
85         break;
86 
87       case MTAPI_TASK_PRIORITY:
88         local_status = embb_mtapi_attr_set_mtapi_uint_t(
89           &attributes->priority, attribute, attribute_size);
90         break;
91 
92       case MTAPI_TASK_AFFINITY:
93         local_status = embb_mtapi_attr_set_mtapi_affinity_t(
94           &attributes->affinity, attribute, attribute_size);
95         break;
96 
97       case MTAPI_TASK_USER_DATA:
98         attributes->user_data = (void*)attribute;
99         local_status = MTAPI_SUCCESS;
100         break;
101 
102       case MTAPI_TASK_COMPLETE_FUNCTION:
103         memcpy(&attributes->complete_func, &attribute, sizeof(void*));
104         local_status = MTAPI_SUCCESS;
105         break;
106 
107       case MTAPI_TASK_PROBLEM_SIZE:
108         local_status = embb_mtapi_attr_set_mtapi_uint_t(
109           &attributes->problem_size, attribute, attribute_size);
110         break;
111 
112       default:
113         /* attribute unknown */
114         local_status = MTAPI_ERR_ATTR_NUM;
115         break;
116       }
117     }
118   } else {
119     local_status = MTAPI_ERR_PARAMETER;
120   }
121 
122   mtapi_status_set(status, local_status);
123 }
124