@@ -34,6 +34,7 @@ typedef struct
34
34
} StringCacheEntry ;
35
35
36
36
static GHashTable * string_cache = NULL ;
37
+ static pthread_mutex_t string_cache_lock = PTHREAD_MUTEX_INITIALIZER ;
37
38
38
39
/* Function to create a new cache entry. */
39
40
static StringCacheEntry *
@@ -68,19 +69,20 @@ string_cache_get (const char *str)
68
69
return NULL ;
69
70
}
70
71
72
+ pthread_mutex_lock (& string_cache_lock );
71
73
StringCacheEntry * entry = g_hash_table_lookup (string_cache , str );
72
74
if (entry )
73
75
{
74
76
entry -> refcount ++ ;
75
- return entry -> str ;
76
77
}
77
78
else
78
79
{
79
80
entry = string_cache_entry_new (str );
80
81
/* Use entry->str as the key to ensure hash/equal functions operate on the string */
81
82
g_hash_table_insert (string_cache , entry -> str , entry );
82
- return entry -> str ;
83
83
}
84
+ pthread_mutex_unlock (& string_cache_lock );
85
+ return entry -> str ;
84
86
}
85
87
86
88
/* Decrease the reference count of a string and remove it if refcount reaches 0 */
@@ -92,6 +94,7 @@ string_cache_release (const char *str)
92
94
return ;
93
95
}
94
96
97
+ pthread_mutex_lock (& string_cache_lock );
95
98
StringCacheEntry * entry = g_hash_table_lookup (string_cache , str );
96
99
if (entry )
97
100
{
@@ -101,6 +104,7 @@ string_cache_release (const char *str)
101
104
g_hash_table_remove (string_cache , str );
102
105
}
103
106
}
107
+ pthread_mutex_unlock (& string_cache_lock );
104
108
}
105
109
106
110
/* Amount of actual memory used to store this string */
@@ -128,18 +132,22 @@ string_cache_memuse (const char *str, bool pss)
128
132
void
129
133
string_cache_init ()
130
134
{
135
+ pthread_mutex_lock (& string_cache_lock );
131
136
if (!string_cache )
132
137
{
133
138
string_cache =
134
139
g_hash_table_new_full (g_str_hash , g_str_equal , NULL ,
135
140
(GDestroyNotify ) string_cache_entry_free );
136
141
}
142
+ pthread_mutex_unlock (& string_cache_lock );
137
143
}
138
144
139
145
/* This function should only be called when *all* strings have been released. */
140
146
void
141
147
string_cache_deinit ()
142
148
{
149
+ pthread_mutex_lock (& string_cache_lock );
143
150
g_hash_table_destroy (string_cache );
144
151
string_cache = NULL ;
152
+ pthread_mutex_unlock (& string_cache_lock );
145
153
}
0 commit comments