void library_unload( object_t id ) { library_t* library = objectmap_lookup( _library_map, id ); if( library ) { if( atomic_decr32( &library->ref ) == 0 ) _library_destroy( library ); } }
static void* dec_thread(void* arg) { int loop = 0; int icount = 0; FOUNDATION_UNUSED(arg); while (!thread_try_wait(0) && (loop < 65535)) { for (icount = 0; icount < 256; ++icount) { atomic_decr32(&val_32); atomic_decr64(&val_64); } ++loop; thread_yield(); } return 0; }
void* dec_thread( object_t thread, void* arg ) { int loop = 0; int icount = 0; while( !thread_should_terminate( thread ) && ( loop < 65535 ) ) { for( icount = 0; icount < 256; ++icount ) { atomic_decr32( &val_32 ); atomic_decr64( &val_64 ); } ++loop; thread_yield(); } return 0; }
void render_buffer_unlock(object_t id) { render_buffer_t* buffer = GET_BUFFER(id); if (!atomic_load32(&buffer->locks)) return; if (atomic_decr32(&buffer->locks) == 0) { buffer->access = nullptr; if ((buffer->flags & RENDERBUFFER_LOCK_WRITE) && !(buffer->flags & RENDERBUFFER_LOCK_NOUPLOAD)) { buffer->flags |= RENDERBUFFER_DIRTY; if ((buffer->policy == RENDERBUFFER_UPLOAD_ONUNLOCK) || (buffer->flags & RENDERBUFFER_LOCK_FORCEUPLOAD)) render_buffer_upload(buffer); } buffer->flags &= ~RENDERBUFFER_LOCK_BITS; } render_buffer_destroy(id); }
bool mutex_try_wait(mutex_t* mutex, unsigned int milliseconds) { #if FOUNDATION_PLATFORM_WINDOWS DWORD ret; #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL struct timeval now; struct timespec then; #endif #if FOUNDATION_PLATFORM_WINDOWS #if !BUILD_DEPLOY profile_wait(STRING_ARGS(mutex->name)); #endif atomic_incr32(&mutex->waiting); ret = WaitForSingleObject(mutex->event, milliseconds); if (ret == WAIT_OBJECT_0) mutex_lock(mutex); if (atomic_decr32(&mutex->waiting) == 0) ResetEvent(mutex->event); return ret == WAIT_OBJECT_0; #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL mutex_lock(mutex); if (mutex->pending) { mutex->pending = false; return true; } if (!milliseconds) { mutex_unlock(mutex); return false; } --mutex->lockcount; bool was_signal = false; if (milliseconds == 0xFFFFFFFF) { int ret = pthread_cond_wait(&mutex->cond, &mutex->mutex); if (ret == 0) { was_signal = true; } else { string_const_t errmsg = system_error_message(ret); log_errorf(0, ERROR_SYSTEM_CALL_FAIL, STRING_CONST("Unable to wait on mutex '%.*s': %.*s (%d)"), STRING_FORMAT(mutex->name), STRING_FORMAT(errmsg), ret); } } else { int ret; gettimeofday(&now, 0); then.tv_sec = now.tv_sec + (time_t)(milliseconds / 1000); then.tv_nsec = (now.tv_usec * 1000) + (long)(milliseconds % 1000) * 1000000L; while (then.tv_nsec >= 1000000000L) { ++then.tv_sec; then.tv_nsec -= 1000000000L; } ret = pthread_cond_timedwait(&mutex->cond, &mutex->mutex, &then); if (ret == 0) { was_signal = true; } else if (ret != ETIMEDOUT) { string_const_t errmsg = system_error_message(ret); log_errorf(0, ERROR_SYSTEM_CALL_FAIL, STRING_CONST("Unable to wait (timed) on mutex '%.*s': %.*s (%d)"), STRING_FORMAT(mutex->name), STRING_FORMAT(errmsg), ret); } } ++mutex->lockcount; mutex->lockedthread = thread_id(); if (was_signal) mutex->pending = false; else mutex_unlock(mutex); return was_signal; #else # error mutex_wait not implemented #endif }
bool mutex_wait( mutex_t* mutex, unsigned int timeout ) { #if FOUNDATION_PLATFORM_WINDOWS DWORD ret; #elif FOUNDATION_PLATFORM_POSIX struct timeval now; struct timespec then; #endif FOUNDATION_ASSERT( mutex ); #if FOUNDATION_PLATFORM_WINDOWS #if !BUILD_DEPLOY profile_wait( mutex->name ); #endif atomic_incr32( &mutex->waiting ); ret = WaitForSingleObject( mutex->event, ( timeout == 0 ) ? INFINITE : timeout ); if( ret == WAIT_OBJECT_0 ) mutex_lock( mutex ); if( atomic_decr32( &mutex->waiting ) == 0 ) ResetEvent( mutex->event ); return ret == WAIT_OBJECT_0; #elif FOUNDATION_PLATFORM_POSIX mutex_lock( mutex ); if( mutex->pending ) { mutex->pending = false; return true; } --mutex->lockcount; bool was_signal = false; if( !timeout ) { int ret = pthread_cond_wait( &mutex->cond, &mutex->mutex ); if( ret == 0 ) { was_signal = true; } else { log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to wait on mutex '%s': %s (%d)", mutex->name, system_error_message( ret ), ret ); } } else { int ret; gettimeofday( &now, 0 ); then.tv_sec = now.tv_sec + ( timeout / 1000 ); then.tv_nsec = ( now.tv_usec * 1000 ) + (long)( timeout % 1000 ) * 1000000L; while( then.tv_nsec > 999999999 ) { ++then.tv_sec; then.tv_nsec -= 1000000000L; } ret = pthread_cond_timedwait( &mutex->cond, &mutex->mutex, &then ); if( ret == 0 ) { was_signal = true; } else if( ret != ETIMEDOUT ) { log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to wait (timed) on mutex '%s': %s (%d)", mutex->name, system_error_message( ret ), ret ); } } ++mutex->lockcount; mutex->lockedthread = thread_id(); if( was_signal ) mutex->pending = false; else mutex_unlock( mutex ); return was_signal; #else # error mutex_wait not implemented #endif }