/********************************************************************** File: sync_int.c BETA atomic lock and manipulation of integral values via POSIX threads. Copyright (C) 1998-2006 Michael M. Lampkin Contact at michael.lampkinieee.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program 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 version 2 along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 **********************************************************************/ #include "sync_int.h" /** INTERNAL STRUCTURE - DO NOT USE DIRECTLY **/ typedef struct { void * ( * exec_func ) ( void *, int ); void * arg; int val; } async_exec_data__; /** INTERNAL FUNCTION **/ static void * async_exec_start__( void * arg ) { void * ( * local_func ) ( void *, int ); void * local_arg; int local_val; local_func = ( ( async_exec_data__ * ) arg )->exec_func; local_arg = ( ( async_exec_data__ * ) arg )->arg; local_val = ( ( async_exec_data__ * ) arg )->val; free( arg ); local_func( local_arg, local_val ); return NULL; } /** INTERNAL FUNCTION **/ static int async_exec_build__( void * ( * exec_func ) ( void *, int ), void * arg, int val ) { pthread_t thread; async_exec_data__ * thread_data; thread_data = ( async_exec_data__ * ) malloc( sizeof( async_exec_data__ ) ); if ( NULL == thread_data ) { return ENOMEM; } else { thread_data->exec_func = exec_func; thread_data->arg = arg; thread_data->val = val; return pthread_create( & thread, NULL, async_exec_start__, thread_data ); } } int sync_init ( sync_int * atom, int value ) { int result; pthread_mutexattr_t attr; result = pthread_mutexattr_init( & attr ); if ( 0 != result ) { errno = result; return result; } result = pthread_mutexattr_settype( & attr, MUTEX_TYPE__ ); if ( 0 != result ) { errno = result; ( void ) pthread_mutexattr_destroy( & attr ); return result; } result = pthread_mutex_init( & atom->mutex, & attr ); ( void ) pthread_mutexattr_destroy( & attr ); if ( 0 != result ) { errno = result; return result; } atom->value = value; return 0; } int sync_init_default( sync_int * atom ) { return sync_init( atom, 0 ); } int sync_destroy( sync_int * atom ) { int result; result = pthread_mutex_destroy( & atom->mutex ); if ( 0 != result ) { errno = result; return -1; } return 0; } int sync_get ( sync_int * atom ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = atom->value; ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_set ( sync_int * atom, int value ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = atom->value; atom->value = value; ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc ( sync_int * atom ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( ++ atom->value ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec ( sync_int * atom ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( -- atom->value ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value += modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value -= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value *= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value /= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value %= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value &= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value |= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value ^= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value <<= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift ( sync_int * atom, int modifier_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value >>= modifier_rhs ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement ( sync_int * atom ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value = ~atom->value ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate ( sync_int * atom ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = atom->value = ( -atom->value ); ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( ++atom->value ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( --atom->value ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value += modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value -= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value *= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value /= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value %= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value &= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value |= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value ^= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value <<= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value >>= modifier_rhs ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value = ~atom->value ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value = -atom->value ); if ( NULL != exec_func ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc_async_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( ++ atom->value ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec_async_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( -- atom->value ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value += modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value -= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value *= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value /= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value %= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value &= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value |= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value ^= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value <<= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift_async_exec ( sync_int * atom, int modifier_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value >>= modifier_rhs ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement_async_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value = ~atom->value ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate_async_exec ( sync_int * atom, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { result = ( atom->value = -atom->value ); if ( NULL != exec_func ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_cmp ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc_cmp ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { ++ atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec_cmp ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { -- atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value += modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value -= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value *= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value /= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value %= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value &= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value |= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value ^= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value <<= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift_cmp ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value >>= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement_cmp ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ~atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate_cmp ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ( -atom->value ); if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_cmp_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc_cmp_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { ++ atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec_cmp_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { -- atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value += modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value -= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value *= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value /= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value %= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value &= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value |= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value ^= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value <<= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift_cmp_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value >>= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement_cmp_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ~atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate_cmp_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ( -atom->value ); if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { ( void ) exec_func( arg, atom->value ); } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_cmp_async_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_inc_cmp_async_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { ++ atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_dec_cmp_async_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { -- atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_add_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value += modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_sub_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value -= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mul_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value *= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_div_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value /= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_mod_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value %= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_and_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value &= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_or_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value |= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_xor_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value ^= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_lshift_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value <<= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_rshift_cmp_async_exec ( sync_int * atom, int modifier_rhs, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value ^= modifier_rhs; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_complement_cmp_async_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ~atom->value; if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int sync_negate_cmp_async_exec ( sync_int * atom, int ( * compare_func ) ( int, int ), int compare_rhs, void * ( * exec_func ) ( void *, int ), void * arg ) { int result; int s_result; if ( 0 != ( result = pthread_mutex_lock( & atom->mutex ) ) ) { errno = result; } else { atom->value = ( -atom->value ); if ( NULL != compare_func ) { result = compare_func( atom->value, compare_rhs ); } else { result = atom->value - compare_rhs; } if ( NULL != exec_func && 0 == result ) { s_result = async_exec_build__( exec_func, arg, atom->value ); if ( 0 != s_result ) { errno = s_result; } } ( void ) pthread_mutex_unlock( & atom->mutex ); } return result; } int main( ) { return 0; }