Make all timestamps use UInt64.
This commit is contained in:
parent
e9af54e4c7
commit
2c715c6e72
14 changed files with 265 additions and 181 deletions
39
src/Cron.c
39
src/Cron.c
|
@ -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);
|
||||
|
|
13
src/Db.c
13
src/Db.c
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
180
src/Int64.c
180
src/Int64.c
|
@ -40,14 +40,14 @@ Int64Str(Int64 x, int base, char *out, size_t len)
|
|||
size_t i = len - 1;
|
||||
size_t j = 0;
|
||||
|
||||
int neg = Int64Sign(x);
|
||||
int neg = Int64Sign(x);
|
||||
|
||||
Int64 base64 = Int64Create(0, base);
|
||||
|
||||
if (neg)
|
||||
{
|
||||
x = Int64Neg(x);
|
||||
}
|
||||
if (neg)
|
||||
{
|
||||
x = Int64Neg(x);
|
||||
}
|
||||
|
||||
/* We only have symbols up to base 16 */
|
||||
if (base < 2 || base > 16)
|
||||
|
@ -65,19 +65,19 @@ Int64Str(Int64 x, int base, char *out, size_t len)
|
|||
x = Int64Div(x, base64);
|
||||
} while (Int64Gt(x, Int64Create(0, 0)));
|
||||
|
||||
/*
|
||||
* Binary, octal, and hexadecimal are known to
|
||||
* be bit representations. Everything else (notably
|
||||
* decimal) should include the negative sign.
|
||||
*/
|
||||
if (base != 2 && base != 8 && base != 16)
|
||||
{
|
||||
if (neg)
|
||||
{
|
||||
out[i] = '-';
|
||||
i--;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Binary, octal, and hexadecimal are known to
|
||||
* be bit representations. Everything else (notably
|
||||
* decimal) should include the negative sign.
|
||||
*/
|
||||
if (base != 2 && base != 8 && base != 16)
|
||||
{
|
||||
if (neg)
|
||||
{
|
||||
out[i] = '-';
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
while (++i < len)
|
||||
{
|
||||
|
@ -128,18 +128,18 @@ Int64Mul(Int64 x, Int64 y)
|
|||
{
|
||||
Int64 z = Int64Create(0, 0);
|
||||
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
|
||||
if (xneg)
|
||||
{
|
||||
x = Int64Neg(x);
|
||||
}
|
||||
if (xneg)
|
||||
{
|
||||
x = Int64Neg(x);
|
||||
}
|
||||
|
||||
if (yneg)
|
||||
{
|
||||
y = Int64Neg(y);
|
||||
}
|
||||
if (yneg)
|
||||
{
|
||||
y = Int64Neg(y);
|
||||
}
|
||||
|
||||
/* while (y > 0) */
|
||||
while (Int64Gt(y, Int64Create(0, 0)))
|
||||
|
@ -154,10 +154,10 @@ Int64Mul(Int64 x, Int64 y)
|
|||
y = Int64Sra(y, 1);
|
||||
}
|
||||
|
||||
if (xneg != yneg)
|
||||
{
|
||||
z = Int64Neg(z);
|
||||
}
|
||||
if (xneg != yneg)
|
||||
{
|
||||
z = Int64Neg(z);
|
||||
}
|
||||
|
||||
return z;
|
||||
}
|
||||
|
@ -175,8 +175,8 @@ Int64LongDivision(Int64 n, Int64 d)
|
|||
|
||||
int i;
|
||||
|
||||
int nneg = Int64Sign(n);
|
||||
int dneg = Int64Sign(d);
|
||||
int nneg = Int64Sign(n);
|
||||
int dneg = Int64Sign(d);
|
||||
|
||||
o.q = Int64Create(0, 0);
|
||||
o.r = Int64Create(0, 0);
|
||||
|
@ -187,19 +187,20 @@ Int64LongDivision(Int64 n, Int64 d)
|
|||
return o;
|
||||
}
|
||||
|
||||
if (nneg)
|
||||
{
|
||||
n = Int64Neg(n);
|
||||
}
|
||||
if (nneg)
|
||||
{
|
||||
n = Int64Neg(n);
|
||||
}
|
||||
|
||||
if (dneg)
|
||||
{
|
||||
d = Int64Neg(d);
|
||||
}
|
||||
if (dneg)
|
||||
{
|
||||
d = Int64Neg(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);
|
||||
|
||||
|
@ -210,11 +211,11 @@ Int64LongDivision(Int64 n, Int64 d)
|
|||
}
|
||||
}
|
||||
|
||||
if (nneg != dneg)
|
||||
{
|
||||
o.r = Int64Neg(o.r);
|
||||
o.q = Int64Neg(o.q);
|
||||
}
|
||||
if (nneg != dneg)
|
||||
{
|
||||
o.r = Int64Neg(o.r);
|
||||
o.q = Int64Neg(o.q);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
|
@ -261,7 +262,7 @@ Int64Sra(Int64 x, int y)
|
|||
{
|
||||
Int64 z;
|
||||
|
||||
int neg = Int64Sign(x);
|
||||
int neg = Int64Sign(x);
|
||||
|
||||
if (!y)
|
||||
{
|
||||
|
@ -280,11 +281,12 @@ Int64Sra(Int64 x, int y)
|
|||
z.i[0] = x.i[1] >> (y - 32);
|
||||
}
|
||||
|
||||
if (neg)
|
||||
{
|
||||
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
|
||||
z = Int64Or(Int64Sll(mask, (64 - y)), z);
|
||||
}
|
||||
if (neg)
|
||||
{
|
||||
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
|
||||
|
||||
z = Int64Or(Int64Sll(mask, (64 - y)), z);
|
||||
}
|
||||
|
||||
return z;
|
||||
}
|
||||
|
@ -322,47 +324,51 @@ Int64Eq(Int64 x, Int64 y)
|
|||
int
|
||||
Int64Lt(Int64 x, Int64 y)
|
||||
{
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
|
||||
if (xneg != yneg)
|
||||
{
|
||||
return xneg > yneg;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
{
|
||||
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
||||
}
|
||||
}
|
||||
if (xneg != yneg)
|
||||
{
|
||||
return xneg > yneg;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
Int64Gt(Int64 x, Int64 y)
|
||||
{
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
int xneg = Int64Sign(x);
|
||||
int yneg = Int64Sign(y);
|
||||
|
||||
if (xneg != yneg)
|
||||
{
|
||||
return xneg < yneg;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
{
|
||||
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
||||
}
|
||||
}
|
||||
if (xneg != yneg)
|
||||
{
|
||||
return xneg < yneg;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -872,7 +872,7 @@ JsonConsumeWhitespace(JsonParserState * state)
|
|||
}
|
||||
else
|
||||
{
|
||||
UtilSleepMillis(delay);
|
||||
UtilSleepMillis(UInt64Create(0, delay));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -626,7 +626,7 @@ StreamCopy(Stream * in, Stream * out)
|
|||
}
|
||||
else
|
||||
{
|
||||
UtilSleepMillis(STREAM_DELAY);
|
||||
UtilSleepMillis(UInt64Create(0, STREAM_DELAY));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
95
src/Util.c
95
src/Util.c
|
@ -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;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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,9 +18,10 @@ int AssertEquals(char *msg, Int64 x, Int64 y)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int Main(void)
|
||||
int
|
||||
Main(void)
|
||||
{
|
||||
Int64 x, y;
|
||||
Int64 x, y;
|
||||
|
||||
Log(LOG_INFO, "sizeof(Int64) = %lu", sizeof(Int64));
|
||||
|
||||
|
@ -31,9 +33,9 @@ int Main(void)
|
|||
|
||||
/* BSR Tests */
|
||||
|
||||
x = Int64Create(0x000000FF, 0x00000000);
|
||||
x = Int64Create(0x000000FF, 0x00000000);
|
||||
|
||||
y = Int64Sra(x, 4);
|
||||
y = Int64Sra(x, 4);
|
||||
AssertEquals("x >> 4", y, Int64Create(0x0000000F, 0xF0000000));
|
||||
|
||||
y = Int64Sra(x, 8);
|
||||
|
@ -42,15 +44,15 @@ int Main(void)
|
|||
y = Int64Sra(x, 36);
|
||||
AssertEquals("x >> 36", y, Int64Create(0x00000000, 0x0000000F));
|
||||
|
||||
x = Int64Create(0xFF000000, 0x00000000);
|
||||
x = Int64Create(0xFF000000, 0x00000000);
|
||||
|
||||
y = Int64Sra(x, 4);
|
||||
y = Int64Sra(x, 4);
|
||||
AssertEquals("x >> 4", y, Int64Create(0xFFF00000, 0x00000000));
|
||||
|
||||
y = Int64Sra(x, 8);
|
||||
y = Int64Sra(x, 8);
|
||||
AssertEquals("x >> 8", y, Int64Create(0xFFFF0000, 0x00000000));
|
||||
|
||||
y = Int64Sra(x, 63);
|
||||
y = Int64Sra(x, 63);
|
||||
AssertEquals("x >> 63", y, Int64Create(0xFFFFFFFF, 0xFFFFFFFF));
|
||||
|
||||
/* BSL Tests */
|
||||
|
@ -76,9 +78,9 @@ int Main(void)
|
|||
y = Int64Create(0x00000000, 0x10000000);
|
||||
AssertEquals("0xF0000000 + 0x10000000", Int64Add(x, y), Int64Create(0x00000001, 0x00000000));
|
||||
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Neg(Int64Create(0, 10));
|
||||
AssertEquals("5 + (-10)", Int64Add(x, y), Int64Neg(Int64Create(0, 5)));
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Neg(Int64Create(0, 10));
|
||||
AssertEquals("5 + (-10)", Int64Add(x, y), Int64Neg(Int64Create(0, 5)));
|
||||
|
||||
/* SUB Tests */
|
||||
x = Int64Create(0x00000000, 0x00000005);
|
||||
|
@ -89,13 +91,13 @@ int Main(void)
|
|||
y = Int64Create(0x00000000, 0x00000001);
|
||||
AssertEquals("0x00000001 0x00000000 - 0x00000001", Int64Sub(x, y), Int64Create(0x00000000, 0xFFFFFFFF));
|
||||
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Create(0, 10);
|
||||
AssertEquals("5 - 10", Int64Sub(x, y), Int64Neg(Int64Create(0, 5)));
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Create(0, 10);
|
||||
AssertEquals("5 - 10", Int64Sub(x, y), Int64Neg(Int64Create(0, 5)));
|
||||
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Neg(Int64Create(0, 10));
|
||||
AssertEquals("5 - (-10)", Int64Sub(x, y), Int64Create(0, 15));
|
||||
x = Int64Create(0, 5);
|
||||
y = Int64Neg(Int64Create(0, 10));
|
||||
AssertEquals("5 - (-10)", Int64Sub(x, y), Int64Create(0, 15));
|
||||
|
||||
/* MUL Tests */
|
||||
x = Int64Create(0, 18);
|
||||
|
@ -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;
|
||||
return 0;
|
||||
}
|
||||
|
|
29
tools/j2s.c
29
tools/j2s.c
|
@ -40,7 +40,8 @@
|
|||
static char *
|
||||
Trim(char c, char *str)
|
||||
{
|
||||
while (*str == c) str++;
|
||||
while (*str == c)
|
||||
str++;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
@ -89,7 +90,7 @@ JsonTypeToStr(JsonType type)
|
|||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON_OBJECT:
|
||||
case JSON_OBJECT:
|
||||
return "JSON_OBJECT";
|
||||
case JSON_ARRAY:
|
||||
return "JSON_ARRAY";
|
||||
|
@ -355,6 +356,7 @@ Main(Array * args)
|
|||
!StrEquals(fieldType, "boolean"))
|
||||
{
|
||||
Node *node = HashMapGet(typeToNode, fieldType);
|
||||
|
||||
if (!node)
|
||||
{
|
||||
node = Malloc(sizeof(Node));
|
||||
|
@ -603,11 +605,11 @@ Main(Array * args)
|
|||
StreamPrintf(implFile, " (void) enumParseRes;\n");
|
||||
StreamPrintf(implFile, "\n");
|
||||
StreamPrintf(implFile, " if (!json | !out)\n"
|
||||
" {\n"
|
||||
" *errp = \"Invalid pointers passed to %sFromJson()\";\n"
|
||||
" return 0;\n"
|
||||
" }\n\n"
|
||||
, type);
|
||||
" {\n"
|
||||
" *errp = \"Invalid pointers passed to %sFromJson()\";\n"
|
||||
" return 0;\n"
|
||||
" }\n\n"
|
||||
,type);
|
||||
for (i = 0; i < ArraySize(keys); i++)
|
||||
{
|
||||
char *key = ArrayGet(keys, i);
|
||||
|
@ -875,8 +877,8 @@ Main(Array * args)
|
|||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||
isPrimitive = StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "boolean") ||
|
||||
StrEquals(fieldType, "float");
|
||||
StrEquals(fieldType, "boolean") ||
|
||||
StrEquals(fieldType, "float");
|
||||
|
||||
|
||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||
|
@ -996,9 +998,9 @@ Main(Array * args)
|
|||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||
isPrimitive = StrEquals(fieldType, "boolean") ||
|
||||
StrEquals(fieldType, "float") ||
|
||||
StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "string");
|
||||
StrEquals(fieldType, "float") ||
|
||||
StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "string");
|
||||
|
||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
|
@ -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);
|
||||
|
|
|
@ -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,9 +18,10 @@ int AssertEquals(char *msg, UInt64 x, UInt64 y)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int Main(void)
|
||||
int
|
||||
Main(void)
|
||||
{
|
||||
UInt64 x, y;
|
||||
UInt64 x, y;
|
||||
|
||||
Log(LOG_INFO, "sizeof(UInt64) = %lu", sizeof(UInt64));
|
||||
|
||||
|
@ -31,9 +33,9 @@ int Main(void)
|
|||
|
||||
/* BSR Tests */
|
||||
|
||||
x = UInt64Create(0x000000FF, 0x00000000);
|
||||
x = UInt64Create(0x000000FF, 0x00000000);
|
||||
|
||||
y = UInt64Srl(x, 4);
|
||||
y = UInt64Srl(x, 4);
|
||||
AssertEquals("x >> 4", y, UInt64Create(0x0000000F, 0xF0000000));
|
||||
|
||||
y = UInt64Srl(x, 8);
|
||||
|
@ -113,5 +115,5 @@ int Main(void)
|
|||
y = UInt64Create(0x00000000, 0x00000010);
|
||||
AssertEquals("0x00000000 0x000000F0 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0));
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue