| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-06-29 15:20:24 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-06-29 15:20:24 +0200 | 
| commit | 8d5c996aef28fae9f848e1fab419b4d2821e8862 (patch) | |
| tree | a2b5fac4ee5b3e21711fe80c273f300541d16844 /src/knowledge/knowledge_locks.c | |
| parent | f7c5db9a32bb0082b59e73fa01656cd083756c3d (diff) | |
First shot at finer mutexes.
Diffstat (limited to 'src/knowledge/knowledge_locks.c')
| -rw-r--r-- | src/knowledge/knowledge_locks.c | 342 | 
1 files changed, 342 insertions, 0 deletions
| diff --git a/src/knowledge/knowledge_locks.c b/src/knowledge/knowledge_locks.c new file mode 100644 index 0000000..8d83664 --- /dev/null +++ b/src/knowledge/knowledge_locks.c @@ -0,0 +1,342 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <stdint.h> /* defines SIZE_MAX */ + +#include "../error/error.h" + +#include "knowledge.h" + +/** WORDS *********************************************************************/ +/**** LOCK ********************************************************************/ +int JH_knowledge_readlock_words +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_rdlock(&(k->words_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain read access to knowledge's words: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writelock_words +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_wrlock(&(k->words_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain write access to knowledge's words: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +/**** UNLOCK ******************************************************************/ +int JH_knowledge_readunlock_words +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_unlock(&(k->words_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release read access to knowledge's words: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writeunlock_words +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_unlock(&(k->words_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release write access to knowledge's words: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +/** SEQUENCES *****************************************************************/ +/**** LOCK ********************************************************************/ +int JH_knowledge_readlock_sequences +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_rdlock(&(k->sequences_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain read access to knowledge's sequences: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writelock_sequences +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_wrlock(&(k->sequences_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain write access to knowledge's sequences: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +/**** UNLOCK ******************************************************************/ +int JH_knowledge_readunlock_sequences +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_unlock(&(k->sequences_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release read access to knowledge's sequences: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writeunlock_sequences +( +   struct JH_knowledge k [const restrict static 1], +   FILE io [const restrict static 1] +) +{ +   int err; + +   err = pthread_rwlock_unlock(&(k->sequences_lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release write access to knowledge's sequences: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +/** WORD **********************************************************************/ +/**** LOCK ********************************************************************/ +int JH_knowledge_readlock_word +( +   struct JH_knowledge k [const restrict static 1], +   const JH_index i, +   FILE io [const restrict static 1] +) +{ +   int err; + +   if (JH_knowledge_readlock_words(k, io) < 0) +   { +      return -1; +   } + +   err = pthread_rwlock_rdlock(&(k->words[i].lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain read access to a knowledge's word: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writelock_word +( +   struct JH_knowledge k [const restrict static 1], +   const JH_index i, +   FILE io [const restrict static 1] +) +{ +   int err; + +   if (JH_knowledge_readlock_words(k, io) < 0) +   { +      return -1; +   } + + +   err = pthread_rwlock_wrlock(&(k->words[i].lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to gain write access to a knowledge's word: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +/**** UNLOCK ******************************************************************/ +int JH_knowledge_readunlock_word +( +   struct JH_knowledge k [const restrict static 1], +   const JH_index i, +   FILE io [const restrict static 1] +) +{ +   int err; + +   if (JH_knowledge_readunlock_words(k, io) < 0) +   { +      return -1; +   } + +   err = pthread_rwlock_unlock(&(k->words[i].lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release read access to a knowledge's word: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} + +int JH_knowledge_writeunlock_word +( +   struct JH_knowledge k [const restrict static 1], +   const JH_index i, +   FILE io [const restrict static 1] +) +{ +   int err; + +   if (JH_knowledge_readunlock_words(k, io) < 0) +   { +      return -1; +   } + +   err = pthread_rwlock_unlock(&(k->words[i].lock)); + +   if (err != 0) +   { +      JH_ERROR +      ( +         io, +         "Unable to release write access to a knowledge's word: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} | 


