From 8d5c996aef28fae9f848e1fab419b4d2821e8862 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 29 Jun 2017 15:20:24 +0200 Subject: First shot at finer mutexes. --- src/knowledge/knowledge_locks.c | 342 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 src/knowledge/knowledge_locks.c (limited to 'src/knowledge/knowledge_locks.c') 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 +#include +#include +#include /* 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; +} -- cgit v1.2.3-70-g09d2