Debian Codegraph @

main/serializer_shutdown_group.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012-2013, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19
20 #include "asterisk.h"
21
22 #include "asterisk/serializer_shutdown_group.h"
23 #include "asterisk/astobj2.h"
24 #include "asterisk/time.h"
25 #include "asterisk/utils.h"
26
27 /*! Serializer group shutdown control object. */
28 struct ast_serializer_shutdown_group {
29 /*! Shutdown thread waits on this conditional. */
30 ast_cond_t cond;
31 /*! Count of serializers needing to shutdown. */
32 int count;
33 };
34
35 static void serializer_shutdown_group_dtor(void *vdoomed)
36 {
37 struct ast_serializer_shutdown_group *doomed = vdoomed;
38
39 ast_cond_destroy(&doomed->cond);
40 }
41
42 struct ast_serializer_shutdown_group *ast_serializer_shutdown_group_alloc(void)
43 {
44 struct ast_serializer_shutdown_group *shutdown_group;
45
46 shutdown_group = ao2_alloc(sizeof(*shutdown_group), serializer_shutdown_group_dtor);
47 if (!shutdown_group) {
48 return NULL;
49 }
50 ast_cond_init(&shutdown_group->cond, NULL);
51 return shutdown_group;
52 }
53
54 int ast_serializer_shutdown_group_join(struct ast_serializer_shutdown_group *shutdown_group, int timeout)
55 {
56 int remaining;
57 ast_mutex_t *lock;
58
59 if (!shutdown_group) {
60 return 0;
61 }
62
63 lock = ao2_object_get_lockaddr(shutdown_group);
64 ast_assert(lock != NULL);
65
66 ao2_lock(shutdown_group);
67 if (timeout) {
68 struct timeval start;
69 struct timespec end;
70
71 start = ast_tvnow();
72 end.tv_sec = start.tv_sec + timeout;
73 end.tv_nsec = start.tv_usec * 1000;
74 while (shutdown_group->count) {
75 if (ast_cond_timedwait(&shutdown_group->cond, lock, &end)) {
76 /* Error or timed out waiting for the count to reach zero. */
77 break;
78 }
79 }
80 } else {
81 while (shutdown_group->count) {
82 if (ast_cond_wait(&shutdown_group->cond, lock)) {
83 /* Error */
84 break;
85 }
86 }
87 }
88 remaining = shutdown_group->count;
89 ao2_unlock(shutdown_group);
90 return remaining;
91 }
92
93 void ast_serializer_shutdown_group_inc(struct ast_serializer_shutdown_group *shutdown_group)
94 {
95 ao2_lock(shutdown_group);
96 ++shutdown_group->count;
97 ao2_unlock(shutdown_group);
98 }
99
100 void ast_serializer_shutdown_group_dec(struct ast_serializer_shutdown_group *shutdown_group)
101 {
102 ao2_lock(shutdown_group);
103 --shutdown_group->count;
104 if (!shutdown_group->count) {
105 ast_cond_signal(&shutdown_group->cond);
106 }
107 ao2_unlock(shutdown_group);
108 }