]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libuuid: split uuidd cache into dedicated struct
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 7 May 2024 11:33:40 +0000 (13:33 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Mon, 1 Jul 2024 19:54:09 +0000 (21:54 +0200)
To clear the struct we need to be able to refer to it by name.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
(cherry picked from commit 26dbc85838b06bd3e5349a81a4ce869efdd9f883)

libuuid/src/gen_uuid.c

index 0984e24e1e23ef9995c95896b7006662e3635835..c40f9d66f64bfe7e8677b7c8830098e6a8365562 100644 (file)
@@ -579,57 +579,63 @@ int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
  * If neither of these is possible (e.g. because of insufficient permissions), it generates
  * the UUID anyway, but returns -1. Otherwise, returns 0.
  */
+
+/* thread local cache for uuidd based requests */
+THREAD_LOCAL struct {
+       int             num;
+       int             cache_size;
+       int             last_used;
+       struct uuid     uu;
+       time_t          last_time;
+} uuidd_cache = {
+       .cache_size = CS_MIN,
+};
+
 static int uuid_generate_time_generic(uuid_t out) {
-       /* thread local cache for uuidd based requests */
-       THREAD_LOCAL int                num = 0;
-       THREAD_LOCAL int                cache_size = CS_MIN;
-       THREAD_LOCAL int                last_used = 0;
-       THREAD_LOCAL struct uuid        uu;
-       THREAD_LOCAL time_t             last_time = 0;
-       time_t                          now;
-
-       if (num > 0) { /* expire cache */
+       time_t  now;
+
+       if (uuidd_cache.num > 0) { /* expire cache */
                now = time(NULL);
-               if (now > last_time+1) {
-                       last_used = cache_size - num;
-                       num = 0;
+               if (now > uuidd_cache.last_time+1) {
+                       uuidd_cache.last_used = uuidd_cache.cache_size - uuidd_cache.num;
+                       uuidd_cache.num = 0;
                }
        }
-       if (num <= 0) { /* fill cache */
+       if (uuidd_cache.num <= 0) { /* fill cache */
                /*
                 * num + OP_BULK provides a local cache in each application.
                 * Start with a small cache size to cover short running applications
                 * and adjust the cache size over the runntime.
                 */
-               if ((last_used == cache_size) && (cache_size < CS_MAX))
-                       cache_size *= CS_FACTOR;
-               else if ((last_used < (cache_size / CS_FACTOR)) && (cache_size > CS_MIN))
-                       cache_size /= CS_FACTOR;
+               if ((uuidd_cache.last_used == uuidd_cache.cache_size) && (uuidd_cache.cache_size < CS_MAX))
+                       uuidd_cache.cache_size *= CS_FACTOR;
+               else if ((uuidd_cache.last_used < (uuidd_cache.cache_size / CS_FACTOR)) && (uuidd_cache.cache_size > CS_MIN))
+                       uuidd_cache.cache_size /= CS_FACTOR;
 
-               num = cache_size;
+               uuidd_cache.num = uuidd_cache.cache_size;
 
                if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID,
-                                       out, &num) == 0) {
-                       last_time = time(NULL);
-                       uuid_unpack(out, &uu);
-                       num--;
+                                       out, &uuidd_cache.num) == 0) {
+                       uuidd_cache.last_time = time(NULL);
+                       uuid_unpack(out, &uuidd_cache.uu);
+                       uuidd_cache.num--;
                        return 0;
                }
                /* request to daemon failed, reset cache */
-               num = 0;
-               cache_size = CS_MIN;
+               uuidd_cache.num = 0;
+               uuidd_cache.cache_size = CS_MIN;
        }
-       if (num > 0) { /* serve uuid from cache */
-               uu.time_low++;
-               if (uu.time_low == 0) {
-                       uu.time_mid++;
-                       if (uu.time_mid == 0)
-                               uu.time_hi_and_version++;
+       if (uuidd_cache.num > 0) { /* serve uuid from cache */
+               uuidd_cache.uu.time_low++;
+               if (uuidd_cache.uu.time_low == 0) {
+                       uuidd_cache.uu.time_mid++;
+                       if (uuidd_cache.uu.time_mid == 0)
+                               uuidd_cache.uu.time_hi_and_version++;
                }
-               num--;
-               uuid_pack(&uu, out);
-               if (num == 0)
-                       last_used = cache_size;
+               uuidd_cache.num--;
+               uuid_pack(&uuidd_cache.uu, out);
+               if (uuidd_cache.num == 0)
+                       uuidd_cache.last_used = uuidd_cache.cache_size;
                return 0;
        }