1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2010 Younes Manton.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#include "util/u_handle_table.h"
29848b8605Smrg#include "os/os_thread.h"
30848b8605Smrg#include "vdpau_private.h"
31848b8605Smrg
32848b8605Smrgstatic struct handle_table *htab = NULL;
33b8e80941Smrgstatic mtx_t htab_lock = _MTX_INITIALIZER_NP;
34848b8605Smrg
35848b8605Smrgboolean vlCreateHTAB(void)
36848b8605Smrg{
37848b8605Smrg   boolean ret;
38b8e80941Smrg
39848b8605Smrg   /* Make sure handle table handles match VDPAU handles. */
40848b8605Smrg   assert(sizeof(unsigned) <= sizeof(vlHandle));
41b8e80941Smrg   mtx_lock(&htab_lock);
42848b8605Smrg   if (!htab)
43848b8605Smrg      htab = handle_table_create();
44848b8605Smrg   ret = htab != NULL;
45b8e80941Smrg   mtx_unlock(&htab_lock);
46848b8605Smrg   return ret;
47848b8605Smrg}
48848b8605Smrg
49848b8605Smrgvoid vlDestroyHTAB(void)
50848b8605Smrg{
51b8e80941Smrg   mtx_lock(&htab_lock);
52848b8605Smrg   if (htab && !handle_table_get_first_handle(htab)) {
53848b8605Smrg      handle_table_destroy(htab);
54848b8605Smrg      htab = NULL;
55848b8605Smrg   }
56b8e80941Smrg   mtx_unlock(&htab_lock);
57848b8605Smrg}
58848b8605Smrg
59848b8605SmrgvlHandle vlAddDataHTAB(void *data)
60848b8605Smrg{
61848b8605Smrg   vlHandle handle = 0;
62b8e80941Smrg
63b8e80941Smrg   assert(data);
64b8e80941Smrg   mtx_lock(&htab_lock);
65848b8605Smrg   if (htab)
66848b8605Smrg      handle = handle_table_add(htab, data);
67b8e80941Smrg   mtx_unlock(&htab_lock);
68848b8605Smrg   return handle;
69848b8605Smrg}
70848b8605Smrg
71848b8605Smrgvoid* vlGetDataHTAB(vlHandle handle)
72848b8605Smrg{
73848b8605Smrg   void *data = NULL;
74b8e80941Smrg
75b8e80941Smrg   assert(handle);
76b8e80941Smrg   mtx_lock(&htab_lock);
77848b8605Smrg   if (htab)
78848b8605Smrg      data = handle_table_get(htab, handle);
79b8e80941Smrg   mtx_unlock(&htab_lock);
80848b8605Smrg   return data;
81848b8605Smrg}
82848b8605Smrg
83848b8605Smrgvoid vlRemoveDataHTAB(vlHandle handle)
84848b8605Smrg{
85b8e80941Smrg   mtx_lock(&htab_lock);
86848b8605Smrg   if (htab)
87848b8605Smrg      handle_table_remove(htab, handle);
88b8e80941Smrg   mtx_unlock(&htab_lock);
89848b8605Smrg}
90