Add coding style guide.
[platform/core/uifw/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                 if (pthread_key_create(&thread_key, NULL) != 0) {
71                         COREGL_ERR("Failed to create thread key.\n");
72                         ret = 0;
73                         goto finish;
74                 }
75                 thread_key_inited = 1;
76         }
77
78         if (pthread_setspecific(thread_key, (void *)tstate) != 0) {
79                 COREGL_ERR("Failed to set thread data.\n");
80                 ret = 0;
81                 goto finish;
82         }
83
84         ret = 1;
85         goto finish;
86
87 finish:
88         AST(mutex_unlock(&thread_key_mutex) == 1);
89
90         return ret;
91 }
92
93 GLThreadState *
94 get_current_thread_state()
95 {
96         GLThreadState *ret = NULL;
97
98         if (thread_key_inited) {
99                 ret = (GLThreadState *)pthread_getspecific(thread_key);
100         }
101         goto finish;
102
103 finish:
104         return ret;
105 }
106