task_test.c revision de9282a1eaa50764fdc2e88046f8ff3522e3092e
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#include "attribute.h"
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#include <stdio.h>
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#include <stdlib.h>
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews#include <unistd.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <isc/memcluster.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/task.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <isc/thread.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <isc/result.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <isc/timer.h>
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafssonisc_memctx_t mctx = NULL;
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_boolean_t
3e14b69d196a3ebeecc4662c426344dcfd7db678Andreas Gustafssonmy_callback(isc_task_t task, isc_event_t event)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence int i, j;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews char *name = event->arg;
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence j = 0;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews for (i = 0; i < 1000000; i++)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews j += 100;
e63d63dc8510c669e1575b2762265842e8783822Evan Hunt printf("task %s (%p): %d\n", name, task, j);
364a82f7c25b62967678027043425201a5e5171aBob Halley
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_FALSE);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_boolean_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonmy_shutdown(isc_task_t task, isc_event_t event) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews char *name = event->arg;
3e14b69d196a3ebeecc4662c426344dcfd7db678Andreas Gustafsson
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews printf("shutdown %s (%p)\n", name, task);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence return (ISC_TRUE);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
673ce7aebbb6d01c75b95f6df1ec491d6422b951Andreas Gustafssonstatic isc_boolean_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonmy_tick(isc_task_t task, isc_event_t event)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence char *name = event->arg;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson printf("task %p tick %s\n", task, name);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (ISC_FALSE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsvoid
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsmain(int argc, char *argv[]) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_taskmgr_t manager = NULL;
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence isc_task_t t1 = NULL, t2 = NULL;
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence isc_task_t t3 = NULL, t4 = NULL;
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence isc_event_t event;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int workers;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_timermgr_t timgr;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews isc_timer_t ti1, ti2;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews os_time_t absolute, interval;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews if (argc > 1)
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews workers = atoi(argv[1]);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson else
e63d63dc8510c669e1575b2762265842e8783822Evan Hunt workers = 2;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson printf("%d workers\n", workers);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt INSIST(isc_task_create(manager, my_shutdown, "1", 0, &t1));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_task_create(manager, my_shutdown, "2", 0, &t2));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_task_create(manager, my_shutdown, "3", 0, &t3));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein INSIST(isc_task_create(manager, my_shutdown, "4", 0, &t4));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence timgr = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ti1 = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson absolute.seconds = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson absolute.nanoseconds = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson interval.seconds = 1;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein interval.nanoseconds = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_timer_create(timgr, isc_timertype_ticker,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson absolute, interval,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson t1, my_tick, "foo", &ti1) == ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ti2 = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(isc_timer_create(timgr, isc_timertype_ticker,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson absolute, interval,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson t2, my_tick, "bar", &ti2) == ISC_R_SUCCESS);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews printf("task 1 = %p\n", t1);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson printf("task 2 = %p\n", t2);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sleep(2);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson event = isc_event_allocate(mctx, main, 1, my_callback, "1",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sizeof *event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_task_send(t1, &event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "1",
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews sizeof *event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_task_send(t1, &event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "1",
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews sizeof *event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_task_send(t1, &event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "1",
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews sizeof *event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_task_send(t1, &event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "1",
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews sizeof *event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_task_send(t1, &event);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "1",
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews sizeof *event);
f621719829356f27e831507b75e88e8a655e48d8Danny Mayer isc_task_send(t1, &event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson event = isc_event_allocate(mctx, main, 1, my_callback, "1",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sizeof *event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_task_send(t1, &event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson event = isc_event_allocate(mctx, main, 1, my_callback, "1",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sizeof *event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_task_send(t1, &event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson event = isc_event_allocate(mctx, main, 1, my_callback, "1",
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sizeof *event);
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt isc_task_send(t1, &event);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson event = isc_event_allocate(mctx, main, 1, my_callback, "2",
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews sizeof *event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_task_send(t2, &event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington event = isc_event_allocate(mctx, main, 1, my_callback, "3",
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews sizeof *event);
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews isc_task_send(t3, &event);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "4",
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews sizeof *event);
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews isc_task_send(t4, &event);
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews event = isc_event_allocate(mctx, main, 1, my_callback, "2",
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews sizeof *event);
f621719829356f27e831507b75e88e8a655e48d8Danny Mayer isc_task_send(t2, &event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington event = isc_event_allocate(mctx, main, 1, my_callback, "3",
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington sizeof *event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_task_send(t3, &event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington event = isc_event_allocate(mctx, main, 1, my_callback, "4",
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington sizeof *event);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_task_send(t4, &event);
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt isc_task_purge(t3, NULL, 0);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews isc_task_detach(&t1);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_task_detach(&t2);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_task_detach(&t3);
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer isc_task_detach(&t4);
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington sleep(10);
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington printf("destroy\n");
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_timer_detach(&ti1);
16bd30ae6987cd4ba4fe3b873e72abf5b7178c26Mark Andrews isc_timer_detach(&ti2);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_timermgr_destroy(&timgr);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_taskmgr_destroy(&manager);
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews printf("destroyed\n");
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington isc_mem_stats(mctx, stdout);
459d31fa838619cee728a90984d27d18345dc18aAndreas Gustafsson isc_memctx_destroy(&mctx);
459d31fa838619cee728a90984d27d18345dc18aAndreas Gustafsson}
c67496c94321dfb68d209019f2b5872a81289c66Michael Sawyer