Make all timestamps use UInt64.

This commit is contained in:
Jordan Bancino 2023-08-12 23:02:06 +00:00
parent 38a303da91
commit 2382638005
14 changed files with 265 additions and 181 deletions

View file

@ -23,6 +23,7 @@
*/
#include <Cron.h>
#include <UInt64.h>
#include <Array.h>
#include <Memory.h>
#include <Util.h>
@ -31,7 +32,7 @@
struct Cron
{
unsigned long tick;
UInt64 tick;
Array *jobs;
pthread_mutex_t lock;
volatile unsigned int stop:1;
@ -40,14 +41,14 @@ struct Cron
typedef struct Job
{
unsigned long interval;
unsigned long lastExec;
UInt64 interval;
UInt64 lastExec;
JobFunc *func;
void *args;
} Job;
static Job *
JobCreate(long interval, JobFunc * func, void *args)
JobCreate(UInt32 interval, JobFunc * func, void *args)
{
Job *job;
@ -62,8 +63,8 @@ JobCreate(long interval, JobFunc * func, void *args)
return NULL;
}
job->interval = interval;
job->lastExec = 0;
job->interval = UInt64Create(0, interval);
job->lastExec = UInt64Create(0, 0);
job->func = func;
job->args = args;
@ -78,8 +79,8 @@ CronThread(void *args)
while (!cron->stop)
{
size_t i;
unsigned long ts; /* tick start */
unsigned long te; /* tick end */
UInt64 ts; /* tick start */
UInt64 te; /* tick end */
pthread_mutex_lock(&cron->lock);
@ -89,13 +90,13 @@ CronThread(void *args)
{
Job *job = ArrayGet(cron->jobs, i);
if (ts - job->lastExec > job->interval)
if (UInt64Gt(UInt64Sub(ts, job->lastExec), job->interval))
{
job->func(job->args);
job->lastExec = ts;
}
if (!job->interval)
if (UInt64Eq(job->interval, UInt64Create(0, 0)))
{
ArrayDelete(cron->jobs, i);
Free(job);
@ -106,21 +107,23 @@ CronThread(void *args)
pthread_mutex_unlock(&cron->lock);
/* Only sleep if the jobs didn't overrun the tick */
if (cron->tick > (te - ts))
if (UInt64Gt(cron->tick, UInt64Sub(te, ts)))
{
const unsigned long microTick = 100;
unsigned long remainingTick = cron->tick - (te - ts);
const UInt64 microTick = UInt64Create(0, 100);
UInt64 remainingTick = UInt64Sub(cron->tick, UInt64Sub(te, ts));
/* Only sleep for microTick ms at a time because if the job
* scheduler is supposed to stop before the tick is up, we
* don't want to be stuck in a long sleep */
while (remainingTick >= microTick && !cron->stop)
while (UInt64Geq(remainingTick, microTick) && !cron->stop)
{
UtilSleepMillis(microTick);
remainingTick -= microTick;
remainingTick = UInt64Sub(remainingTick, microTick);
}
if (remainingTick && !cron->stop)
if (UInt64Neq(remainingTick, UInt64Create(0, 0)) && !cron->stop)
{
UtilSleepMillis(remainingTick);
}
@ -131,7 +134,7 @@ CronThread(void *args)
}
Cron *
CronCreate(unsigned long tick)
CronCreate(UInt32 tick)
{
Cron *cron = Malloc(sizeof(Cron));
@ -147,7 +150,7 @@ CronCreate(unsigned long tick)
return NULL;
}
cron->tick = tick;
cron->tick = UInt64Create(0, tick);
cron->stop = 1;
pthread_mutex_init(&cron->lock, NULL);

View file

@ -23,6 +23,7 @@
*/
#include <Db.h>
#include <UInt64.h>
#include <Memory.h>
#include <Json.h>
#include <Util.h>
@ -76,7 +77,7 @@ struct DbRef
{
HashMap *json;
unsigned long ts;
UInt64 ts;
size_t size;
Array *name;
@ -494,12 +495,12 @@ DbLockFromArr(Db * db, Array * args)
if (ref) /* In cache */
{
unsigned long diskTs = UtilLastModified(file);
UInt64 diskTs = UtilLastModified(file);
ref->fd = fd;
ref->stream = stream;
if (diskTs > ref->ts)
if (UInt64Gt(diskTs, ref->ts))
{
/* File was modified on disk since it was cached */
HashMap *json = JsonDecode(ref->stream);
@ -651,7 +652,7 @@ DbCreate(Db * db, size_t nArgs,...)
file = DbFileName(db, args);
if (UtilLastModified(file))
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
{
Free(file);
ArrayFree(args);
@ -754,7 +755,7 @@ DbDelete(Db * db, size_t nArgs,...)
Free(hash);
if (UtilLastModified(file))
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
{
ret = remove(file) == 0;
}
@ -872,7 +873,7 @@ DbExists(Db * db, size_t nArgs,...)
pthread_mutex_lock(&db->lock);
file = DbFileName(db, args);
ret = UtilLastModified(file);
ret = UInt64Neq(UtilLastModified(file), UInt64Create(0, 0));
pthread_mutex_unlock(&db->lock);

View file

@ -465,7 +465,7 @@ HttpServerWorkerThread(void *args)
ssize_t i = 0;
HttpRequestMethod requestMethod;
long firstRead;
UInt64 firstRead;
fp = DequeueConnection(server);
@ -473,7 +473,7 @@ HttpServerWorkerThread(void *args)
{
/* Block for 1 millisecond before continuing so we don't
* murder the CPU if the queue is empty. */
UtilSleepMillis(1);
UtilSleepMillis(UInt64Create(0, 1));
continue;
}
@ -492,12 +492,12 @@ HttpServerWorkerThread(void *args)
/* If the server is stopped, or it's been a while, just
* give up so we aren't wasting a thread on this client. */
if (server->stop || (UtilServerTs() - firstRead) > 1000 * 30)
if (server->stop || UInt64Gt(UInt64Sub(UtilServerTs(), firstRead), UInt64Create(0, 1000 * 30)))
{
goto finish;
}
UtilSleepMillis(5);
UtilSleepMillis(UInt64Create(0, 5));
}
if (lineLen == -1)

View file

@ -200,6 +200,7 @@ Int64LongDivision(Int64 n, Int64 d)
for (i = 63; i >= 0; i--)
{
Int64 bit = Int64And(Int64Sra(n, i), Int64Create(0, 1));
o.r = Int64Sll(o.r, 1);
o.r = Int64Or(o.r, bit);
@ -283,6 +284,7 @@ Int64Sra(Int64 x, int y)
if (neg)
{
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
z = Int64Or(Int64Sll(mask, (64 - y)), z);
}
@ -331,12 +333,14 @@ Int64Lt(Int64 x, Int64 y)
}
else
{
if (xneg) /* Both negative */
if (xneg)
{
/* Both negative */
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
}
else /* Both positive */
else
{
/* Both positive */
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
}
}
@ -354,12 +358,14 @@ Int64Gt(Int64 x, Int64 y)
}
else
{
if (xneg) /* Both negative */
if (xneg)
{
/* Both negative */
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
}
else /* Both positive */
else
{
/* Both positive */
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
}
}

View file

@ -872,7 +872,7 @@ JsonConsumeWhitespace(JsonParserState * state)
}
else
{
UtilSleepMillis(delay);
UtilSleepMillis(UInt64Create(0, delay));
continue;
}
}

View file

@ -24,6 +24,7 @@
#include <Rand.h>
#include <Int.h>
#include <UInt64.h>
#include <Util.h>
#include <Memory.h>
@ -140,7 +141,8 @@ RandIntN(int *buf, size_t size, unsigned int max)
if (!state)
{
/* Generate a seed from the system time, PID, and TID */
UInt32 seed = UtilServerTs() ^ getpid() ^ (unsigned long) pthread_self();
UInt64 ts = UtilServerTs();
UInt32 seed = UInt64Low(ts) ^ getpid() ^ (unsigned long) pthread_self();
state = Malloc(sizeof(RandState));
RandSeed(state, seed);

View file

@ -626,7 +626,7 @@ StreamCopy(Stream * in, Stream * out)
}
else
{
UtilSleepMillis(STREAM_DELAY);
UtilSleepMillis(UInt64Create(0, STREAM_DELAY));
continue;
}
}

View file

@ -144,6 +144,7 @@ UInt64LongDivision(UInt64 n, UInt64 d)
for (i = 63; i >= 0; i--)
{
UInt64 bit = UInt64And(UInt64Srl(n, i), UInt64Create(0, 1));
o.r = UInt64Sll(o.r, 1);
o.r = UInt64Or(o.r, bit);

View file

@ -40,6 +40,8 @@
#include <limits.h>
#include <pthread.h>
#include <UInt64.h>
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
@ -48,33 +50,84 @@
#define SSIZE_MAX LONG_MAX
#endif
unsigned long
UInt64
UtilServerTs(void)
{
struct timeval tv;
unsigned long ts;
UInt64 ts;
UInt64 sec;
UInt64 usec;
gettimeofday(&tv, NULL);
ts = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
/*
* POSIX defines struct timeval to be:
*
* struct timeval {
* time_t tv_sec;
* suseconds_t tv_usec;
* };
*
* Note: Although the C standard does not require that
* time_t be an integer type (it may be floating point),
* according to POSIX, time_t shall be an integer type.
* As we are targeting POSIX, not ANSI/ISO C, we can
* rely on this.
*
* The same goes for suseconds_t.
*/
if (sizeof(time_t) == sizeof(UInt64))
{
/* 64 bit time_t: convert it to a 64 bit integer */
time_t ms = tv.tv_sec * 1000;
UInt32 high = (UInt32) (ms >> 32);
UInt32 low = (UInt32) ms;
sec = UInt64Create(high, low);
}
else
{
/* Assume 32 bit time_t: promote to 64 bit, then multiply, in
* case multiplication overflows 32 bits. */
sec = UInt64Create(0, tv.tv_sec);
sec = UInt64Mul(sec, UInt64Create(0, 1000));
}
usec = UInt64Create(0, tv.tv_usec / 1000);
ts = UInt64Add(sec, usec);
return ts;
}
unsigned long
UInt64
UtilLastModified(char *path)
{
struct stat st;
unsigned long ts;
UInt64 ts = UInt64Create(0, 0);
if (stat(path, &st) == 0)
{
ts = (st.st_mtim.tv_sec * 1000) + (st.st_mtim.tv_nsec / 1000000);
return ts;
if (sizeof(time_t) == sizeof(UInt64))
{
/* 64 bit time_t: convert it to a 64 bit integer */
time_t ms = st.st_mtim.tv_sec * 1000;
UInt32 high = (UInt32) (ms >> 32);
UInt32 low = (UInt32) ms;
ts = UInt64Create(high, low);
}
else
{
return 0;
ts = UInt64Create(0, st.st_mtim.tv_sec);
ts = UInt64Mul(ts, UInt64Create(0, 1000));
}
/* nsec gauanteed to fit in 32 bits */
ts = UInt64Add(ts, UInt64Create(0, st.st_mtim.tv_nsec / 1000000));
}
return ts;
}
int
@ -149,13 +202,21 @@ UtilMkdir(const char *dir, const mode_t mode)
}
int
UtilSleepMillis(long ms)
UtilSleepMillis(UInt64 ms)
{
struct timespec ts;
int res;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
if (sizeof(time_t) == sizeof(UInt64))
{
ts.tv_sec = ((time_t) UInt64High(ms) << 32 | UInt64Low(ms)) / 1000;
}
else
{
ts.tv_sec = UInt64Low(ms) / 1000;
}
ts.tv_nsec = UInt64Low(UInt64Rem(ms, UInt64Create(0, 1000))) * 1000000;
res = nanosleep(&ts, &ts);
@ -251,14 +312,14 @@ ThreadNoDestructor(void *p)
free(p);
}
unsigned long
UInt32
UtilThreadNo(void)
{
static pthread_key_t key;
static int createdKey = 0;
static unsigned long count = 0;
unsigned long *no;
UInt32 *no;
if (!createdKey)
{
@ -269,7 +330,7 @@ UtilThreadNo(void)
no = pthread_getspecific(key);
if (!no)
{
no = malloc(sizeof(unsigned long));
no = malloc(sizeof(UInt32));
*no = count++;
pthread_setspecific(key, no);
}

View file

@ -56,6 +56,8 @@
* by any means.
*/
#include <Int.h>
/**
* All functions defined here operate on a structure opaque to the
* caller.
@ -80,8 +82,7 @@ typedef void (JobFunc) (void *);
* .Pp
* This function takes the tick interval in milliseconds.
*/
extern Cron *
CronCreate(unsigned long);
extern Cron * CronCreate(UInt32);
/**
* Schedule a one-off job to be executed only at the next tick, and

View file

@ -43,6 +43,7 @@
#include <sys/types.h>
#include <Stream.h>
#include <UInt64.h>
/**
* Get the current timestamp in milliseconds since the Unix epoch. This
@ -60,7 +61,7 @@
* numbers than the architecture supports. But we can always
* re-evaluate over the next few years.
*/
extern unsigned long UtilServerTs(void);
extern UInt64 UtilServerTs(void);
/**
* Use
@ -69,7 +70,7 @@ extern unsigned long UtilServerTs(void);
* was an error getting the last modified time of a file. This is
* primarily useful for caching file data.
*/
extern unsigned long UtilLastModified(char *);
extern UInt64 UtilLastModified(char *);
/**
* This function behaves just like the system call
@ -85,7 +86,7 @@ extern int UtilMkdir(const char *, const mode_t);
* .Xr nanosleep 2
* to make its usage much, much simpler.
*/
extern int UtilSleepMillis(long);
extern int UtilSleepMillis(UInt64);
/**
* This function works identically to the POSIX
@ -111,6 +112,6 @@ extern ssize_t UtilGetLine(char **, size_t *, Stream *);
* .Fn pthread_self
* to a number.
*/
extern unsigned long UtilThreadNo(void);
extern UInt32 UtilThreadNo(void);
#endif /* CYTOPLASM_UTIL_H */

View file

@ -3,7 +3,8 @@
#include <Log.h>
/* AssertEquals(actual, expected) */
int AssertEquals(char *msg, Int64 x, Int64 y)
int
AssertEquals(char *msg, Int64 x, Int64 y)
{
if (!Int64Eq(x, y))
{
@ -17,7 +18,8 @@ int AssertEquals(char *msg, Int64 x, Int64 y)
return 1;
}
int Main(void)
int
Main(void)
{
Int64 x, y;
@ -136,7 +138,8 @@ int Main(void)
y = Int64Create(0x00000000, 0x00000010);
AssertEquals("0x00000000 0x000000F0 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0));
/* TODO: Add more tests for negative multiplication, division, and mod */
/* TODO: Add more tests for negative multiplication, division, and
* mod */
return 0;
}

View file

@ -40,7 +40,8 @@
static char *
Trim(char c, char *str)
{
while (*str == c) str++;
while (*str == c)
str++;
return str;
}
@ -355,6 +356,7 @@ Main(Array * args)
!StrEquals(fieldType, "boolean"))
{
Node *node = HashMapGet(typeToNode, fieldType);
if (!node)
{
node = Malloc(sizeof(Node));
@ -607,7 +609,7 @@ Main(Array * args)
" *errp = \"Invalid pointers passed to %sFromJson()\";\n"
" return 0;\n"
" }\n\n"
, type);
,type);
for (i = 0; i < ArraySize(keys); i++)
{
char *key = ArrayGet(keys, i);
@ -1014,7 +1016,8 @@ Main(Array * args)
}
else
{
/* Ignore primitives but call the appropriate free method on declared types */
/* Ignore primitives but call the appropriate free
* method on declared types */
if (!isEnum && HashMapGet(types, fieldType))
{
StreamPrintf(implFile, " %sFree(&val->%s);\n", fieldType, key);

View file

@ -3,7 +3,8 @@
#include <Log.h>
/* AssertEquals(actual, expected) */
int AssertEquals(char *msg, UInt64 x, UInt64 y)
int
AssertEquals(char *msg, UInt64 x, UInt64 y)
{
if (!UInt64Eq(x, y))
{
@ -17,7 +18,8 @@ int AssertEquals(char *msg, UInt64 x, UInt64 y)
return 1;
}
int Main(void)
int
Main(void)
{
UInt64 x, y;