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

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