diff --git a/src/midi/fluid_seq.c b/src/midi/fluid_seq.c index 0d6d0f95..a3b96191 100644 --- a/src/midi/fluid_seq.c +++ b/src/midi/fluid_seq.c @@ -42,7 +42,7 @@ /* Private data for SEQUENCER */ struct _fluid_sequencer_t { unsigned int startMs; - int currentMs; + fluid_atomic_int_t currentMs; int useSystemTimer; double scale; // ticks per second fluid_list_t* clients; diff --git a/src/rvoice/fluid_rvoice_event.c b/src/rvoice/fluid_rvoice_event.c index 51d5a80f..a0f8147c 100644 --- a/src/rvoice/fluid_rvoice_event.c +++ b/src/rvoice/fluid_rvoice_event.c @@ -229,7 +229,7 @@ new_fluid_rvoice_eventhandler(int is_threadsafe, int queuesize, * that too many events are dispatched too early, causing incorrectly timed audio */ eventhandler->is_threadsafe = TRUE; - eventhandler->queue_stored = 0; + fluid_atomic_int_set(&eventhandler->queue_stored, 0); eventhandler->finished_voices = new_fluid_ringbuffer(finished_voices_size, sizeof(fluid_rvoice_t*)); diff --git a/src/rvoice/fluid_rvoice_event.h b/src/rvoice/fluid_rvoice_event.h index c2bb6696..19bd7c5b 100644 --- a/src/rvoice/fluid_rvoice_event.h +++ b/src/rvoice/fluid_rvoice_event.h @@ -50,7 +50,7 @@ void fluid_rvoice_event_dispatch(fluid_rvoice_event_t* event); struct _fluid_rvoice_eventhandler_t { int is_threadsafe; /* False for optimal performance, true for atomic operations */ fluid_ringbuffer_t* queue; /**< List of fluid_rvoice_event_t */ - int queue_stored; /**< Extras pushed but not flushed */ + fluid_atomic_int_t queue_stored; /**< Extras pushed but not flushed */ fluid_ringbuffer_t* finished_voices; /**< return queue from handler, list of fluid_rvoice_t* */ fluid_rvoice_mixer_t* mixer; }; diff --git a/src/rvoice/fluid_rvoice_mixer.c b/src/rvoice/fluid_rvoice_mixer.c index 8ed5f1c9..6668a722 100644 --- a/src/rvoice/fluid_rvoice_mixer.c +++ b/src/rvoice/fluid_rvoice_mixer.c @@ -45,7 +45,7 @@ struct _fluid_mixer_buffers_t { fluid_rvoice_t** finished_voices; /* List of voices who have finished */ int finished_voice_count; - int ready; /**< Atomic: buffers are ready for mixing */ + fluid_atomic_int_t ready; /**< Atomic: buffers are ready for mixing */ int buf_blocks; /**< Number of blocks allocated in the buffers */ @@ -87,8 +87,8 @@ struct _fluid_rvoice_mixer_t { #ifdef ENABLE_MIXER_THREADS // int sleeping_threads; /**< Atomic: number of threads currently asleep */ // int active_threads; /**< Atomic: number of threads in the thread loop */ - int threads_should_terminate; /**< Atomic: Set to TRUE when threads should terminate */ - int current_rvoice; /**< Atomic: for the threads to know next voice to */ + fluid_atomic_int_t threads_should_terminate; /**< Atomic: Set to TRUE when threads should terminate */ + fluid_atomic_int_t current_rvoice; /**< Atomic: for the threads to know next voice to */ fluid_cond_t* wakeup_threads; /**< Signalled when the threads should wake up */ fluid_cond_mutex_t* wakeup_threads_m; /**< wakeup_threads mutex companion */ fluid_cond_t* thread_ready; /**< Signalled from thread, when the thread has a buffer ready for mixing */ diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index 2273d2ae..3badba7d 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -439,18 +439,12 @@ fluid_synth_init(void) static FLUID_INLINE unsigned int fluid_synth_get_ticks(fluid_synth_t* synth) { - if (synth->eventhandler->is_threadsafe) - return fluid_atomic_int_get(&synth->ticks_since_start); - else - return synth->ticks_since_start; + return fluid_atomic_int_get(&synth->ticks_since_start); } static FLUID_INLINE void fluid_synth_add_ticks(fluid_synth_t* synth, int val) { - if (synth->eventhandler->is_threadsafe) - fluid_atomic_int_add((int*) &synth->ticks_since_start, val); - else - synth->ticks_since_start += val; + fluid_atomic_int_add(&synth->ticks_since_start, val); } @@ -555,6 +549,8 @@ new_fluid_synth(fluid_settings_t *settings) double gain; int i, nbuf; int with_ladspa = 0; + int with_reverb; + int with_chorus; /* initialize all the conversion tables and other stuff */ if (fluid_synth_initialized == 0) @@ -595,8 +591,10 @@ new_fluid_synth(fluid_settings_t *settings) synth->settings = settings; - fluid_settings_getint(settings, "synth.reverb.active", &synth->with_reverb); - fluid_settings_getint(settings, "synth.chorus.active", &synth->with_chorus); + fluid_settings_getint(settings, "synth.reverb.active", &with_reverb); + fluid_atomic_int_set(&synth->with_reverb, with_reverb); + fluid_settings_getint(settings, "synth.chorus.active", &with_chorus); + fluid_atomic_int_set(&synth->with_chorus, with_chorus); fluid_settings_getint(settings, "synth.verbose", &synth->verbose); fluid_settings_getint(settings, "synth.polyphony", &synth->polyphony); @@ -678,7 +676,7 @@ new_fluid_synth(fluid_settings_t *settings) synth->sfont_info = NULL; synth->sfont_hash = new_fluid_hashtable (NULL, NULL); synth->noteid = 0; - synth->ticks_since_start = 0; + fluid_atomic_int_set(&synth->ticks_since_start, 0); synth->tuning = NULL; fluid_private_init(synth->tuning_iter); @@ -762,24 +760,24 @@ new_fluid_synth(fluid_settings_t *settings) fluid_synth_update_overflow(synth, "", 0.0f); fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_polyphony, synth->polyphony, 0.0f); - fluid_synth_set_reverb_on(synth, synth->with_reverb); - fluid_synth_set_chorus_on(synth, synth->with_chorus); + fluid_synth_set_reverb_on(synth, fluid_atomic_int_get(&synth->with_reverb)); + fluid_synth_set_chorus_on(synth, fluid_atomic_int_get(&synth->with_chorus)); synth->cur = FLUID_BUFSIZE; synth->curmax = 0; synth->dither_index = 0; - synth->reverb_roomsize = FLUID_REVERB_DEFAULT_ROOMSIZE; - synth->reverb_damping = FLUID_REVERB_DEFAULT_DAMP; - synth->reverb_width = FLUID_REVERB_DEFAULT_WIDTH; - synth->reverb_level = FLUID_REVERB_DEFAULT_LEVEL; + fluid_atomic_float_set(&synth->reverb_roomsize, FLUID_REVERB_DEFAULT_ROOMSIZE); + fluid_atomic_float_set(&synth->reverb_damping, FLUID_REVERB_DEFAULT_DAMP); + fluid_atomic_float_set(&synth->reverb_width, FLUID_REVERB_DEFAULT_WIDTH); + fluid_atomic_float_set(&synth->reverb_level, FLUID_REVERB_DEFAULT_LEVEL); fluid_rvoice_eventhandler_push5(synth->eventhandler, fluid_rvoice_mixer_set_reverb_params, synth->eventhandler->mixer, - FLUID_REVMODEL_SET_ALL, synth->reverb_roomsize, - synth->reverb_damping, synth->reverb_width, - synth->reverb_level, 0.0f); + FLUID_REVMODEL_SET_ALL, fluid_atomic_float_get(&synth->reverb_roomsize), + fluid_atomic_float_get(&synth->reverb_damping), fluid_atomic_float_get(&synth->reverb_width), + fluid_atomic_float_get(&synth->reverb_level), 0.0f); /* Initialize multi-core variables if multiple cores enabled */ if (synth->cores > 1) @@ -2748,7 +2746,7 @@ fluid_synth_nwrite_float(fluid_synth_t* synth, int len, synth->cur = num; time = fluid_utime() - time; - cpu_load = 0.5 * (synth->cpu_load + time * synth->sample_rate / len / 10000.0); + cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0); fluid_atomic_float_set (&synth->cpu_load, cpu_load); if (!synth->eventhandler->is_threadsafe) @@ -2859,7 +2857,7 @@ fluid_synth_write_float(fluid_synth_t* synth, int len, synth->cur = l; time = fluid_utime() - time; - cpu_load = 0.5 * (synth->cpu_load + time * synth->sample_rate / len / 10000.0); + cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0); fluid_atomic_float_set (&synth->cpu_load, cpu_load); if (!synth->eventhandler->is_threadsafe) @@ -2984,7 +2982,7 @@ fluid_synth_write_s16(fluid_synth_t* synth, int len, fluid_profile(FLUID_PROF_WRITE, prof_ref); time = fluid_utime() - time; - cpu_load = 0.5 * (synth->cpu_load + time * synth->sample_rate / len / 10000.0); + cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0); fluid_atomic_float_set (&synth->cpu_load, cpu_load); if (!synth->eventhandler->is_threadsafe) diff --git a/src/synth/fluid_synth.h b/src/synth/fluid_synth.h index 0557675b..4ab0a2d7 100644 --- a/src/synth/fluid_synth.h +++ b/src/synth/fluid_synth.h @@ -113,8 +113,8 @@ struct _fluid_synth_t fluid_settings_t* settings; /**< the synthesizer settings */ int device_id; /**< Device ID used for SYSEX messages */ int polyphony; /**< Maximum polyphony */ - int with_reverb; /**< Should the synth use the built-in reverb unit? */ - int with_chorus; /**< Should the synth use the built-in chorus unit? */ + fluid_atomic_int_t with_reverb; /**< Should the synth use the built-in reverb unit? */ + fluid_atomic_int_t with_chorus; /**< Should the synth use the built-in chorus unit? */ int verbose; /**< Turn verbose mode on? */ double sample_rate; /**< The sample rate */ int midi_channels; /**< the number of MIDI channels (>= 16) */ @@ -124,7 +124,7 @@ struct _fluid_synth_t Typically equal to audio_channels. */ int effects_channels; /**< the number of effects channels (>= 2) */ int state; /**< the synthesizer state */ - unsigned int ticks_since_start; /**< the number of audio samples since the start */ + fluid_atomic_uint_t ticks_since_start; /**< the number of audio samples since the start */ unsigned int start; /**< the start in msec, as returned by system clock */ fluid_overflow_prio_t overflow; /**< parameters for overflow priority (aka voice-stealing) */ @@ -142,22 +142,22 @@ struct _fluid_synth_t unsigned int storeid; fluid_rvoice_eventhandler_t* eventhandler; - float reverb_roomsize; /**< Shadow of reverb roomsize */ - float reverb_damping; /**< Shadow of reverb damping */ - float reverb_width; /**< Shadow of reverb width */ - float reverb_level; /**< Shadow of reverb level */ + fluid_atomic_float_t reverb_roomsize; /**< Shadow of reverb roomsize */ + fluid_atomic_float_t reverb_damping; /**< Shadow of reverb damping */ + fluid_atomic_float_t reverb_width; /**< Shadow of reverb width */ + fluid_atomic_float_t reverb_level; /**< Shadow of reverb level */ - int chorus_nr; /**< Shadow of chorus number */ - float chorus_level; /**< Shadow of chorus level */ - float chorus_speed; /**< Shadow of chorus speed */ - float chorus_depth; /**< Shadow of chorus depth */ - int chorus_type; /**< Shadow of chorus type */ + fluid_atomic_int_t chorus_nr; /**< Shadow of chorus number */ + fluid_atomic_float_t chorus_level; /**< Shadow of chorus level */ + fluid_atomic_float_t chorus_speed; /**< Shadow of chorus speed */ + fluid_atomic_float_t chorus_depth; /**< Shadow of chorus depth */ + fluid_atomic_int_t chorus_type; /**< Shadow of chorus type */ int cur; /**< the current sample in the audio buffers to be output */ int curmax; /**< current amount of samples present in the audio buffers */ int dither_index; /**< current index in random dither value buffer: fluid_synth_(write_s16|dither_s16) */ - float cpu_load; /**< CPU load in percent (CPU time required / audio synthesized time * 100) */ + fluid_atomic_float_t cpu_load; /**< CPU load in percent (CPU time required / audio synthesized time * 100) */ fluid_tuning_t*** tuning; /**< 128 banks of 128 programs for the tunings */ fluid_private_t tuning_iter; /**< Tuning iterators per each thread */ diff --git a/src/synth/fluid_tuning.c b/src/synth/fluid_tuning.c index cf7efe2b..6783c7b7 100644 --- a/src/synth/fluid_tuning.c +++ b/src/synth/fluid_tuning.c @@ -48,7 +48,7 @@ fluid_tuning_t* new_fluid_tuning(const char* name, int bank, int prog) tuning->pitch[i] = i * 100.0; } - tuning->refcount = 1; /* Start with a refcount of 1 */ + fluid_atomic_int_set(&tuning->refcount, 1); /* Start with a refcount of 1 */ return tuning; } @@ -86,7 +86,7 @@ fluid_tuning_duplicate (fluid_tuning_t *tuning) for (i = 0; i < 128; i++) new_tuning->pitch[i] = tuning->pitch[i]; - new_tuning->refcount = 1; /* Start with a refcount of 1 */ + fluid_atomic_int_set(&new_tuning->refcount, 1); /* Start with a refcount of 1 */ return new_tuning; } diff --git a/src/synth/fluid_tuning.h b/src/synth/fluid_tuning.h index 2380def0..39d5ed8b 100644 --- a/src/synth/fluid_tuning.h +++ b/src/synth/fluid_tuning.h @@ -39,7 +39,7 @@ struct _fluid_tuning_t { int bank; int prog; double pitch[128]; /* the pitch of every key, in cents */ - int refcount; /* Tuning reference count */ + fluid_atomic_int_t refcount; /* Tuning reference count */ }; fluid_tuning_t* new_fluid_tuning(const char* name, int bank, int prog); diff --git a/src/utils/fluid_hash.c b/src/utils/fluid_hash.c index e0c1cd7c..87d16417 100644 --- a/src/utils/fluid_hash.c +++ b/src/utils/fluid_hash.c @@ -389,7 +389,7 @@ new_fluid_hashtable_full (fluid_hash_func_t hash_func, hashtable->nnodes = 0; hashtable->hash_func = hash_func ? hash_func : fluid_direct_hash; hashtable->key_equal_func = key_equal_func; - hashtable->ref_count = 1; + fluid_atomic_int_set(&hashtable->ref_count, 1); hashtable->key_destroy_func = key_destroy_func; hashtable->value_destroy_func = value_destroy_func; hashtable->nodes = FLUID_ARRAY (fluid_hashnode_t*, hashtable->size); @@ -616,7 +616,7 @@ fluid_hashtable_t* fluid_hashtable_ref (fluid_hashtable_t *hashtable) { fluid_return_val_if_fail (hashtable != NULL, NULL); - fluid_return_val_if_fail (hashtable->ref_count > 0, hashtable); + fluid_return_val_if_fail (fluid_atomic_int_get(&hashtable->ref_count) > 0, hashtable); fluid_atomic_int_add (&hashtable->ref_count, 1); return hashtable; @@ -637,7 +637,7 @@ void fluid_hashtable_unref (fluid_hashtable_t *hashtable) { fluid_return_if_fail (hashtable != NULL); - fluid_return_if_fail (hashtable->ref_count > 0); + fluid_return_if_fail (fluid_atomic_int_get(&hashtable->ref_count) > 0); if (fluid_atomic_int_exchange_and_add (&hashtable->ref_count, -1) - 1 == 0) { @@ -662,7 +662,7 @@ void delete_fluid_hashtable (fluid_hashtable_t *hashtable) { fluid_return_if_fail (hashtable != NULL); - fluid_return_if_fail (hashtable->ref_count > 0); + fluid_return_if_fail (fluid_atomic_int_get(&hashtable->ref_count) > 0); fluid_hashtable_remove_all (hashtable); fluid_hashtable_unref (hashtable); @@ -753,7 +753,7 @@ fluid_hashtable_insert_internal (fluid_hashtable_t *hashtable, void *key, unsigned int key_hash; fluid_return_if_fail (hashtable != NULL); - fluid_return_if_fail (hashtable->ref_count > 0); + fluid_return_if_fail (fluid_atomic_int_get(&hashtable->ref_count) > 0); node_ptr = fluid_hashtable_lookup_node (hashtable, key, &key_hash); diff --git a/src/utils/fluid_hash.h b/src/utils/fluid_hash.h index 3beff060..4fd398cf 100644 --- a/src/utils/fluid_hash.h +++ b/src/utils/fluid_hash.h @@ -65,7 +65,7 @@ struct _fluid_hashtable_t fluid_hashnode_t **nodes; fluid_hash_func_t hash_func; fluid_equal_func_t key_equal_func; - volatile int ref_count; + fluid_atomic_int_t ref_count; fluid_destroy_notify_t key_destroy_func; fluid_destroy_notify_t value_destroy_func; fluid_rec_mutex_t mutex; // Optionally used in other modules (fluid_settings.c for example) diff --git a/src/utils/fluid_ringbuffer.c b/src/utils/fluid_ringbuffer.c index fb65a520..e276c04e 100644 --- a/src/utils/fluid_ringbuffer.c +++ b/src/utils/fluid_ringbuffer.c @@ -67,7 +67,7 @@ new_fluid_ringbuffer (int count, int elementsize) queue->totalcount = count; queue->elementsize = elementsize; - queue->count = 0; + fluid_atomic_int_set(&queue->count, 0); queue->in = 0; queue->out = 0; diff --git a/src/utils/fluid_ringbuffer.h b/src/utils/fluid_ringbuffer.h index 417807e2..eff598e4 100644 --- a/src/utils/fluid_ringbuffer.h +++ b/src/utils/fluid_ringbuffer.h @@ -30,7 +30,7 @@ struct _fluid_ringbuffer_t { char *array; /**< Queue array of arbitrary size elements */ int totalcount; /**< Total count of elements in array */ - int count; /**< Current count of elements */ + fluid_atomic_int_t count; /**< Current count of elements */ int in; /**< Index in queue to store next pushed element */ int out; /**< Index in queue of next popped element */ int elementsize; /**< Size of each element */ diff --git a/src/utils/fluidsynth_priv.h b/src/utils/fluidsynth_priv.h index cc9f3aed..a0f54b7b 100644 --- a/src/utils/fluidsynth_priv.h +++ b/src/utils/fluidsynth_priv.h @@ -180,6 +180,11 @@ typedef guint32 uint32; //typedef gint64 sint64; //typedef guint64 uint64; +/** Atomic types */ +typedef int fluid_atomic_int_t; +typedef unsigned int fluid_atomic_uint_t; +typedef float fluid_atomic_float_t; + /*************************************************************** *