1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include <toku_portability.h>
40 #include "toku_os.h"
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <toku_assert.h>
45 #include <toku_pthread.h>
46 #include "util/queue.h"
47 
48 static int verbose=1;
49 
50 static int count_0 = 0;
51 static uint64_t e_max_weight=0, d_max_weight = 0; // max weight seen by enqueue thread and dequeue thread respectively.
52 
start_0(void * arg)53 static void *start_0 (void *arg) {
54     QUEUE q = (QUEUE)arg;
55     void *item;
56     uint64_t weight;
57     long count = 0;
58     while (1) {
59 	uint64_t this_max_weight;
60 	int r=toku_queue_deq(q, &item, &weight, &this_max_weight);
61 	if (r==EOF) break;
62 	assert(r==0);
63 	if (this_max_weight>d_max_weight) d_max_weight=this_max_weight;
64 	long v = (long)item;
65 	//printf("D(%ld)=%ld %ld\n", v, this_max_weight, d_max_weight);
66 	assert(v==count);
67 	count_0++;
68 	count++;
69     }
70     return NULL;
71 }
72 
enq(QUEUE q,long v,uint64_t weight)73 static void enq (QUEUE q, long v, uint64_t weight) {
74     uint64_t this_max_weight;
75     int r = toku_queue_enq(q, (void*)v, (weight==0)?0:1, &this_max_weight);
76     assert(r==0);
77     if (this_max_weight>e_max_weight) e_max_weight=this_max_weight;
78     //printf("E(%ld)=%ld %ld\n", v, this_max_weight, e_max_weight);
79 }
80 
queue_test_0(uint64_t weight)81 static void queue_test_0 (uint64_t weight)
82 // Test a queue that can hold WEIGHT items.
83 {
84     //printf("\n");
85     count_0 = 0;
86     e_max_weight = 0;
87     d_max_weight = 0;
88     QUEUE q;
89     int r;
90     r = toku_queue_create(&q, weight);
91     assert(r == 0);
92     toku_pthread_t thread;
93     r = toku_pthread_create(toku_uninstrumented, &thread, nullptr, start_0, q);
94     assert(r == 0);
95     enq(q, 0L, weight);
96     enq(q, 1L, weight);
97     enq(q, 2L, weight);
98     enq(q, 3L, weight);
99     sleep(1);
100     enq(q, 4L, weight);
101     enq(q, 5L, weight);
102     r = toku_queue_eof(q);                                      assert(r==0);
103     void *result;
104     r = toku_pthread_join(thread, &result);	           assert(r==0);
105     assert(result==NULL);
106     assert(count_0==6);
107     r = toku_queue_destroy(q);
108     assert(d_max_weight <= weight);
109     assert(e_max_weight <= weight);
110 }
111 
112 
parse_args(int argc,const char * argv[])113 static void parse_args (int argc, const char *argv[]) {
114     const char *progname=argv[0];
115     argc--; argv++;
116     while (argc>0) {
117 	if (strcmp(argv[0],"-v")==0) {
118 	    verbose++;
119 	} else if (strcmp(argv[0],"-q")==0) {
120 	    verbose--;
121 	} else {
122 	    fprintf(stderr, "Usage:\n %s [-v] [-q]\n", progname);
123 	    exit(1);
124 	}
125 	argc--; argv++;
126     }
127     if (verbose<0) verbose=0;
128 }
129 
main(int argc,const char * argv[])130 int main (int argc, const char *argv[]) {
131     parse_args(argc, argv);
132     queue_test_0(0LL);
133     queue_test_0(1LL);
134     queue_test_0(2LL);
135     return 0;
136 }
137