[m-dev.] diff: fix memory leak in semaphore.m
Peter Ross
peter.ross at miscrit.be
Thu Sep 14 00:59:01 AEDT 2000
Hi,
Found the memory leak!
The reason why it only showed up under Windows was because linux
pthreads don't do any allocate any resources for a condvars and mutexs
so they don't need to be explicitly destroyed.
===================================================================
Estimated hours taken: 32
Fix a memory leak in semaphore.m
extras/concurrency/semaphore.m:
Semaphores use pthread condvars and mutexs for their underlying
implementation. These need to be explicitly destroyed when the
semaphore is garbage collected. This is done by registering a
finalization routine for the semaphore.
Index: semaphore.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/concurrency/semaphore.m,v
retrieving revision 1.3
diff -u -r1.3 semaphore.m
--- semaphore.m 2000/09/08 09:14:54 1.3
+++ semaphore.m 2000/09/13 13:50:23
@@ -89,9 +89,38 @@
#ifdef MR_THREAD_SAFE
pthread_mutex_init(&(sem->lock), MR_MUTEX_ATTR);
#endif
+
+ /*
+ ** The condvar and the mutex will need to be destroyed
+ ** when the semaphore is garbage collected.
+ */
+#ifdef CONSERVATIVE_GC
+ GC_REGISTER_FINALIZER(sem, finalize_semaphore, NULL, NULL, NULL);
+#endif
+
Semaphore = (MR_Word) sem;
IO = IO0;
}").
+
+:- pragma c_code("
+#ifdef CONSERVATIVE_GC
+ void finalize_semaphore(GC_PTR obj, GC_PTR cd)
+ {
+ ME_Semaphore *sem;
+
+ sem = (ME_Semaphore *) obj;
+
+ #ifdef MR_HIGHLEVEL_CODE
+ pthread_cond_destroy(&(sem->cond));
+ #endif
+ #ifdef MR_THREAD_SAFE
+ pthread_mutex_destroy(&(sem->lock));
+ #endif
+
+ return;
+ }
+#endif
+").
% because semaphore__signal has a local label, we may get
% C compilation errors if inlining leads to multiple copies
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list