1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSProviderScheduler.h"
24 #include <tinyara/config.h>
27 pthread_t NSThread[THREAD_COUNT];
28 pthread_mutex_t NSMutex[THREAD_COUNT];
29 sem_t NSSemaphore[THREAD_COUNT];
30 bool NSIsRunning[THREAD_COUNT] = { false, };
32 NSTask* NSHeadMsg[THREAD_COUNT];
33 NSTask* NSTailMsg[THREAD_COUNT];
35 void * NSCallbackResponseSchedule(void *ptr);
36 void * NSDiscoverySchedule(void *ptr);
37 void * NSSubScriptionSchedule(void *ptr);
38 void * NSNotificationSchedule(void *ptr);
39 void * NSTopicSchedule(void * ptr);
41 bool NSInitScheduler()
43 NS_LOG(DEBUG, "NSInitScheduler - IN");
47 for (i = 0; i < THREAD_COUNT; i++)
49 pthread_mutex_init(&NSMutex[i], NULL);
50 NSIsRunning[i] = true;
51 sem_init(&(NSSemaphore[i]), 0, 0);
54 NS_LOG(DEBUG, "NSInitScheduler - OUT");
60 static int ns_pthread_create(pthread_t *thread, pthread_startroutine_t start_routine,
61 pthread_addr_t arg, const char *task_name, int stack_size)
63 /* All callers have null attr, so ignore it for simple implementation*/
64 pthread_attr_t task_attr;
66 pthread_attr_init(&task_attr);
68 (void)pthread_attr_setschedparam(&task_attr, PTHREAD_DEFAULT_PRIORITY);
69 (void)pthread_attr_setstacksize(&task_attr, stack_size);
71 int ret = pthread_create(thread, &task_attr, start_routine, NULL);
77 pthread_setname_np(*thread, task_name);
83 bool NSStartScheduler()
87 for (i = 0; i < THREAD_COUNT; i++)
89 pthread_mutex_lock(&NSMutex[i]);
93 case CALLBACK_RESPONSE_SCHEDULER:
95 NS_LOG(DEBUG, "CASE RESPONSE_SCHEDULER :");
97 ns_pthread_create(&NSThread[i], NSCallbackResponseSchedule, NULL,
98 "IoT_NS_CallbackResponseSchedule",
99 CONFIG_IOTIVITY_NS_CALLBACKRESPONSESCHED_PTHREAD_STACKSIZE);
101 pthread_create(&NSThread[i], NULL, NSCallbackResponseSchedule, NULL);
106 case DISCOVERY_SCHEDULER:
108 NS_LOG(DEBUG, "CASE DISCOVERY_SCHEDULER :");
110 ns_pthread_create(&NSThread[i], NSDiscoverySchedule, NULL,
111 "IoT_NS_DiscoverySchedule",
112 CONFIG_IOTIVITY_NS_DISCOVERSCHED_PTHREAD_STACKSIZE);
114 pthread_create(&NSThread[i], NULL, NSDiscoverySchedule, NULL);
119 case SUBSCRIPTION_SCHEDULER:
121 NS_LOG(DEBUG, "CASE SUBSCRIPTION_SCHEDULER :");
123 ns_pthread_create(&NSThread[i], NSSubScriptionSchedule, NULL,
124 "IOT_NS_SubScriptionSchedule",
125 CONFIG_IOTIVITY_NS_SUBSCRIPTIONSCHED_PTHREAD_STACKSIZE);
127 pthread_create(&NSThread[i], NULL, NSSubScriptionSchedule, NULL);
132 case NOTIFICATION_SCHEDULER:
134 NS_LOG(DEBUG, "CASE NOTIFICATION_SCHEDULER :");
136 ns_pthread_create(&NSThread[i], NSNotificationSchedule, NULL,
137 "IoT_NS_NotificationSchedule",
138 CONFIG_IOTIVITY_NS_NOTIFICATIONSCHED_PTHREAD_STACKSIZE);
140 pthread_create(&NSThread[i], NULL, NSNotificationSchedule, NULL);
145 case TOPIC_SCHEDULER:
147 NS_LOG(DEBUG, "CASE TOPIC_SCHEDULER :");
149 ns_pthread_create(&NSThread[i], NSTopicSchedule, NULL, "IoT_NS_TopicSchedule",
150 CONFIG_IOTIVITY_NS_TOPICSCHED_PTHREAD_STACKSIZE);
152 if (pthread_create(&NSThread[i], NULL, NSTopicSchedule, NULL) != 0)
154 NS_LOG(ERROR, "Unable to create the NSThread");
165 NSHeadMsg[i] = NSTailMsg[i] = NULL;
167 pthread_mutex_unlock(&NSMutex[i]);
174 bool NSStopScheduler()
176 NS_LOG(DEBUG, "NSStopScheduler - IN");
179 for (i = THREAD_COUNT - 1; i >= 0; --i)
183 NSIsRunning[i] = false;
185 sem_post(&(NSSemaphore[i]));
186 pthread_join(NSThread[i], (void **) &status);
190 pthread_mutex_lock(&NSMutex[i]);
192 while (NSHeadMsg[i] != NULL)
194 NSTask* temp = NSHeadMsg[i];
195 NSHeadMsg[i] = NSHeadMsg[i]->nextTask;
200 NSTailMsg[i] = NSHeadMsg[i] = NULL;
202 pthread_mutex_unlock(&NSMutex[i]);
203 pthread_mutex_destroy(&NSMutex[i]);
206 NS_LOG(DEBUG, "NSStopScheduler - OUT");
211 void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data)
214 if (!NSIsRunning[schedulerType])
219 pthread_mutex_lock(&NSMutex[schedulerType]);
221 NS_LOG(DEBUG, "NSPushQueue - IN");
222 NS_LOG_V(DEBUG, "NSSchedulerType = %d", schedulerType);
223 NS_LOG_V(DEBUG, "NSTaskType = %d", taskType);
225 if (NSHeadMsg[schedulerType] == NULL)
227 NSHeadMsg[schedulerType] = (NSTask*) OICMalloc(sizeof(NSTask));
229 if (NSHeadMsg[schedulerType])
231 NSHeadMsg[schedulerType]->taskType = taskType;
232 NSHeadMsg[schedulerType]->taskData = data;
233 NSHeadMsg[schedulerType]->nextTask = NULL;
234 NSTailMsg[schedulerType] = NSHeadMsg[schedulerType];
239 NSTask* newNode = (NSTask*) OICMalloc(sizeof(NSTask));
242 newNode->taskType = taskType;
243 newNode->taskData = data;
244 newNode->nextTask = NULL;
246 NSTailMsg[schedulerType]->nextTask = newNode;
247 NSTailMsg[schedulerType] = newNode;
251 sem_post(&(NSSemaphore[schedulerType]));
252 NS_LOG(DEBUG, "NSPushQueue - OUT");
253 pthread_mutex_unlock(&NSMutex[schedulerType]);
256 void NSFreeData(NSSchedulerType type, NSTask * task)
258 NS_LOG(DEBUG, "NSFreeData - IN");
260 if (type == CALLBACK_RESPONSE_SCHEDULER)
262 switch (task->taskType)
264 case TASK_CB_SUBSCRIPTION:
265 NS_LOG(DEBUG, "CASE TASK_CB_SUBSCRIPTION : Free");
266 NSFreeOCEntityHandlerRequest((OCEntityHandlerRequest*) task->taskData);
269 NS_LOG(DEBUG, "CASE TASK_CB_SYNC : Free");
270 NSFreeSync((NSSyncInfo*) task->taskData);
273 NS_LOG(DEBUG, "No Task Type");
277 else if (type == DISCOVERY_SCHEDULER)
279 switch (task->taskType)
281 case TASK_START_PRESENCE:
282 case TASK_STOP_PRESENCE:
283 case TASK_REGISTER_RESOURCE:
284 NS_LOG(DEBUG, "Not required Free");
287 NS_LOG(DEBUG, "No Task Type");
291 else if (type == SUBSCRIPTION_SCHEDULER)
293 switch (task->taskType)
295 case TASK_SEND_POLICY:
296 case TASK_RECV_SUBSCRIPTION:
297 case TASK_RECV_UNSUBSCRIPTION:
298 case TASK_SYNC_SUBSCRIPTION:
299 NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest : Free ");
300 NSFreeOCEntityHandlerRequest((OCEntityHandlerRequest*) task->taskData);
302 case TASK_SEND_ALLOW:
304 NS_LOG(DEBUG, "NSFreeConsumer : Free ");
305 NSFreeConsumer((NSConsumer *) task->taskData);
308 NS_LOG(DEBUG, "No Task Type");
312 else if (type == NOTIFICATION_SCHEDULER)
314 switch (task->taskType)
316 case TASK_SEND_NOTIFICATION:
318 NS_LOG(DEBUG, "NSFreeMessage : Free ");
319 NSFreeMessage((NSMessage *)task->taskData);
324 NS_LOG(DEBUG, "NSFreeSync : Free ");
325 NSFreeSync((NSSyncInfo*) task->taskData);
329 NS_LOG(DEBUG, "No Task Type");
333 else if (type == TOPIC_SCHEDULER)
335 switch (task->taskType)
337 case TASK_SUBSCRIBE_TOPIC:
338 case TASK_UNSUBSCRIBE_TOPIC:
340 NSCacheTopicSubData * data = task->taskData;
341 NSOICFree(data->topicName);
345 case TASK_REGISTER_TOPIC:
346 case TASK_UNREGISTER_TOPIC:
348 NSOICFree(task->taskData);
351 case TASK_SEND_TOPICS:
352 case TASK_POST_TOPIC:
354 NS_LOG(DEBUG, "TASK_POST_TOPIC : ");
355 NSFreeOCEntityHandlerRequest((OCEntityHandlerRequest*) task->taskData);
362 NS_LOG(DEBUG, "NSFreeData - OUT");