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

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