-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add pthread cond implementation
- Loading branch information
1 parent
dcc6bf0
commit 18ca293
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2024 OpenSIPS Solutions | ||
* | ||
* This file is part of opensips, a free SIP server. | ||
* | ||
* opensips is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version | ||
* | ||
* opensips is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "cond.h" | ||
#include "../dprint.h" | ||
|
||
int cond_init(gen_cond_t *cond) | ||
{ | ||
int ret = -1; | ||
pthread_condattr_t cattr; | ||
pthread_mutexattr_t mattr; | ||
|
||
if (pthread_mutexattr_init(&mattr) != 0) { | ||
LM_ERR("could not initialize mutex attributes\n"); | ||
return -1; | ||
} | ||
if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) { | ||
LM_ERR("could not mark mutex attribute as shared\n"); | ||
goto mutex_error; | ||
} | ||
if (pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST) != 0) { | ||
LM_ERR("could not mark mutex attribute as robust\n"); | ||
goto mutex_error; | ||
} | ||
if (pthread_mutex_init(&cond->m, &mattr) != 0) { | ||
LM_ERR("could not initialize mutex\n"); | ||
goto mutex_error; | ||
} | ||
if (pthread_condattr_init(&cattr) != 0) { | ||
LM_ERR("could not initialize cond attributes\n"); | ||
goto cond_error; | ||
} | ||
if (pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED) != 0) { | ||
LM_ERR("could not mark mutex cond as shared\n"); | ||
goto cond_error; | ||
} | ||
if (pthread_cond_init(&cond->c, &cattr) != 0) { | ||
LM_ERR("could not initialize cond\n"); | ||
goto cond_error; | ||
} | ||
return 0; | ||
cond_error: | ||
pthread_condattr_destroy(&cattr); | ||
mutex_error: | ||
pthread_mutexattr_destroy(&mattr); | ||
return ret; | ||
} | ||
|
||
void cond_destroy(gen_cond_t *cond) | ||
{ | ||
pthread_cond_destroy(&cond->c); | ||
pthread_mutex_destroy(&cond->m); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (C) 2024 OpenSIPS Solutions | ||
* | ||
* This file is part of opensips, a free SIP server. | ||
* | ||
* opensips is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version | ||
* | ||
* opensips is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef __OSIPS_COND__ | ||
#define __OSIPS_COND__ | ||
|
||
#include <pthread.h> | ||
|
||
typedef struct gen_cond { | ||
pthread_mutex_t m; | ||
pthread_cond_t c; | ||
} gen_cond_t; | ||
|
||
/* initializes a condition allocated in shared memory */ | ||
int cond_init(gen_cond_t *cond); | ||
|
||
/* destroyes a condition */ | ||
void cond_destroy(gen_cond_t *cond); | ||
|
||
#define cond_lock(_c) pthread_mutex_lock(&(_c)->m) | ||
#define cond_unlock(_c) pthread_mutex_unlock(&(_c)->m) | ||
#define cond_wait(_c) pthread_cond_wait(&(_c)->c, &(_c)->m) | ||
/* make sure we reset the errno, to avoid confusion when resumed */ | ||
#define cond_timedwait(_c, _ts) \ | ||
do { \ | ||
errno = 0; \ | ||
pthread_cond_timedwait(&(_c)->c, &(_c)->m, (_ts)); \ | ||
} while (0) | ||
#define cond_has_timedout(_c) (errno == ETIMEDOUT || errno == EAGAIN)/* TODO do we need to store this during wait? */ | ||
#define cond_signal(_c) pthread_cond_signal(&(_c)->c) | ||
#define cond_broadcast(_c) pthread_cond_broadcast(&(_c)->c) | ||
|
||
#endif /* __OSIPS_COND__ */ |