1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Thread Safety Implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <stdlib.h> |
|
19 #include "xathreadsafety.h" |
|
20 |
|
21 typedef struct |
|
22 { |
|
23 XAImplMutexHandle mutexTable[XATSCount]; |
|
24 XAboolean tsEnabled; |
|
25 } XAThreadSafetyImpl; |
|
26 |
|
27 static XAThreadSafetyImpl* threadSafety; |
|
28 |
|
29 /* |
|
30 * XAresult XAThreadSafety_Init() |
|
31 * Description: Creates mutex table for thread safety support |
|
32 * @return: Success value |
|
33 */ |
|
34 XAresult XAThreadSafety_Init(XAboolean tsEnable) |
|
35 { |
|
36 XAresult ret = XA_RESULT_SUCCESS; |
|
37 XAint32 i = 0; |
|
38 DEBUG_API_A1("->XAThreadSafety_Init - tsEnable:%lu",tsEnable); |
|
39 |
|
40 /* Initialize thread safety only once */ |
|
41 if (!threadSafety) |
|
42 { |
|
43 threadSafety = (XAThreadSafetyImpl *) calloc(1, |
|
44 sizeof(XAThreadSafetyImpl)); |
|
45 if (!threadSafety) |
|
46 { |
|
47 DEBUG_ERR("XA_RESULT_MEMORY_FAILURE"); |
|
48 DEBUG_API("<-XAThreadSafety_Init"); |
|
49 /* memory allocation failed */ |
|
50 return XA_RESULT_MEMORY_FAILURE; |
|
51 } |
|
52 |
|
53 threadSafety->tsEnabled = tsEnable; |
|
54 |
|
55 if (tsEnable) |
|
56 { |
|
57 for (i = 0; i < XATSCount; i++) |
|
58 { |
|
59 ret = XAImpl_CreateMutex(&threadSafety->mutexTable[i]); |
|
60 if (ret != XA_RESULT_SUCCESS) |
|
61 { |
|
62 break; |
|
63 } |
|
64 DEBUG_INFO_A2("Created %s:%x",MEDIAOBJECTNAME(i), threadSafety->mutexTable[i] ); |
|
65 } |
|
66 } |
|
67 else |
|
68 { |
|
69 DEBUG_INFO("Thread safety: disabled."); |
|
70 } |
|
71 } |
|
72 |
|
73 DEBUG_API("<-XAThreadSafety_Init"); |
|
74 return ret; |
|
75 } |
|
76 |
|
77 /* |
|
78 * XAresult XAThreadSafety_Destroy() |
|
79 * Description: Destroys mutex table created for thread safety support |
|
80 */ |
|
81 XAresult XAThreadSafety_Destroy() |
|
82 { |
|
83 XAresult ret = XA_RESULT_SUCCESS; |
|
84 XAint32 i = 0; |
|
85 DEBUG_API("->XAThreadSafety_Destroy"); |
|
86 |
|
87 if (threadSafety) |
|
88 { |
|
89 if (threadSafety->tsEnabled) |
|
90 { |
|
91 for (i = 0; i < XATSCount; i++) |
|
92 { |
|
93 DEBUG_INFO_A2("Free %s:%x",MEDIAOBJECTNAME(i), threadSafety->mutexTable[i] ); |
|
94 XAImpl_DeleteMutex(threadSafety->mutexTable[i]); |
|
95 } |
|
96 } |
|
97 free(threadSafety); |
|
98 } |
|
99 else |
|
100 { |
|
101 DEBUG_INFO("Thread safety: disabled."); |
|
102 } |
|
103 |
|
104 DEBUG_API("<-XAThreadSafety_Destroy"); |
|
105 return ret; |
|
106 |
|
107 } |
|
108 |
|
109 /* |
|
110 * XAresult XAThreadSafety_Unlock( XAThreadSafetyMediaObjects mediaObject ) |
|
111 * Description: |
|
112 * @param XAThreadSafetyMediaObjects mediaObject |
|
113 * @return |
|
114 */ |
|
115 XAresult XAThreadSafety_Unlock(XAThreadSafetyMediaObjects mediaObject) |
|
116 { |
|
117 XAresult ret = XA_RESULT_SUCCESS; |
|
118 DEBUG_API("->XAThreadSafety_Unlock"); |
|
119 |
|
120 if (threadSafety) |
|
121 { |
|
122 if (threadSafety->tsEnabled) |
|
123 { |
|
124 ret = XAImpl_UnlockMutex(threadSafety->mutexTable[mediaObject]); |
|
125 if (ret == XA_RESULT_SUCCESS) |
|
126 { |
|
127 DEBUG_INFO_A2("Released lock for %s:%x",MEDIAOBJECTNAME(mediaObject), threadSafety->mutexTable[mediaObject] ); |
|
128 } |
|
129 } |
|
130 } |
|
131 else |
|
132 { |
|
133 DEBUG_INFO("Thread safety: disabled."); |
|
134 } |
|
135 DEBUG_API("<-XAThreadSafety_Unlock"); |
|
136 return ret; |
|
137 } |
|
138 /* |
|
139 * XAresult XAThreadSafety_TryLock( XAThreadSafetyMediaObjects mediaObject ); |
|
140 * Description: |
|
141 * @param XAThreadSafetyMediaObjects mediaObject |
|
142 * @return |
|
143 */ |
|
144 XAresult XAThreadSafety_TryLock(XAThreadSafetyMediaObjects mediaObject) |
|
145 { |
|
146 XAresult ret = XA_RESULT_SUCCESS; |
|
147 DEBUG_API("->XAThreadSafety_TryLock"); |
|
148 |
|
149 if (threadSafety) |
|
150 { |
|
151 if (threadSafety->tsEnabled) |
|
152 { |
|
153 ret = XAImpl_TryLockMutex(threadSafety->mutexTable[mediaObject]); |
|
154 if (ret == XA_RESULT_SUCCESS) |
|
155 { |
|
156 DEBUG_INFO_A2("Locked %s:%x",MEDIAOBJECTNAME(mediaObject), threadSafety->mutexTable[mediaObject] ); |
|
157 } |
|
158 } |
|
159 } |
|
160 else |
|
161 { |
|
162 DEBUG_INFO("Thread safety: disabled."); |
|
163 } |
|
164 |
|
165 DEBUG_API("<-XAThreadSafety_TryLock"); |
|
166 return ret; |
|
167 } |
|
168 |
|