+
+
+//=================================================
+#ifdef TEST_PROGRAM
+
+#define TEST_SLEEP_TIME_IN_SECONDS 3
+#define TEST_MAX_NUM_WORKERS 5
+#define TEST_NUM_WORKS 10
+
+
+void *callback(void *ctx)
+{
+ JCR* jcr = (JCR*)ctx;
+
+ if (jcr)
+ {
+ Jmsg1(jcr, M_INFO, 0, _("workq_test: thread %d : now starting work....\n"), (int)pthread_self());
+ sleep(TEST_SLEEP_TIME_IN_SECONDS);
+ Jmsg1(jcr, M_INFO, 0, _("workq_test: thread %d : ...work completed.\n"), (int)pthread_self());
+ }
+ return NULL;
+}
+
+
+char *configfile = NULL;
+//STORES *me = NULL; /* our Global resource */
+bool forge_on = false; /* proceed inspite of I/O errors */
+pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
+
+int main (int argc, char *argv[])
+{
+ pthread_attr_t attr;
+
+ void * start_heap = sbrk(0);
+ (void)start_heap;
+
+ setlocale(LC_ALL, "");
+ bindtextdomain("bacula", LOCALEDIR);
+ textdomain("bacula");
+ init_stack_dump();
+ my_name_is(argc, argv, "workq_test");
+ init_msg(NULL, NULL);
+ daemon_start_time = time(NULL);
+ set_thread_concurrency(150);
+ lmgr_init_thread(); /* initialize the lockmanager stack */
+ pthread_attr_init(&attr);
+
+ int stat(-1);
+ berrno be;
+
+ workq_t queue;
+ /* Start work queues */
+ if ((stat = workq_init(&queue, TEST_MAX_NUM_WORKERS, callback)) != 0)
+ {
+ be.set_errno(stat);
+ Emsg1(M_ABORT, 0, _("Could not init work queue: ERR=%s\n"), be.bstrerror());
+ }
+
+ /* job1 is created and pseudo-submits some work to the work queue*/
+ JCR *jcr1 = new_jcr(sizeof(JCR), NULL);
+ jcr1->JobId = 1;
+ workq_ele_t * ret(0);
+ for (int w=0; w<TEST_NUM_WORKS; ++w)
+ {
+ if ((stat = workq_add(&queue, jcr1, &ret, 0)) != 0)
+ {
+ be.set_errno(stat);
+ Emsg1(M_ABORT, 0, _("Could not add work to queue: ERR=%s\n"), be.bstrerror());
+ }
+ }
+
+ JCR *jcr2 = new_jcr(sizeof(JCR), NULL);
+ jcr2->JobId = 2;
+ for (int w=0; w<TEST_NUM_WORKS; ++w)
+ {
+ if ((stat = workq_add(&queue, jcr2, &ret, 0)) != 0)
+ {
+ be.set_errno(stat);
+ Emsg1(M_ABORT, 0, _("Could not add work to queue: ERR=%s\n"), be.bstrerror());
+ }
+ }
+
+ printf("--------------------------------------------------------------\n");
+ printf("Start workq_wait_idle ....\n");
+ if ((stat = workq_wait_idle(&queue)) != 0)
+ {
+ be.set_errno(stat);
+ Emsg1(M_ABORT, 0, _("Waiting for workq to be empty: ERR=%s\n"), be.bstrerror());
+ }
+ printf("... workq_wait_idle completed.\n");
+ printf("--------------------------------------------------------------\n");
+
+ printf("Start workq_destroy ....\n");
+ if ((stat = workq_destroy(&queue)) != 0)
+ {
+ be.set_errno(stat);
+ Emsg1(M_ABORT, 0, _("Error in workq_destroy: ERR=%s\n"), be.bstrerror());
+ }
+ printf("... workq_destroy completed.\n");
+
+ return 0;
+
+}
+
+#endif