Blame SOURCES/0002-Encapsulate-all-jthread_map-calls-inside-critical-se.patch

f9a98e
From df4bc975a7cd4600e50aff5a4fdc9a777d414605 Mon Sep 17 00:00:00 2001
f9a98e
From: Jakub Filak <jfilak@redhat.com>
f9a98e
Date: Tue, 5 Nov 2013 13:45:42 +0100
f9a98e
Subject: [PATCH 02/39] Encapsulate all jthread_map calls inside critical
f9a98e
 section
f9a98e
f9a98e
Every single thread has to remember that an exception was already
f9a98e
reported in order to prevent multiple reports for a single exception.
f9a98e
The reported exceptions are stored in a list and each thread has own
f9a98e
list of reported exceptions. These lists are stored in a global map.
f9a98e
f9a98e
Refrences: commit 4144d9dade18645642f4360a9a4cd2fd318b4de4, issue #11
f9a98e
f9a98e
ThreadStart, ThreadEnd and Exception callbacks are called from different
f9a98e
threads but all of the access the thread to reported exception map.
f9a98e
Therefore access to internal data must be serialized through
f9a98e
lock-protected critical section.
f9a98e
f9a98e
Related to rhbz#1026208
f9a98e
Related to rhbz#1051483
f9a98e
---
f9a98e
 src/jthread_map.c | 23 +++++++++++++++++++++--
f9a98e
 1 file changed, 21 insertions(+), 2 deletions(-)
f9a98e
f9a98e
diff --git a/src/jthread_map.c b/src/jthread_map.c
f9a98e
index 7b70953..29c5c29 100644
f9a98e
--- a/src/jthread_map.c
f9a98e
+++ b/src/jthread_map.c
f9a98e
@@ -20,6 +20,7 @@
f9a98e
 #include "jthrowable_circular_buf.h"
f9a98e
 
f9a98e
 #include <stdlib.h>
f9a98e
+#include <pthread.h>
f9a98e
 #include <assert.h>
f9a98e
 
f9a98e
 
f9a98e
@@ -42,6 +43,7 @@ typedef struct jthread_map_item {
f9a98e
 
f9a98e
 struct jthread_map {
f9a98e
     T_jthreadMapItem *items[MAP_SIZE]; ///< map elements
f9a98e
+    pthread_mutex_t mutex;
f9a98e
 };
f9a98e
 
f9a98e
 
f9a98e
@@ -54,6 +56,8 @@ T_jthreadMap *jthread_map_new()
f9a98e
         fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": calloc() error\n");
f9a98e
     }
f9a98e
 
f9a98e
+    pthread_mutex_init(&map->mutex, /*use default attributes*/NULL);
f9a98e
+
f9a98e
     return map;
f9a98e
 }
f9a98e
 
f9a98e
@@ -66,6 +70,7 @@ void jthread_map_free(T_jthreadMap *map)
f9a98e
         return;
f9a98e
     }
f9a98e
 
f9a98e
+    pthread_mutex_destroy(&map->mutex);
f9a98e
     free(map);
f9a98e
 }
f9a98e
 
f9a98e
@@ -104,6 +109,8 @@ void jthread_map_push(T_jthreadMap *map, jlong tid, T_jthrowableCircularBuf *buf
f9a98e
 {
f9a98e
     assert(NULL != map);
f9a98e
 
f9a98e
+    pthread_mutex_lock(&map->mutex);
f9a98e
+
f9a98e
     const long index = tid % MAP_SIZE;
f9a98e
     T_jthreadMapItem *last = NULL;
f9a98e
     T_jthreadMapItem *itm = map->items[index];
f9a98e
@@ -125,6 +132,8 @@ void jthread_map_push(T_jthreadMap *map, jlong tid, T_jthrowableCircularBuf *buf
f9a98e
             last->next = new;
f9a98e
         }
f9a98e
     }
f9a98e
+
f9a98e
+    pthread_mutex_unlock(&map->mutex);
f9a98e
 }
f9a98e
 
f9a98e
 
f9a98e
@@ -133,17 +142,23 @@ T_jthrowableCircularBuf *jthread_map_get(T_jthreadMap *map, jlong tid)
f9a98e
 {
f9a98e
     assert(NULL != map);
f9a98e
 
f9a98e
+    pthread_mutex_lock(&map->mutex);
f9a98e
+
f9a98e
     const size_t index = tid % MAP_SIZE;
f9a98e
+    T_jthrowableCircularBuf *buffer = NULL;
f9a98e
 
f9a98e
     for (T_jthreadMapItem *itm = map->items[index]; NULL != itm; itm = itm->next)
f9a98e
     {
f9a98e
         if (itm->tid == tid)
f9a98e
         {
f9a98e
-            return itm->buffer;
f9a98e
+            buffer = itm->buffer;
f9a98e
+            break;
f9a98e
         }
f9a98e
     }
f9a98e
 
f9a98e
-    return NULL;
f9a98e
+    pthread_mutex_unlock(&map->mutex);
f9a98e
+
f9a98e
+    return buffer;
f9a98e
 }
f9a98e
 
f9a98e
 
f9a98e
@@ -152,6 +167,8 @@ T_jthrowableCircularBuf *jthread_map_pop(T_jthreadMap *map, jlong tid)
f9a98e
 {
f9a98e
     assert(NULL != map);
f9a98e
 
f9a98e
+    pthread_mutex_lock(&map->mutex);
f9a98e
+
f9a98e
     const size_t index = tid % MAP_SIZE;
f9a98e
     T_jthrowableCircularBuf *buffer = NULL;
f9a98e
     if (NULL != map->items[index])
f9a98e
@@ -181,6 +198,8 @@ T_jthrowableCircularBuf *jthread_map_pop(T_jthreadMap *map, jlong tid)
f9a98e
         }
f9a98e
     }
f9a98e
 
f9a98e
+    pthread_mutex_unlock(&map->mutex);
f9a98e
+
f9a98e
     return buffer;
f9a98e
 }
f9a98e
 
f9a98e
-- 
f9a98e
1.8.3.1
f9a98e