More wq tests

上级 e305634b
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
#include "amd64.h" #include "amd64.h"
#include "wq.hh" #include "wq.hh"
#define NEW_DELETE_OPS(classname) \
static void* operator new(unsigned long nbytes) { \
assert(nbytes == sizeof(classname)); \
return malloc(sizeof(classname)); \
} \
\
static void operator delete(void *p) { \
free(p); \
}
struct testwork : public work { struct testwork : public work {
testwork(forframe *b) : barrier_(b) {} testwork(forframe *b) : barrier_(b) {}
...@@ -12,24 +22,17 @@ struct testwork : public work { ...@@ -12,24 +22,17 @@ struct testwork : public work {
delete this; delete this;
} }
static void* operator new(unsigned long nbytes) { NEW_DELETE_OPS(testwork);
assert(nbytes == sizeof(testwork));
return xmalloc(sizeof(testwork));
}
static void operator delete(void*p) {
xfree(p, sizeof(testwork));
}
struct forframe *barrier_; struct forframe *barrier_;
}; };
static void static void
test(void) test0(void)
{ {
enum { pushes = 100 }; enum { pushes = 100 };
struct forframe wqbarrier(pushes); struct forframe wqbarrier(pushes);
printf("test0...\n");
for (int i = 0; i < pushes; i++) { for (int i = 0; i < pushes; i++) {
testwork *w = new testwork(&wqbarrier); testwork *w = new testwork(&wqbarrier);
wq_push(w); wq_push(w);
...@@ -37,14 +40,53 @@ test(void) ...@@ -37,14 +40,53 @@ test(void)
while (!wqbarrier.zero()) while (!wqbarrier.zero())
nop_pause(); nop_pause();
printf("test0 done\n");
}
struct forkwork : public work {
forkwork(forframe *b) : barrier_(b) {}
virtual void run() {
int pid;
pid = fork(0);
if (pid < 0)
die("forkwork::run: fork");
else if (pid == 0)
exit();
wait();
barrier_->dec();
delete this;
}
NEW_DELETE_OPS(forkwork);
struct forframe *barrier_;
};
static void
testfork(void)
{
enum { forks = 100 };
struct forframe wqbarrier(forks);
printf("testfork...\n");
for (int i = 0; i < forks; i++) {
forkwork *w = new forkwork(&wqbarrier);
wq_push(w);
}
while (!wqbarrier.zero())
nop_pause();
printf("testfork done\n");
} }
int int
main(int ac, char **av) main(int ac, char **av)
{ {
initwq(); initwq();
test(); test0();
testfork();
exitwq(); exitwq();
printf("all done!\n");
return 0; return 0;
} }
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论