aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gsldatacache.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* GSL - Generic Sound Layer
  2. * Copyright (C) 2001-2002 Tim Janik
  3. * Copyright (C) 2004 Stefan Westerfeld
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General
  16. * Public License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "gsldatacache.h"
  21. #include "gslcommon.h"
  22. #include "gsldatahandle.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <time.h>
  29. /* --- macros --- */
  30. #define NODEP_INDEX(dcache, node_p) ((node_p) - (dcache)->nodes)
  31. #define UPPER_POWER2(n) (gsl_alloc_upper_power2 (MAX (n, 4)))
  32. #define CONFIG_NODE_SIZE() (gsl_get_config ()->dcache_block_size)
  33. #define AGE_EPSILON (3) /* must be < smallest sweep */
  34. #define LOW_PERSISTENCY_SWEEP (5)
  35. /* we use one global lock to protect the dcache list, the list
  36. * count (length) and the number of aged (unused) nodes.
  37. * also, each dcache has its own mutext to protect updates in
  38. * the reference count, nodes or node data blocks.
  39. * in order to avoid deadlocks, if both locks need
  40. * to be held, they always have to be acquired in the order
  41. * 1) global lock, 2) dcache lock.
  42. * asyncronous data block filling for a new node occours without
  43. * the dcache lock being held (as most calls to GslDataHandle
  44. * functions).
  45. * however, assignment of the newly acquired data is again
  46. * protected by the dcache lock. concurrent API entries
  47. * which require demand loading of such data will wait on
  48. * a global condition which is always signaled once a new data
  49. * block read has been completed. using one global condition
  50. * is considered sufficient until shown otherwise by further
  51. * profiling/debugging measures.
  52. */
  53. /* --- prototypes --- */
  54. static void dcache_free (GslDataCache *dcache);
  55. static GslDataCacheNode* data_cache_new_node_L (GslDataCache *dcache,
  56. gsize offset,
  57. guint pos,
  58. gboolean demand_load);
  59. /* --- variables --- */
  60. static GslMutex global_dcache_mutex = { 0, };
  61. static GslCond global_dcache_cond_node_filled = { 0, };
  62. static GslRing *global_dcache_list = NULL;
  63. static guint global_dcache_count = 0;
  64. static guint global_dcache_n_aged_nodes = 0;
  65. /* --- functions --- */
  66. void
  67. _gsl_init_data_caches (void)
  68. {
  69. static gboolean initialized = FALSE;
  70. g_assert (initialized == FALSE);
  71. initialized++;
  72. g_assert (AGE_EPSILON < LOW_PERSISTENCY_SWEEP);
  73. gsl_cond_init (&global_dcache_cond_node_filled);
  74. gsl_mutex_init (&global_dcache_mutex);
  75. }
  76. GslDataCache*
  77. gsl_data_cache_new (GslDataHandle *dhandle,
  78. guint padding)
  79. {
  80. guint node_size = CONFIG_NODE_SIZE () / sizeof (GslDataType);
  81. GslDataCache *dcache;
  82. g_return_val_if_fail (dhandle != NULL, NULL);
  83. g_return_val_if_fail (padding > 0, NULL);
  84. g_return_val_if_fail (dhandle->name != NULL, NULL);
  85. g_assert (node_size == gsl_alloc_upper_power2 (node_size));
  86. g_return_val_if_fail (padding < node_size / 2, NULL);
  87. /* allocate new closed dcache if necessary */
  88. dcache = gsl_new_struct (GslDataCache, 1);
  89. dcache->dhandle = gsl_data_handle_ref (dhandle);
  90. dcache->open_count = 0;
  91. gsl_mutex_init (&dcache->mutex);
  92. dcache->ref_count = 1;
  93. dcache->node_size = node_size;
  94. dcache->padding = padding;
  95. dcache->max_age = 0;
  96. dcache->low_persistency = FALSE; /* FIXME: !gsl_data_handle_needs_cache (dcache->dhandle); */
  97. dcache->n_nodes = 0;
  98. dcache->nodes = g_renew (GslDataCacheNode*, NULL, UPPER_POWER2 (dcache->n_nodes));
  99. GSL_SPIN_LOCK (&global_dcache_mutex);
  100. global_dcache_list = gsl_ring_append (global_dcache_list, dcache);
  101. global_dcache_count++;
  102. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  103. return dcache;
  104. }
  105. void
  106. gsl_data_cache_open (GslDataCache *dcache)
  107. {
  108. g_return_if_fail (dcache != NULL);
  109. g_return_if_fail (dcache->ref_count > 0);
  110. GSL_SPIN_LOCK (&dcache->mutex);
  111. if (!dcache->open_count)
  112. {
  113. GslErrorType error;
  114. error = gsl_data_handle_open (dcache->dhandle);
  115. if (error)
  116. {
  117. /* FIXME: this is pretty fatal, throw out zero blocks now? */
  118. gsl_message_send (GSL_MSG_DATA_CACHE, "Open",
  119. error,
  120. "failed to open \"%s\": %s",
  121. dcache->dhandle->name,
  122. gsl_strerror (error));
  123. }
  124. else
  125. {
  126. dcache->open_count = 1;
  127. dcache->ref_count++;
  128. }
  129. }
  130. else
  131. dcache->open_count++;
  132. GSL_SPIN_UNLOCK (&dcache->mutex);
  133. }
  134. void
  135. gsl_data_cache_close (GslDataCache *dcache)
  136. {
  137. gboolean need_unref;
  138. g_return_if_fail (dcache != NULL);
  139. g_return_if_fail (dcache->ref_count > 0);
  140. g_return_if_fail (dcache->open_count > 0);
  141. GSL_SPIN_LOCK (&dcache->mutex);
  142. dcache->open_count--;
  143. need_unref = !dcache->open_count;
  144. if (!dcache->open_count)
  145. gsl_data_handle_close (dcache->dhandle);
  146. GSL_SPIN_UNLOCK (&dcache->mutex);
  147. if (need_unref)
  148. gsl_data_cache_unref (dcache);
  149. }
  150. GslDataCache*
  151. gsl_data_cache_ref (GslDataCache *dcache)
  152. {
  153. g_return_val_if_fail (dcache != NULL, NULL);
  154. g_return_val_if_fail (dcache->ref_count > 0, NULL);
  155. /* we might get invoked with global_dcache_mutex locked */
  156. GSL_SPIN_LOCK (&dcache->mutex);
  157. dcache->ref_count++;
  158. GSL_SPIN_UNLOCK (&dcache->mutex);
  159. return dcache;
  160. }
  161. static void
  162. dcache_free (GslDataCache *dcache)
  163. {
  164. guint i;
  165. g_return_if_fail (dcache->ref_count == 0);
  166. g_return_if_fail (dcache->open_count == 0);
  167. gsl_data_handle_unref (dcache->dhandle);
  168. gsl_mutex_destroy (&dcache->mutex);
  169. for (i = 0; i < dcache->n_nodes; i++)
  170. {
  171. GslDataCacheNode *node = dcache->nodes[i];
  172. guint size;
  173. size = dcache->node_size + (dcache->padding << 1);
  174. gsl_delete_structs (GslDataType, size, node->data - dcache->padding);
  175. gsl_delete_struct (GslDataCacheNode, node);
  176. }
  177. g_free (dcache->nodes);
  178. gsl_delete_struct (GslDataCache, dcache);
  179. }
  180. void
  181. gsl_data_cache_unref (GslDataCache *dcache)
  182. {
  183. g_return_if_fail (dcache != NULL);
  184. restart:
  185. g_return_if_fail (dcache->ref_count > 0);
  186. if (dcache->ref_count == 1) /* possible destruction, need global lock */
  187. {
  188. g_return_if_fail (dcache->open_count == 0);
  189. GSL_SPIN_LOCK (&global_dcache_mutex);
  190. GSL_SPIN_LOCK (&dcache->mutex);
  191. if (dcache->ref_count != 1)
  192. {
  193. /* damn, some other thread trapped in, restart */
  194. GSL_SPIN_UNLOCK (&dcache->mutex);
  195. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  196. goto restart;
  197. }
  198. dcache->ref_count = 0;
  199. global_dcache_list = gsl_ring_remove (global_dcache_list, dcache);
  200. GSL_SPIN_UNLOCK (&dcache->mutex);
  201. global_dcache_count--;
  202. global_dcache_n_aged_nodes -= dcache->n_nodes;
  203. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  204. dcache_free (dcache);
  205. }
  206. else
  207. {
  208. GSL_SPIN_LOCK (&dcache->mutex);
  209. if (dcache->ref_count < 2)
  210. {
  211. /* damn, some other thread trapped in, restart */
  212. GSL_SPIN_UNLOCK (&dcache->mutex);
  213. goto restart;
  214. }
  215. dcache->ref_count--;
  216. GSL_SPIN_UNLOCK (&dcache->mutex);
  217. }
  218. }
  219. static inline GslDataCacheNode**
  220. data_cache_lookup_nextmost_node_L (GslDataCache *dcache,
  221. gsize offset)
  222. {
  223. if (dcache->n_nodes > 0)
  224. {
  225. GslDataCacheNode **check, **nodes = dcache->nodes;
  226. guint n_nodes = dcache->n_nodes, node_size = dcache->node_size;
  227. /* caller has to figure himself whether we return nextmost vs. exact match */
  228. nodes -= 1;
  229. do
  230. {
  231. register gint cmp;
  232. register guint i;
  233. i = (n_nodes + 1) >> 1;
  234. check = nodes + i;
  235. cmp = offset < (*check)->offset ? -1 : offset >= (*check)->offset + node_size;
  236. if (cmp == 0)
  237. return check; /* exact match */
  238. else if (cmp > 0)
  239. {
  240. n_nodes -= i;
  241. nodes = check;
  242. }
  243. else /* if (cmp < 0) */
  244. n_nodes = i - 1;
  245. }
  246. while (n_nodes);
  247. return check; /* nextmost */
  248. }
  249. return NULL;
  250. }
  251. static inline GslDataCacheNode*
  252. data_cache_new_node_L (GslDataCache *dcache,
  253. gsize offset,
  254. guint pos,
  255. gboolean demand_load)
  256. {
  257. GslDataCacheNode **node_p, *dnode;
  258. GslDataCacheNode *left_node;
  259. GslDataType *data, *node_data;
  260. guint new_node_array_size, old_node_array_size = UPPER_POWER2 (dcache->n_nodes);
  261. GslLong dhandle_length;
  262. guint i, size;
  263. gint result;
  264. i = dcache->n_nodes++;
  265. new_node_array_size = UPPER_POWER2 (dcache->n_nodes);
  266. if (old_node_array_size != new_node_array_size)
  267. dcache->nodes = g_renew (GslDataCacheNode*, dcache->nodes, new_node_array_size);
  268. node_p = dcache->nodes + pos;
  269. g_memmove (node_p + 1, node_p, (i - pos) * sizeof (*node_p));
  270. dnode = gsl_new_struct (GslDataCacheNode, 1);
  271. (*node_p) = dnode;
  272. dnode->offset = offset & ~(dcache->node_size - 1);
  273. dnode->ref_count = 1;
  274. dnode->age = 0;
  275. dnode->data = NULL;
  276. GSL_SPIN_UNLOCK (&dcache->mutex);
  277. size = dcache->node_size + (dcache->padding << 1);
  278. data = gsl_new_struct (GslDataType, size);
  279. node_data = data + dcache->padding;
  280. offset = dnode->offset;
  281. if (dcache->padding > offset) /* pad out bytes before data start */
  282. {
  283. guint short_pad = dcache->padding - offset;
  284. memset (data, 0, short_pad * sizeof (GslDataType));
  285. size -= short_pad;
  286. data += short_pad;
  287. offset -= (dcache->padding - short_pad); /* should always result in offset=0 */
  288. }
  289. else
  290. offset -= dcache->padding;
  291. if (!demand_load)
  292. g_message (G_STRLOC ":FIXME: lazy data loading not yet supported");
  293. /* if we have a left node, and it contains data that we need, copy it */
  294. left_node = pos ? dcache->nodes[pos - 1] : NULL;
  295. if (left_node)
  296. {
  297. guint left_node_size = dcache->node_size;
  298. gint left_node_offset = left_node->offset;
  299. GslDataType *left_node_data = left_node->data;
  300. /* padding around left_node */
  301. left_node_size += (dcache->padding << 1);
  302. left_node_offset -= dcache->padding;
  303. left_node_data -= dcache->padding;
  304. if (offset < left_node_offset + left_node_size)
  305. {
  306. guint left_node_copy = left_node_offset + left_node_size - offset;
  307. memcpy (data, left_node_data + offset - left_node_offset,
  308. left_node_copy * sizeof (GslDataType));
  309. offset += left_node_copy;
  310. size -= left_node_copy;
  311. data += left_node_copy;
  312. }
  313. }
  314. dhandle_length = gsl_data_handle_length (dcache->dhandle);
  315. do
  316. {
  317. if (offset >= dhandle_length)
  318. break;
  319. size = MIN (size, dhandle_length - offset);
  320. result = gsl_data_handle_read (dcache->dhandle, offset, size, data);
  321. if (result < 0)
  322. {
  323. gsl_message_send (GSL_MSG_DATA_CACHE, "ReadAhead",
  324. GSL_ERROR_READ_FAILED,
  325. "reading from \"%s\"", dcache->dhandle->name);
  326. break;
  327. }
  328. else
  329. {
  330. offset += result;
  331. size -= result;
  332. data += result;
  333. }
  334. }
  335. while (size && result > 0);
  336. memset (data, 0, size * sizeof (data[0]));
  337. GSL_SPIN_LOCK (&dcache->mutex);
  338. dnode->data = node_data;
  339. gsl_cond_broadcast (&global_dcache_cond_node_filled);
  340. return dnode;
  341. }
  342. GslDataCacheNode*
  343. gsl_data_cache_ref_node (GslDataCache *dcache,
  344. gsize offset,
  345. GslDataCacheRequest load_request)
  346. {
  347. GslDataCacheNode **node_p, *node;
  348. guint insertion_pos;
  349. g_return_val_if_fail (dcache != NULL, NULL);
  350. g_return_val_if_fail (dcache->ref_count > 0, NULL);
  351. g_return_val_if_fail (dcache->open_count > 0, NULL);
  352. g_return_val_if_fail (offset < gsl_data_handle_length (dcache->dhandle), NULL);
  353. GSL_SPIN_LOCK (&dcache->mutex);
  354. node_p = data_cache_lookup_nextmost_node_L (dcache, offset);
  355. if (node_p)
  356. {
  357. node = *node_p;
  358. if (offset >= node->offset && offset < node->offset + dcache->node_size)
  359. {
  360. gboolean rejuvenate_node = !node->ref_count;
  361. if (load_request == GSL_DATA_CACHE_PEEK)
  362. {
  363. if (node->data)
  364. node->ref_count++;
  365. else
  366. node = NULL;
  367. GSL_SPIN_UNLOCK (&dcache->mutex);
  368. if (node && rejuvenate_node)
  369. {
  370. GSL_SPIN_LOCK (&global_dcache_mutex); /* different lock */
  371. global_dcache_n_aged_nodes--;
  372. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  373. }
  374. return node;
  375. }
  376. node->ref_count++;
  377. if (load_request == GSL_DATA_CACHE_DEMAND_LOAD)
  378. while (!node->data)
  379. gsl_cond_wait (&global_dcache_cond_node_filled, &dcache->mutex);
  380. GSL_SPIN_UNLOCK (&dcache->mutex);
  381. /* g_printerr ("hit: %d :%d: %d\n", node->offset, offset, node->offset + dcache->node_size); */
  382. if (rejuvenate_node)
  383. {
  384. GSL_SPIN_LOCK (&global_dcache_mutex); /* different lock */
  385. global_dcache_n_aged_nodes--;
  386. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  387. }
  388. return node; /* exact match */
  389. }
  390. insertion_pos = NODEP_INDEX (dcache, node_p); /* insert before neighbour */
  391. if (offset > node->offset) /* insert after neighbour */
  392. insertion_pos += 1;
  393. /* g_printerr ("mis: %d :%d: %d\n", node->offset, offset, node->offset + dcache->node_size); */
  394. }
  395. else
  396. insertion_pos = 0; /* insert at start */
  397. if (load_request != GSL_DATA_CACHE_PEEK)
  398. node = data_cache_new_node_L (dcache, offset, insertion_pos, load_request == GSL_DATA_CACHE_DEMAND_LOAD);
  399. else
  400. node = NULL;
  401. GSL_SPIN_UNLOCK (&dcache->mutex);
  402. return node;
  403. }
  404. static gboolean /* still locked */
  405. data_cache_free_olders_Lunlock (GslDataCache *dcache,
  406. guint max_lru) /* how many lru nodes to keep */
  407. {
  408. GslDataCacheNode **slot_p;
  409. guint i, rejuvenate, size;
  410. guint n_freed = 0;
  411. g_return_val_if_fail (dcache != NULL, TRUE);
  412. /* it doesn't make sense to free nodes below the jitter that
  413. * AGE_EPSILON attempts to prevent.
  414. */
  415. max_lru = MAX (AGE_EPSILON, max_lru);
  416. if (max_lru >= dcache->max_age)
  417. return TRUE;
  418. rejuvenate = dcache->max_age - max_lru;
  419. if (0)
  420. g_print ("start sweep: dcache (%p) with %u nodes, max_age: %u, rejuvenate: %u (max_lru: %u)\n",
  421. dcache, dcache->n_nodes, dcache->max_age, rejuvenate, max_lru);
  422. size = dcache->node_size + (dcache->padding << 1);
  423. slot_p = NULL;
  424. for (i = 0; i < dcache->n_nodes; i++)
  425. {
  426. GslDataCacheNode *node = dcache->nodes[i];
  427. if (!node->ref_count && node->age <= rejuvenate)
  428. {
  429. gsl_delete_structs (GslDataType, size, node->data - dcache->padding);
  430. gsl_delete_struct (GslDataCacheNode, node);
  431. if (!slot_p)
  432. slot_p = dcache->nodes + i;
  433. n_freed++;
  434. }
  435. else
  436. {
  437. node->age -= MIN (rejuvenate, node->age);
  438. if (slot_p)
  439. {
  440. *slot_p = node;
  441. slot_p++;
  442. }
  443. }
  444. }
  445. dcache->max_age = max_lru;
  446. if (slot_p)
  447. dcache->n_nodes = NODEP_INDEX (dcache, slot_p);
  448. GSL_SPIN_UNLOCK (&dcache->mutex);
  449. if (n_freed)
  450. {
  451. GSL_SPIN_LOCK (&global_dcache_mutex);
  452. global_dcache_n_aged_nodes -= n_freed;
  453. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  454. }
  455. if (0)
  456. g_printerr ("freed %u nodes (%u bytes) remaining %u bytes\n",
  457. n_freed, n_freed * CONFIG_NODE_SIZE (),
  458. global_dcache_n_aged_nodes * CONFIG_NODE_SIZE ());
  459. return FALSE;
  460. }
  461. void
  462. gsl_data_cache_unref_node (GslDataCache *dcache,
  463. GslDataCacheNode *node)
  464. {
  465. GslDataCacheNode **node_p;
  466. gboolean check_cache;
  467. g_return_if_fail (dcache != NULL);
  468. g_return_if_fail (node != NULL);
  469. g_return_if_fail (node->ref_count > 0);
  470. GSL_SPIN_LOCK (&dcache->mutex);
  471. node_p = data_cache_lookup_nextmost_node_L (dcache, node->offset);
  472. g_assert (node_p && *node_p == node); /* paranoid check lookup, yeah! */
  473. node->ref_count -= 1;
  474. check_cache = !node->ref_count;
  475. if (!node->ref_count &&
  476. (node->age + AGE_EPSILON <= dcache->max_age ||
  477. dcache->max_age < AGE_EPSILON))
  478. node->age = ++dcache->max_age;
  479. GSL_SPIN_UNLOCK (&dcache->mutex);
  480. if (check_cache)
  481. {
  482. guint node_size = CONFIG_NODE_SIZE ();
  483. guint cache_mem = gsl_get_config ()->dcache_cache_memory;
  484. guint current_mem;
  485. /* FIXME: cache sweeping should not be done from _unref_node for high persistency caches */
  486. GSL_SPIN_LOCK (&global_dcache_mutex);
  487. global_dcache_n_aged_nodes++;
  488. current_mem = node_size * global_dcache_n_aged_nodes;
  489. if (current_mem > cache_mem)
  490. {
  491. guint dcache_count, needs_unlock;
  492. dcache = gsl_ring_pop_head (&global_dcache_list);
  493. GSL_SPIN_LOCK (&dcache->mutex);
  494. dcache->ref_count++;
  495. global_dcache_list = gsl_ring_append (global_dcache_list, dcache);
  496. dcache_count = global_dcache_count;
  497. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  498. if (dcache->low_persistency)
  499. needs_unlock = data_cache_free_olders_Lunlock (dcache, LOW_PERSISTENCY_SWEEP);
  500. else
  501. {
  502. guint max_lru;
  503. /* try to free the actual cache overflow from the
  504. * dcache we just picked, but don't free more than
  505. * 25% of its nodes yet.
  506. * overflow is actual overhang + ~6% of cache size,
  507. * so cache sweeps are triggered less frequently.
  508. */
  509. current_mem -= cache_mem; /* overhang */
  510. current_mem += cache_mem >> 4; /* overflow = overhang + 6% */
  511. current_mem /= node_size; /* n_nodes to free */
  512. current_mem = MIN (current_mem, dcache->n_nodes);
  513. max_lru = dcache->n_nodes >> 1;
  514. max_lru += max_lru >> 1; /* 75% of n_nodes */
  515. max_lru = MAX (max_lru, dcache->n_nodes - current_mem);
  516. needs_unlock = data_cache_free_olders_Lunlock (dcache, MAX (max_lru, LOW_PERSISTENCY_SWEEP));
  517. }
  518. if (needs_unlock)
  519. GSL_SPIN_UNLOCK (&dcache->mutex);
  520. }
  521. else
  522. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  523. }
  524. }
  525. void
  526. gsl_data_cache_free_olders (GslDataCache *dcache,
  527. guint max_age)
  528. {
  529. gboolean needs_unlock;
  530. g_return_if_fail (dcache != NULL);
  531. GSL_SPIN_LOCK (&dcache->mutex);
  532. needs_unlock = data_cache_free_olders_Lunlock (dcache, max_age);
  533. if (needs_unlock)
  534. GSL_SPIN_UNLOCK (&dcache->mutex);
  535. }
  536. GslDataCache*
  537. gsl_data_cache_from_dhandle (GslDataHandle *dhandle,
  538. guint min_padding)
  539. {
  540. GslRing *ring;
  541. g_return_val_if_fail (dhandle != NULL, NULL);
  542. GSL_SPIN_LOCK (&global_dcache_mutex);
  543. for (ring = global_dcache_list; ring; ring = gsl_ring_walk (global_dcache_list, ring))
  544. {
  545. GslDataCache *dcache = ring->data;
  546. if (dcache->dhandle == dhandle && dcache->padding >= min_padding)
  547. {
  548. gsl_data_cache_ref (dcache);
  549. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  550. return dcache;
  551. }
  552. }
  553. GSL_SPIN_UNLOCK (&global_dcache_mutex);
  554. return gsl_data_cache_new (dhandle, min_padding);
  555. }