Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2024 OpenSIPS Solutions |
3 | | * |
4 | | * This file is part of opensips, a free SIP server. |
5 | | * |
6 | | * opensips 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 2 of the License, or |
9 | | * (at your option) any later version |
10 | | * |
11 | | * opensips 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, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include "cond.h" |
22 | | #include "../dprint.h" |
23 | | |
24 | | int cond_init(gen_cond_t *cond) |
25 | 0 | { |
26 | 0 | int ret = -1; |
27 | 0 | pthread_condattr_t cattr; |
28 | 0 | pthread_mutexattr_t mattr; |
29 | |
|
30 | 0 | if (pthread_mutexattr_init(&mattr) != 0) { |
31 | 0 | LM_ERR("could not initialize mutex attributes\n"); |
32 | 0 | return -1; |
33 | 0 | } |
34 | 0 | if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) { |
35 | 0 | LM_ERR("could not mark mutex attribute as shared\n"); |
36 | 0 | goto mutex_error; |
37 | 0 | } |
38 | 0 | if (pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST) != 0) { |
39 | 0 | LM_ERR("could not mark mutex attribute as robust\n"); |
40 | 0 | goto mutex_error; |
41 | 0 | } |
42 | 0 | if (pthread_mutex_init(&cond->m, &mattr) != 0) { |
43 | 0 | LM_ERR("could not initialize mutex\n"); |
44 | 0 | goto mutex_error; |
45 | 0 | } |
46 | 0 | if (pthread_condattr_init(&cattr) != 0) { |
47 | 0 | LM_ERR("could not initialize cond attributes\n"); |
48 | 0 | goto cond_error; |
49 | 0 | } |
50 | 0 | if (pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED) != 0) { |
51 | 0 | LM_ERR("could not mark mutex cond as shared\n"); |
52 | 0 | goto cond_error; |
53 | 0 | } |
54 | 0 | if (pthread_cond_init(&cond->c, &cattr) != 0) { |
55 | 0 | LM_ERR("could not initialize cond\n"); |
56 | 0 | goto cond_error; |
57 | 0 | } |
58 | 0 | return 0; |
59 | 0 | cond_error: |
60 | 0 | pthread_condattr_destroy(&cattr); |
61 | 0 | mutex_error: |
62 | 0 | pthread_mutexattr_destroy(&mattr); |
63 | 0 | return ret; |
64 | 0 | } |
65 | | |
66 | | void cond_destroy(gen_cond_t *cond) |
67 | 0 | { |
68 | 0 | if (pthread_cond_destroy(&cond->c) != 0) |
69 | 0 | LM_ERR("could not destroy condition variable\n"); |
70 | 0 | if (pthread_mutex_destroy(&cond->m) != 0) |
71 | 0 | LM_ERR("could not destroy mutex\n"); |
72 | 0 | } |