Add critical section to avoid thread safety issue
[platform/core/uifw/stt.git] / client / stt_file_client.c
1 /*
2 *  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <dlog.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18
19 #include "stt_file_client.h"
20
21 static stt_file_client_s* g_client_info = NULL;
22
23 int stt_file_client_new()
24 {
25         if (NULL != g_client_info) {
26                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Already allocated client info");
27                 return STT_FILE_ERROR_INVALID_STATE;
28         }
29
30         g_client_info = (stt_file_client_s*)calloc(1, sizeof(stt_file_client_s));
31         if (!g_client_info) {
32                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to allocate memory");
33                 return STT_FILE_ERROR_OUT_OF_MEMORY;
34         }
35
36         /* initialize client data */
37         g_client_info->recognition_result_cb = NULL;
38         g_client_info->recognition_result_user_data = NULL;
39
40         g_client_info->result_time_cb = NULL;
41         g_client_info->result_time_user_data = NULL;
42
43         g_client_info->state_changed_cb = NULL;
44         g_client_info->state_changed_user_data = NULL;
45
46         g_client_info->supported_lang_cb = NULL;
47         g_client_info->supported_lang_user_data = NULL;
48
49         g_client_info->current_engine_id = -1;
50
51         g_client_info->time_info = NULL;
52
53         g_client_info->before_state = STT_FILE_STATE_READY;
54         g_client_info->current_state = STT_FILE_STATE_READY;
55
56         g_client_info->cb_ref_count = 0;
57
58         return 0;
59 }
60
61 int stt_file_client_destroy()
62 {
63         if (NULL == g_client_info) {
64                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Client info is NULL");
65                 return STT_FILE_ERROR_INVALID_STATE;
66         } else {
67                 while (0 != g_client_info->cb_ref_count) {
68                         /* wait for release callback function */
69                 }
70
71                 free(g_client_info);
72
73                 g_client_info = NULL;
74         }
75
76         return 0;
77 }
78
79 stt_file_client_s* stt_file_client_get()
80 {
81         return g_client_info;
82 }
83
84 int stt_file_client_use_callback(stt_file_client_s* client)
85 {
86         client->cb_ref_count++;
87         return 0;
88 }
89
90 int stt_file_client_not_use_callback(stt_file_client_s* client)
91 {
92         client->cb_ref_count--;
93         return 0;
94 }
95
96 int stt_file_client_get_use_callback(stt_file_client_s* client)
97 {
98         return client->cb_ref_count;
99 }