added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSThread.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSThread.h"
22
23 #include "NSConstants.h"
24 #include "NSConsumerCommon.h"
25
26 #include <memory.h>
27 #include "oic_malloc.h"
28
29 void NSDestroyThreadHandle(NSThreadHandle *);
30
31 NSThreadHandle * NSThreadInit(NSThreadFunc func, void * data)
32 {
33     if (!func)
34     {
35         NS_LOG(ERROR, "thread function is null");
36         return NULL;
37     }
38
39     NSThreadHandle * handle = (NSThreadHandle *)OICMalloc(sizeof(NSThreadHandle));
40     if (!handle)
41     {
42         NS_LOG(ERROR, "thread allocation fail");
43         return NULL;
44     }
45
46     memset(handle, 0, sizeof(NSThreadHandle));
47
48     pthread_mutexattr_init(&(handle->mutex_attr));
49     if (pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE))
50     {
51         NS_LOG(ERROR, "thread mutex_attr init fail");
52         NSDestroyThreadHandle(handle);
53         return NULL;
54     }
55
56     if (pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr)))
57     {
58         NS_LOG(ERROR, "thread mutex init fail");
59         NSDestroyThreadHandle(handle);
60         return NULL;
61     }
62
63     if (pthread_mutex_lock(&(handle->mutex)))
64     {
65         NS_LOG(ERROR, "thread mutex lock fail");
66         NSDestroyThreadHandle(handle);
67         return NULL;
68     }
69
70     handle->isStarted = true;
71
72     if (pthread_create(&(handle->thread_id), NULL, func,
73             (data == NULL) ? (void *) handle : (void *)data))
74     {
75         NS_LOG(ERROR, "thread create fail");
76         NSDestroyThreadHandle(handle);
77         return NULL;
78     }
79
80     pthread_mutex_unlock(&(handle->mutex));
81
82     return handle;
83 }
84
85 void NSThreadLock(NSThreadHandle * handle)
86 {
87     pthread_mutex_lock(&(handle->mutex));
88 }
89
90 void NSThreadUnlock(NSThreadHandle * handle)
91 {
92     pthread_mutex_unlock(&(handle->mutex));
93 }
94
95 void NSThreadStop(NSThreadHandle * handle)
96 {
97     NSDestroyThreadHandle(handle);
98 }
99
100 void NSDestroyThreadHandle(NSThreadHandle * handle)
101 {
102     handle->isStarted = false;
103
104     if (handle->thread_id)
105     {
106         pthread_join(handle->thread_id, NULL);
107     }
108
109     pthread_mutex_destroy(&(handle->mutex));
110     pthread_mutexattr_destroy(&(handle->mutex_attr));
111 }
112