]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
EC-ISA: avoid usage of table cache lock outside the class implementation by introduci... 2409/head
authorAndreas-Joachim Peters <Andreas.Joachim.Peters@cern.ch>
Tue, 9 Sep 2014 07:37:41 +0000 (09:37 +0200)
committerAndreas-Joachim Peters <Andreas.Joachim.Peters@cern.ch>
Wed, 10 Sep 2014 09:25:05 +0000 (11:25 +0200)
src/erasure-code/isa/ErasureCodeIsa.cc
src/erasure-code/isa/ErasureCodeIsaTableCache.cc
src/erasure-code/isa/ErasureCodeIsaTableCache.h

index f3a947e8180f29707da105998e7d33b5dac336ed..962ab81f0437545d3e577e3a6354c61a3810fcc5 100644 (file)
@@ -379,8 +379,6 @@ int ErasureCodeIsaDefault::parse(const map<std::string,
 void
 ErasureCodeIsaDefault::prepare()
 {
-  Mutex::Locker lock(tcache.codec_tables_guard);
-
   // setup shared encoding table and coefficients
   unsigned char** p_enc_table =
     tcache.getEncodingTable(matrixtype, k, m);
@@ -392,13 +390,17 @@ ErasureCodeIsaDefault::prepare()
     dout(10) << "[ cache tables ] creating coeff for k=" <<
       k << " m=" << m << dendl;
     // build encoding coefficients which need to be computed once for each (k,m)
-    *p_enc_coeff = (unsigned char*) malloc(k * (m + k));
-    encode_coeff = *p_enc_coeff;
+    encode_coeff = (unsigned char*) malloc(k * (m + k));
 
     if (matrixtype == kVandermonde)
       gf_gen_rs_matrix(encode_coeff, k + m, k);
     if (matrixtype == kCauchy)
       gf_gen_cauchy1_matrix(encode_coeff, k + m, k);
+
+      // either our new created coefficients are stored or if they have been
+      // created in the meanwhile the locally allocated coefficients will be
+      // freed by setEncodingCoefficient
+    encode_coeff = tcache.setEncodingCoefficient(matrixtype, k, m, encode_coeff);
   } else {
     encode_coeff = *p_enc_coeff;
   }
@@ -407,9 +409,13 @@ ErasureCodeIsaDefault::prepare()
     dout(10) << "[ cache tables ] creating tables for k=" <<
       k << " m=" << m << dendl;
     // build encoding table which needs to be computed once for each (k,m)
-    *p_enc_table = (unsigned char*) malloc(k * (m + k)*32);
-    encode_tbls = *p_enc_table;
+    encode_tbls = (unsigned char*) malloc(k * (m + k)*32);
     ec_init_tables(k, m, &encode_coeff[k * k], encode_tbls);
+
+    // either our new created table is stored or if it has been
+    // created in the meanwhile the locally allocated table will be
+    // freed by setEncodingTable
+    encode_tbls = tcache.setEncodingTable(matrixtype, k, m, encode_tbls);
   } else {
     encode_tbls = *p_enc_table;
   }
index 88436bc290f1cfba301d83ec2d89ef6da9041822..003ff2224a4b9e51a36a233ef7f15705ea02c66e 100644 (file)
@@ -143,9 +143,15 @@ ErasureCodeIsaTableCache::getDecodingTablesLru(int matrix_type)
 unsigned char**
 ErasureCodeIsaTableCache::getEncodingTable(int matrix, int k, int m)
 {
-  // the caller must hold the guard mutex:
-  // => Mutex::Locker lock(codec_tables_guard);
+  Mutex::Locker lock(codec_tables_guard);
+  return getEncodingTableNoLock(matrix,k,m);
+}
 
+// -----------------------------------------------------------------------------
+
+unsigned char**
+ErasureCodeIsaTableCache::getEncodingTableNoLock(int matrix, int k, int m)
+{
   // create a pointer to store an encoding table address
   if (!encoding_table[matrix][k][m]) {
     encoding_table[matrix][k][m] = new (unsigned char*);
@@ -159,9 +165,15 @@ ErasureCodeIsaTableCache::getEncodingTable(int matrix, int k, int m)
 unsigned char**
 ErasureCodeIsaTableCache::getEncodingCoefficient(int matrix, int k, int m)
 {
-  // the caller must hold the guard mutex:
-  // => Mutex::Locker lock(codec_tables_guard);
+  Mutex::Locker lock(codec_tables_guard);
+  return getEncodingCoefficientNoLock(matrix,k,m);
+}
 
+// -----------------------------------------------------------------------------
+
+unsigned char**
+ErasureCodeIsaTableCache::getEncodingCoefficientNoLock(int matrix, int k, int m)
+{
   // create a pointer to store an encoding coefficients adddress
   if (!encoding_coefficient[matrix][k][m]) {
     encoding_coefficient[matrix][k][m] = new (unsigned char*);
@@ -172,6 +184,44 @@ ErasureCodeIsaTableCache::getEncodingCoefficient(int matrix, int k, int m)
 
 // -----------------------------------------------------------------------------
 
+unsigned char*
+ErasureCodeIsaTableCache::setEncodingTable(int matrix, int k, int m, unsigned char* ec_in_table)
+{
+  Mutex::Locker lock(codec_tables_guard);
+  unsigned char** ec_out_table = getEncodingTableNoLock(matrix, k, m);
+  if (*ec_out_table) {
+    // somebody might have deposited this table in the meanwhile, so clean
+    // the input table and return the stored one
+    free (ec_in_table);
+    return *ec_out_table;
+  } else {
+    // we store the provided input table and return this one
+    *encoding_table[matrix][k][m] = ec_in_table;
+    return ec_in_table;
+  }
+}
+
+// -----------------------------------------------------------------------------
+
+unsigned char*
+ErasureCodeIsaTableCache::setEncodingCoefficient(int matrix, int k, int m, unsigned char* ec_in_coeff)
+{
+  Mutex::Locker lock(codec_tables_guard);
+  unsigned char** ec_out_coeff = getEncodingCoefficientNoLock(matrix, k, m);
+  if (*ec_out_coeff) {
+    // somebody might have deposited these coefficients in the meanwhile, so clean
+    // the input coefficients and return the stored ones
+    free (ec_in_coeff);
+    return *ec_out_coeff;
+  } else {
+    // we store the provided input coefficients and return these
+    *encoding_coefficient[matrix][k][m] = ec_in_coeff;
+    return ec_in_coeff;
+  }
+}
+
+// -----------------------------------------------------------------------------
+
 Mutex*
 ErasureCodeIsaTableCache::getLock()
 {
index 27ec603485d8de0e3a738743d0c8943bb0f0f3d4..64aaae752af87ecd784311c08600e3687daf4ea6 100644 (file)
@@ -79,6 +79,12 @@ public:
   unsigned char** getEncodingTable(int matrix, int k, int m);
   unsigned char** getEncodingCoefficient(int matrix, int k, int m);
 
+  unsigned char** getEncodingTableNoLock(int matrix, int k, int m);
+  unsigned char** getEncodingCoefficientNoLock(int matrix, int k, int m);
+
+  unsigned char* setEncodingTable(int matrix, int k, int m, unsigned char*);
+  unsigned char* setEncodingCoefficient(int matrix, int k, int m, unsigned char*);
+
   int getDecodingTableCacheSize(int matrixtype = 0);
 
 private: