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