torture3: Test blocking posix locks
[bbaumbach/samba-autobuild/.git] / source3 / torture / test_pthreadpool_tevent.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Test pthreadpool_tevent
4  * Copyright (C) Volker Lendecke 2016
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "system/select.h"
22 #include "proto.h"
23 #include "lib/pthreadpool/pthreadpool_tevent.h"
24
25 static void job_fn(void *private_data);
26
27 bool run_pthreadpool_tevent(int dummy)
28 {
29         struct tevent_context *ev;
30         struct pthreadpool_tevent *pool;
31         struct tevent_req *req;
32         int ret, val;
33         bool ok;
34
35         ev = tevent_context_init_byname(NULL, "poll");
36         if (ev == NULL) {
37                 fprintf(stderr, "tevent_context_init failed\n");
38                 return false;
39         }
40
41         ret = pthreadpool_tevent_init(ev, 100, &pool);
42         if (ret != 0) {
43                 fprintf(stderr, "pthreadpool_tevent_init failed: %s\n",
44                         strerror(ret));
45                 return false;
46         }
47
48         val = -1;
49
50         req = pthreadpool_tevent_job_send(ev, ev, pool, job_fn, &val);
51         if (req == NULL) {
52                 fprintf(stderr, "pthreadpool_tevent_job_send failed\n");
53                 return false;
54         }
55
56         ok = tevent_req_poll(req, ev);
57         if (!ok) {
58                 fprintf(stderr, "tevent_req_poll failed\n");
59                 return false;
60         }
61
62         ret = pthreadpool_tevent_job_recv(req);
63         if (ret != 0) {
64                 fprintf(stderr, "pthreadpool_tevent_job failed: %s\n",
65                         strerror(ret));
66                 return false;
67         }
68
69         printf("%d\n", val);
70
71         TALLOC_FREE(pool);
72         TALLOC_FREE(ev);
73         return true;
74 }
75
76 static void job_fn(void *private_data)
77 {
78         int *pret = private_data;
79         *pret = 4711;
80
81         poll(NULL, 0, 100);
82 }