| summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/knowledge/knowledge.c')
| -rw-r--r-- | src/knowledge/knowledge.c | 108 |
1 files changed, 43 insertions, 65 deletions
diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c index 93351da..9ef9c6e 100644 --- a/src/knowledge/knowledge.c +++ b/src/knowledge/knowledge.c @@ -24,9 +24,36 @@ int JH_knowledge_initialize (struct JH_knowledge k [const restrict static 1]) k->sequences_sorted = (JH_index *) NULL; #ifndef JH_RUNNING_FRAMA_C - error = pthread_mutex_init(&(k->mutex), (const pthread_mutexattr_t *) NULL); + error = + pthread_rwlock_init + ( + &(k->words_lock), + (const pthread_rwlockattr_t *) NULL + ); +#else + error = 0; +#endif + + if (error != 0) + { + JH_FATAL + ( + stderr, + "Unable to initialize knowledge's words lock: %s.", + strerror(error) + ); + + return -1; + } + +#ifndef JH_RUNNING_FRAMA_C + error = + pthread_rwlock_init + ( + &(k->sequences_lock), + (const pthread_rwlockattr_t *) NULL + ); #else - k->mutex = 1; error = 0; #endif @@ -35,7 +62,7 @@ int JH_knowledge_initialize (struct JH_knowledge k [const restrict static 1]) JH_FATAL ( stderr, - "Unable to initialize knowledge mutex: %s.", + "Unable to initialize knowledge's sequences lock: %s.", strerror(error) ); @@ -75,82 +102,30 @@ int JH_knowledge_initialize (struct JH_knowledge k [const restrict static 1]) return 0; } -int JH_knowledge_lock_access -( - struct JH_knowledge k [const restrict static 1], - FILE io [const restrict static 1] -) -{ - int err; - -#ifndef JH_RUNNING_FRAMA_C - err = pthread_mutex_lock(&(k->mutex)); -#else - /*@ assert (k->mutex == 1); @*/ - k->mutex = 0; - err = 0; -#endif - - if (err != 0) - { - JH_ERROR - ( - io, - "Unable to get exclusive access to knowledge: %s", - strerror(err) - ); - - return -1; - } - - return 0; -} - -void JH_knowledge_unlock_access -( - struct JH_knowledge k [const restrict static 1], - FILE io [const restrict static 1] -) -{ - int err; - -#ifndef JH_RUNNING_FRAMA_C - err = pthread_mutex_unlock(&(k->mutex)); -#else - /*@ assert (k->mutex == 0); @*/ - k->mutex = 1; - err = 0; -#endif - - if (err != 0) - { - JH_ERROR - ( - io, - "Unable to release exclusive access to knowledge: %s", - strerror(err) - ); - } -} - void JH_knowledge_get_word ( - const struct JH_knowledge k [const static 1], + struct JH_knowledge k [const static 1], const JH_index word_ref, const JH_char * word [const restrict static 1], - JH_index word_length [const restrict static 1] + JH_index word_length [const restrict static 1], + FILE io [const restrict static 1] ) { + JH_knowledge_readlock_words(k, io); + *word = k->words[word_ref].word; *word_length = k->words[word_ref].word_length; + + JH_knowledge_readunlock_words(k, io); } int JH_knowledge_rarest_word ( - const struct JH_knowledge k [const static 1], + struct JH_knowledge k [const static 1], const JH_index sequence [const restrict static 1], const size_t sequence_length, - JH_index word_id [const restrict static 1] + JH_index word_id [const restrict static 1], + FILE io [const restrict static 1] ) { JH_index current_max_score; @@ -163,6 +138,7 @@ int JH_knowledge_rarest_word for (i = 0; i < sequence_length; ++i) { + JH_knowledge_readlock_word(k, sequence[i], io); if ( (k->words[sequence[i]].occurrences <= current_max_score) @@ -174,6 +150,8 @@ int JH_knowledge_rarest_word *word_id = sequence[i]; success = 0; } + + JH_knowledge_readunlock_word(k, sequence[i], io); } return success; |


