mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 02:37:10 +00:00 
			
		
		
		
	This patch addresses compilation errors on OS X. It's been a while, so there's quite a few things. * Fixed __attribute__ decls in route.h to be portable. * Fixed htonll and ntohll to work when they are defined as macros. * Replaced sem_t usage with our ast_sem wrapper. * Added ast_sem_timedwait to our ast_sem wrapper. * Fixed some GCC 4.9 warnings using sig*set() functions. * Fixed some format strings for portability. * Fixed compilation issues with res_timing_kqueue (although tests still fail on OS X). * Fixed menuconfig /sbin/launchd detection, which disables res_timing_kqueue on OS X). ASTERISK-24539 #close Reported by: George Joseph ASTERISK-24544 #close Reported by: George Joseph Review: https://reviewboard.asterisk.org/r/4327/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@431092 65c4cc65-6c06-0410-ace0-fbb531ad65f3
		
			
				
	
	
		
			148 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			148 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * Asterisk -- An open source telephony toolkit.
 | |
|  *
 | |
|  * Copyright (C) 2013, Digium, Inc.
 | |
|  *
 | |
|  * David M. Lee, II <dlee@digium.com>
 | |
|  *
 | |
|  * See http://www.asterisk.org for more information about
 | |
|  * the Asterisk project. Please do not directly contact
 | |
|  * any of the maintainers of this project for assistance;
 | |
|  * the project provides a web site, mailing lists and IRC
 | |
|  * channels for your use.
 | |
|  *
 | |
|  * This program is free software, distributed under the terms of
 | |
|  * the GNU General Public License Version 2. See the LICENSE file
 | |
|  * at the top of the source tree.
 | |
|  */
 | |
| 
 | |
| /*! \file
 | |
|  *
 | |
|  * \brief Asterisk semaphore support.
 | |
|  */
 | |
| 
 | |
| #include "asterisk.h"
 | |
| 
 | |
| ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 | |
| 
 | |
| #include "asterisk/sem.h"
 | |
| #include "asterisk/utils.h"
 | |
| 
 | |
| #ifndef HAS_WORKING_SEMAPHORE
 | |
| 
 | |
| /* DIY semaphores! */
 | |
| 
 | |
| int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
 | |
| {
 | |
| 	if (pshared) {
 | |
| 		/* Don't need it... yet */
 | |
| 		errno = ENOSYS;
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	/* Since value is unsigned, this will also catch attempts to init with
 | |
| 	 * a negative value */
 | |
| 	if (value > AST_SEM_VALUE_MAX) {
 | |
| 		errno = EINVAL;
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	sem->count = value;
 | |
| 	sem->waiters = 0;
 | |
| 	ast_mutex_init(&sem->mutex);
 | |
| 	ast_cond_init(&sem->cond, NULL);
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| int ast_sem_destroy(struct ast_sem *sem)
 | |
| {
 | |
| 	ast_mutex_destroy(&sem->mutex);
 | |
| 	ast_cond_destroy(&sem->cond);
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| int ast_sem_post(struct ast_sem *sem)
 | |
| {
 | |
| 	SCOPED_MUTEX(lock, &sem->mutex);
 | |
| 
 | |
| 	ast_assert(sem->count >= 0);
 | |
| 
 | |
| 	if (sem->count == AST_SEM_VALUE_MAX) {
 | |
| 		errno = EOVERFLOW;
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	/* Give it up! */
 | |
| 	++sem->count;
 | |
| 
 | |
| 	/* Release a waiter, if needed */
 | |
| 	if (sem->waiters) {
 | |
| 		ast_cond_signal(&sem->cond);
 | |
| 	}
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| int ast_sem_wait(struct ast_sem *sem)
 | |
| {
 | |
| 	int res;
 | |
| 	SCOPED_MUTEX(lock, &sem->mutex);
 | |
| 
 | |
| 	ast_assert(sem->count >= 0);
 | |
| 
 | |
| 	/* Wait for a non-zero count */
 | |
| 	++sem->waiters;
 | |
| 	while (sem->count == 0) {
 | |
| 		res = ast_cond_wait(&sem->cond, &sem->mutex);
 | |
| 		/* Give up on error */
 | |
| 		if (res != 0) {
 | |
| 			--sem->waiters;
 | |
| 			return res;
 | |
| 		}
 | |
| 	}
 | |
| 	--sem->waiters;
 | |
| 
 | |
| 	/* Take it! */
 | |
| 	--sem->count;
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| int ast_sem_timedwait(struct ast_sem *sem, const struct timespec *abs_timeout)
 | |
| {
 | |
| 	int res;
 | |
| 	SCOPED_MUTEX(lock, &sem->mutex);
 | |
| 
 | |
| 	ast_assert(sem->count >= 0);
 | |
| 
 | |
| 	/* Wait for a non-zero count */
 | |
| 	++sem->waiters;
 | |
| 	while (sem->count == 0) {
 | |
| 		res = ast_cond_timedwait(&sem->cond, &sem->mutex, abs_timeout);
 | |
| 		/* Give up on error */
 | |
| 		if (res != 0) {
 | |
| 			--sem->waiters;
 | |
| 			return res;
 | |
| 		}
 | |
| 	}
 | |
| 	--sem->waiters;
 | |
| 
 | |
| 	/* Take it! */
 | |
| 	--sem->count;
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| int ast_sem_getvalue(struct ast_sem *sem, int *sval)
 | |
| {
 | |
| 	SCOPED_MUTEX(lock, &sem->mutex);
 | |
| 
 | |
| 	ast_assert(sem->count >= 0);
 | |
| 
 | |
| 	*sval = sem->count;
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| #endif
 |