[m-dev.] for review: changes to runtime required for webserver
Peter Ross
peter.ross at miscrit.be
Tue Aug 22 00:20:47 AEST 2000
Hi,
For anyone to review.
===================================================================
Estimated hours taken: 2
Changes required to the Mercury runtime so as to build the webserver
written by Peter Ross.
runtime/Mmakefile:
Add the new semaphore files.
runtime/mercury_semaphore.h:
runtime/mercury_semaphore.c:
Add an implementation of semaphores written by Tom Wagner for
pthreads. The only changes made to the code was so that it obeys
the Mercury C coding convention.
runtime/mercury_wrapper.c:
When compiling in the hlc.par.gc grade the runqueue doesn't exist
so don't do any processing on it.
Index: runtime/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/Mmakefile,v
retrieving revision 1.62
diff -u -r1.62 Mmakefile
--- runtime/Mmakefile 2000/08/17 05:31:09 1.62
+++ runtime/Mmakefile 2000/08/21 14:11:16
@@ -69,6 +69,7 @@
mercury_reg_workarounds.h \
mercury_regorder.h \
mercury_regs.h \
+ mercury_semaphore.h \
mercury_signal.h \
mercury_std.h \
mercury_stack_layout.h \
@@ -139,6 +140,7 @@
mercury_prof_mem.c \
mercury_reg_workarounds.c \
mercury_regs.c \
+ mercury_semaphore.c \
mercury_signal.c \
mercury_stack_trace.c \
mercury_stacks.c \
Index: runtime/mercury_semaphore.c
===================================================================
RCS file: mercury_semaphore.c
diff -N mercury_semaphore.c
--- /dev/null Sat Aug 7 21:45:41 1999
+++ mercury_semaphore.c Tue Aug 22 00:11:18 2000
@@ -0,0 +1,209 @@
+/****************************************************************************\
+*
+* Written by
+* Tom Wagner (wagner at cs.umass.edu)
+* at the Distributed Problem Solving Lab
+* Department of Computer Science, University of Massachusetts,
+* Amherst, MA 01003
+*
+* Copyright (c) 1995 UMASS CS Dept. All rights are reserved.
+*
+* Development of this code was partially supported by:
+* ONR grant N00014-92-J-1450
+* NSF contract CDA-8922572
+*
+* ---------------------------------------------------------------------------
+*
+* This code is free software; you can redistribute it and/or modify it.
+* However, this header must remain intact and unchanged. Additional
+* information may be appended after this header. Publications based on
+* this code must also include an appropriate reference.
+*
+* This code is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE.
+*
+\****************************************************************************/
+
+#include "mercury_semaphore.h"
+
+#include "mercury_misc.h"
+
+int checked_pthread_cond_signal(pthread_cond_t *c);
+int checked_pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m);
+int checked_pthread_mutex_unlock(pthread_mutex_t *m);
+int checked_pthread_mutex_lock(pthread_mutex_t *m);
+
+/*
+** This function must be called prior to semaphore use.
+** The semaphore is initialised to one.
+*/
+void
+MR_semaphore_init(MR_Semaphore *s)
+{
+ s->v = 1;
+ if (pthread_mutex_init(&(s->mutex), NULL) == -1) {
+ MR_fatal_error("Error setting up semaphore mutex");
+ }
+
+ if (pthread_cond_init(&(s->cond), NULL) == -1) {
+ MR_fatal_error("Error setting up semaphore condition signal");
+ }
+}
+
+/*
+** This function should be called when there is no longer a need for
+** the semaphore.
+*/
+void
+MR_semaphore_destroy(MR_Semaphore *s)
+{
+ if (pthread_mutex_destroy(&(s->mutex)) == -1) {
+ MR_fatal_error("Error destroying semaphore mutex");
+ }
+
+ if (pthread_cond_destroy(&(s->cond)) == -1) {
+ MR_fatal_error("Error destroying semaphore condition signal");
+ }
+}
+
+/*
+** This function increments the semaphore and signals any threads that
+** are blocked waiting a change in the semaphore.
+*/
+int
+MR_semaphore_up(MR_Semaphore *s)
+{
+ int value_after_op;
+
+ checked_pthread_mutex_lock(&(s->mutex));
+
+ (s->v)++;
+ value_after_op = s->v;
+
+ checked_pthread_mutex_unlock(&(s->mutex));
+ checked_pthread_cond_signal(&(s->cond));
+
+ return (value_after_op);
+}
+
+/*
+** This function decrements the semaphore and blocks if the semaphore is
+** <= 0 until another thread signals a change.
+*/
+int
+MR_semaphore_down(MR_Semaphore *s)
+{
+ int value_after_op;
+
+ checked_pthread_mutex_lock(&(s->mutex));
+ while (s->v <= 0) {
+ checked_pthread_cond_wait(&(s->cond), &(s->mutex));
+ }
+
+ (s->v)--;
+ value_after_op = s->v;
+
+ checked_pthread_mutex_unlock(&(s->mutex));
+
+ return (value_after_op);
+}
+
+/*
+** This function does NOT block but simply decrements the semaphore.
+** should not be used instead of down -- only for programs where
+** multiple threads must up on a semaphore before another thread
+** can go down, i.e., allows programmer to set the semaphore to
+** a negative value prior to using it for synchronization.
+*/
+int
+MR_semaphore_decrement(MR_Semaphore *s)
+{
+ int value_after_op;
+
+ checked_pthread_mutex_lock(&(s->mutex));
+ s->v--;
+ value_after_op = s->v;
+ checked_pthread_mutex_unlock(&(s->mutex));
+
+ return (value_after_op);
+}
+
+/*
+** This function returns the value of the semaphore at the time the
+** critical section is accessed. obviously the value is not guarenteed
+** after the function unlocks the critical section. provided only
+** for casual debugging, a better approach is for the programmar to
+** protect one semaphore with another and then check its value.
+** an alternative is to simply record the value returned by semaphore_up
+** or semaphore_down.
+*/
+int
+MR_semaphore_value(MR_Semaphore *s)
+{
+ /* not for sync */
+ int value_after_op;
+
+ checked_pthread_mutex_lock(&(s->mutex));
+ value_after_op = s->v;
+ checked_pthread_mutex_unlock(&(s->mutex));
+
+ return (value_after_op);
+}
+
+
+
+/* -------------------------------------------------------------------- */
+/* The following functions replace standard library functions in that */
+/* they exit on any error returned from the system calls. Saves us */
+/* from having to check each and every call above. */
+/* -------------------------------------------------------------------- */
+
+
+int
+checked_pthread_mutex_unlock(pthread_mutex_t *m)
+{
+ int return_value;
+
+ if ((return_value = pthread_mutex_unlock(m)) == -1) {
+ MR_fatal_error("pthread_mutex_unlock");
+ }
+
+ return (return_value);
+}
+
+int
+checked_pthread_mutex_lock(pthread_mutex_t *m)
+{
+ int return_value;
+
+ if ((return_value = pthread_mutex_lock(m)) == -1) {
+ MR_fatal_error("pthread_mutex_lock");
+ }
+
+ return (return_value);
+}
+
+int
+checked_pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m)
+{
+ int return_value;
+
+ if ((return_value = pthread_cond_wait(c, m)) == -1) {
+ MR_fatal_error("pthread_cond_wait");
+ }
+
+ return (return_value);
+}
+
+int
+checked_pthread_cond_signal(pthread_cond_t *c)
+{
+ int return_value;
+
+ if ((return_value = pthread_cond_signal(c)) == -1) {
+ MR_fatal_error("pthread_cond_signal");
+ }
+
+ return (return_value);
+}
Index: runtime/mercury_semaphore.h
===================================================================
RCS file: mercury_semaphore.h
diff -N mercury_semaphore.h
--- /dev/null Sat Aug 7 21:45:41 1999
+++ mercury_semaphore.h Tue Aug 22 00:11:18 2000
@@ -0,0 +1,49 @@
+/****************************************************************************\
+*
+* Written by
+* Tom Wagner (wagner at cs.umass.edu)
+* at the Distributed Problem Solving Lab
+* Department of Computer Science, University of Massachusetts,
+* Amherst, MA 01003
+*
+* Copyright (c) 1995 UMASS CS Dept. All rights are reserved.
+*
+* Development of this code was partially supported by:
+* ONR grant N00014-92-J-1450
+* NSF contract CDA-8922572
+*
+* ---------------------------------------------------------------------------
+*
+* This code is free software; you can redistribute it and/or modify it.
+* However, this header must remain intact and unchanged. Additional
+* information may be appended after this header. Publications based on
+* this code must also include an appropriate reference.
+*
+* This code is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+* or FITNESS FOR A PARTICULAR PURPOSE.
+*
+\****************************************************************************/
+
+#ifndef MERCURY_SEMAPHORE_H
+#define MERCURY_SEMAPHORE_H
+
+#include <stdio.h>
+#include <pthread.h>
+
+typedef struct {
+ int v;
+ pthread_mutex_t mutex;
+ pthread_cond_t cond;
+} MR_Semaphore;
+
+void MR_semaphore_init(MR_Semaphore *s);
+void MR_semaphore_destroy(MR_Semaphore *s);
+
+int MR_semaphore_up(MR_Semaphore *s);
+int MR_semaphore_down(MR_Semaphore *s);
+int MR_semaphore_decrement(MR_Semaphore *s);
+
+int MR_semaphore_value(MR_Semaphore *s);
+
+#endif
Index: runtime/mercury_wrapper.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.c,v
retrieving revision 1.68
diff -u -r1.68 mercury_wrapper.c
--- runtime/mercury_wrapper.c 2000/08/15 08:07:32 1.68
+++ runtime/mercury_wrapper.c 2000/08/21 14:11:34
@@ -1329,9 +1329,11 @@
if (MR_profiling) MR_prof_finish();
-#ifdef MR_THREAD_SAFE
+#ifndef MR_HIGHLEVEL_CODE
+ #ifdef MR_THREAD_SAFE
MR_exit_now = TRUE;
pthread_cond_broadcast(MR_runqueue_cond);
+ #endif
#endif
terminate_engine();
--------------------------------------------------------------------------
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