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

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