• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright (C) 2013 DENSO CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ****************************************************************************/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <memory.h>
22 #include <unistd.h>
23 #include <pthread.h>
24 #include "ilm_common.h"
25 #include "ilm_common_platform.h"
26 #include "ilm_types.h"
27 
28 ILM_EXPORT ilmErrorTypes ilmControl_init(t_ilm_nativedisplay);
29 ILM_EXPORT ilmErrorTypes ilmControl_registerShutdownNotification(
30 				shutdownNotificationFunc callback,
31 				void *user_data);
32 ILM_EXPORT void ilmControl_destroy(void);
33 
34 static pthread_mutex_t g_initialize_lock = PTHREAD_MUTEX_INITIALIZER;
35 static pthread_mutex_t g_deinitialize_lock = PTHREAD_MUTEX_INITIALIZER;
36 static int gInitialized = 0;
37 
38 ILM_EXPORT ilmErrorTypes
ilm_init(void)39 ilm_init(void)
40 {
41     return ilm_initWithNativedisplay(0);
42 }
43 
44 ILM_EXPORT ilmErrorTypes
ilm_initWithNativedisplay(t_ilm_nativedisplay nativedisplay)45 ilm_initWithNativedisplay(t_ilm_nativedisplay nativedisplay)
46 {
47     ilmErrorTypes err = ILM_SUCCESS;
48     ilmErrorTypes ret = ILM_FAILED;
49     t_ilm_nativedisplay display = 0;
50 
51     pthread_mutex_lock(&g_initialize_lock);
52 
53     do
54     {
55         init_ilmCommonPlatformTable();
56 
57         if (ilm_isInitialized())
58         {
59             fprintf(stderr, "[Warning] ilm_init or ilm_initWithNativedisplay is called,\n"
60                             "          but ilmClientLib has been already initialized.\n"
61                             "          gInitialized is incremented by one.\n"
62                             "          Returning success and incrementing gInitialized.\n"
63                             "          Initialization is skipped at this time.\n");
64             gInitialized++;
65             ret = ILM_SUCCESS;
66             break;
67         }
68 
69         err = gIlmCommonPlatformFunc.init(nativedisplay);
70         if (ILM_SUCCESS != err)
71         {
72             break;
73         }
74 
75         display = gIlmCommonPlatformFunc.getNativedisplay();
76 
77         err = ilmControl_init(display);
78         if (ILM_SUCCESS != err)
79         {
80             gIlmCommonPlatformFunc.destroy();
81             break;
82         }
83 
84         gInitialized++;
85         ret = ILM_SUCCESS;
86 
87     } while (0);
88 
89     pthread_mutex_unlock(&g_initialize_lock);
90 
91     return ret;
92 }
93 
94 ILM_EXPORT t_ilm_bool
ilm_isInitialized(void)95 ilm_isInitialized(void)
96 {
97     if(gIlmCommonPlatformFunc.isInitialized)
98         return gIlmCommonPlatformFunc.isInitialized();
99 
100     return ILM_FALSE;
101 }
102 
103 ILM_EXPORT ilmErrorTypes
ilm_registerShutdownNotification(shutdownNotificationFunc callback,void * user_data)104 ilm_registerShutdownNotification(shutdownNotificationFunc callback, void *user_data)
105 {
106     return ilmControl_registerShutdownNotification(callback, user_data);
107 }
108 
109 ILM_EXPORT ilmErrorTypes
ilm_destroy(void)110 ilm_destroy(void)
111 {
112     ilmErrorTypes retVal;
113 
114     pthread_mutex_lock(&g_deinitialize_lock);
115     if (gInitialized > 1)
116     {
117         fprintf(stderr, "[Warning] ilm_destroy is called, but gInitialized is %d.\n"
118                         "          Returning success and deinitialization is skipped\n"
119                         "          at this time.\n", gInitialized);
120         retVal = ILM_SUCCESS;
121     }
122     else
123     {
124         ilmControl_destroy(); // block until control thread is stopped
125         retVal = gIlmCommonPlatformFunc.destroy();
126     }
127 
128     if (retVal == ILM_SUCCESS)
129     {
130         gInitialized--;
131     }
132 
133     pthread_mutex_unlock(&g_deinitialize_lock);
134 
135     return retVal;
136 }
137