[Vm-dev] [commit][2886] Use pthread_atfork to reinstall the heartbeat thread post fork.

commits at squeakvm.org commits at squeakvm.org
Fri Mar 28 23:11:51 UTC 2014


Revision: 2886
Author:   eliot
Date:     2014-03-28 16:11:48 -0700 (Fri, 28 Mar 2014)
Log Message:
-----------
Use pthread_atfork to reinstall the heartbeat thread post fork.

Modified Paths:
--------------
    branches/Cog/platforms/unix/vm/sqUnixHeartbeat.c

Property Changed:
----------------
    branches/Cog/platforms/Cross/vm/sqSCCSVersion.h


Property changes on: branches/Cog/platforms/Cross/vm/sqSCCSVersion.h
___________________________________________________________________
Modified: checkindate
   - Fri Mar 28 12:16:44 PDT 2014
   + Fri Mar 28 16:10:05 PDT 2014

Modified: branches/Cog/platforms/unix/vm/sqUnixHeartbeat.c
===================================================================
--- branches/Cog/platforms/unix/vm/sqUnixHeartbeat.c	2014-03-28 19:19:16 UTC (rev 2885)
+++ branches/Cog/platforms/unix/vm/sqUnixHeartbeat.c	2014-03-28 23:11:48 UTC (rev 2886)
@@ -335,7 +335,9 @@
 
 typedef enum { dead, condemned, nascent, quiescent, active } machine_state;
 
-static int					stateMachinePolicy;
+#define UNDEFINED 0xBADF00D
+
+static int					stateMachinePolicy = UNDEFINED;
 static struct sched_param	stateMachinePriority;
 
 static volatile machine_state beatState = nascent;
@@ -386,19 +388,30 @@
 	struct timespec halfAMo;
 	pthread_t careLess;
 
-	if ((er = pthread_getschedparam(pthread_self(),
-									&stateMachinePolicy,
-									&stateMachinePriority))) {
-		errno = er;
-		perror("pthread_getschedparam failed");
-		exit(errno);
+	/* First time through choose a policy and priority for the heartbeat thread,
+	 * and install ioInitHeartbeat via pthread_atfork to be run again in a forked
+	 * child, restarting the heartbeat in a forked child.
+	 */
+	if (stateMachinePolicy == UNDEFINED) {
+		if ((er = pthread_getschedparam(pthread_self(),
+										&stateMachinePolicy,
+										&stateMachinePriority))) {
+			errno = er;
+			perror("pthread_getschedparam failed");
+			exit(errno);
+		}
+		assert(stateMachinePolicy != UNDEFINED);
+		++stateMachinePriority.sched_priority;
+		/* If the priority isn't appropriate for the policy (typically
+		 * SCHED_OTHER) then change policy.
+		 */
+		if (sched_get_priority_max(stateMachinePolicy) < stateMachinePriority.sched_priority)
+			stateMachinePolicy = SCHED_FIFO;
+		pthread_atfork(0, /*prepare*/ 0, /*parent*/ ioInitHeartbeat /*child*/);
 	}
-	++stateMachinePriority.sched_priority;
-	/* If the priority isn't appropriate for the policy (typically SCHED_OTHER)
-	 * then change policy.
-	 */
-	if (sched_get_priority_max(stateMachinePolicy) < stateMachinePriority.sched_priority)
-		stateMachinePolicy = SCHED_FIFO;
+	else /* subsequently (in the child) init beatState before creating thread */
+		beatState = nascent;
+
 	halfAMo.tv_sec  = 0;
 	halfAMo.tv_nsec = 1000 * 100;
 	if ((er= pthread_create(&careLess,



More information about the Vm-dev mailing list