@@ -60,7 +60,7 @@ static size_t tsrm_reserved_pos = 0;
60
60
static size_t tsrm_reserved_size = 0 ;
61
61
62
62
static MUTEX_T tsmm_mutex ; /* thread-safe memory manager mutex */
63
- static MUTEX_T tsrm_env_mutex ; /* tsrm environ mutex */
63
+ static RWLOCK_T tsrm_env_mutex ; /* tsrm environ mutex */
64
64
65
65
/* New thread handlers */
66
66
static tsrm_thread_begin_func_t tsrm_new_thread_begin_handler = NULL ;
@@ -156,7 +156,7 @@ TSRM_API bool tsrm_startup(int expected_threads, int expected_resources, int deb
156
156
tsrm_reserved_pos = 0 ;
157
157
tsrm_reserved_size = 0 ;
158
158
159
- tsrm_env_mutex = tsrm_mutex_alloc ();
159
+ tsrm_env_mutex = tsrm_rwlock_alloc ();
160
160
161
161
return 1 ;
162
162
}/*}}}*/
@@ -212,7 +212,7 @@ TSRM_API void tsrm_shutdown(void)
212
212
free (tsrm_tls_table );
213
213
free (resource_types_table );
214
214
tsrm_mutex_free (tsmm_mutex );
215
- tsrm_mutex_free (tsrm_env_mutex );
215
+ tsrm_rwlock_free (tsrm_env_mutex );
216
216
TSRM_ERROR ((TSRM_ERROR_LEVEL_CORE , "Shutdown TSRM" ));
217
217
if (tsrm_error_file != stderr ) {
218
218
fclose (tsrm_error_file );
@@ -236,12 +236,20 @@ TSRM_API void tsrm_shutdown(void)
236
236
237
237
/* {{{ */
238
238
/* environ lock api */
239
- TSRM_API void tsrm_env_lock (void ) {
240
- tsrm_mutex_lock (tsrm_env_mutex );
239
+ TSRM_API void tsrm_env_lock (bool write ) {
240
+ if (write ) {
241
+ tsrm_rwlock_wrlock (tsrm_env_mutex );
242
+ } else {
243
+ tsrm_rwlock_rlock (tsrm_env_mutex );
244
+ }
241
245
}
242
246
243
- TSRM_API void tsrm_env_unlock (void ) {
244
- tsrm_mutex_unlock (tsrm_env_mutex );
247
+ TSRM_API void tsrm_env_unlock (bool write ) {
248
+ if (write ) {
249
+ tsrm_rwlock_wrunlock (tsrm_env_mutex );
250
+ } else {
251
+ tsrm_rwlock_runlock (tsrm_env_mutex );
252
+ }
245
253
} /* }}} */
246
254
247
255
/* enlarge the arrays for the already active threads */
@@ -648,6 +656,47 @@ TSRM_API void tsrm_mutex_free(MUTEX_T mutexp)
648
656
#endif
649
657
}/*}}}*/
650
658
659
+ /* Allocate a read-write lock */
660
+ TSRM_API RWLOCK_T tsrm_rwlock_alloc (void )
661
+ {/*{{{*/
662
+ RWLOCK_T rwlock ;
663
+ #ifdef TSRM_WIN32
664
+ rwlock = malloc (sizeof (SRWLOCK ));
665
+ if (!rwlock ) {
666
+ fprintf (stderr , "Failed to allocate SRWLOCK\n" );
667
+ return NULL ;
668
+ }
669
+ InitializeSRWLock (rwlock );
670
+ #else
671
+ rwlock = (pthread_rwlock_t * )malloc (sizeof (pthread_rwlock_t ));
672
+ if (!rwlock ) {
673
+ fprintf (stderr , "Failed to allocate pthread_rwlock_t\n" );
674
+ return NULL ;
675
+ }
676
+ pthread_rwlock_init (rwlock , NULL );
677
+ #endif
678
+ #ifdef THR_DEBUG
679
+ printf ("Read-Write Lock created thread: %d\n" , mythreadid ());
680
+ #endif
681
+ return rwlock ;
682
+ }/*}}}*/
683
+
684
+ /* Free a read-write lock */
685
+ TSRM_API void tsrm_rwlock_free (RWLOCK_T rwlock )
686
+ {/*{{{*/
687
+ if (rwlock ) {
688
+ #ifdef TSRM_WIN32
689
+ // No explicit free function for SRWLOCK, but we still free the memory.
690
+ free (rwlock );
691
+ #else
692
+ pthread_rwlock_destroy (rwlock );
693
+ free (rwlock );
694
+ #endif
695
+ }
696
+ #ifdef THR_DEBUG
697
+ printf ("Read-Write Lock freed thread: %d\n" , mythreadid ());
698
+ #endif
699
+ }/*}}}*/
651
700
652
701
/*
653
702
Lock a mutex.
@@ -664,6 +713,65 @@ TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp)
664
713
#endif
665
714
}/*}}}*/
666
715
716
+ /*
717
+ Lock a mutex for writing.
718
+ A return value of 0 indicates success
719
+ */
720
+ TSRM_API int tsrm_rwlock_wrlock (RWLOCK_T rwlock )
721
+ {/*{{{*/
722
+ TSRM_ERROR ((TSRM_ERROR_LEVEL_INFO , "Write locked thread: %ld" , tsrm_thread_id ()));
723
+ #ifdef TSRM_WIN32
724
+ AcquireSRWLockExclusive (rwlock );
725
+ return 0 ;
726
+ #else
727
+ return pthread_rwlock_wrlock (rwlock );
728
+ #endif
729
+ }/*}}}*/
730
+
731
+ /*
732
+ Lock a mutex for reading.
733
+ A return value of 0 indicates success
734
+ */
735
+ TSRM_API int tsrm_rwlock_rlock (RWLOCK_T rwlock )
736
+ {/*{{{*/
737
+ TSRM_ERROR ((TSRM_ERROR_LEVEL_INFO , "Read locked thread: %ld" , tsrm_thread_id ()));
738
+ #ifdef TSRM_WIN32
739
+ AcquireSRWLockShared (rwlock );
740
+ return 0 ;
741
+ #else
742
+ return pthread_rwlock_rdlock (rwlock );
743
+ #endif
744
+ }/*}}}*/
745
+
746
+ /*
747
+ Unlock a mutex for reading.
748
+ A return value of 0 indicates success
749
+ */
750
+ TSRM_API int tsrm_rwlock_runlock (RWLOCK_T rwlock )
751
+ {/*{{{*/
752
+ TSRM_ERROR ((TSRM_ERROR_LEVEL_INFO , "Read unlocked thread: %ld" , tsrm_thread_id ()));
753
+ #ifdef TSRM_WIN32
754
+ ReleaseSRWLockShared (rwlock );
755
+ return 0 ;
756
+ #else
757
+ return pthread_rwlock_unlock (rwlock );
758
+ #endif
759
+ }/*}}}*/
760
+
761
+ /*
762
+ Unlock a mutex for writing.
763
+ A return value of 0 indicates success
764
+ */
765
+ TSRM_API int tsrm_rwlock_wrunlock (RWLOCK_T rwlock )
766
+ {/*{{{*/
767
+ TSRM_ERROR ((TSRM_ERROR_LEVEL_INFO , "Write unlocked thread: %ld" , tsrm_thread_id ()));
768
+ #ifdef TSRM_WIN32
769
+ ReleaseSRWLockExclusive (rwlock );
770
+ return 0 ;
771
+ #else
772
+ return pthread_rwlock_unlock (rwlock );
773
+ #endif
774
+ }/*}}}*/
667
775
668
776
/*
669
777
Unlock a mutex.
0 commit comments