add minimal pthread barrier support
Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
diff --git a/Android.mk b/Android.mk
index 23676b1..bf4366a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -21,6 +21,7 @@
src/os.cpp \
src/os_factory.cpp \
src/pattern.cpp \
+ src/pthread_barrier.cpp \
src/queue.cpp \
src/sat.cpp \
src/sat_factory.cpp \
diff --git a/src/pthread_barrier.cpp b/src/pthread_barrier.cpp
new file mode 100644
index 0000000..8d64f84
--- /dev/null
+++ b/src/pthread_barrier.cpp
@@ -0,0 +1,67 @@
+/*
+ * pthread_barrier.c - Barrier handling for pthreads.
+ *
+ * This file is part of the Portable.NET C library.
+ * Copyright (C) 2004 Southern Storm Software, Pty Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "pthread_barrier.h"
+#include <errno.h>
+
+int pthread_barrier_init (pthread_barrier_t *barrier, unsigned int count)
+{
+ if (count == 0)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+ if (pthread_mutex_init (&(barrier->mutex), 0) < 0)
+ return -1;
+ if (pthread_cond_init (&(barrier->condition), 0) < 0)
+ {
+ pthread_mutex_destroy (&(barrier->mutex));
+ return -1;
+ }
+ barrier->desired_count = count;
+ barrier->current_count = 0;
+ return 0;
+}
+
+int pthread_barrier_destroy (pthread_barrier_t *barrier)
+{
+ pthread_cond_destroy (&(barrier->condition));
+ pthread_mutex_destroy (&(barrier->mutex));
+ return 0;
+}
+
+int pthread_barrier_wait (pthread_barrier_t *barrier)
+{
+ pthread_mutex_lock (&(barrier->mutex));
+ ++(barrier->current_count);
+ if (barrier->current_count >= barrier->desired_count)
+ {
+ barrier->current_count = 0;
+ pthread_cond_broadcast (&(barrier->condition));
+ pthread_mutex_unlock (&(barrier->mutex));
+ return 1;
+ } else {
+ pthread_cond_wait (&(barrier->condition), &(barrier->mutex));
+ pthread_mutex_unlock (&(barrier->mutex));
+ return 0;
+ }
+}
diff --git a/src/pthread_barrier.h b/src/pthread_barrier.h
new file mode 100644
index 0000000..cf38441
--- /dev/null
+++ b/src/pthread_barrier.h
@@ -0,0 +1,41 @@
+/*
+ * pthread_barrier.c - Barrier handling for pthreads.
+ *
+ * This file is part of the Portable.NET C library.
+ * Copyright (C) 2004 Southern Storm Software, Pty Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#ifndef PTHREAD_BARRIER_H_
+#define PTHREAD_BARRIER_H_
+
+#include <pthread.h>
+
+#define PTHREAD_BARRIER_SERIAL_THREAD -1
+
+typedef struct {
+ int desired_count;
+ int current_count;
+ pthread_mutex_t mutex;
+ pthread_cond_t condition;
+} pthread_barrier_t;
+
+int pthread_barrier_init (pthread_barrier_t *barrier, unsigned int count);
+int pthread_barrier_destroy (pthread_barrier_t *barrier);
+int pthread_barrier_wait (pthread_barrier_t *barrier);
+
+#endif // PTHREAD_BARRIER_H_
diff --git a/src/worker.cpp b/src/worker.cpp
index 2fab28e..c274c4a 100644
--- a/src/worker.cpp
+++ b/src/worker.cpp
@@ -147,7 +147,7 @@
void WorkerStatus::Initialize() {
sat_assert(0 == pthread_mutex_init(&num_workers_mutex_, NULL));
sat_assert(0 == pthread_rwlock_init(&status_rwlock_, NULL));
- sat_assert(0 == pthread_barrier_init(&pause_barrier_, NULL,
+ sat_assert(0 == pthread_barrier_init(&pause_barrier_,
num_workers_ + 1));
}
@@ -220,7 +220,7 @@
// Decrement num_workers_ and reinitialize pause_barrier_, which we know isn't
// in use because (status != PAUSE).
sat_assert(0 == pthread_barrier_destroy(&pause_barrier_));
- sat_assert(0 == pthread_barrier_init(&pause_barrier_, NULL, num_workers_));
+ sat_assert(0 == pthread_barrier_init(&pause_barrier_, num_workers_));
--num_workers_;
ReleaseNumWorkersLock();
diff --git a/src/worker.h b/src/worker.h
index 7aae5f2..cf83621 100644
--- a/src/worker.h
+++ b/src/worker.h
@@ -21,7 +21,7 @@
#ifndef STRESSAPPTEST_WORKER_H_
#define STRESSAPPTEST_WORKER_H_
-#include <pthread.h>
+#include "pthread_barrier.h"
#include <sys/time.h>
#include <sys/types.h>