]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/thread.c
zebra: silence harmless ioctl warning when retrieving interface speed
[mirror_frr.git] / lib / thread.c
index e811bf88c815539b9f6536c212acd6c91dfd4600..44c9e2b2f1689d93af58cadd431e4b123fb2709b 100644 (file)
@@ -68,7 +68,7 @@ static unsigned int cpu_record_hash_key(struct cpu_thread_history *a)
        return jhash(&a->func, size, 0);
 }
 
-static int cpu_record_hash_cmp(const struct cpu_thread_history *a,
+static bool cpu_record_hash_cmp(const struct cpu_thread_history *a,
                               const struct cpu_thread_history *b)
 {
        return a->func == b->func;
@@ -434,7 +434,7 @@ struct thread_master *thread_master_create(const char *name)
 
        rv->cpu_record = hash_create_size(
                8, (unsigned int (*)(void *))cpu_record_hash_key,
-               (int (*)(const void *, const void *))cpu_record_hash_cmp,
+               (bool (*)(const void *, const void *))cpu_record_hash_cmp,
                "Thread Hash");
 
 
@@ -626,7 +626,7 @@ void thread_master_free(struct thread_master *m)
        {
                listnode_delete(masters, m);
                if (masters->count == 0) {
-                       list_delete_and_null(&masters);
+                       list_delete(&masters);
                }
        }
        pthread_mutex_unlock(&masters_mtx);
@@ -641,7 +641,7 @@ void thread_master_free(struct thread_master *m)
        pthread_cond_destroy(&m->cancel_cond);
        close(m->io_pipe[0]);
        close(m->io_pipe[1]);
-       list_delete_and_null(&m->cancel_req);
+       list_delete(&m->cancel_req);
        m->cancel_req = NULL;
 
        hash_clean(m->cpu_record, cpu_record_hash_free);
@@ -655,20 +655,26 @@ void thread_master_free(struct thread_master *m)
        XFREE(MTYPE_THREAD_MASTER, m);
 }
 
-/* Return remain time in second. */
-unsigned long thread_timer_remain_second(struct thread *thread)
+/* Return remain time in miliseconds. */
+unsigned long thread_timer_remain_msec(struct thread *thread)
 {
        int64_t remain;
 
        pthread_mutex_lock(&thread->mtx);
        {
-               remain = monotime_until(&thread->u.sands, NULL) / 1000000LL;
+               remain = monotime_until(&thread->u.sands, NULL) / 1000LL;
        }
        pthread_mutex_unlock(&thread->mtx);
 
        return remain < 0 ? 0 : remain;
 }
 
+/* Return remain time in seconds. */
+unsigned long thread_timer_remain_second(struct thread *thread)
+{
+       return thread_timer_remain_msec(thread) / 1000LL;
+}
+
 #define debugargdef  const char *funcname, const char *schedfrom, int fromln
 #define debugargpass funcname, schedfrom, fromln
 
@@ -1648,25 +1654,27 @@ void funcname_thread_execute(struct thread_master *m,
                             int (*func)(struct thread *), void *arg, int val,
                             debugargdef)
 {
-       struct cpu_thread_history tmp;
-       struct thread dummy;
-
-       memset(&dummy, 0, sizeof(struct thread));
+       struct thread *thread;
 
-       pthread_mutex_init(&dummy.mtx, NULL);
-       dummy.type = THREAD_EVENT;
-       dummy.add_type = THREAD_EXECUTE;
-       dummy.master = NULL;
-       dummy.arg = arg;
-       dummy.u.val = val;
+       /* Get or allocate new thread to execute. */
+       pthread_mutex_lock(&m->mtx);
+       {
+               thread = thread_get(m, THREAD_EVENT, func, arg, debugargpass);
 
-       tmp.func = dummy.func = func;
-       tmp.funcname = dummy.funcname = funcname;
-       dummy.hist = hash_get(m->cpu_record, &tmp,
-                             (void *(*)(void *))cpu_record_hash_alloc);
+               /* Set its event value. */
+               pthread_mutex_lock(&thread->mtx);
+               {
+                       thread->add_type = THREAD_EXECUTE;
+                       thread->u.val = val;
+                       thread->ref = &thread;
+               }
+               pthread_mutex_unlock(&thread->mtx);
+       }
+       pthread_mutex_unlock(&m->mtx);
 
-       dummy.schedfrom = schedfrom;
-       dummy.schedfrom_line = fromln;
+       /* Execute thread doing all accounting. */
+       thread_call(thread);
 
-       thread_call(&dummy);
+       /* Give back or free thread. */
+       thread_add_unuse(m, thread);
 }