1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
21 #include "NSProviderScheduler.h"
\r
23 pthread_t NSThread[THREAD_COUNT];
\r
24 pthread_mutex_t NSMutex[THREAD_COUNT];
\r
25 sem_t NSSemaphore[THREAD_COUNT];
\r
26 bool NSIsRunning[THREAD_COUNT] =
\r
29 NSTask* NSHeadMsg[THREAD_COUNT] =
\r
31 NSTask* NSTailMsg[THREAD_COUNT] =
\r
34 void * NSResponseSchedule(void *ptr);
\r
35 void * NSDiscoverySchedule(void *ptr);
\r
36 void * NSSubScriptionSchedule(void *ptr);
\r
37 void * NSNotificationSchedule(void *ptr);
\r
39 bool NSInitScheduler()
\r
41 OIC_LOG(INFO, SCHEDULER_TAG, "NSInitScheduler()");
\r
44 // NsQsStopScheduler(ALL_SCHEDULER);
\r
46 for (i = 0; i < THREAD_COUNT; i++)
\r
48 pthread_mutex_init(&NSMutex[i], NULL);
\r
49 NSIsRunning[i] = true;
\r
50 sem_init(&(NSSemaphore[i]), 0, 0);
\r
56 bool NSStartScheduler()
\r
60 for (i = 0; i < THREAD_COUNT; i++)
\r
62 pthread_mutex_lock(&NSMutex[i]);
\r
66 case RESPONSE_SCHEDULER:
\r
68 pthread_create(&NSThread[i], NULL, NSResponseSchedule, NULL);
\r
72 case DISCOVERY_SCHEDULER:
\r
74 pthread_create(&NSThread[i], NULL, NSDiscoverySchedule, NULL);
\r
78 case SUBSCRIPTION_SCHEDULER:
\r
80 pthread_create(&NSThread[i], NULL, NSSubScriptionSchedule, NULL);
\r
84 case NOTIFICATION_SCHEDULER:
\r
86 pthread_create(&NSThread[i], NULL, NSNotificationSchedule, NULL);
\r
95 NSHeadMsg[i] = NSTailMsg[i] = NULL;
\r
97 pthread_mutex_unlock(&NSMutex[i]);
\r
104 bool NSStopScheduler()
\r
108 for (i = THREAD_COUNT - 1; i >= 0; --i)
\r
111 NSIsRunning[i] = false;
\r
113 pthread_join(NSThread[i], (void *) NULL);
\r
116 pthread_mutex_lock(&NSMutex[i]);
\r
118 while (NSHeadMsg[i]->nextTask != NULL)
\r
120 NSTask* temp = NSHeadMsg[i];
\r
121 NSHeadMsg[i] = NSHeadMsg[i]->nextTask;
\r
125 OICFree(NSHeadMsg[i]);
\r
127 pthread_mutex_unlock(&NSMutex[i]);
\r
133 void NSPushQueue(NSSchedulerType schedulerType, NSTaskType taskType, void* data)
\r
135 pthread_mutex_lock(&NSMutex[schedulerType]);
\r
137 if (NSHeadMsg[schedulerType] == NULL)
\r
139 printf("first schedule");
\r
140 NSHeadMsg[schedulerType] = (NSTask*) malloc(sizeof(NSTask));
\r
141 memset(NSHeadMsg[schedulerType], 0, sizeof(NSTask));
\r
142 NSHeadMsg[schedulerType]->taskType = taskType;
\r
143 NSHeadMsg[schedulerType]->taskData = data;
\r
144 NSHeadMsg[schedulerType]->nextTask = NULL;
\r
145 NSTailMsg[schedulerType] = NSHeadMsg[schedulerType];
\r
149 printf("after first schedule");
\r
150 NSTask* newNode = (NSTask*) malloc(sizeof(NSTask));
\r
151 memset(newNode, 0, sizeof(NSTask));
\r
152 newNode->taskType = taskType;
\r
153 newNode->taskData = data;
\r
154 newNode->nextTask = NULL;
\r
156 NSTailMsg[schedulerType]->nextTask = newNode;
\r
157 NSTailMsg[schedulerType] = newNode;
\r
160 sem_post(&(NSSemaphore[schedulerType]));
\r
161 pthread_mutex_unlock(&NSMutex[schedulerType]);
\r