From 79f8204766bdef54f745ba5e72dc4e17445cbefc Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Fri, 12 May 2017 11:01:30 +0200 Subject: [PATCH] kern: move tsemaquire to userspace (given a generalized awake) --- sys/include/lib9.h | 1 + sys/include/libc.h | 2 ++ sys/src/lib/c/9sys/sysfatal.c | 1 + sys/src/lib/c/9sys/tsemacquire.c | 36 ++++++++++++++++++++++++++++++++ sys/src/lib/c/build.json | 1 + sys/src/lib/c/port/lock.c | 16 +------------- sys/src/sysconf.json | 11 ---------- 7 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 sys/src/lib/c/9sys/tsemacquire.c diff --git a/sys/include/lib9.h b/sys/include/lib9.h index 6a17cbb..f0a9683 100644 --- a/sys/include/lib9.h +++ b/sys/include/lib9.h @@ -236,6 +236,7 @@ #define toupper jehanne_toupper #define ainc jehanne_ainc #define adec jehanne_adec +#define tsemacquire jehanne_tsemacquire #define _tas jehanne__tas #define lock jehanne_lock #define lockt jehanne_lockt diff --git a/sys/include/libc.h b/sys/include/libc.h index bb0756f..89e03b7 100644 --- a/sys/include/libc.h +++ b/sys/include/libc.h @@ -378,6 +378,8 @@ struct Lock { int32_t sem; } Lock; +extern int jehanne_tsemacquire(int* addr, long ms); + extern int jehanne__tas(int*); extern void jehanne_lock(Lock*); diff --git a/sys/src/lib/c/9sys/sysfatal.c b/sys/src/lib/c/9sys/sysfatal.c index d6d652a..959526c 100644 --- a/sys/src/lib/c/9sys/sysfatal.c +++ b/sys/src/lib/c/9sys/sysfatal.c @@ -34,4 +34,5 @@ jehanne_sysfatal(const char *fmt, ...) va_start(arg, fmt); (*_sysfatal)(fmt, arg); va_end(arg); + jehanne_exits("sysfatal"); } diff --git a/sys/src/lib/c/9sys/tsemacquire.c b/sys/src/lib/c/9sys/tsemacquire.c new file mode 100644 index 0000000..4563c8a --- /dev/null +++ b/sys/src/lib/c/9sys/tsemacquire.c @@ -0,0 +1,36 @@ +/* + * This file is part of Jehanne. + * + * Copyright (C) 2017 Giacomo Tesio + * + * Jehanne is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * Jehanne 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jehanne. If not, see . + */ + +#include +#include + +int +jehanne_tsemacquire(int* addr, long ms) +{ + long wkup; + wkup = awake(ms); + while(semacquire(addr, 1) < 0){ + if(jehanne_awakened(wkup)){ + /* copy canlock semantic for return values */ + return 0; + } + /* interrupted; try again */ + } + jehanne_forgivewkp(wkup); + return 1; +} diff --git a/sys/src/lib/c/build.json b/sys/src/lib/c/build.json index 326bcf8..cc5469e 100644 --- a/sys/src/lib/c/build.json +++ b/sys/src/lib/c/build.json @@ -80,6 +80,7 @@ "9sys/times.c", "9sys/tm2sec.c", "9sys/truerand.c", + "9sys/tsemacquire.c", "9sys/wait.c", "9sys/waitpid.c", "9sys/werrstr.c", diff --git a/sys/src/lib/c/port/lock.c b/sys/src/lib/c/port/lock.c index 3af191d..5f4bbdf 100644 --- a/sys/src/lib/c/port/lock.c +++ b/sys/src/lib/c/port/lock.c @@ -46,22 +46,8 @@ jehanne_canlock(Lock *l) int jehanne_lockt(Lock *l, uint32_t ms) { - int semr; - int64_t start, end; - if(jehanne_ainc(&l->key) == 1) return 1; /* changed from 0 -> 1: we hold lock */ /* otherwise wait in kernel */ - semr = 0; - start = jehanne_nsec() / (1000 * 1000); - end = start + ms; - while(start < end && (semr = tsemacquire(&l->sem, ms)) < 0){ - /* interrupted; try again */ - start = jehanne_nsec() / (1000 * 1000); - ms = end - start; - } - /* copy canlock semantic for return values */ - if(semr == 1) - return 1; /* success, lock acquired */ - return 0; /* timed out or interrupt at timeout */ + return jehanne_tsemacquire(&l->sem, ms); } diff --git a/sys/src/sysconf.json b/sys/src/sysconf.json index d911ccb..1e62ab0 100644 --- a/sys/src/sysconf.json +++ b/sys/src/sysconf.json @@ -304,17 +304,6 @@ "int" ] }, - { - "Args": [ - "int*", - "uint64_t" - ], - "Id": 25, - "Name": "tsemacquire", - "Ret": [ - "int" - ] - }, { "Args": [ "const char*",