From: Jaroslav Kysela Date: Tue, 8 Mar 2016 16:07:17 +0000 (+0100) Subject: atomic operations: cleanups (add get/set for readability) X-Git-Tag: v4.2.1~931 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79808f326c720a62fd875495735f8413c586ae39;p=thirdparty%2Ftvheadend.git atomic operations: cleanups (add get/set for readability) --- diff --git a/src/atomic.h b/src/atomic.h index b5b8f9699..726e10f71 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -122,30 +122,101 @@ atomic_dec_u64(volatile uint64_t *ptr, uint64_t decr) #endif } +static inline int64_t +atomic_dec_s64(volatile int64_t *ptr, int64_t decr) +{ +#if ENABLE_ATOMIC64 + return __sync_fetch_and_sub(ptr, decr); +#else + int64_t ret; + pthread_mutex_lock(&atomic_lock); + ret = *ptr; + *ptr -= decr; + pthread_mutex_unlock(&atomic_lock); + return ret; +#endif +} + /* * Atomic EXCHANGE operation */ static inline int -atomic_exchange(volatile int *ptr, int new) +atomic_exchange(volatile int *ptr, int val) { - return __sync_lock_test_and_set(ptr, new); + return __sync_lock_test_and_set(ptr, val); } -static inline int -atomic_exchange_u64(volatile uint64_t *ptr, uint64_t new) +static inline uint64_t +atomic_exchange_u64(volatile uint64_t *ptr, uint64_t val) { - return __sync_lock_test_and_set(ptr, new); + return __sync_lock_test_and_set(ptr, val); } +static inline int64_t +atomic_exchange_s64(volatile int64_t *ptr, int64_t val) +{ + return __sync_lock_test_and_set(ptr, val); +} + +static inline time_t +atomic_exchange_time_t(volatile time_t *ptr, time_t val) +{ + return __sync_lock_test_and_set(ptr, val); +} + +/* + * Atomic get operation + */ + static inline int -atomic_exchange_s64(volatile int64_t *ptr, int64_t new) +atomic_get(volatile int *ptr) { - return __sync_lock_test_and_set(ptr, new); + return atomic_add(ptr, 0); } +static inline uint64_t +atomic_get_u64(volatile int *ptr) +{ + return atomic_add_u64(ptr, 0); +} + +static inline int64_t +atomic_get_s64(volatile int *ptr) +{ + return atomic_add_s64(ptr, 0); +} + +static inline time_t +atomic_get_time_t(volatile time_t *ptr) +{ + return atomic_add_time_t(ptr, 0); +} + +/* + * Atomic set operation + */ + static inline int -atomic_exchange_time_t(volatile time_t *ptr, int new) +atomic_set(volatile int *ptr, int val) +{ + return atomic_exchange(ptr, val); +} + +static inline uint64_t +atomic_set_u64(volatile int *ptr, uint64_t val) +{ + return atomic_exchange_u64(ptr, val); +} + +static inline int64_t +atomic_set_s64(volatile int *ptr, int64_t val) +{ + return atomic_exchange_s64(ptr, val); +} + +static inline time_t +atomic_set_time_t(volatile time_t *ptr, time_t val) { - return __sync_lock_test_and_set(ptr, new); + return atomic_exchange_time_t(ptr, val); } diff --git a/src/clock.h b/src/clock.h index 1a169ec84..7d914e469 100644 --- a/src/clock.h +++ b/src/clock.h @@ -45,12 +45,12 @@ extern time_t __gdispatch_clock; static inline int64_t mclk(void) { - return atomic_add_s64(&__mdispatch_clock, 0); + return atomic_get_s64(&__mdispatch_clock); } static inline time_t gclk(void) { - return atomic_add_time_t(&__gdispatch_clock, 0); + return atomic_get_time_t(&__gdispatch_clock); } #define MONOCLOCK_RESOLUTION 1000000LL /* microseconds */ diff --git a/src/dvr/dvr_db.c b/src/dvr/dvr_db.c index 9fbe38866..e6453b629 100644 --- a/src/dvr/dvr_db.c +++ b/src/dvr/dvr_db.c @@ -1795,9 +1795,9 @@ void dvr_event_running(epg_broadcast_t *e, epg_source_t esrc, epg_running_t runn return; LIST_FOREACH(de, &e->channel->ch_dvrs, de_channel_link) { if (!dvr_entry_get_epg_running(de)) { - atomic_exchange_time_t(&de->de_running_start, 0); - atomic_exchange_time_t(&de->de_running_stop, 0); - atomic_exchange_time_t(&de->de_running_pause, 0); + atomic_set_time_t(&de->de_running_start, 0); + atomic_set_time_t(&de->de_running_stop, 0); + atomic_set_time_t(&de->de_running_pause, 0); continue; } if (running == EPG_RUNNING_NOW && de->de_dvb_eid == e->dvb_eid) { @@ -1806,17 +1806,17 @@ void dvr_event_running(epg_broadcast_t *e, epg_source_t esrc, epg_running_t runn idnode_uuid_as_str(&de->de_id, ubuf), epg_broadcast_get_title(e, NULL), channel_get_name(e->channel)); - atomic_exchange_time_t(&de->de_running_pause, 0); + atomic_set_time_t(&de->de_running_pause, 0); } if (!de->de_running_start) { tvhdebug("dvr", "dvr entry %s event %s on %s - EPG marking start", idnode_uuid_as_str(&de->de_id, ubuf), epg_broadcast_get_title(e, NULL), channel_get_name(e->channel)); - atomic_exchange_time_t(&de->de_running_start, gclk()); + atomic_set_time_t(&de->de_running_start, gclk()); } if (dvr_entry_get_start_time(de, 1) > gclk()) { - atomic_exchange_time_t(&de->de_start, gclk()); + atomic_set_time_t(&de->de_start, gclk()); dvr_entry_set_timer(de); tvhdebug("dvr", "dvr entry %s event %s on %s - EPG start", idnode_uuid_as_str(&de->de_id, ubuf), @@ -1841,8 +1841,8 @@ void dvr_event_running(epg_broadcast_t *e, epg_source_t esrc, epg_running_t runn epg_broadcast_get_title(e, NULL), channel_get_name(de->de_channel)); } - atomic_exchange_time_t(&de->de_running_stop, gclk()); - atomic_exchange_time_t(&de->de_running_pause, 0); + atomic_set_time_t(&de->de_running_stop, gclk()); + atomic_set_time_t(&de->de_running_pause, 0); if (de->de_sched_state == DVR_RECORDING && de->de_running_start) { dvr_stop_recording(de, SM_CODE_OK, 0, 0); tvhdebug("dvr", "dvr entry %s %s %s on %s - EPG stop", @@ -1856,7 +1856,7 @@ void dvr_event_running(epg_broadcast_t *e, epg_source_t esrc, epg_running_t runn idnode_uuid_as_str(&de->de_id, ubuf), epg_broadcast_get_title(e, NULL), channel_get_name(e->channel)); - atomic_exchange_time_t(&de->de_running_pause, gclk()); + atomic_set_time_t(&de->de_running_pause, gclk()); } } } diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index 87fc4a10e..e5868cc5b 100644 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -140,7 +140,7 @@ dvr_rec_subscribe(dvr_entry_t *de) de->de_chain = prch; - atomic_exchange(&de->de_thread_shutdown, 0); + atomic_set(&de->de_thread_shutdown, 0); tvhthread_create(&de->de_thread, NULL, dvr_thread, de, "dvr"); return 0; } @@ -1272,11 +1272,11 @@ dvr_thread(void *aux) if (running_disabled) { epg_running = real_start <= gclk(); } else if (sm->sm_type == SMT_PACKET || sm->sm_type == SMT_MPEGTS) { - running_start = atomic_add_time_t(&de->de_running_start, 0); - running_stop = atomic_add_time_t(&de->de_running_stop, 0); + running_start = atomic_get_time_t(&de->de_running_start); + running_stop = atomic_get_time_t(&de->de_running_stop); if (running_start > 0) { epg_running = running_start >= running_stop ? 1 : 0; - if (epg_running && atomic_add_time_t(&de->de_running_pause, 0) >= running_start) + if (epg_running && atomic_get_time_t(&de->de_running_pause) >= running_start) epg_running = 2; } else if (running_stop == 0) { if (start_time + 2 >= gclk()) { diff --git a/src/input.c b/src/input.c index dbfbbd8ce..f93cdcaec 100644 --- a/src/input.c +++ b/src/input.c @@ -86,12 +86,12 @@ tvh_input_instance_clear_stats ( tvh_input_instance_t *tii ) { tvh_input_stream_stats_t *s = &tii->tii_stats; - atomic_exchange(&s->ber, 0); - atomic_exchange(&s->unc, 0); - atomic_exchange(&s->cc, 0); - atomic_exchange(&s->te, 0); - atomic_exchange(&s->ec_block, 0); - atomic_exchange(&s->tc_block, 0); + atomic_set(&s->ber, 0); + atomic_set(&s->unc, 0); + atomic_set(&s->cc, 0); + atomic_set(&s->te, 0); + atomic_set(&s->ec_block, 0); + atomic_set(&s->tc_block, 0); } /* diff --git a/src/main.c b/src/main.c index 4369bd7e0..c6c37fb64 100644 --- a/src/main.c +++ b/src/main.c @@ -531,12 +531,12 @@ mdispatch_clock_update(void) { int64_t mono = getmonoclock(); - if (mono > atomic_add_s64(&mtimer_periodic, 0)) { - atomic_exchange_s64(&mtimer_periodic, mono + MONOCLOCK_RESOLUTION); + if (mono > atomic_get_s64(&mtimer_periodic)) { + atomic_set_s64(&mtimer_periodic, mono + MONOCLOCK_RESOLUTION); comet_flush(); /* Flush idle comet mailboxes */ } - atomic_exchange_s64(&__mdispatch_clock, mono); + atomic_set_s64(&__mdispatch_clock, mono); return mono; } @@ -548,7 +548,7 @@ mtimer_tick_thread(void *aux) { while (tvheadend_running) { /* update clocks each 10x in one second */ - atomic_exchange_s64(&__mdispatch_clock, getmonoclock()); + atomic_set_s64(&__mdispatch_clock, getmonoclock()); tvh_safe_usleep(100000); } return NULL; @@ -622,7 +622,7 @@ static inline time_t gdispatch_clock_update(void) { time_t now = time(NULL); - atomic_exchange_time_t(&__gdispatch_clock, now); + atomic_set_time_t(&__gdispatch_clock, now); return now; } diff --git a/src/subscriptions.c b/src/subscriptions.c index a715ab938..54d2eab40 100644 --- a/src/subscriptions.c +++ b/src/subscriptions.c @@ -971,9 +971,9 @@ subscription_status_callback ( void *p ) LIST_FOREACH(s, &subscriptions, ths_global_link) { /* Store the difference between total bytes from the last round */ uint64_t in_prev = s->ths_total_bytes_in_prev; - uint64_t in_curr = atomic_add_u64(&s->ths_total_bytes_in, 0); + uint64_t in_curr = atomic_get_u64(&s->ths_total_bytes_in); uint64_t out_prev = s->ths_total_bytes_out_prev; - uint64_t out_curr = atomic_add_u64(&s->ths_total_bytes_out, 0); + uint64_t out_curr = atomic_get_u64(&s->ths_total_bytes_out); s->ths_bytes_in_avg = (int)(in_curr - in_prev); s->ths_total_bytes_in_prev = s->ths_total_bytes_in; diff --git a/src/tvhlog.c b/src/tvhlog.c index 4dc1187e7..c8d2b6cbc 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -283,7 +283,7 @@ void tvhlogv ( const char *file, int line, options = tvhlog_options; if (severity >= LOG_DEBUG) { ok = 0; - if (severity <= atomic_add(&tvhlog_level, 0)) { + if (severity <= atomic_get(&tvhlog_level)) { if (tvhlog_trace) { ok = htsmsg_get_u32_or_default(tvhlog_trace, "all", 0); ok = htsmsg_get_u32_or_default(tvhlog_trace, subsys, ok); @@ -573,14 +573,14 @@ static const void * tvhlog_class_trace_get ( void *o ) { static int si; - si = atomic_add(&tvhlog_level, 0) >= LOG_TRACE; + si = atomic_get(&tvhlog_level) >= LOG_TRACE; return &si; } static int tvhlog_class_trace_set ( void *o, const void *v ) { - atomic_exchange(&tvhlog_level, *(int *)v ? LOG_TRACE : LOG_DEBUG); + atomic_set(&tvhlog_level, *(int *)v ? LOG_TRACE : LOG_DEBUG); return 1; } diff --git a/src/tvhlog.h b/src/tvhlog.h index b76b0e06c..3636ded6c 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -97,7 +97,7 @@ static inline int tvhlog_limit ( tvhlog_limit_t *limit, uint32_t delay ) #define tvhlog_spawn(severity, subsys, fmt, ...)\ _tvhlog(__FILE__, __LINE__, 0, severity, subsys, fmt, ##__VA_ARGS__) #if ENABLE_TRACE -#define tvhtrace_enabled() (LOG_TRACE <= atomic_add(&tvhlog_level, 0)) +#define tvhtrace_enabled() (LOG_TRACE <= atomic_get(&tvhlog_level)) #define tvhtrace(subsys, fmt, ...) \ do { \ if (tvhtrace_enabled()) \