summaryrefslogtreecommitdiffstats
path: root/src/lib9/lock-tas.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2004-09-21 01:11:28 +0000
committerrsc <devnull@localhost>2004-09-21 01:11:28 +0000
commitc6687d4591964cb13df87f55ec4770e778a4a55c (patch)
treeee669a419904e929ae8ae54fd04c2bb41b5a57b1 /src/lib9/lock-tas.c
parent3d5e34e146b5ba5c973230abb624ce9126024569 (diff)
downloadplan9port-c6687d4591964cb13df87f55ec4770e778a4a55c.tar.gz
plan9port-c6687d4591964cb13df87f55ec4770e778a4a55c.zip
Continue the pthreads torture.
Diffstat (limited to 'src/lib9/lock-tas.c')
-rw-r--r--src/lib9/lock-tas.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/lib9/lock-tas.c b/src/lib9/lock-tas.c
new file mode 100644
index 00000000..e6f54de6
--- /dev/null
+++ b/src/lib9/lock-tas.c
@@ -0,0 +1,57 @@
+#include <u.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sched.h>
+#include <libc.h>
+
+int _ntas;
+static int
+_xtas(void *v)
+{
+ int x;
+
+ _ntas++;
+ x = _tas(v);
+ if(x != 0 && x != 0xcafebabe){
+ print("bad tas value %d\n", x);
+ abort();
+ }
+ return x;
+}
+
+int
+canlock(Lock *l)
+{
+ return !_xtas(&l->val);
+}
+
+void
+unlock(Lock *l)
+{
+ l->val = 0;
+}
+
+void
+lock(Lock *lk)
+{
+ int i;
+
+ /* once fast */
+ if(!_xtas(&lk->val))
+ return;
+ /* a thousand times pretty fast */
+ for(i=0; i<1000; i++){
+ if(!_xtas(&lk->val))
+ return;
+ sched_yield();
+ }
+ /* now nice and slow */
+ for(i=0; i<1000; i++){
+ if(!_xtas(&lk->val))
+ return;
+ usleep(100*1000);
+ }
+ /* take your time */
+ while(_xtas(&lk->val))
+ usleep(1000*1000);
+}