forked from i-rinat/libvdpau-va-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle-storage.c
184 lines (163 loc) · 4.73 KB
/
handle-storage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright 2013 Rinat Ibragimov
*
* This file is part of libvdpau-va-gl
*
* libvdpau-va-gl is distributed under the terms of the LGPLv3. See COPYING for details.
*/
#define _XOPEN_SOURCE 500
#include "handle-storage.h"
#include <pthread.h>
#include <glib.h>
#include <unistd.h>
GPtrArray *vdpHandles;
GHashTable *xdpy_copies; //< Copies of X Display connections
GHashTable *xdpy_copies_refcount; //< Reference count of X Display connection copy
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void
handle_initialize_storage(void)
{
pthread_mutex_lock(&lock);
vdpHandles = g_ptr_array_new();
// adding dummy element to ensure all handles start from 1
g_ptr_array_add(vdpHandles, NULL);
xdpy_copies = g_hash_table_new(g_direct_hash, g_direct_equal);
xdpy_copies_refcount = g_hash_table_new(g_direct_hash, g_direct_equal);
pthread_mutex_unlock(&lock);
}
int
handle_insert(void *data)
{
pthread_mutex_lock(&lock);
g_ptr_array_add(vdpHandles, data);
int id = vdpHandles->len - 1;
pthread_mutex_unlock(&lock);
return id;
}
// lock unsafe function
static
int
_is_valid(int handle, HandleType type)
{
VdpGenericHandle *gh;
// return false if index is out of range
if (handle < 1 || handle >= (int)vdpHandles->len)
return 0;
// return false if entry was deleted
gh = g_ptr_array_index(vdpHandles, handle);
if (!gh)
return 0;
// return true if caller wants any handle type
if (HANDLETYPE_ANY == type)
return 1;
// check handle type
if (gh->type == type)
return 1;
return 0;
}
void *
handle_acquire(int handle, HandleType type)
{
VdpGenericHandle *res = NULL;
while (1) {
pthread_mutex_lock(&lock);
if (!_is_valid(handle, type))
break;
res = g_ptr_array_index(vdpHandles, handle);
if (pthread_mutex_trylock(&res->lock) == 0)
break;
pthread_mutex_unlock(&lock);
usleep(1);
}
pthread_mutex_unlock(&lock);
return res;
}
void
handle_release(int handle)
{
pthread_mutex_lock(&lock);
if (handle > 0 && handle < (int)vdpHandles->len) {
VdpGenericHandle *gh = g_ptr_array_index(vdpHandles, handle);
if (gh)
pthread_mutex_unlock(&gh->lock);
}
pthread_mutex_unlock(&lock);
}
void
handle_expunge(int handle)
{
pthread_mutex_lock(&lock);
if (_is_valid(handle, HANDLETYPE_ANY)) {
VdpGenericHandle *gh = g_ptr_array_index(vdpHandles, handle);
if (gh)
pthread_mutex_unlock(&gh->lock);
g_ptr_array_index(vdpHandles, handle) = NULL;
}
pthread_mutex_unlock(&lock);
}
void
handle_destory_storage(void)
{
pthread_mutex_lock(&lock);
g_ptr_array_unref(vdpHandles);
g_hash_table_unref(xdpy_copies);
g_hash_table_unref(xdpy_copies_refcount);
vdpHandles = NULL;
xdpy_copies = NULL;
xdpy_copies_refcount = NULL;
pthread_mutex_unlock(&lock);
}
void
handle_execute_for_all(void (*callback)(int idx, void *entry, void *p), void *param)
{
unsigned int k = 0;
pthread_mutex_lock(&lock);
while (k < vdpHandles->len) {
void *item = g_ptr_array_index(vdpHandles, k);
if (item) {
pthread_mutex_unlock(&lock);
// TODO: race condition. Supply integer handle instead of pointer to fix.
callback(k, item, param);
pthread_mutex_lock(&lock);
}
k ++;
}
pthread_mutex_unlock(&lock);
}
void *
handle_xdpy_ref(void *dpy_orig)
{
pthread_mutex_lock(&lock);
Display *dpy = g_hash_table_lookup(xdpy_copies, dpy_orig);
if (NULL == dpy) {
dpy = XOpenDisplay(XDisplayString(dpy_orig));
if (!dpy)
goto quit;
g_hash_table_replace(xdpy_copies, dpy_orig, dpy);
g_hash_table_replace(xdpy_copies_refcount, dpy_orig, GINT_TO_POINTER(1));
} else {
int refcount = GPOINTER_TO_INT(g_hash_table_lookup(xdpy_copies_refcount, dpy_orig));
g_hash_table_replace(xdpy_copies_refcount, dpy_orig, GINT_TO_POINTER(refcount+1));
}
quit:
pthread_mutex_unlock(&lock);
return dpy;
}
void
handle_xdpy_unref(void *dpy_orig)
{
pthread_mutex_lock(&lock);
int refcount = GPOINTER_TO_INT(g_hash_table_lookup(xdpy_copies_refcount, dpy_orig));
refcount = refcount - 1;
if (0 == refcount) {
// do close connection, nobody refers it anymore
Display *dpy = g_hash_table_lookup(xdpy_copies, dpy_orig);
XCloseDisplay(dpy);
g_hash_table_remove(xdpy_copies, dpy_orig);
g_hash_table_remove(xdpy_copies_refcount, dpy_orig);
} else {
// just update refcount
g_hash_table_replace(xdpy_copies_refcount, dpy_orig, GINT_TO_POINTER(refcount));
}
pthread_mutex_unlock(&lock);
}