-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
143 lines (120 loc) · 3.51 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* main.c
*
* Copyright (c) 2009 Christian Hergert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <glib.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#else
#ifdef __APPLE__
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#error "Building on platforms other than Linux or OSX is not yet supported."
#endif /* __APPLE__ */
#endif /* __linux__ */
#include "lf-queue.h"
static gint
get_num_cpu(void)
{
#ifdef __linux__
return get_nprocs();
#endif
#ifdef __APPLE__
gint i = 0;
size_t s = sizeof(i);
if (sysctlbyname("hw.ncpu", &i, &s, NULL, 0))
return 1;
return i;
#endif
}
static void
test_LfQueue_basic(void)
{
LfQueue *q;
q = lf_queue_new();
g_assert(q);
lf_queue_enqueue(q, "String 1");
lf_queue_enqueue(q, "String 2");
lf_queue_enqueue(q, "String 3");
lf_queue_enqueue(q, "String 4");
g_assert_cmpstr(lf_queue_dequeue(q), ==, "String 1");
g_assert_cmpstr(lf_queue_dequeue(q), ==, "String 2");
g_assert_cmpstr(lf_queue_dequeue(q), ==, "String 3");
g_assert_cmpstr(lf_queue_dequeue(q), ==, "String 4");
g_assert(!lf_queue_dequeue(q));
}
static gpointer
test_LfQueue_threaded_alternate_enq_deq_thread_func(gpointer data)
{
LfQueue *q = data;
gint i, n;
g_assert(q);
n = g_test_perf() ? 10000000 : 1000000;
for (i = 1; i <= n; i++) {
if (i % 2 == 1) {
lf_queue_enqueue(q, GINT_TO_POINTER(i));
} else {
g_assert(lf_queue_dequeue(q));
}
}
return NULL;
}
/*
* This test will spawn (N_CPU * 2) threads and have them all work on adding
* and removing many items concurrently. Each thread will alternate between
* adding an item and removing an item such that the typical potential for
* an ABA[1] problem is increased. The hazard pointers should always prevent
* the ABA however.
*
* [1] http://en.wikipedia.org/wiki/ABA_problem
*/
static void
test_LfQueue_threaded_alternate_enq_deq(void)
{
gint n_threads = get_num_cpu() * 2;
LfQueue *q;
GThread **threads;
gint i;
threads = g_malloc(sizeof(gpointer) * n_threads);
q = lf_queue_new();
g_assert(q);
for (i = 0; i < n_threads; i++) {
threads[i] = g_thread_create(
test_LfQueue_threaded_alternate_enq_deq_thread_func,
q, TRUE, NULL);
}
for (i = 0; i < n_threads; i++) {
g_thread_join(threads[i]);
}
g_free(threads);
}
gint
main(gint argc,
gchar *argv[])
{
g_test_init(&argc, &argv, NULL);
g_thread_init(NULL);
g_test_add_func("/LfQueue/basic", test_LfQueue_basic);
g_test_add_func("/LfQueue/threaded_alternate_enq_deq",
test_LfQueue_threaded_alternate_enq_deq);
return g_test_run();
}