]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
atomic operations: cleanups (add get/set for readability)
authorJaroslav Kysela <perex@perex.cz>
Tue, 8 Mar 2016 16:07:17 +0000 (17:07 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 8 Mar 2016 16:08:58 +0000 (17:08 +0100)
src/atomic.h
src/clock.h
src/dvr/dvr_db.c
src/dvr/dvr_rec.c
src/input.c
src/main.c
src/subscriptions.c
src/tvhlog.c
src/tvhlog.h

index b5b8f9699de7d55a3f83ef666579d6ba400fe0f1..726e10f716628e4e69c926b267d28daa5b50f080 100644 (file)
@@ -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);
 }
index 1a169ec847b950435cac9199c3841a601452f081..7d914e469a64059ce61b79088cdf1c75a6de0c0f 100644 (file)
@@ -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 */
index 9fbe388664fdac58e39d1a09e16e64ed61927583..e6453b62976cd1976c041345d03a243c0dc70b54 100644 (file)
@@ -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());
       }
     }
   }
index 87fc4a10e3cb86fadc74ac8e45620ab76c98e5e9..e5868cc5bab8cf47ac079d977fe697f4755d80ce 100644 (file)
@@ -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()) {
index dbfbbd8ce4ea1cfe35641d1996a23e1aa44a68e0..f93cdcaec9f1052d3acd8df3dc6912ef6173ff11 100644 (file)
@@ -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);
 }
 
 /*
index 4369bd7e021e49f42c4acb13e66447a0ef64ee16..c6c37fb64e9743deaa2a5336b8fc1351284607a7 100644 (file)
@@ -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;
 }
 
index a715ab938e2e006d45a764bc63a4214827c3789c..54d2eab40b9c5d8af527e8c5f9d14f38759d1b73 100644 (file)
@@ -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;
index 4dc1187e72f6f1d9596add726c64f8aa5a8a8c4c..c8d2b6cbce56efcdefc238c7bd58611b0690b700 100644 (file)
@@ -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;
 }
 
index b76b0e06cc473f465fb2c51a460fc4e94bcecb3e..3636ded6cac0969ad0daf90839c1a39fa3cc98cc 100644 (file)
@@ -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()) \