4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <asm/unistd.h>
27 #include "media-info-util.h"
28 #include "media-info-error.h"
29 #include "media-info-debug.h"
30 #include "media-svc.h"
31 #include "audio-svc.h"
32 #include "audio-svc-error.h"
35 static GHashTable *g_handle_table = NULL;
36 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
38 int _media_info_get_thread_id()
40 return syscall(__NR_gettid);
43 int _media_info_init_handle_tbl()
45 pthread_mutex_lock(&mutex);
47 if (g_handle_table == NULL)
48 g_handle_table = g_hash_table_new(g_int_hash, g_int_equal);
50 pthread_mutex_unlock(&mutex);
52 if (g_handle_table == NULL)
58 int _media_info_finalize_handle_tbl()
60 pthread_mutex_lock(&mutex);
62 if (g_handle_table != NULL) {
63 int size = g_hash_table_size(g_handle_table);
65 g_hash_table_destroy(g_handle_table);
66 g_handle_table = NULL;
68 mediainfo_dbg("handle table is not empty");
71 pthread_mutex_unlock(&mutex);
77 _media_info_insert_handle(int **tid_key, int tid, HandleTable ** handle_table)
79 *tid_key = g_malloc(sizeof **tid_key);
81 *handle_table = g_malloc(sizeof **handle_table);
83 pthread_mutex_lock(&mutex);
85 g_hash_table_insert(g_handle_table, (gpointer) * tid_key,
86 (gpointer) * handle_table);
88 (*handle_table)->ref_cnt = 1;
90 pthread_mutex_unlock(&mutex);
95 int _media_info_remove_handle(int tid)
98 HandleTable *val = NULL;
100 pthread_mutex_lock(&mutex);
102 if (!g_hash_table_lookup_extended
103 (g_handle_table, &tid, (gpointer) & key, (gpointer) & val)) {
104 pthread_mutex_unlock(&mutex);
107 if (g_hash_table_remove(g_handle_table, (gpointer) & tid))
108 mediainfo_dbg("g_hash_table_remove done");
110 mediainfo_dbg("g_hash_table_remove fails");
117 pthread_mutex_unlock(&mutex);
122 HandleTable *_media_info_search_handle(int tid)
124 pthread_mutex_lock(&mutex);
127 (HandleTable *) g_hash_table_lookup(g_handle_table,
130 pthread_mutex_unlock(&mutex);
135 sqlite3 *_media_info_get_proper_handle()
137 int tid = _media_info_get_thread_id();
139 (HandleTable *) g_hash_table_lookup(g_handle_table,
145 return value->handle;
148 void _media_info_atomic_add_counting(HandleTable *handle_table)
150 pthread_mutex_lock(&mutex);
151 handle_table->ref_cnt++;
152 pthread_mutex_unlock(&mutex);
156 void _media_info_atomic_sub_counting(HandleTable *handle_table)
158 pthread_mutex_lock(&mutex);
159 handle_table->ref_cnt--;
160 pthread_mutex_unlock(&mutex);
163 char *_media_info_generate_uuid()
166 static char uuid_unparsed[50];
168 uuid_generate(uuid_value);
169 uuid_unparse(uuid_value, uuid_unparsed);
171 //mediainfo_dbg("UUID : %s", uuid_unparsed);
172 return uuid_unparsed;