"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/src/libponyrt/asio/asio.c" (1 Nov 2019, 2025 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "asio.c" see the Fossies "Dox" file reference documentation.

    1 #define PONY_WANT_ATOMIC_DEFS
    2 
    3 #include <string.h>
    4 #include <stdlib.h>
    5 #include <errno.h>
    6 #include <stdio.h>
    7 
    8 #include "asio.h"
    9 #include "../mem/pool.h"
   10 
   11 struct asio_base_t
   12 {
   13   pony_thread_id_t tid;
   14   asio_backend_t* backend;
   15   PONY_ATOMIC(uint64_t) noisy_count;
   16 };
   17 
   18 static asio_base_t running_base;
   19 static uint32_t asio_cpu;
   20 
   21 /** Start an asynchronous I/O event mechanism.
   22  *
   23  *  Errors are always delegated to the owning actor of an I/O subscription and
   24  *  never handled within the runtime system.
   25  *
   26  *  In any case (independent of the underlying backend) only one I/O dispatcher
   27  *  thread will be started. Since I/O events are subscribed by actors, we do
   28  *  not need to maintain a thread pool. Instead, I/O is processed in the
   29  *  context of the owning actor.
   30  */
   31 asio_backend_t* ponyint_asio_get_backend()
   32 {
   33   return running_base.backend;
   34 }
   35 
   36 uint32_t ponyint_asio_get_cpu()
   37 {
   38   return asio_cpu;
   39 }
   40 
   41 void ponyint_asio_init(uint32_t cpu)
   42 {
   43   asio_cpu = cpu;
   44   running_base.backend = ponyint_asio_backend_init();
   45 }
   46 
   47 bool ponyint_asio_start()
   48 {
   49   // if the backend wasn't successfully initialized
   50   if(running_base.backend == NULL)
   51     return false;
   52 
   53   if(!ponyint_thread_create(&running_base.tid, ponyint_asio_backend_dispatch,
   54     asio_cpu, running_base.backend))
   55     return false;
   56 
   57   return true;
   58 }
   59 
   60 bool ponyint_asio_stop()
   61 {
   62   if(!ponyint_asio_stoppable())
   63     return false;
   64 
   65   if(running_base.backend != NULL)
   66   {
   67     ponyint_asio_backend_final(running_base.backend);
   68     ponyint_thread_join(running_base.tid);
   69 
   70     running_base.backend = NULL;
   71     running_base.tid = 0;
   72   }
   73 
   74   return true;
   75 }
   76 
   77 bool ponyint_asio_stoppable()
   78 {
   79   // can only stop if we have no noisy actors
   80   return atomic_load_explicit(&running_base.noisy_count, memory_order_relaxed) == 0;
   81 }
   82 
   83 uint64_t ponyint_asio_noisy_add()
   84 {
   85   return atomic_fetch_add_explicit(&running_base.noisy_count, 1, memory_order_relaxed);
   86 }
   87 
   88 uint64_t ponyint_asio_noisy_remove()
   89 {
   90   return atomic_fetch_sub_explicit(&running_base.noisy_count, 1, memory_order_relaxed);
   91 }