aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorYAMAMOTO Mitsuharu2007-08-07 08:56:08 +0000
committerYAMAMOTO Mitsuharu2007-08-07 08:56:08 +0000
commit3ceeb306d2d5843f7277a51c5425386d64678e31 (patch)
tree53ff56524362a2a40b4f5be2fe098d1abd2cd183 /src/gmalloc.c
parent5467331d9c585bbf2e68dcfef9041d4f4554d39d (diff)
downloademacs-3ceeb306d2d5843f7277a51c5425386d64678e31.tar.gz
emacs-3ceeb306d2d5843f7277a51c5425386d64678e31.zip
(_malloc_thread_enabled_p) [USE_PTHREAD]: New variable.
[USE_PTHREAD] (LOCK, UNLOCK, LOCK_ALIGNED_BLOCKS) (UNLOCK_ALIGNED_BLOCKS): Conditionalize with it. (malloc_atfork_handler_prepare, malloc_atfork_handler_parent) (malloc_atfork_handler_child, malloc_enable_thread) [USE_PTHREAD]: New functions.
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c83
1 files changed, 66 insertions, 17 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index cf79b9159f2..ea6ccc4bf1f 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -136,6 +136,10 @@ extern __ptr_t memalign PP ((__malloc_size_t __alignment,
136extern __ptr_t valloc PP ((__malloc_size_t __size)); 136extern __ptr_t valloc PP ((__malloc_size_t __size));
137#endif 137#endif
138 138
139#ifdef USE_PTHREAD
140/* Set up mutexes and make malloc etc. thread-safe. */
141extern void malloc_enable_thread PP ((void));
142#endif
139 143
140#ifdef _MALLOC_INTERNAL 144#ifdef _MALLOC_INTERNAL
141 145
@@ -242,10 +246,27 @@ extern void _free_internal_nolock PP ((__ptr_t __ptr));
242 246
243#ifdef USE_PTHREAD 247#ifdef USE_PTHREAD
244extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex; 248extern pthread_mutex_t _malloc_mutex, _aligned_blocks_mutex;
245#define LOCK() pthread_mutex_lock (&_malloc_mutex) 249extern int _malloc_thread_enabled_p;
246#define UNLOCK() pthread_mutex_unlock (&_malloc_mutex) 250#define LOCK() \
247#define LOCK_ALIGNED_BLOCKS() pthread_mutex_lock (&_aligned_blocks_mutex) 251 do { \
248#define UNLOCK_ALIGNED_BLOCKS() pthread_mutex_unlock (&_aligned_blocks_mutex) 252 if (_malloc_thread_enabled_p) \
253 pthread_mutex_lock (&_malloc_mutex); \
254 } while (0)
255#define UNLOCK() \
256 do { \
257 if (_malloc_thread_enabled_p) \
258 pthread_mutex_unlock (&_malloc_mutex); \
259 } while (0)
260#define LOCK_ALIGNED_BLOCKS() \
261 do { \
262 if (_malloc_thread_enabled_p) \
263 pthread_mutex_lock (&_aligned_blocks_mutex); \
264 } while (0)
265#define UNLOCK_ALIGNED_BLOCKS() \
266 do { \
267 if (_malloc_thread_enabled_p) \
268 pthread_mutex_unlock (&_aligned_blocks_mutex); \
269 } while (0)
249#else 270#else
250#define LOCK() 271#define LOCK()
251#define UNLOCK() 272#define UNLOCK()
@@ -563,6 +584,47 @@ register_heapinfo ()
563#ifdef USE_PTHREAD 584#ifdef USE_PTHREAD
564pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER; 585pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
565pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER; 586pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
587int _malloc_thread_enabled_p;
588
589static void
590malloc_atfork_handler_prepare ()
591{
592 LOCK ();
593 LOCK_ALIGNED_BLOCKS ();
594}
595
596static void
597malloc_atfork_handler_parent ()
598{
599 UNLOCK_ALIGNED_BLOCKS ();
600 UNLOCK ();
601}
602
603static void
604malloc_atfork_handler_child ()
605{
606 UNLOCK_ALIGNED_BLOCKS ();
607 UNLOCK ();
608}
609
610/* Set up mutexes and make malloc etc. thread-safe. */
611void
612malloc_enable_thread ()
613{
614 if (_malloc_thread_enabled_p)
615 return;
616
617 /* Some pthread implementations call malloc for statically
618 initialized mutexes when they are used first. To avoid such a
619 situation, we initialize mutexes here while their use is
620 disabled in malloc etc. */
621 pthread_mutex_init (&_malloc_mutex, NULL);
622 pthread_mutex_init (&_aligned_blocks_mutex, NULL);
623 pthread_atfork (malloc_atfork_handler_prepare,
624 malloc_atfork_handler_parent,
625 malloc_atfork_handler_child);
626 _malloc_thread_enabled_p = 1;
627}
566#endif 628#endif
567 629
568static void 630static void
@@ -575,19 +637,6 @@ malloc_initialize_1 ()
575 if (__malloc_initialize_hook) 637 if (__malloc_initialize_hook)
576 (*__malloc_initialize_hook) (); 638 (*__malloc_initialize_hook) ();
577 639
578 /* We don't use recursive mutex because pthread_mutexattr_init may
579 call malloc internally. */
580#if 0 /* defined (USE_PTHREAD) */
581 {
582 pthread_mutexattr_t attr;
583
584 pthread_mutexattr_init (&attr);
585 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
586 pthread_mutex_init (&_malloc_mutex, &attr);
587 pthread_mutexattr_destroy (&attr);
588 }
589#endif
590
591 heapsize = HEAP / BLOCKSIZE; 640 heapsize = HEAP / BLOCKSIZE;
592 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info)); 641 _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
593 if (_heapinfo == NULL) 642 if (_heapinfo == NULL)