tizen 2.4 release
[framework/graphics/coregl.git] / src / coregl_thread_pthread.c
1 #include "coregl_internal.h"
2
3 //////////////////////////////////////////////////////////////////////////
4 // Need implement this
5 int mutex_init(Mutex *mt);
6 int mutex_lock(Mutex *mt);
7 int mutex_unlock(Mutex *mt);
8 int get_current_thread();
9 int set_current_thread_state(GLThreadState *tstate);
10 GLThreadState * get_current_thread_state();
11 //////////////////////////////////////////////////////////////////////////
12
13 static Mutex            thread_key_mutex = MUTEX_INITIALIZER;
14 static int              thread_key_inited = 0;
15 static pthread_key_t    thread_key = 0;
16
17 int
18 mutex_init(Mutex *mt)
19 {
20         int ret = 0;
21
22         if (pthread_mutex_init(mt, NULL) == 0)
23                 ret = 1;
24         else
25                 ret = 0;
26
27         return ret;
28 }
29
30 int
31 mutex_lock(Mutex *mt)
32 {
33         int ret = 0;
34
35         if (pthread_mutex_lock(mt) == 0)
36                 ret = 1;
37         else
38                 ret = 0;
39
40         return ret;
41 }
42
43 int
44 mutex_unlock(Mutex *mt)
45 {
46         int ret = 0;
47
48         if (pthread_mutex_unlock(mt) == 0)
49                 ret = 1;
50         else
51                 ret = 0;
52
53         return ret;
54 }
55
56 int
57 get_current_thread()
58 {
59         return pthread_self();
60 }
61
62 int
63 set_current_thread_state(GLThreadState *tstate)
64 {
65         int ret = 0;
66
67         AST(mutex_lock(&thread_key_mutex) == 1);
68
69         if (thread_key_inited == 0)
70         {
71                 if (pthread_key_create(&thread_key, NULL) != 0)
72                 {
73                         COREGL_ERR("Failed to create thread key.\n");
74                         ret = 0;
75                         goto finish;
76                 }
77                 thread_key_inited = 1;
78         }
79
80         if (pthread_setspecific(thread_key, (void *)tstate) != 0)
81         {
82                 COREGL_ERR("Failed to set thread data.\n");
83                 ret = 0;
84                 goto finish;
85         }
86
87         ret = 1;
88         goto finish;
89
90 finish:
91         AST(mutex_unlock(&thread_key_mutex) == 1);
92
93         return ret;
94 }
95
96 GLThreadState *
97 get_current_thread_state()
98 {
99         GLThreadState *ret = NULL;
100
101         if (thread_key_inited)
102         {
103                 ret = (GLThreadState *)pthread_getspecific(thread_key);
104         }
105         goto finish;
106
107 finish:
108         return ret;
109 }
110