xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/vdpau/htab.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2010 Younes Manton.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "util/simple_mtx.h"
29 #include "util/u_handle_table.h"
30 #include "util/u_thread.h"
31 #include "vdpau_private.h"
32 
33 static struct handle_table *htab = NULL;
34 static simple_mtx_t htab_lock = SIMPLE_MTX_INITIALIZER;
35 
vlCreateHTAB(void)36 bool vlCreateHTAB(void)
37 {
38    bool ret;
39 
40    /* Make sure handle table handles match VDPAU handles. */
41    assert(sizeof(unsigned) <= sizeof(vlHandle));
42    simple_mtx_lock(&htab_lock);
43    if (!htab)
44       htab = handle_table_create();
45    ret = htab != NULL;
46    simple_mtx_unlock(&htab_lock);
47    return ret;
48 }
49 
vlDestroyHTAB(void)50 void vlDestroyHTAB(void)
51 {
52    simple_mtx_lock(&htab_lock);
53    if (htab && !handle_table_get_first_handle(htab)) {
54       handle_table_destroy(htab);
55       htab = NULL;
56    }
57    simple_mtx_unlock(&htab_lock);
58 }
59 
vlAddDataHTAB(void * data)60 vlHandle vlAddDataHTAB(void *data)
61 {
62    vlHandle handle = 0;
63 
64    assert(data);
65    simple_mtx_lock(&htab_lock);
66    if (htab)
67       handle = handle_table_add(htab, data);
68    simple_mtx_unlock(&htab_lock);
69    return handle;
70 }
71 
vlGetDataHTAB(vlHandle handle)72 void* vlGetDataHTAB(vlHandle handle)
73 {
74    void *data = NULL;
75 
76    assert(handle);
77    simple_mtx_lock(&htab_lock);
78    if (htab)
79       data = handle_table_get(htab, handle);
80    simple_mtx_unlock(&htab_lock);
81    return data;
82 }
83 
vlRemoveDataHTAB(vlHandle handle)84 void vlRemoveDataHTAB(vlHandle handle)
85 {
86    simple_mtx_lock(&htab_lock);
87    if (htab)
88       handle_table_remove(htab, handle);
89    simple_mtx_unlock(&htab_lock);
90 }
91