063b0326236deb39d333cf56a89087d3a6672338
[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 static pthread_mutex_t g_create_mutex;
30
31 void NSDestroyThreadHandle(NSConsumerThread *);
32
33 NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
34 {
35     NS_VERIFY_NOT_NULL(func, NULL);
36
37     pthread_mutex_init(&g_create_mutex, NULL);
38
39     NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
40     NS_VERIFY_NOT_NULL(handle, NULL);
41
42     memset(handle, 0, sizeof(NSConsumerThread));
43
44     pthread_mutexattr_init(&(handle->mutex_attr));
45
46     int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
47     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
48             NULL, NSDestroyThreadHandle(handle));
49
50     pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
51     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
52             NULL, NSDestroyThreadHandle(handle));
53
54     pthread_mutex_lock(&g_create_mutex);
55
56     handle->isStarted = true;
57
58     pthreadResult = pthread_create(&(handle->thread_id), NULL, func,
59                            (data == NULL) ? (void *) handle : (void *)data);
60     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
61             NULL, NSDestroyThreadHandle(handle));
62
63     pthread_mutex_unlock(&g_create_mutex);
64
65     return handle;
66 }
67
68 void NSThreadLock(NSConsumerThread * handle)
69 {
70     pthread_mutex_lock(&(handle->mutex));
71 }
72
73 void NSThreadUnlock(NSConsumerThread * handle)
74 {
75     pthread_mutex_unlock(&(handle->mutex));
76 }
77
78 void NSThreadStop(NSConsumerThread * handle)
79 {
80     NSDestroyThreadHandle(handle);
81 }
82
83 void NSThreadJoin(NSConsumerThread * handle)
84 {
85         if (handle->thread_id)
86         {
87                 pthread_join(handle->thread_id, NULL);
88         }
89 }
90
91 void NSDestroyThreadHandle(NSConsumerThread * handle)
92 {
93     handle->isStarted = false;
94
95     NSThreadJoin(handle);
96
97     pthread_mutex_destroy(&(handle->mutex));
98     pthread_mutexattr_destroy(&(handle->mutex_attr));
99 }
100