aboutsummaryrefslogtreecommitdiff
path: root/REORG.TODO/nptl/tst-spin4.c
diff options
context:
space:
mode:
authorZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
committerZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
commit5046dbb4a7eba5eccfd258f92f4735c9ffc8d069 (patch)
tree4470480d904b65cf14ca524f96f79eca818c3eaf /REORG.TODO/nptl/tst-spin4.c
parent199fc19d3aaaf57944ef036e15904febe877fc93 (diff)
downloadglibc-5046dbb4a7eba5eccfd258f92f4735c9ffc8d069.tar
glibc-5046dbb4a7eba5eccfd258f92f4735c9ffc8d069.tar.gz
glibc-5046dbb4a7eba5eccfd258f92f4735c9ffc8d069.tar.bz2
glibc-5046dbb4a7eba5eccfd258f92f4735c9ffc8d069.zip
Prepare for radical source tree reorganization.zack/build-layout-experiment
All top-level files and directories are moved into a temporary storage directory, REORG.TODO, except for files that will certainly still exist in their current form at top level when we're done (COPYING, COPYING.LIB, LICENSES, NEWS, README), all old ChangeLog files (which are moved to the new directory OldChangeLogs, instead), and the generated file INSTALL (which is just deleted; in the new order, there will be no generated files checked into version control).
Diffstat (limited to 'REORG.TODO/nptl/tst-spin4.c')
-rw-r--r--REORG.TODO/nptl/tst-spin4.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/REORG.TODO/nptl/tst-spin4.c b/REORG.TODO/nptl/tst-spin4.c
new file mode 100644
index 0000000000..5b23a172ca
--- /dev/null
+++ b/REORG.TODO/nptl/tst-spin4.c
@@ -0,0 +1,109 @@
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static int count = 0;
+
+static void *
+thread_add_one (void *arg)
+{
+ int tmp;
+ pthread_spinlock_t *lock = (pthread_spinlock_t *) arg;
+
+ /* When do_test holds the lock for 1 sec, the two thread will be
+ in contention for the lock. */
+ if (pthread_spin_lock (lock) != 0)
+ {
+ puts ("thread_add_one(): spin_lock failed");
+ pthread_exit ((void *) 1l);
+ }
+
+ /* sleep 1s before modifying count */
+ tmp = count;
+ sleep (1);
+ count = tmp + 1;
+
+ if (pthread_spin_unlock (lock) != 0)
+ {
+ puts ("thread_add_one(): spin_unlock failed");
+ pthread_exit ((void *) 1l);
+ }
+
+ return NULL;
+}
+
+static int
+do_test (void)
+{
+ pthread_t thr1, thr2;
+ pthread_spinlock_t lock;
+ int tmp;
+
+ if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0)
+ {
+ puts ("spin_init failed");
+ return 1;
+ }
+
+ if (pthread_spin_lock (&lock) != 0)
+ {
+ puts ("1st spin_lock failed");
+ return 1;
+ }
+
+ if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0)
+ {
+ puts ("1st pthread_create failed");
+ return 1;
+ }
+
+ if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0)
+ {
+ puts ("2nd pthread_create failed");
+ return 1;
+ }
+
+ /* sleep 1s before modifying count */
+ tmp = count;
+ sleep (1);
+ count = tmp + 1;
+
+ if (pthread_spin_unlock (&lock) != 0)
+ {
+ puts ("1st spin_unlock failed");
+ return 1;
+ }
+
+ void *status;
+ if (pthread_join (thr1, &status) != 0)
+ {
+ puts ("1st pthread_join failed");
+ return 1;
+ }
+ if (status != NULL)
+ {
+ puts ("failure in the 1st thread");
+ return 1;
+ }
+ if (pthread_join (thr2, &status) != 0)
+ {
+ puts ("2nd pthread_join failed");
+ return 1;
+ }
+ if (status != NULL)
+ {
+ puts ("failure in the 2nd thread");
+ return 1;
+ }
+
+ if (count != 3)
+ {
+ printf ("count is %d, should be 3\n", count);
+ return 1;
+ }
+ return 0;
+}
+
+#define TIMEOUT 5
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"