queue: don't unlock mutex before call alloc callback
[platform/core/uifw/libtbm.git] / src / tbm_dummy_display.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2018 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>,
8                  Changyeon Lee <cyeon.lee@samsung.com>,
9                  Sangjin Lee <lsj119@samsung.com>
10
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the
13 "Software"), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sub license, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice (including the
20 next paragraph) shall be included in all copies or substantial portions
21 of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
31 **************************************************************************/
32 #include "config.h"
33 #include "tbm_dummy_display.h"
34 #include "tbm_bufmgr_int.h"
35 #include "list.h"
36
37 /* LCOV_EXCL_START */
38
39 static int init_list = 0;
40 static struct list_head g_dummy_display_list;
41 static pthread_mutex_t tbm_dummy_display_lock;
42 void _tbm_dummy_display_mutex_unlock(void);
43
44 struct _tbm_dummy_display {
45         struct list_head link;
46 };
47
48 static bool
49 _tbm_dummy_display_mutex_init(void)
50 {
51         static bool tbm_dummy_display_mutex_init = false;
52
53         if (tbm_dummy_display_mutex_init)
54                 return true;
55
56         if (pthread_mutex_init(&tbm_dummy_display_lock, NULL)) {
57                 TBM_ERR("fail: pthread_mutex_init for tbm_dummy_display_lock.\n");
58                 return false;
59         }
60
61         tbm_dummy_display_mutex_init = true;
62
63         return true;
64 }
65
66 void
67 _tbm_dummy_display_mutex_lock(void)
68 {
69         if (!_tbm_dummy_display_mutex_init()) {
70                 TBM_ERR("fail: _tbm_dummy_display_mutex_init.\n");
71                 return;
72         }
73
74         pthread_mutex_lock(&tbm_dummy_display_lock);
75 }
76
77 void
78 _tbm_dummy_display_mutex_unlock(void)
79 {
80         pthread_mutex_unlock(&tbm_dummy_display_lock);
81 }
82
83 static int
84 _tbm_dummy_display_is_valid(struct _tbm_dummy_display *dummy_dpy)
85 {
86         struct _tbm_dummy_display *tmp_dpy = NULL;
87
88         if (!init_list) {
89                 _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
90                 return 0;
91         }
92
93         if (!LIST_IS_EMPTY(&g_dummy_display_list)) {
94                 LIST_FOR_EACH_ENTRY(tmp_dpy, &g_dummy_display_list, link) {
95                         if (tmp_dpy == dummy_dpy) {
96                                 TBM_INFO("tbm_dummy_display(%p)\n", dummy_dpy);
97                                 return 1;
98                         }
99                 }
100         }
101
102         _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
103
104         return 0;
105 }
106
107 tbm_dummy_display *
108 tbm_dummy_display_create(void)
109 {
110         struct _tbm_dummy_display *dpy = NULL;
111
112         _tbm_dummy_display_mutex_lock();
113         _tbm_set_last_result(TBM_ERROR_NONE);
114
115         /* init list */
116         if (!init_list) {
117                 LIST_INITHEAD(&g_dummy_display_list);
118                 init_list = 1;
119         }
120
121         dpy = calloc(1, sizeof(struct _tbm_dummy_display));
122         if (!dpy) {
123                 /* LCOV_EXCL_START */
124                 TBM_ERR("fail to allocate struct _tbm_dummy_display.\n");
125                 _tbm_set_last_result(TBM_ERROR_OUT_OF_MEMORY);
126                 _tbm_dummy_display_mutex_unlock();
127                 return NULL;
128                 /* LCOV_EXCL_STOP */
129         }
130
131         LIST_ADD(&dpy->link, &g_dummy_display_list);
132
133         _tbm_dummy_display_mutex_unlock();
134
135         return (tbm_dummy_display *)dpy;
136 }
137
138 void
139 tbm_dummy_display_destroy(tbm_dummy_display *dpy)
140 {
141         struct _tbm_dummy_display *dummy_dpy = NULL;
142
143         _tbm_dummy_display_mutex_lock();
144         _tbm_set_last_result(TBM_ERROR_NONE);
145
146         dummy_dpy = (struct _tbm_dummy_display *)dpy;
147
148         if (!_tbm_dummy_display_is_valid(dummy_dpy)) {
149                 _tbm_dummy_display_mutex_unlock();
150                 return;
151         }
152
153         LIST_DEL(&dummy_dpy->link);
154
155         free(dummy_dpy);
156
157         _tbm_dummy_display_mutex_unlock();
158 }
159
160 int
161 tbm_dummy_display_is_valid(tbm_dummy_display *dpy)
162 {
163         int ret = 0;
164         struct _tbm_dummy_display *dummy_dpy = NULL;
165
166         _tbm_dummy_display_mutex_lock();
167         _tbm_set_last_result(TBM_ERROR_NONE);
168
169         dummy_dpy = (struct _tbm_dummy_display *)dpy;
170
171         /* Return silently if dummy_display is null. */
172         if (!dummy_dpy) {
173                 _tbm_set_last_result(TBM_ERROR_INVALID_PARAMETER);
174                 _tbm_dummy_display_mutex_unlock();
175                 return 0;
176         }
177
178         ret = _tbm_dummy_display_is_valid(dummy_dpy);
179
180         _tbm_dummy_display_mutex_unlock();
181
182         return ret;
183 }
184
185 /* LCOV_EXCL_STOP */