[CONPRO-1172] allocates too much memory
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caqueueingthread.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 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 /**
22  * @file
23  *
24  * This file contains common utility function for handling message ques.
25  */
26
27 #ifndef CA_QUEUEING_THREAD_H_
28 #define CA_QUEUEING_THREAD_H_
29
30 #include <stdint.h>
31
32 #include "cathreadpool.h"
33 #include "octhread.h"
34 #include "uqueue.h"
35 #include "cacommon.h"
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40
41 /** Thread function to be invoked. **/
42 typedef void (*CAThreadTask)(void *threadData);
43
44 /** Data destroy function. **/
45 typedef void (*CADataDestroyFunction)(void *data, uint32_t size);
46
47 /** Context based Data destroy function. **/
48 typedef bool (*CAContextDataDestroy)(void *data, uint32_t size, void *ctx);
49
50 typedef struct
51 {
52     /** Thread pool of the thread started. **/
53     ca_thread_pool_t threadPool;
54     /** mutex for synchronization. **/
55     oc_mutex threadMutex;
56     /** conditional mutex for synchronization. **/
57     oc_cond threadCond;
58     /** Thread function to be invoked. **/
59     CAThreadTask threadTask;
60     /** Data destroy function. **/
61     CADataDestroyFunction destroy;
62     /** Variable to inform the thread to stop. **/
63     bool isStop;
64     /** Que on which the thread is operating. **/
65     u_queue_t *dataQueue;
66 } CAQueueingThread_t;
67
68 /**
69  * Initializes the queuing thread.
70  * @param[in]   thread       thread data for each thread.
71  * @param[in]   handle       thread pool handle created.
72  * @param[in]   task         function to be called for each data.
73  * @param[in]   destroy      function to data destroy.
74  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
75  */
76 CAResult_t CAQueueingThreadInitialize(CAQueueingThread_t *thread, ca_thread_pool_t handle,
77                                       CAThreadTask task, CADataDestroyFunction destroy);
78
79 /**
80  * Start the queuing thread.
81  * @param[in]   thread        thread data that needs to be started.
82  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
83  */
84 #ifndef __TIZENRT__
85 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread);
86 #else
87 CAResult_t CAQueueingThreadStart(CAQueueingThread_t *thread, const char *thread_name);
88 #endif
89 /**
90  * Add queuing thread data for new thread.
91  * @param[in]   thread       thread data for new thread control.
92  * @param[in]   data         data that needs to be given for each thread.
93  * @param[in]   size         length of the data.
94  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
95  */
96 CAResult_t CAQueueingThreadAddData(CAQueueingThread_t *thread, void *data, uint32_t size);
97
98 /**
99  * Clears queue thread data.
100  * @param[in]   thread       thread data for new thread control.
101  * @return  ::CA_STATUS_OK or Appropriate error code.
102  */
103 CAResult_t CAQueueingThreadClearData(CAQueueingThread_t *thread);
104
105 /**
106  * Clears queue thread data of specific context.
107  * @param[in]   thread           thread data for new thread control.
108  * @param[in]   callback         Function which should return true if the data
109  *                               needs to be deleted, else returns false
110  * @param[in]   ctx              Data to pass to callback
111  */
112 CAResult_t CAQueueingThreadClearContextData(CAQueueingThread_t *thread,
113                                             CAContextDataDestroy callback, void *ctx);
114
115 /**
116  * Stop the queuing thread.
117  * @param[in]   thread       thread data that needs to be started.
118  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
119  */
120
121 CAResult_t CAQueueingThreadStop(CAQueueingThread_t *thread);
122
123 /**
124  * Terminate the queuing thread.
125  * @param[in]   thread       thread data for each thread.
126  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
127  */
128
129 CAResult_t CAQueueingThreadDestroy(CAQueueingThread_t *thread);
130
131 #ifdef __cplusplus
132 } /* extern "C" */
133 #endif
134
135 #endif  /* CA_QUEUEING_THREAD_H_ */
136