Hi

This patch removes the race in the worker init. If one worker incremented the 
static variable wid and went to sleep, another worker could access a wrong 
sched_list structure.

- Lauri
>From b4f247ba226284bc86087531c024504e245b8a71 Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <[email protected]>
Date: Thu, 31 May 2012 19:54:49 +0300
Subject: [PATCH 2/3] sched: Protect the wid init with a mutex


Signed-off-by: Lauri Kasanen <[email protected]>
---
 src/mk_scheduler.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/mk_scheduler.c b/src/mk_scheduler.c
index bd87107..229fcc5 100644
--- a/src/mk_scheduler.c
+++ b/src/mk_scheduler.c
@@ -42,6 +42,8 @@
 #include "mk_utils.h"
 #include "mk_macros.h"
 
+static pthread_mutex_t mutex_sched_init = PTHREAD_MUTEX_INITIALIZER;
+
 /*
  * Returns the worker id which should take a new incomming connection,
  * it returns the worker id with less active connections
@@ -216,6 +218,12 @@ int mk_sched_register_thread(int efd)
     struct sched_list_node *sl;
     static int wid = 0;
 
+    /*
+     * If this thread slept inside this section, some other thread may touch 
wid.
+     * So protect it with a mutex, only one thread may handle wid.
+     */
+    pthread_mutex_lock(&mutex_sched_init);
+
     sl = &sched_list[wid];
     sl->idx = wid++;
     sl->tid = pthread_self();
@@ -233,6 +241,8 @@ int mk_sched_register_thread(int efd)
     sl->pid = syscall(__NR_gettid);
     sl->epoll_fd = efd;
 
+    pthread_mutex_unlock(&mutex_sched_init);
+
     mk_list_init(&sl->busy_queue);
     mk_list_init(&sl->av_queue);
 
-- 
1.7.2.1

_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to